diff options
Diffstat (limited to 'src/orchestrator/pkg')
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation.go | 24 | ||||
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation_test.go | 37 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/app.go | 1 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/composite_profile.go | 6 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/instantiation.go | 142 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/module.go | 4 | ||||
-rw-r--r-- | src/orchestrator/pkg/rtcontext/rtcontext_test.go | 4 |
7 files changed, 208 insertions, 10 deletions
diff --git a/src/orchestrator/pkg/infra/validation/validation.go b/src/orchestrator/pkg/infra/validation/validation.go index 0b44a8ba..448ea5de 100644 --- a/src/orchestrator/pkg/infra/validation/validation.go +++ b/src/orchestrator/pkg/infra/validation/validation.go @@ -71,21 +71,37 @@ func IsTarGz(r io.Reader) error { } func IsIpv4Cidr(cidr string) error { - _, _, err := net.ParseCIDR(cidr) - if err != nil { - return pkgerrors.Wrapf(err, "could not parse subnet %v", cidr) + ip, _, err := net.ParseCIDR(cidr) + if err != nil || ip.To4() == nil { + return pkgerrors.Wrapf(err, "could not parse ipv4 cidr %v", cidr) } return nil } -func IsIpv4(ip string) error { +func IsIp(ip string) error { addr := net.ParseIP(ip) if addr == nil { + return pkgerrors.Errorf("invalid ip address %v", ip) + } + return nil +} + +func IsIpv4(ip string) error { + addr := net.ParseIP(ip) + if addr == nil || addr.To4() == nil { return pkgerrors.Errorf("invalid ipv4 address %v", ip) } return nil } +func IsMac(mac string) error { + _, err := net.ParseMAC(mac) + if err != nil { + return pkgerrors.Errorf("invalid MAC address %v", mac) + } + return nil +} + // default name check - matches valid label value with addtion that length > 0 func IsValidName(name string) []string { var errs []string diff --git a/src/orchestrator/pkg/infra/validation/validation_test.go b/src/orchestrator/pkg/infra/validation/validation_test.go index 5109b6c7..6a7f504b 100644 --- a/src/orchestrator/pkg/infra/validation/validation_test.go +++ b/src/orchestrator/pkg/infra/validation/validation_test.go @@ -185,6 +185,43 @@ func TestIsIpv4(t *testing.T) { }) } +func TestIsMac(t *testing.T) { + t.Run("Valid MAC", func(t *testing.T) { + validmacs := []string{ + "11:22:33:44:55:66", + "ab-cd-ef-12-34-56", + "AB-CD-EF-12-34-56", + } + for _, mac := range validmacs { + err := IsMac(mac) + if err != nil { + t.Errorf("Valid MAC string failed to pass: %v", mac) + } + } + }) + + t.Run("Invalid MAC", func(t *testing.T) { + invalidmacs := []string{ + "", + "1.2.3.4.5", + "1.2.3.45/32", + "ab:cd:ef:gh:12:34", + "11:22-33-44:55:66", + "11,22,33,44,55,66", + "11|22|33|44|55|66", + "11:22:33:44:55:66:77", + "11-22-33-44-55", + "11-22-33-44-55-66-77", + } + for _, mac := range invalidmacs { + err := IsMac(mac) + if err == nil { + t.Errorf("Invalid MAC passed: %v", mac) + } + } + }) +} + func TestIsValidString(t *testing.T) { t.Run("Valid Strings", func(t *testing.T) { validStrings := []struct { diff --git a/src/orchestrator/pkg/module/app.go b/src/orchestrator/pkg/module/app.go index 1e1a5974..40659de8 100644 --- a/src/orchestrator/pkg/module/app.go +++ b/src/orchestrator/pkg/module/app.go @@ -38,6 +38,7 @@ type AppMetaData struct { } //AppContent contains fileContent +// TODO : This should have been []byte type AppContent struct { FileContent string } diff --git a/src/orchestrator/pkg/module/composite_profile.go b/src/orchestrator/pkg/module/composite_profile.go index dca2116a..25a9721c 100644 --- a/src/orchestrator/pkg/module/composite_profile.go +++ b/src/orchestrator/pkg/module/composite_profile.go @@ -121,7 +121,7 @@ func (c *CompositeProfileClient) CreateCompositeProfile(cpf CompositeProfile, p return cpf, nil } -// GetCompositeProfile shall take arguments - name of the composite profile, name of //// the project, name of the composite app and version of the composite app. It shall return the CompositeProfile if its present. +// GetCompositeProfile shall take arguments - name of the composite profile, name of the project, name of the composite app and version of the composite app. It shall return the CompositeProfile if its present. func (c *CompositeProfileClient) GetCompositeProfile(cpf string, p string, ca string, v string) (CompositeProfile, error) { key := CompositeProfileKey{ Name: cpf, @@ -147,7 +147,7 @@ func (c *CompositeProfileClient) GetCompositeProfile(cpf string, p string, ca st return CompositeProfile{}, pkgerrors.New("Error getting CompositeProfile") } -// GetCompositeProfile shall take arguments - name of the composite profile, name of //// the project, name of the composite app and version of the composite app. It shall return the CompositeProfile if its present. +// GetCompositeProfiles shall take arguments - name of the project, name of the composite profile and version of the composite app. It shall return an array of CompositeProfile. func (c *CompositeProfileClient) GetCompositeProfiles(p string, ca string, v string) ([]CompositeProfile, error) { key := CompositeProfileKey{ Name: "", @@ -175,7 +175,7 @@ func (c *CompositeProfileClient) GetCompositeProfiles(p string, ca string, v str return resp, nil } -// DeleteCompositeProfile the intent from the database +// DeleteCompositeProfile deletes the compsiteApp profile from the database func (c *CompositeProfileClient) DeleteCompositeProfile(cpf string, p string, ca string, v string) error { key := CompositeProfileKey{ Name: cpf, diff --git a/src/orchestrator/pkg/module/instantiation.go b/src/orchestrator/pkg/module/instantiation.go new file mode 100644 index 00000000..5fabe81e --- /dev/null +++ b/src/orchestrator/pkg/module/instantiation.go @@ -0,0 +1,142 @@ +/* + * 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 ( + "fmt" + "github.com/onap/multicloud-k8s/src/orchestrator/utils/helm" + + pkgerrors "github.com/pkg/errors" + + "encoding/base64" + "log" +) + +// ManifestFileName is the name given to the manifest file in the profile package +const ManifestFileName = "manifest.yaml" + +// InstantiationClient implements the InstantiationManager +type InstantiationClient struct { + storeName string + tagMetaData string +} + +// InstantiationManager is an interface which exposes the +// InstantiationManager functionalities +type InstantiationManager interface { + //ApproveInstantiation(p string, ca string, v string, di string) (error) + Instantiate(p string, ca string, v string, di string) error +} + +// NewInstantiationClient returns an instance of InstantiationClient +func NewInstantiationClient() *InstantiationClient { + return &InstantiationClient{ + storeName: "orchestrator", + tagMetaData: "instantiation", + } +} + +// TODO +//ApproveInstantiation approves an instantiation +// func (c InstantiationClient) ApproveInstantiation(p string, ca string, v string, di string) (error){ +// } + +func getOverrideValuesByAppName(ov []OverrideValues, a string) map[string]string { + for _, eachOverrideVal := range ov { + if eachOverrideVal.AppName == a { + return eachOverrideVal.ValuesObj + } + } + return map[string]string{} +} + +// GetSortedTemplateForApp returns the sorted templates. +//It takes in arguments - appName, project, compositeAppName, releaseName, compositeProfileName, array of override values +func GetSortedTemplateForApp(appName, p, ca, v, rName, cp string, overrideValues []OverrideValues) ([]helm.KubernetesResourceTemplate, error) { + + log.Println("Processing App.. ", appName) + + var sortedTemplates []helm.KubernetesResourceTemplate + + aC, err := NewAppClient().GetAppContent(appName, p, ca, v) + if err != nil { + return sortedTemplates, pkgerrors.Wrap(err, fmt.Sprint("Not finding the content of app:: ", appName)) + } + appContent, err := base64.StdEncoding.DecodeString(aC.FileContent) + if err != nil { + return sortedTemplates, pkgerrors.Wrap(err, "Fail to convert to byte array") + } + log.Println("Got the app content..") + + appPC, err := NewAppProfileClient().GetAppProfileContentByApp(p, ca, v, cp, appName) + if err != nil { + return sortedTemplates, pkgerrors.Wrap(err, fmt.Sprintf("Not finding the appProfileContent for:: %s", appName)) + } + appProfileContent, err := base64.StdEncoding.DecodeString(appPC.Profile) + if err != nil { + return sortedTemplates, pkgerrors.Wrap(err, "Fail to convert to byte array") + } + + log.Println("Got the app Profile content ...") + + overrideValuesOfApp := getOverrideValuesByAppName(overrideValues, appName) + //Convert override values from map to array of strings of the following format + //foo=bar + overrideValuesOfAppStr := []string{} + if overrideValuesOfApp != nil { + for k, v := range overrideValuesOfApp { + overrideValuesOfAppStr = append(overrideValuesOfAppStr, k+"="+v) + } + } + + sortedTemplates, err = helm.NewTemplateClient("", "default", rName, + ManifestFileName).Resolve(appContent, + appProfileContent, overrideValuesOfAppStr, + appName) + + log.Printf("The len of the sortedTemplates :: %d", len(sortedTemplates)) + + return sortedTemplates, err +} + +// Instantiate methods takes in project +func (c InstantiationClient) Instantiate(p string, ca string, v string, di string) error { + + dIGrp, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroup(di, p, ca, v) + if err != nil { + return pkgerrors.Wrap(err, "Not finding the deploymentIntentGroup") + } + rName := dIGrp.Spec.Version //rName is releaseName + overrideValues := dIGrp.Spec.OverrideValuesObj + cp := dIGrp.Spec.Profile + + log.Printf("dIGrp :: %s, releaseName :: %s and cp :: %s \n", dIGrp.MetaData.Name, rName, cp) + allApps, err := NewAppClient().GetApps(p, ca, v) + if err != nil { + return pkgerrors.Wrap(err, "Not finding the apps") + } + for _, eachApp := range allApps { + sortedTemplates, err := GetSortedTemplateForApp(eachApp.Metadata.Name, p, ca, v, rName, cp, overrideValues) + if err != nil { + return pkgerrors.Wrap(err, "Unable to get the sorted templates for app") + } + log.Printf("Resolved all the templates for app :: %s under the compositeApp...", eachApp.Metadata.Name) + log.Printf("sortedTemplates :: %v ", sortedTemplates) + } + log.Printf("Done with instantiation...") + return err +} diff --git a/src/orchestrator/pkg/module/module.go b/src/orchestrator/pkg/module/module.go index c697bbff..e05b8753 100644 --- a/src/orchestrator/pkg/module/module.go +++ b/src/orchestrator/pkg/module/module.go @@ -1,5 +1,5 @@ /* - * Copyright 2018 Intel Corporation, Inc + * 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. @@ -29,6 +29,7 @@ type Client struct { CompositeProfile *CompositeProfileClient AppProfile *AppProfileClient // Add Clients for API's here + Instantiation *InstantiationClient } // NewClient creates a new client for using the services @@ -45,5 +46,6 @@ func NewClient() *Client { c.CompositeProfile = NewCompositeProfileClient() c.AppProfile = NewAppProfileClient() // Add Client API handlers here + c.Instantiation = NewInstantiationClient() return c } diff --git a/src/orchestrator/pkg/rtcontext/rtcontext_test.go b/src/orchestrator/pkg/rtcontext/rtcontext_test.go index e9610ef0..eedbeb82 100644 --- a/src/orchestrator/pkg/rtcontext/rtcontext_test.go +++ b/src/orchestrator/pkg/rtcontext/rtcontext_test.go @@ -59,7 +59,7 @@ func (c *MockContextDb) Delete(key string) error { // Delete all function func (c *MockContextDb) DeleteAll(key string) error { - for kvKey, _ := range c.Items { + for kvKey := range c.Items { delete(c.Items, kvKey) } return c.Err @@ -69,7 +69,7 @@ func (c *MockContextDb) DeleteAll(key string) error { func (c *MockContextDb) GetAllKeys(path string) ([]string, error) { var keys []string - for k, _ := range c.Items { + for k := range c.Items { keys = append(keys, string(k)) } return keys, c.Err |