diff options
-rw-r--r-- | src/k8splugin/csar/parser.go | 162 | ||||
-rw-r--r-- | src/k8splugin/mock_files/mock_yamls/metadata.yaml | 4 |
2 files changed, 76 insertions, 90 deletions
diff --git a/src/k8splugin/csar/parser.go b/src/k8splugin/csar/parser.go index abd6ad92..af4546c6 100644 --- a/src/k8splugin/csar/parser.go +++ b/src/k8splugin/csar/parser.go @@ -28,121 +28,104 @@ import ( "k8splugin/krd" ) -func generateExternalVNFID(charLen int) string { - b := make([]byte, charLen/2) +func generateExternalVNFID() string { + b := make([]byte, 2) rand.Read(b) return hex.EncodeToString(b) } -// CreateVNF reads the CSAR files from the files system and creates them one by one -var CreateVNF = func(csarID string, cloudRegionID string, namespace string, kubeclient *kubernetes.Clientset) (string, map[string][]string, error) { +func ensuresNamespace(namespace string, kubeclient *kubernetes.Clientset) error { namespacePlugin, ok := krd.LoadedPlugins["namespace"] if !ok { - return "", nil, pkgerrors.New("No plugin for namespace resource found") + return pkgerrors.New("No plugin for namespace resource found") } symGetNamespaceFunc, err := namespacePlugin.Lookup("GetResource") if err != nil { - return "", nil, pkgerrors.Wrap(err, "Error fetching namespace plugin") + return pkgerrors.Wrap(err, "Error fetching get namespace function") } - present, err := symGetNamespaceFunc.(func(string, *kubernetes.Clientset) (bool, error))( + exists, err := symGetNamespaceFunc.(func(string, *kubernetes.Clientset) (bool, error))( namespace, kubeclient) if err != nil { - return "", nil, pkgerrors.Wrap(err, "Error in plugin namespace plugin") + return pkgerrors.Wrap(err, "An error ocurred during the get namespace execution") } - if present == false { + if !exists { + log.Println("Creating " + namespace + " namespace") symGetNamespaceFunc, err := namespacePlugin.Lookup("CreateResource") if err != nil { - return "", nil, pkgerrors.Wrap(err, "Error fetching namespace plugin") + return pkgerrors.Wrap(err, "Error fetching create namespace plugin") } err = symGetNamespaceFunc.(func(string, *kubernetes.Clientset) error)( namespace, kubeclient) if err != nil { - return "", nil, pkgerrors.Wrap(err, "Error creating "+namespace+" namespace") + return pkgerrors.Wrap(err, "Error creating "+namespace+" namespace") } } + return nil +} - var path string - - // uuid - externalVNFID := generateExternalVNFID(8) - - // cloud1-default-uuid +// CreateVNF reads the CSAR files from the files system and creates them one by one +var CreateVNF = func(csarID string, cloudRegionID string, namespace string, kubeclient *kubernetes.Clientset) (string, map[string][]string, error) { + if err := ensuresNamespace(namespace, kubeclient); err != nil { + return "", nil, pkgerrors.Wrap(err, "Error while ensuring namespace: "+namespace) + } + externalVNFID := generateExternalVNFID() internalVNFID := cloudRegionID + "-" + namespace + "-" + externalVNFID csarDirPath := os.Getenv("CSAR_DIR") + "/" + csarID metadataYAMLPath := csarDirPath + "/metadata.yaml" - seqFile, err := ReadMetadataFile(metadataYAMLPath) + log.Println("Reading " + metadataYAMLPath + " file") + metadataFile, err := ReadMetadataFile(metadataYAMLPath) if err != nil { return "", nil, pkgerrors.Wrap(err, "Error while reading Metadata File: "+metadataYAMLPath) } + var path string resourceYAMLNameMap := make(map[string][]string) + // Iterates over the resources defined in the metadata file to create kubernetes resources + log.Println(string(len(metadataFile.ResourceTypePathMap)) + " resource(s) type(s) to be processed") + for resource, fileNames := range metadataFile.ResourceTypePathMap { + log.Println("Processing items of " + string(resource) + " resource") + var resourcesCreated []string + for _, filename := range fileNames { + path = csarDirPath + "/" + filename + + if _, err := os.Stat(path); os.IsNotExist(err) { + return "", nil, pkgerrors.New("File " + path + "does not exists") + } + log.Println("Processing file: " + path) + + genericKubeData := &krd.GenericKubeResourceData{ + YamlFilePath: path, + Namespace: namespace, + InternalVNFID: internalVNFID, + } + + typePlugin, ok := krd.LoadedPlugins[resource] + if !ok { + return "", nil, pkgerrors.New("No plugin for resource " + resource + " found") + } + + symCreateResourceFunc, err := typePlugin.Lookup("CreateResource") + if err != nil { + return "", nil, pkgerrors.Wrap(err, "Error fetching "+resource+" plugin") + } - for _, resource := range seqFile.ResourceTypePathMap { - for resourceName, resourceFileNames := range resource { - // Load/Use Deployment data/client - - var resourceNameList []string - - for _, filename := range resourceFileNames { - path = csarDirPath + "/" + filename - - _, err = os.Stat(path) - if os.IsNotExist(err) { - return "", nil, pkgerrors.New("File " + path + "does not exists") - } - - log.Println("Processing file: " + path) - - genericKubeData := &krd.GenericKubeResourceData{ - YamlFilePath: path, - Namespace: namespace, - InternalVNFID: internalVNFID, - } - - typePlugin, ok := krd.LoadedPlugins[resourceName] - if !ok { - return "", nil, pkgerrors.New("No plugin for resource " + resourceName + " found") - } - - symCreateResourceFunc, err := typePlugin.Lookup("CreateResource") - if err != nil { - return "", nil, pkgerrors.Wrap(err, "Error fetching "+resourceName+" plugin") - } - - // cloud1-default-uuid-sisedeploy - internalResourceName, err := symCreateResourceFunc.(func(*krd.GenericKubeResourceData, *kubernetes.Clientset) (string, error))( - genericKubeData, kubeclient) - if err != nil { - return "", nil, pkgerrors.Wrap(err, "Error in plugin "+resourceName+" plugin") - } - - // ["cloud1-default-uuid-sisedeploy1", "cloud1-default-uuid-sisedeploy2", ... ] - resourceNameList = append(resourceNameList, internalResourceName) - - /* - { - "deployment": ["cloud1-default-uuid-sisedeploy1", "cloud1-default-uuid-sisedeploy2", ... ] - } - */ - resourceYAMLNameMap[resourceName] = resourceNameList + internalResourceName, err := symCreateResourceFunc.(func(*krd.GenericKubeResourceData, *kubernetes.Clientset) (string, error))( + genericKubeData, kubeclient) + if err != nil { + return "", nil, pkgerrors.Wrap(err, "Error in plugin "+resource+" plugin") } + log.Print(internalResourceName + " succesful resource created") + resourcesCreated = append(resourcesCreated, internalResourceName) } + resourceYAMLNameMap[resource] = resourcesCreated } - /* - uuid, - { - "deployment": ["cloud1-default-uuid-sisedeploy1", "cloud1-default-uuid-sisedeploy2", ... ] - "service": ["cloud1-default-uuid-sisesvc1", "cloud1-default-uuid-sisesvc2", ... ] - }, - nil - */ return externalVNFID, resourceYAMLNameMap, nil } @@ -183,25 +166,28 @@ var DestroyVNF = func(data map[string][]string, namespace string, kubeclient *ku // MetadataFile stores the metadata of execution type MetadataFile struct { - ResourceTypePathMap []map[string][]string `yaml:"resources"` + ResourceTypePathMap map[string][]string `yaml:"resources"` } // ReadMetadataFile reads the metadata yaml to return the order or reads -var ReadMetadataFile = func(yamlFilePath string) (MetadataFile, error) { - var seqFile MetadataFile +var ReadMetadataFile = func(path string) (MetadataFile, error) { + var metadataFile MetadataFile - if _, err := os.Stat(yamlFilePath); err == nil { - log.Println("Reading metadata YAML: " + yamlFilePath) - rawBytes, err := ioutil.ReadFile(yamlFilePath) - if err != nil { - return seqFile, pkgerrors.Wrap(err, "Metadata YAML file read error") - } + if _, err := os.Stat(path); os.IsNotExist(err) { + return metadataFile, pkgerrors.Wrap(err, "Metadata YAML file does not exist") + } - err = yaml.Unmarshal(rawBytes, &seqFile) - if err != nil { - return seqFile, pkgerrors.Wrap(err, "Metadata YAML file read error") - } + log.Println("Reading metadata YAML: " + path) + yamlFile, err := ioutil.ReadFile(path) + if err != nil { + return metadataFile, pkgerrors.Wrap(err, "Metadata YAML file read error") + } + + err = yaml.Unmarshal(yamlFile, &metadataFile) + if err != nil { + return metadataFile, pkgerrors.Wrap(err, "Metadata YAML file unmarshal error") } + log.Printf("metadata:\n%v", metadataFile) - return seqFile, nil + return metadataFile, nil } diff --git a/src/k8splugin/mock_files/mock_yamls/metadata.yaml b/src/k8splugin/mock_files/mock_yamls/metadata.yaml index dcc1c32e..0289214a 100644 --- a/src/k8splugin/mock_files/mock_yamls/metadata.yaml +++ b/src/k8splugin/mock_files/mock_yamls/metadata.yaml @@ -10,7 +10,7 @@ # limitations under the License. resources: - - deployment: + deployment: - deployment.yaml - - service: + service: - service.yaml |