summaryrefslogtreecommitdiffstats
path: root/src/dcm/pkg/module
diff options
context:
space:
mode:
Diffstat (limited to 'src/dcm/pkg/module')
-rw-r--r--src/dcm/pkg/module/apply.go875
-rw-r--r--src/dcm/pkg/module/logicalcloud.go343
2 files changed, 594 insertions, 624 deletions
diff --git a/src/dcm/pkg/module/apply.go b/src/dcm/pkg/module/apply.go
index c918f749..dbcbf8ac 100644
--- a/src/dcm/pkg/module/apply.go
+++ b/src/dcm/pkg/module/apply.go
@@ -1,451 +1,424 @@
-/*
-* Copyright 2020 Intel Corporation, Inc
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-package module
-
-import (
- "strings"
- "fmt"
- "crypto/rsa"
- "crypto/rand"
- "crypto/x509"
- "crypto/x509/pkix"
- "encoding/json"
- "encoding/pem"
- "encoding/base64"
- "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext"
- log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils"
- "gopkg.in/yaml.v2"
- pkgerrors "github.com/pkg/errors"
-
-)
-
-type Resource struct {
- ApiVersion string `yaml:"apiVersion"`
- Kind string `yaml:"kind"`
- MetaData MetaDatas `yaml:"metadata"`
- Specification Specs `yaml:"spec,omitempty"`
- Rules []RoleRules `yaml:"rules,omitempty"`
- Subjects []RoleSubjects `yaml:"subjects,omitempty"`
- RoleRefs RoleRef `yaml:"roleRef,omitempty"`
-}
-
-type MetaDatas struct {
- Name string `yaml:"name"`
- Namespace string `yaml:"namespace,omitempty"`
-}
-
-type Specs struct {
- Request string `yaml:"request,omitempty"`
- Usages []string `yaml:"usages,omitempty"`
- //Hard logicalcloud.QSpec `yaml:"hard,omitempty"`
- Hard QSpec `yaml:"hard,omitempty"`
-}
-
-type RoleRules struct {
- ApiGroups []string `yaml:"apiGroups"`
- Resources []string `yaml:"resources"`
- Verbs []string `yaml:"verbs"`
-}
-
-type RoleSubjects struct {
- Kind string `yaml:"kind"`
- Name string `yaml:"name"`
- ApiGroup string `yaml:"apiGroup"`
-}
-
-type RoleRef struct {
- Kind string `yaml:"kind"`
- Name string `yaml:"name"`
- ApiGroup string `yaml:"apiGroup"`
-}
-
-
-func createNamespace(logicalcloud LogicalCloud) (string, error) {
-
- namespace := Resource{
- ApiVersion: "v1",
- Kind: "Namespace",
- MetaData: MetaDatas{
- Name: logicalcloud.Specification.NameSpace,
- },
- }
-
- nsData, err := yaml.Marshal(&namespace)
- if err != nil {
- return "", err
- }
-
-
- return string(nsData), nil
-}
-
-func createRole(logicalcloud LogicalCloud) (string, error) {
-
- userPermissions := logicalcloud.Specification.User.UserPermissions[0]
-
- role := Resource{
- ApiVersion: "rbac.authorization.k8s.io/v1beta1",
- Kind: "Role",
- MetaData: MetaDatas{
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""),
- Namespace: logicalcloud.Specification.NameSpace,
- },
- Rules: []RoleRules{ RoleRules{
- ApiGroups: userPermissions.APIGroups,
- Resources: userPermissions.Resources,
- Verbs: userPermissions.Verbs,
- },
- },
-
- }
-
- roleData, err := yaml.Marshal(&role)
- if err != nil {
- return "", err
- }
-
- return string(roleData), nil
-}
-
-func createRoleBinding(logicalcloud LogicalCloud) (string, error) {
-
- roleBinding := Resource{
- ApiVersion: "rbac.authorization.k8s.io/v1beta1",
- Kind: "RoleBinding",
- MetaData: MetaDatas{
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-roleBinding"}, ""),
- Namespace: logicalcloud.Specification.NameSpace,
- },
- Subjects: []RoleSubjects{ RoleSubjects{
- Kind: "User",
- Name: logicalcloud.Specification.User.UserName,
- ApiGroup: "",
- },
- },
-
- RoleRefs: RoleRef{
- Kind: "Role",
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""),
- ApiGroup: "",
- },
- }
-
- rBData, err := yaml.Marshal(&roleBinding)
- if err != nil {
- return "", err
- }
-
-
- return string(rBData), nil
-
-}
-
-func createQuota(quota []Quota, namespace string) (string, error) {
- lcQuota := quota[0]
-
- q := Resource{
- ApiVersion: "v1",
- Kind: "ResourceQuota",
- MetaData: MetaDatas{
- Name: lcQuota.MetaData.QuotaName,
- Namespace: namespace,
- },
- Specification: Specs{
- Hard: lcQuota.Specification,
- },
- }
-
- qData, err := yaml.Marshal(&q)
- if err != nil {
- return "", err
- }
-
-
-
- return string(qData), nil
-
-}
-
-func createUserCSR(logicalcloud LogicalCloud) (string, error) {
- KEYSIZE := 4096
- userName := logicalcloud.Specification.User.UserName
-
- key, err := rsa.GenerateKey(rand.Reader, KEYSIZE)
- if err != nil {
- return "", err
- }
-
- csrTemplate := x509.CertificateRequest{Subject: pkix.Name{CommonName: userName,},
- }
-
- csrCert, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key)
- if err != nil {
- return "", err
- }
-
- //Encode csr
- csr := pem.EncodeToMemory(&pem.Block{
- Type: "CERTIFICATE REQUEST",
- Bytes: csrCert,
- })
-
- csrObj := Resource{
- ApiVersion: "certificates.k8s.io/v1beta1",
- Kind: "CertificateSigningRequest",
- MetaData: MetaDatas{
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-user-csr"}, ""),
- Namespace: logicalcloud.Specification.NameSpace,
- },
- Specification: Specs{
- Request: base64.StdEncoding.EncodeToString(csr),
- Usages: []string{"digital signature", "key encipherment"},
- },
- }
-
- csrData, err := yaml.Marshal(&csrObj)
- if err != nil {
- return "", err
- }
-
-
- return string(csrData), nil
-
-
-}
-
-// TODO:
-// Install istio
-// Store user key for user creation
-// Code to run kubectl commands for user
-// kubectl certificate approve lc1-user-cert
-// kubectl get csr lc1-user-cert -o jsonpath='{.status.certificate}' | base64 --decode > user.crt
-// kubectl config set-credentials user --client-certificate=<user.crt> --client-key=<user.key>
-// kubectl config set-context user-context --cluster=cluster-name --namespace=lc1 --user=user
-
-
-func CreateEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster,
- quotaList []Quota ) error {
-
- APP := "logical-cloud"
- logicalCloudName := logicalcloud.MetaData.LogicalCloudName
-
-
- //Resource Names
- namespaceName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+namespace"}, "")
- roleName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+role"}, "")
- roleBindingName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+roleBinding"}, "")
- quotaName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+quota"}, "")
- csrName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+CertificateSigningRequest"}, "")
-
- // Get resources to be added
- namespace, err := createNamespace(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating Namespace YAML for logical cloud")
- }
-
- role, err := createRole(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating Role YAML for logical cloud")
- }
-
- roleBinding, err := createRoleBinding(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating RoleBinding YAML for logical cloud")
- }
-
- quota, err := createQuota(quotaList, logicalcloud.Specification.NameSpace)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating Quota YAML for logical cloud")
- }
-
- csr, err := createUserCSR(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating User CSR for logical cloud")
- }
-
-
- context := appcontext.AppContext{}
- ctxVal, err := context.InitAppContext()
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating AppContext")
- }
-
- fmt.Printf("%v\n", ctxVal)
-
- handle, err := context.CreateCompositeApp()
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating AppContext CompositeApp")
- }
-
-
- appHandle, err := context.AddApp(handle, APP)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext CompositeApp create failure", log.Fields{
- "logical-cloud": logicalCloudName,
- })
- }
- return pkgerrors.Wrap(err, "Error adding App to AppContext")
- }
-
-
- // Iterate through cluster list and add all the clusters
- for _, cluster:= range clusterList {
- clusterName := strings.Join([]string{cluster.Specification.ClusterProvider, "+", cluster.Specification.ClusterName, }, "")
- clusterHandle, err := context.AddCluster(appHandle, clusterName)
-
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add cluster failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding Cluster to AppContext")
- }
-
- // Add namespace resource to each cluster
- _, err = context.AddResource(clusterHandle, namespaceName, namespace)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add namespace resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding Namespace Resource to AppContext")
- }
-
- // Add csr resource to each cluster
- _, err = context.AddResource(clusterHandle, csrName, csr)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add CSR resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding CSR Resource to AppContext")
- }
-
-
-
- // Add Role resource to each cluster
- _, err = context.AddResource(clusterHandle, roleName, role)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add role resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding role Resource to AppContext")
- }
-
- // Add RoleBinding resource to each cluster
- _, err = context.AddResource(clusterHandle, roleBindingName, roleBinding)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add roleBinding resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding roleBinding Resource to AppContext")
- }
-
- // Add quota resource to each cluster
- _, err = context.AddResource(clusterHandle, quotaName, quota)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add quota resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding quota Resource to AppContext")
- }
-
- // Add Resource Order and Resource Dependency
- resOrder, err := json.Marshal(map[string][]string{"resorder" : []string{namespaceName, quotaName, csrName, roleName, roleBindingName}})
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating resource order JSON")
- }
-
- resDependency, err := json.Marshal(map[string]map[string]string{"resdependency" : map[string]string{namespaceName : "go",
- quotaName : strings.Join([]string{"wait on ", namespaceName}, ""), csrName: strings.Join([]string{"wait on ", quotaName}, ""),
- roleName : strings.Join([]string{"wait on ", csrName}, ""), roleBindingName: strings.Join([]string{"wait on ", roleName}, ""),}})
-
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating resource dependency JSON")
- }
-
- _, err = context.AddInstruction(clusterHandle, "resource", "order", string(resOrder))
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding instruction order to AppContext")
- }
-
- _, err = context.AddInstruction(clusterHandle, "resource", "dependency", string(resDependency))
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding instruction dependency to AppContext")
- }
-
-
-
- }
-
- return nil
-
-
-}
+/*
+* Copyright 2020 Intel Corporation, Inc
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package module
+
+import (
+ "crypto/rand"
+ "crypto/rsa"
+ "crypto/x509"
+ "crypto/x509/pkix"
+ "encoding/base64"
+ "encoding/json"
+ "encoding/pem"
+ "fmt"
+ "strings"
+
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext"
+ log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils"
+ pkgerrors "github.com/pkg/errors"
+ "gopkg.in/yaml.v2"
+)
+
+type Resource struct {
+ ApiVersion string `yaml:"apiVersion"`
+ Kind string `yaml:"kind"`
+ MetaData MetaDatas `yaml:"metadata"`
+ Specification Specs `yaml:"spec,omitempty"`
+ Rules []RoleRules `yaml:"rules,omitempty"`
+ Subjects []RoleSubjects `yaml:"subjects,omitempty"`
+ RoleRefs RoleRef `yaml:"roleRef,omitempty"`
+}
+
+type MetaDatas struct {
+ Name string `yaml:"name"`
+ Namespace string `yaml:"namespace,omitempty"`
+}
+
+type Specs struct {
+ Request string `yaml:"request,omitempty"`
+ Usages []string `yaml:"usages,omitempty"`
+ //Hard logicalcloud.QSpec `yaml:"hard,omitempty"`
+ Hard QSpec `yaml:"hard,omitempty"`
+}
+
+type RoleRules struct {
+ ApiGroups []string `yaml:"apiGroups"`
+ Resources []string `yaml:"resources"`
+ Verbs []string `yaml:"verbs"`
+}
+
+type RoleSubjects struct {
+ Kind string `yaml:"kind"`
+ Name string `yaml:"name"`
+ ApiGroup string `yaml:"apiGroup"`
+}
+
+type RoleRef struct {
+ Kind string `yaml:"kind"`
+ Name string `yaml:"name"`
+ ApiGroup string `yaml:"apiGroup"`
+}
+
+func createNamespace(logicalcloud LogicalCloud) (string, error) {
+
+ namespace := Resource{
+ ApiVersion: "v1",
+ Kind: "Namespace",
+ MetaData: MetaDatas{
+ Name: logicalcloud.Specification.NameSpace,
+ },
+ }
+
+ nsData, err := yaml.Marshal(&namespace)
+ if err != nil {
+ return "", err
+ }
+
+ return string(nsData), nil
+}
+
+func createRole(logicalcloud LogicalCloud) (string, error) {
+
+ userPermissions := logicalcloud.Specification.User.UserPermissions[0]
+
+ role := Resource{
+ ApiVersion: "rbac.authorization.k8s.io/v1beta1",
+ Kind: "Role",
+ MetaData: MetaDatas{
+ Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""),
+ Namespace: logicalcloud.Specification.NameSpace,
+ },
+ Rules: []RoleRules{RoleRules{
+ ApiGroups: userPermissions.APIGroups,
+ Resources: userPermissions.Resources,
+ Verbs: userPermissions.Verbs,
+ },
+ },
+ }
+
+ roleData, err := yaml.Marshal(&role)
+ if err != nil {
+ return "", err
+ }
+
+ return string(roleData), nil
+}
+
+func createRoleBinding(logicalcloud LogicalCloud) (string, error) {
+
+ roleBinding := Resource{
+ ApiVersion: "rbac.authorization.k8s.io/v1beta1",
+ Kind: "RoleBinding",
+ MetaData: MetaDatas{
+ Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-roleBinding"}, ""),
+ Namespace: logicalcloud.Specification.NameSpace,
+ },
+ Subjects: []RoleSubjects{RoleSubjects{
+ Kind: "User",
+ Name: logicalcloud.Specification.User.UserName,
+ ApiGroup: "",
+ },
+ },
+
+ RoleRefs: RoleRef{
+ Kind: "Role",
+ Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""),
+ ApiGroup: "",
+ },
+ }
+
+ rBData, err := yaml.Marshal(&roleBinding)
+ if err != nil {
+ return "", err
+ }
+
+ return string(rBData), nil
+
+}
+
+func createQuota(quota []Quota, namespace string) (string, error) {
+ lcQuota := quota[0]
+
+ q := Resource{
+ ApiVersion: "v1",
+ Kind: "ResourceQuota",
+ MetaData: MetaDatas{
+ Name: lcQuota.MetaData.QuotaName,
+ Namespace: namespace,
+ },
+ Specification: Specs{
+ Hard: lcQuota.Specification,
+ },
+ }
+
+ qData, err := yaml.Marshal(&q)
+ if err != nil {
+ return "", err
+ }
+
+ return string(qData), nil
+
+}
+
+func createUserCSR(logicalcloud LogicalCloud) (string, error) {
+ KEYSIZE := 4096
+ userName := logicalcloud.Specification.User.UserName
+
+ key, err := rsa.GenerateKey(rand.Reader, KEYSIZE)
+ if err != nil {
+ return "", err
+ }
+
+ csrTemplate := x509.CertificateRequest{Subject: pkix.Name{CommonName: userName}}
+
+ csrCert, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key)
+ if err != nil {
+ return "", err
+ }
+
+ //Encode csr
+ csr := pem.EncodeToMemory(&pem.Block{
+ Type: "CERTIFICATE REQUEST",
+ Bytes: csrCert,
+ })
+
+ csrObj := Resource{
+ ApiVersion: "certificates.k8s.io/v1beta1",
+ Kind: "CertificateSigningRequest",
+ MetaData: MetaDatas{
+ Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-user-csr"}, ""),
+ Namespace: logicalcloud.Specification.NameSpace,
+ },
+ Specification: Specs{
+ Request: base64.StdEncoding.EncodeToString(csr),
+ Usages: []string{"digital signature", "key encipherment"},
+ },
+ }
+
+ csrData, err := yaml.Marshal(&csrObj)
+ if err != nil {
+ return "", err
+ }
+
+ return string(csrData), nil
+
+}
+
+// TODO:
+// Install istio
+// Store user key for user creation
+// Code to run kubectl commands for user
+// kubectl certificate approve lc1-user-cert
+// kubectl get csr lc1-user-cert -o jsonpath='{.status.certificate}' | base64 --decode > user.crt
+// kubectl config set-credentials user --client-certificate=<user.crt> --client-key=<user.key>
+// kubectl config set-context user-context --cluster=cluster-name --namespace=lc1 --user=user
+
+func CreateEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster,
+ quotaList []Quota) error {
+
+ APP := "logical-cloud"
+ logicalCloudName := logicalcloud.MetaData.LogicalCloudName
+
+ //Resource Names
+ namespaceName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+namespace"}, "")
+ roleName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+role"}, "")
+ roleBindingName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+roleBinding"}, "")
+ quotaName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+quota"}, "")
+ csrName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+CertificateSigningRequest"}, "")
+
+ // Get resources to be added
+ namespace, err := createNamespace(logicalcloud)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error Creating Namespace YAML for logical cloud")
+ }
+
+ role, err := createRole(logicalcloud)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error Creating Role YAML for logical cloud")
+ }
+
+ roleBinding, err := createRoleBinding(logicalcloud)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error Creating RoleBinding YAML for logical cloud")
+ }
+
+ quota, err := createQuota(quotaList, logicalcloud.Specification.NameSpace)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error Creating Quota YAML for logical cloud")
+ }
+
+ csr, err := createUserCSR(logicalcloud)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error Creating User CSR for logical cloud")
+ }
+
+ context := appcontext.AppContext{}
+ ctxVal, err := context.InitAppContext()
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error creating AppContext")
+ }
+
+ fmt.Printf("%v\n", ctxVal)
+
+ handle, err := context.CreateCompositeApp()
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error creating AppContext CompositeApp")
+ }
+
+ appHandle, err := context.AddApp(handle, APP)
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext CompositeApp create failure", log.Fields{
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding App to AppContext")
+ }
+
+ // Iterate through cluster list and add all the clusters
+ for _, cluster := range clusterList {
+ clusterName := strings.Join([]string{cluster.Specification.ClusterProvider, "+", cluster.Specification.ClusterName}, "")
+ clusterHandle, err := context.AddCluster(appHandle, clusterName)
+
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add cluster failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding Cluster to AppContext")
+ }
+
+ // Add namespace resource to each cluster
+ _, err = context.AddResource(clusterHandle, namespaceName, namespace)
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add namespace resource failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding Namespace Resource to AppContext")
+ }
+
+ // Add csr resource to each cluster
+ _, err = context.AddResource(clusterHandle, csrName, csr)
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add CSR resource failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding CSR Resource to AppContext")
+ }
+
+ // Add Role resource to each cluster
+ _, err = context.AddResource(clusterHandle, roleName, role)
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add role resource failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding role Resource to AppContext")
+ }
+
+ // Add RoleBinding resource to each cluster
+ _, err = context.AddResource(clusterHandle, roleBindingName, roleBinding)
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add roleBinding resource failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding roleBinding Resource to AppContext")
+ }
+
+ // Add quota resource to each cluster
+ _, err = context.AddResource(clusterHandle, quotaName, quota)
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add quota resource failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding quota Resource to AppContext")
+ }
+
+ // Add Resource Order and Resource Dependency
+ resOrder, err := json.Marshal(map[string][]string{"resorder": []string{namespaceName, quotaName, csrName, roleName, roleBindingName}})
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error creating resource order JSON")
+ }
+
+ resDependency, err := json.Marshal(map[string]map[string]string{"resdependency": map[string]string{namespaceName: "go",
+ quotaName: strings.Join([]string{"wait on ", namespaceName}, ""), csrName: strings.Join([]string{"wait on ", quotaName}, ""),
+ roleName: strings.Join([]string{"wait on ", csrName}, ""), roleBindingName: strings.Join([]string{"wait on ", roleName}, "")}})
+
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error creating resource dependency JSON")
+ }
+
+ _, err = context.AddInstruction(clusterHandle, "resource", "order", string(resOrder))
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding instruction order to AppContext")
+ }
+
+ _, err = context.AddInstruction(clusterHandle, "resource", "dependency", string(resDependency))
+ if err != nil {
+ cleanuperr := context.DeleteCompositeApp()
+ if cleanuperr != nil {
+ log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{
+ "cluster-provider": cluster.Specification.ClusterProvider,
+ "cluster": cluster.Specification.ClusterName,
+ "logical-cloud": logicalCloudName,
+ })
+ }
+ return pkgerrors.Wrap(err, "Error adding instruction dependency to AppContext")
+ }
+
+ }
+
+ return nil
+
+}
diff --git a/src/dcm/pkg/module/logicalcloud.go b/src/dcm/pkg/module/logicalcloud.go
index 9fb1b6fb..51ee387d 100644
--- a/src/dcm/pkg/module/logicalcloud.go
+++ b/src/dcm/pkg/module/logicalcloud.go
@@ -12,280 +12,277 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-*/
+ */
package module
import (
- "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
- "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
- pkgerrors "github.com/pkg/errors"
+ pkgerrors "github.com/pkg/errors"
)
// LogicalCloud contains the parameters needed for a Logical Cloud
type LogicalCloud struct {
- MetaData MetaDataList `json:"metadata"`
- Specification Spec `json:"spec"`
+ MetaData MetaDataList `json:"metadata"`
+ Specification Spec `json:"spec"`
}
-
// MetaData contains the parameters needed for metadata
type MetaDataList struct {
- LogicalCloudName string `json:"name"`
- Description string `json:"description"`
- UserData1 string `json:"userData1"`
- UserData2 string `json:"userData2"`
+ LogicalCloudName string `json:"name"`
+ Description string `json:"description"`
+ UserData1 string `json:"userData1"`
+ UserData2 string `json:"userData2"`
}
// Spec contains the parameters needed for spec
type Spec struct {
- NameSpace string `json:"namespace"`
- User UserData `json:"user"`
-
+ NameSpace string `json:"namespace"`
+ User UserData `json:"user"`
}
// UserData contains the parameters needed for user
type UserData struct {
- UserName string `json:"user-name"`
- Type string `json:"type"`
- UserPermissions []UserPerm `json:"user-permissions"`
+ UserName string `json:"user-name"`
+ Type string `json:"type"`
+ UserPermissions []UserPerm `json:"user-permissions"`
}
// UserPerm contains the parameters needed for user permissions
type UserPerm struct {
- PermName string `json:"permission-name"`
- APIGroups []string `json:"apiGroups"`
- Resources []string `json:"resources"`
- Verbs []string `json:"verbs"`
+ PermName string `json:"permission-name"`
+ APIGroups []string `json:"apiGroups"`
+ Resources []string `json:"resources"`
+ Verbs []string `json:"verbs"`
}
// LogicalCloudKey is the key structure that is used in the database
type LogicalCloudKey struct {
- Project string `json:"project"`
- LogicalCloudName string `json:"logical-cloud-name"`
+ Project string `json:"project"`
+ LogicalCloudName string `json:"logical-cloud-name"`
}
// LogicalCloudManager is an interface that exposes the connection
// functionality
type LogicalCloudManager interface {
- Create(project string, c LogicalCloud) (LogicalCloud, error)
- Get(project, name string) (LogicalCloud, error)
- GetAll(project string) ([]LogicalCloud, error)
- Delete(project, name string) error
- Update(project, name string, c LogicalCloud) (LogicalCloud, error)
-
+ Create(project string, c LogicalCloud) (LogicalCloud, error)
+ Get(project, name string) (LogicalCloud, error)
+ GetAll(project string) ([]LogicalCloud, error)
+ Delete(project, name string) error
+ Update(project, name string, c LogicalCloud) (LogicalCloud, error)
}
// Interface facilitates unit testing by mocking functions
type Utility interface {
- DBInsert(storeName string, key db.Key, query interface{}, meta string, c interface{}) error
- DBFind(storeName string, key db.Key, meta string) ([][]byte, error)
- DBUnmarshal(value []byte, out interface{}) error
- DBRemove(storeName string, key db.Key) error
- CheckProject(project string) error
- CheckLogicalCloud(project, logicalCloud string) error
+ DBInsert(storeName string, key db.Key, query interface{}, meta string, c interface{}) error
+ DBFind(storeName string, key db.Key, meta string) ([][]byte, error)
+ DBUnmarshal(value []byte, out interface{}) error
+ DBRemove(storeName string, key db.Key) error
+ CheckProject(project string) error
+ CheckLogicalCloud(project, logicalCloud string) error
}
// LogicalCloudClient implements the LogicalCloudManager
// It will also be used to maintain some localized state
type LogicalCloudClient struct {
- storeName string
- tagMeta string
- util Utility
+ storeName string
+ tagMeta string
+ util Utility
}
// Added for unit testing; implements Utility interface
-type DBService struct {}
+type DBService struct{}
// LogicalCloudClient returns an instance of the LogicalCloudClient
// which implements the LogicalCloudManager
func NewLogicalCloudClient() *LogicalCloudClient {
- service := DBService{}
- return &LogicalCloudClient{
- storeName: "orchestrator",
- tagMeta: "logicalcloud",
- util: service,
- }
+ service := DBService{}
+ return &LogicalCloudClient{
+ storeName: "orchestrator",
+ tagMeta: "logicalcloud",
+ util: service,
+ }
}
// Create entry for the logical cloud resource in the database
func (v *LogicalCloudClient) Create(project string, c LogicalCloud) (LogicalCloud, error) {
- //Construct key consisting of name
- key := LogicalCloudKey{
- Project: project,
- LogicalCloudName: c.MetaData.LogicalCloudName,
- }
-
- //Check if project exists
- err := v.util.CheckProject(project)
- if err != nil {
- return LogicalCloud{}, pkgerrors.New("Unable to find the project")
- }
-
- //Check if this Logical Cloud already exists
- _, err = v.Get(project, c.MetaData.LogicalCloudName)
- if err == nil {
- return LogicalCloud{}, pkgerrors.New("Logical Cloud already exists")
- }
-
- err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c)
- if err != nil {
- return LogicalCloud{}, pkgerrors.Wrap(err, "Creating DB Entry")
- }
-
- return c, nil
+ //Construct key consisting of name
+ key := LogicalCloudKey{
+ Project: project,
+ LogicalCloudName: c.MetaData.LogicalCloudName,
+ }
+
+ //Check if project exists
+ err := v.util.CheckProject(project)
+ if err != nil {
+ return LogicalCloud{}, pkgerrors.New("Unable to find the project")
+ }
+
+ //Check if this Logical Cloud already exists
+ _, err = v.Get(project, c.MetaData.LogicalCloudName)
+ if err == nil {
+ return LogicalCloud{}, pkgerrors.New("Logical Cloud already exists")
+ }
+
+ err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c)
+ if err != nil {
+ return LogicalCloud{}, pkgerrors.Wrap(err, "Creating DB Entry")
+ }
+
+ return c, nil
}
// Get returns Logical Cloud corresponding to logical cloud name
func (v *LogicalCloudClient) Get(project, logicalCloudName string) (LogicalCloud, error) {
- //Construct the composite key to select the entry
- key := LogicalCloudKey{
- Project: project,
- LogicalCloudName: logicalCloudName,
- }
- value, err := v.util.DBFind(v.storeName, key, v.tagMeta)
- if err != nil {
- return LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Cloud")
- }
-
- //value is a byte array
- if value != nil {
- lc := LogicalCloud{}
- err = v.util.DBUnmarshal(value[0], &lc)
- if err != nil {
- return LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling value")
- }
- return lc, nil
- }
-
- return LogicalCloud{}, pkgerrors.New("Error getting Logical Cloud")
+ //Construct the composite key to select the entry
+ key := LogicalCloudKey{
+ Project: project,
+ LogicalCloudName: logicalCloudName,
+ }
+ value, err := v.util.DBFind(v.storeName, key, v.tagMeta)
+ if err != nil {
+ return LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Cloud")
+ }
+
+ //value is a byte array
+ if value != nil {
+ lc := LogicalCloud{}
+ err = v.util.DBUnmarshal(value[0], &lc)
+ if err != nil {
+ return LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling value")
+ }
+ return lc, nil
+ }
+
+ return LogicalCloud{}, pkgerrors.New("Error getting Logical Cloud")
}
// GetAll returns Logical Clouds in the project
func (v *LogicalCloudClient) GetAll(project string) ([]LogicalCloud, error) {
- //Construct the composite key to select the entry
- key := LogicalCloudKey{
- Project: project,
- LogicalCloudName: "",
- }
-
- var resp []LogicalCloud
- values, err := v.util.DBFind(v.storeName, key, v.tagMeta)
- if err != nil {
- return []LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Clouds")
- }
-
- for _, value := range values {
- lc := LogicalCloud{}
- err = v.util.DBUnmarshal(value, &lc)
- if err != nil {
- return []LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling values")
- }
- resp = append(resp, lc)
- }
-
- return resp, nil
+ //Construct the composite key to select the entry
+ key := LogicalCloudKey{
+ Project: project,
+ LogicalCloudName: "",
+ }
+
+ var resp []LogicalCloud
+ values, err := v.util.DBFind(v.storeName, key, v.tagMeta)
+ if err != nil {
+ return []LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Clouds")
+ }
+
+ for _, value := range values {
+ lc := LogicalCloud{}
+ err = v.util.DBUnmarshal(value, &lc)
+ if err != nil {
+ return []LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling values")
+ }
+ resp = append(resp, lc)
+ }
+
+ return resp, nil
}
// Delete the Logical Cloud entry from database
func (v *LogicalCloudClient) Delete(project, logicalCloudName string) error {
- //Construct the composite key to select the entry
- key := LogicalCloudKey{
- Project: project,
- LogicalCloudName: logicalCloudName,
- }
- err := v.util.DBRemove(v.storeName, key)
- if err != nil {
- return pkgerrors.Wrap(err, "Delete Logical Cloud")
- }
-
- return nil
+ //Construct the composite key to select the entry
+ key := LogicalCloudKey{
+ Project: project,
+ LogicalCloudName: logicalCloudName,
+ }
+ err := v.util.DBRemove(v.storeName, key)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Delete Logical Cloud")
+ }
+
+ return nil
}
// Update an entry for the Logical Cloud in the database
func (v *LogicalCloudClient) Update(project, logicalCloudName string, c LogicalCloud) (LogicalCloud, error) {
- key := LogicalCloudKey{
- Project: project,
- LogicalCloudName: logicalCloudName,
- }
- // Check for mismatch, logicalCloudName and payload logical cloud name
- if c.MetaData.LogicalCloudName != logicalCloudName {
- return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud name mismatch")
- }
- //Check if this Logical Cloud exists
- _, err := v.Get(project, logicalCloudName)
- if err != nil {
- return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud doesn't exist")
- }
- err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c)
- if err != nil {
- return LogicalCloud{}, pkgerrors.Wrap(err, "Updating DB Entry")
- }
- return c, nil
+ key := LogicalCloudKey{
+ Project: project,
+ LogicalCloudName: logicalCloudName,
+ }
+ // Check for mismatch, logicalCloudName and payload logical cloud name
+ if c.MetaData.LogicalCloudName != logicalCloudName {
+ return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud name mismatch")
+ }
+ //Check if this Logical Cloud exists
+ _, err := v.Get(project, logicalCloudName)
+ if err != nil {
+ return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud doesn't exist")
+ }
+ err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c)
+ if err != nil {
+ return LogicalCloud{}, pkgerrors.Wrap(err, "Updating DB Entry")
+ }
+ return c, nil
}
func (d DBService) DBInsert(storeName string, key db.Key, query interface{}, meta string, c interface{}) error {
- err := db.DBconn.Insert(storeName, key, nil, meta, c)
- if err != nil {
- return pkgerrors.Wrap(err, "Creating DB Entry")
- }
+ err := db.DBconn.Insert(storeName, key, nil, meta, c)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Creating DB Entry")
+ }
- return nil
+ return nil
}
func (d DBService) DBFind(storeName string, key db.Key, meta string) ([][]byte, error) {
- value, err := db.DBconn.Find(storeName, key, meta)
- if err != nil {
- return [][]byte{}, pkgerrors.Wrap(err, "Get Resource")
- }
+ value, err := db.DBconn.Find(storeName, key, meta)
+ if err != nil {
+ return [][]byte{}, pkgerrors.Wrap(err, "Get Resource")
+ }
- return value, nil
+ return value, nil
}
func (d DBService) DBUnmarshal(value []byte, out interface{}) error {
- err := db.DBconn.Unmarshal(value, out)
- if err != nil {
- return pkgerrors.Wrap(err, "Unmarshaling Value")
- }
+ err := db.DBconn.Unmarshal(value, out)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Unmarshaling Value")
+ }
- return nil
+ return nil
}
func (d DBService) DBRemove(storeName string, key db.Key) error {
- err := db.DBconn.Remove(storeName, key)
- if err != nil {
- return pkgerrors.Wrap(err, "Delete Resource")
- }
+ err := db.DBconn.Remove(storeName, key)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Delete Resource")
+ }
- return nil
+ return nil
}
func (d DBService) CheckProject(project string) error {
- // Check if project exists
- _, err := module.NewProjectClient().GetProject(project)
- if err != nil {
- return pkgerrors.New("Unable to find the project")
- }
+ // Check if project exists
+ _, err := module.NewProjectClient().GetProject(project)
+ if err != nil {
+ return pkgerrors.New("Unable to find the project")
+ }
- return nil
+ return nil
}
func (d DBService) CheckLogicalCloud(project, logicalCloud string) error {
- // Check if logical cloud exists
- _, err := NewLogicalCloudClient().Get(project, logicalCloud)
- if err != nil {
- return pkgerrors.New("Unable to find the logical cloud")
- }
+ // Check if logical cloud exists
+ _, err := NewLogicalCloudClient().Get(project, logicalCloud)
+ if err != nil {
+ return pkgerrors.New("Unable to find the logical cloud")
+ }
- return nil
+ return nil
}