2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
23 "k8s.io/client-go/kubernetes"
25 pkgerrors "github.com/pkg/errors"
26 yaml "gopkg.in/yaml.v2"
28 utils "k8splugin/internal"
31 func generateExternalVNFID() string {
34 return hex.EncodeToString(b)
37 func ensuresNamespace(namespace string, kubeclient kubernetes.Interface) error {
38 namespacePlugin, ok := utils.LoadedPlugins["namespace"]
40 return pkgerrors.New("No plugin for namespace resource found")
43 symGetNamespaceFunc, err := namespacePlugin.Lookup("Get")
45 return pkgerrors.Wrap(err, "Error fetching get namespace function")
48 ns, err := symGetNamespaceFunc.(func(string, string, kubernetes.Interface) (string, error))(
49 namespace, namespace, kubeclient)
51 return pkgerrors.Wrap(err, "An error ocurred during the get namespace execution")
55 log.Println("Creating " + namespace + " namespace")
56 symGetNamespaceFunc, err := namespacePlugin.Lookup("Create")
58 return pkgerrors.Wrap(err, "Error fetching create namespace plugin")
60 namespaceResource := &utils.ResourceData{
64 _, err = symGetNamespaceFunc.(func(*utils.ResourceData, kubernetes.Interface) (string, error))(
65 namespaceResource, kubeclient)
67 return pkgerrors.Wrap(err, "Error creating "+namespace+" namespace")
73 // CreateVNF reads the CSAR files from the files system and creates them one by one
74 var CreateVNF = func(csarID string, cloudRegionID string, namespace string, kubeclient *kubernetes.Clientset) (string, map[string][]string, error) {
75 if err := ensuresNamespace(namespace, kubeclient); err != nil {
76 return "", nil, pkgerrors.Wrap(err, "Error while ensuring namespace: "+namespace)
78 externalVNFID := generateExternalVNFID()
79 internalVNFID := cloudRegionID + "-" + namespace + "-" + externalVNFID
81 csarDirPath := os.Getenv("CSAR_DIR") + "/" + csarID
82 metadataYAMLPath := csarDirPath + "/metadata.yaml"
84 log.Println("Reading " + metadataYAMLPath + " file")
85 metadataFile, err := ReadMetadataFile(metadataYAMLPath)
87 return "", nil, pkgerrors.Wrap(err, "Error while reading Metadata File: "+metadataYAMLPath)
91 resourceYAMLNameMap := make(map[string][]string)
92 // Iterates over the resources defined in the metadata file to create kubernetes resources
93 log.Println(string(len(metadataFile.ResourceTypePathMap)) + " resource(s) type(s) to be processed")
94 for resource, fileNames := range metadataFile.ResourceTypePathMap {
95 log.Println("Processing items of " + string(resource) + " resource")
96 var resourcesCreated []string
97 for _, filename := range fileNames {
98 path = csarDirPath + "/" + filename
100 if _, err := os.Stat(path); os.IsNotExist(err) {
101 return "", nil, pkgerrors.New("File " + path + "does not exists")
103 log.Println("Processing file: " + path)
105 genericKubeData := &utils.ResourceData{
107 Namespace: namespace,
108 VnfId: internalVNFID,
111 typePlugin, ok := utils.LoadedPlugins[resource]
113 return "", nil, pkgerrors.New("No plugin for resource " + resource + " found")
116 symCreateResourceFunc, err := typePlugin.Lookup("Create")
118 return "", nil, pkgerrors.Wrap(err, "Error fetching "+resource+" plugin")
121 internalResourceName, err := symCreateResourceFunc.(func(*utils.ResourceData, kubernetes.Interface) (string, error))(
122 genericKubeData, kubeclient)
124 return "", nil, pkgerrors.Wrap(err, "Error in plugin "+resource+" plugin")
126 log.Print(internalResourceName + " succesful resource created")
127 resourcesCreated = append(resourcesCreated, internalResourceName)
129 resourceYAMLNameMap[resource] = resourcesCreated
132 return externalVNFID, resourceYAMLNameMap, nil
135 // DestroyVNF deletes VNFs based on data passed
136 var DestroyVNF = func(data map[string][]string, namespace string, kubeclient *kubernetes.Clientset) error {
139 "deployment": ["cloud1-default-uuid-sisedeploy1", "cloud1-default-uuid-sisedeploy2", ... ]
140 "service": ["cloud1-default-uuid-sisesvc1", "cloud1-default-uuid-sisesvc2", ... ]
144 for resourceName, resourceList := range data {
145 typePlugin, ok := utils.LoadedPlugins[resourceName]
147 return pkgerrors.New("No plugin for resource " + resourceName + " found")
150 symDeleteResourceFunc, err := typePlugin.Lookup("Delete")
152 return pkgerrors.Wrap(err, "Error fetching "+resourceName+" plugin")
155 for _, resourceName := range resourceList {
157 log.Println("Deleting resource: " + resourceName)
159 err = symDeleteResourceFunc.(func(string, string, kubernetes.Interface) error)(
160 resourceName, namespace, kubeclient)
162 return pkgerrors.Wrap(err, "Error destroying "+resourceName)
170 // MetadataFile stores the metadata of execution
171 type MetadataFile struct {
172 ResourceTypePathMap map[string][]string `yaml:"resources"`
175 // ReadMetadataFile reads the metadata yaml to return the order or reads
176 var ReadMetadataFile = func(path string) (MetadataFile, error) {
177 var metadataFile MetadataFile
179 if _, err := os.Stat(path); os.IsNotExist(err) {
180 return metadataFile, pkgerrors.Wrap(err, "Metadata YAML file does not exist")
183 log.Println("Reading metadata YAML: " + path)
184 yamlFile, err := ioutil.ReadFile(path)
186 return metadataFile, pkgerrors.Wrap(err, "Metadata YAML file read error")
189 err = yaml.Unmarshal(yamlFile, &metadataFile)
191 return metadataFile, pkgerrors.Wrap(err, "Metadata YAML file unmarshal error")
193 log.Printf("metadata:\n%v", metadataFile)
195 return metadataFile, nil