From c257a136355a794f5bf778f670c041e8958c3608 Mon Sep 17 00:00:00 2001 From: Rajamohan Raj Date: Tue, 26 May 2020 20:08:58 +0000 Subject: Adding cluster meta data and saving in etcd As part of this patch, we assign groupNumbers for the set of clusters which are under anyOf, or in other words are optional for deployement of app. Also refactored the instantiation flow by separating out the etcd interactions Issue-ID: MULTICLOUD-1064 Signed-off-by: Rajamohan Raj Change-Id: I21ece189daf6e6b3a7cfdba5df22d57b3d33ca78 --- src/orchestrator/pkg/module/instantiation.go | 123 +----------- .../pkg/module/instantiation_appcontext_helper.go | 217 +++++++++++++++++++++ 2 files changed, 224 insertions(+), 116 deletions(-) create mode 100644 src/orchestrator/pkg/module/instantiation_appcontext_helper.go (limited to 'src/orchestrator/pkg/module') diff --git a/src/orchestrator/pkg/module/instantiation.go b/src/orchestrator/pkg/module/instantiation.go index 32e84c34..d7ec663d 100644 --- a/src/orchestrator/pkg/module/instantiation.go +++ b/src/orchestrator/pkg/module/instantiation.go @@ -19,15 +19,11 @@ package module import ( "encoding/base64" "fmt" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - "github.com/onap/multicloud-k8s/src/orchestrator/utils" + log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" "github.com/onap/multicloud-k8s/src/orchestrator/utils/helm" pkgerrors "github.com/pkg/errors" - "io/ioutil" - //"log" - log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" ) // ManifestFileName is the name given to the manifest file in the profile package @@ -172,101 +168,6 @@ func GetSortedTemplateForApp(appName, p, ca, v, rName, cp string, overrideValues return sortedTemplates, err } -// resource consists of name of reource -type resource struct { - name string - filecontent []byte -} - -// getResources shall take in the sorted templates and output the resources -// which consists of name(name+kind) and filecontent -func getResources(st []helm.KubernetesResourceTemplate) ([]resource, error) { - var resources []resource - for _, t := range st { - yamlStruct, err := utils.ExtractYamlParameters(t.FilePath) - yamlFile, err := ioutil.ReadFile(t.FilePath) - if err != nil { - return nil, pkgerrors.Wrap(err, "Failed to get the resources..") - } - n := yamlStruct.Metadata.Name + SEPARATOR + yamlStruct.Kind - - resources = append(resources, resource{name: n, filecontent: yamlFile}) - - log.Info(":: Added resource into resource-order ::", log.Fields{"ResourceName": n}) - } - return resources, nil -} - -func addResourcesToCluster(ct appcontext.AppContext, ch interface{}, resources []resource, resourceOrder []string) error { - - for _, resource := range resources { - resourceOrder = append(resourceOrder, resource.name) - _, err := ct.AddResource(ch, resource.name, resource.filecontent) - if err != nil { - cleanuperr := ct.DeleteCompositeApp() - if cleanuperr != nil { - log.Info(":: Error Cleaning up AppContext after add resource failure ::", log.Fields{"Resource": resource.name, "Error": cleanuperr.Error}) - } - return pkgerrors.Wrapf(err, "Error adding resource ::%s to AppContext", resource.name) - } - _, err = ct.AddInstruction(ch, "resource", "order", resourceOrder) - if err != nil { - cleanuperr := ct.DeleteCompositeApp() - if cleanuperr != nil { - log.Info(":: Error Cleaning up AppContext after add instruction failure ::", log.Fields{"Resource": resource.name, "Error": cleanuperr.Error}) - } - return pkgerrors.Wrapf(err, "Error adding instruction for resource ::%s to AppContext", resource.name) - } - } - return nil -} - -func addClustersToAppContext(l gpic.Clusters, ct appcontext.AppContext, appHandle interface{}, resources []resource) error { - for _, c := range l.ClustersWithName { - p := c.ProviderName - n := c.ClusterName - var resourceOrder []string - clusterhandle, err := ct.AddCluster(appHandle, p+SEPARATOR+n) - if err != nil { - cleanuperr := ct.DeleteCompositeApp() - if cleanuperr != nil { - log.Info(":: Error Cleaning up AppContext after add cluster failure ::", log.Fields{"cluster-provider": p, "cluster-name": n, "Error": cleanuperr.Error}) - } - return pkgerrors.Wrapf(err, "Error adding Cluster(provider::%s and name::%s) to AppContext", p, n) - } - - err = addResourcesToCluster(ct, clusterhandle, resources, resourceOrder) - if err != nil { - return pkgerrors.Wrapf(err, "Error adding Resources to Cluster(provider::%s and name::%s) to AppContext", p, n) - } - } - return nil -} - -/* -verifyResources method is just to check if the resource handles are correctly saved. -*/ - -func verifyResources(l gpic.Clusters, ct appcontext.AppContext, resources []resource, appName string) error { - for _, c := range l.ClustersWithName { - p := c.ProviderName - n := c.ClusterName - cn := p + SEPARATOR + n - for _, res := range resources { - - rh, err := ct.GetResourceHandle(appName, cn, res.name) - if err != nil { - return pkgerrors.Wrapf(err, "Error getting resoure handle for resource :: %s, app:: %s, cluster :: %s", appName, res.name, cn) - } - log.Info(":: GetResourceHandle ::", log.Fields{"ResourceHandler": rh, "appName": appName, "Cluster": cn, "Resource": res.name}) - - } - - } - - return nil -} - /* Instantiate methods takes in projectName, compositeAppName, compositeAppVersion, DeploymentIntentName. This method is responsible for template resolution, intent @@ -295,24 +196,13 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin return pkgerrors.Wrap(err, "Not finding the apps") } - // Make an app context for the compositeApp - context := appcontext.AppContext{} - ctxval, err := context.InitAppContext() + cca, err := makeAppContextForCompositeApp(p, ca, v, rName) if err != nil { - return pkgerrors.Wrap(err, "Error creating AppContext CompositeApp") - } - compositeHandle, err := context.CreateCompositeApp() - if err != nil { - return pkgerrors.Wrap(err, "Error creating CompositeApp handle") - } - err = context.AddCompositeAppMeta(appcontext.CompositeAppMeta{Project: p, CompositeApp: ca, Version: v, Release: rName}) - if err != nil { - return pkgerrors.Wrap(err, "Error Adding CompositeAppMeta") + return err } - - m, err := context.GetCompositeAppMeta() - - log.Info(":: The meta data stored in the runtime context :: ", log.Fields{"Project": m.Project, "CompositeApp": m.CompositeApp, "Version": m.Version, "Release": m.Release}) + context := cca.context + ctxval := cca.ctxval + compositeHandle := cca.compositeAppHandle var appOrder []string @@ -336,6 +226,7 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin if err != nil { return pkgerrors.Wrap(err, "Unable to get the intents for app") } + // listOfClusters shall have both mandatoryClusters and optionalClusters where the app needs to be installed. listOfClusters, err := gpic.IntentResolver(specData.Intent) if err != nil { return pkgerrors.Wrap(err, "Unable to get the intents resolved for app") diff --git a/src/orchestrator/pkg/module/instantiation_appcontext_helper.go b/src/orchestrator/pkg/module/instantiation_appcontext_helper.go new file mode 100644 index 00000000..1734a0c8 --- /dev/null +++ b/src/orchestrator/pkg/module/instantiation_appcontext_helper.go @@ -0,0 +1,217 @@ +/* + * 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 + +/* +This file deals with the interaction of instantiation flow and etcd. +It contains methods for creating appContext, saving cluster and resource details to etcd. + +*/ +import ( + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" + gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic" + log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" + "github.com/onap/multicloud-k8s/src/orchestrator/utils" + "github.com/onap/multicloud-k8s/src/orchestrator/utils/helm" + pkgerrors "github.com/pkg/errors" + "io/ioutil" +) + +// resource consists of name of reource +type resource struct { + name string + filecontent []byte +} + +type contextForCompositeApp struct { + context appcontext.AppContext + ctxval interface{} + compositeAppHandle interface{} +} + +// makeAppContext creates an appContext for a compositeApp and returns the output as contextForCompositeApp +func makeAppContextForCompositeApp(p, ca, v, rName string) (contextForCompositeApp, error) { + context := appcontext.AppContext{} + ctxval, err := context.InitAppContext() + if err != nil { + return contextForCompositeApp{}, pkgerrors.Wrap(err, "Error creating AppContext CompositeApp") + } + compositeHandle, err := context.CreateCompositeApp() + if err != nil { + return contextForCompositeApp{}, pkgerrors.Wrap(err, "Error creating CompositeApp handle") + } + err = context.AddCompositeAppMeta(appcontext.CompositeAppMeta{Project: p, CompositeApp: ca, Version: v, Release: rName}) + if err != nil { + return contextForCompositeApp{}, pkgerrors.Wrap(err, "Error Adding CompositeAppMeta") + } + + m, err := context.GetCompositeAppMeta() + + log.Info(":: The meta data stored in the runtime context :: ", log.Fields{"Project": m.Project, "CompositeApp": m.CompositeApp, "Version": m.Version, "Release": m.Release}) + + cca := contextForCompositeApp{context: context, ctxval: ctxval, compositeAppHandle: compositeHandle} + + return cca, nil + +} + +// getResources shall take in the sorted templates and output the resources +// which consists of name(name+kind) and filecontent +func getResources(st []helm.KubernetesResourceTemplate) ([]resource, error) { + var resources []resource + for _, t := range st { + yamlStruct, err := utils.ExtractYamlParameters(t.FilePath) + yamlFile, err := ioutil.ReadFile(t.FilePath) + if err != nil { + return nil, pkgerrors.Wrap(err, "Failed to get the resources..") + } + n := yamlStruct.Metadata.Name + SEPARATOR + yamlStruct.Kind + + resources = append(resources, resource{name: n, filecontent: yamlFile}) + + log.Info(":: Added resource into resource-order ::", log.Fields{"ResourceName": n}) + } + return resources, nil +} + +func addResourcesToCluster(ct appcontext.AppContext, ch interface{}, resources []resource, resourceOrder []string) error { + + for _, resource := range resources { + resourceOrder = append(resourceOrder, resource.name) + _, err := ct.AddResource(ch, resource.name, resource.filecontent) + if err != nil { + cleanuperr := ct.DeleteCompositeApp() + if cleanuperr != nil { + log.Info(":: Error Cleaning up AppContext after add resource failure ::", log.Fields{"Resource": resource.name, "Error": cleanuperr.Error}) + } + return pkgerrors.Wrapf(err, "Error adding resource ::%s to AppContext", resource.name) + } + _, err = ct.AddInstruction(ch, "resource", "order", resourceOrder) + if err != nil { + cleanuperr := ct.DeleteCompositeApp() + if cleanuperr != nil { + log.Info(":: Error Cleaning up AppContext after add instruction failure ::", log.Fields{"Resource": resource.name, "Error": cleanuperr.Error}) + } + return pkgerrors.Wrapf(err, "Error adding instruction for resource ::%s to AppContext", resource.name) + } + } + return nil +} + +//addClustersToAppContext method shall add cluster details save into etcd +func addClustersToAppContext(l gpic.ClusterList, ct appcontext.AppContext, appHandle interface{}, resources []resource) error { + mc := l.MandatoryClusters + gc := l.ClusterGroups + + for _, c := range mc { + p := c.ProviderName + n := c.ClusterName + var resourceOrder []string + clusterhandle, err := ct.AddCluster(appHandle, p+SEPARATOR+n) + if err != nil { + cleanuperr := ct.DeleteCompositeApp() + if cleanuperr != nil { + log.Info(":: Error Cleaning up AppContext after add cluster failure ::", log.Fields{"cluster-provider": p, "cluster-name": n, "Error": cleanuperr.Error}) + } + return pkgerrors.Wrapf(err, "Error adding Cluster(provider::%s and name::%s) to AppContext", p, n) + } + + err = addResourcesToCluster(ct, clusterhandle, resources, resourceOrder) + if err != nil { + return pkgerrors.Wrapf(err, "Error adding Resources to Cluster(provider::%s and name::%s) to AppContext", p, n) + } + } + + for _, eachGrp := range gc { + oc := eachGrp.OptionalClusters + gn := eachGrp.GroupNumber + + for _, eachCluster := range oc { + p := eachCluster.ProviderName + n := eachCluster.ClusterName + + var resourceOrder []string + clusterhandle, err := ct.AddCluster(appHandle, p+SEPARATOR+n) + + if err != nil { + cleanuperr := ct.DeleteCompositeApp() + if cleanuperr != nil { + log.Info(":: Error Cleaning up AppContext after add cluster failure ::", log.Fields{"cluster-provider": p, "cluster-name": n, "GroupName": gn, "Error": cleanuperr.Error}) + } + return pkgerrors.Wrapf(err, "Error adding Cluster(provider::%s and name::%s) to AppContext", p, n) + } + + err = ct.AddClusterMetaGrp(clusterhandle, gn) + if err != nil { + cleanuperr := ct.DeleteCompositeApp() + if cleanuperr != nil { + log.Info(":: Error Cleaning up AppContext after add cluster failure ::", log.Fields{"cluster-provider": p, "cluster-name": n, "GroupName": gn, "Error": cleanuperr.Error}) + } + return pkgerrors.Wrapf(err, "Error adding Cluster(provider::%s and name::%s) to AppContext", p, n) + } + + err = addResourcesToCluster(ct, clusterhandle, resources, resourceOrder) + if err != nil { + return pkgerrors.Wrapf(err, "Error adding Resources to Cluster(provider::%s, name::%s and groupName:: %s) to AppContext", p, n, gn) + } + } + } + return nil +} + +/* +verifyResources method is just to check if the resource handles are correctly saved. +*/ +func verifyResources(l gpic.ClusterList, ct appcontext.AppContext, resources []resource, appName string) error { + + for _, cg := range l.ClusterGroups { + gn := cg.GroupNumber + oc := cg.OptionalClusters + for _, eachCluster := range oc { + p := eachCluster.ProviderName + n := eachCluster.ClusterName + cn := p + SEPARATOR + n + + for _, res := range resources { + rh, err := ct.GetResourceHandle(appName, cn, res.name) + if err != nil { + return pkgerrors.Wrapf(err, "Error getting resoure handle for resource :: %s, app:: %s, cluster :: %s, groupName :: %s", appName, res.name, cn, gn) + } + log.Info(":: GetResourceHandle ::", log.Fields{"ResourceHandler": rh, "appName": appName, "Cluster": cn, "Resource": res.name}) + } + } + grpMap, err := ct.GetClusterGroupMap(appName) + if err != nil { + return pkgerrors.Wrapf(err, "Error getting GetGroupMap for app:: %s, groupName :: %s", appName, gn) + } + log.Info(":: GetGroupMapReults ::", log.Fields{"GroupMap": grpMap}) + } + + for _, mc := range l.MandatoryClusters { + p := mc.ProviderName + n := mc.ClusterName + cn := p + SEPARATOR + n + for _, res := range resources { + rh, err := ct.GetResourceHandle(appName, cn, res.name) + if err != nil { + return pkgerrors.Wrapf(err, "Error getting resoure handle for resource :: %s, app:: %s, cluster :: %s", appName, res.name, cn) + } + log.Info(":: GetResourceHandle ::", log.Fields{"ResourceHandler": rh, "appName": appName, "Cluster": cn, "Resource": res.name}) + } + } + return nil +} -- cgit 1.2.3-korg