diff options
-rw-r--r-- | src/k8splugin/csar/parser.go | 162 | ||||
-rw-r--r-- | src/k8splugin/mock_files/mock_yamls/metadata.yaml | 4 | ||||
-rw-r--r-- | tox.ini | 5 | ||||
-rw-r--r-- | vagrant/Vagrantfile | 2 | ||||
-rw-r--r-- | vagrant/playbooks/krd-vars.yml | 2 |
5 files changed, 82 insertions, 93 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 @@ -14,10 +14,13 @@ deps = rstcheck whitelist_externals = bash commands = bash -c "find {toxinidir} -not -path {toxinidir}/.tox/\* \ + -not -path {toxinidir}/pkg/dep/\* \ + -not -path {toxinidir}/src/k8splugin/vendor/\* \ + -not -path {toxinidir}/src/github.com/\* \ -name \*.sh -type f \ # E006 check for lines longer than 79 columns -print0 | xargs -0 bashate -v -iE006" - bash -c "find {toxinidir} -not -path {toxinidir}/.tox/\* \ + bash -c "find {toxinidir}/docs \ -name \*.rst -type f -print0 | xargs -0 rstcheck --report warning" [testenv:docs] diff --git a/vagrant/Vagrantfile b/vagrant/Vagrantfile index 61031bba..7ff18f06 100644 --- a/vagrant/Vagrantfile +++ b/vagrant/Vagrantfile @@ -109,7 +109,7 @@ Vagrant.configure("2") do |config| installer.vm.synced_folder '../', '/root/go/src/k8-plugin-multicloud/', type: sync_type installer.vm.provision 'shell' do |sh| sh.path = "installer.sh" - sh.args = ['-p', '-v', '-w', '/vagrant'] + sh.args = ['-p', '-v', '-w', '/root/go/src/k8-plugin-multicloud/vagrant'] end end end diff --git a/vagrant/playbooks/krd-vars.yml b/vagrant/playbooks/krd-vars.yml index 7c4f97f1..941f0fa2 100644 --- a/vagrant/playbooks/krd-vars.yml +++ b/vagrant/playbooks/krd-vars.yml @@ -12,7 +12,7 @@ base_dest: /tmp multus_dest: "{{ base_dest }}/multus-cni" multus_source_type: "tarball" -multus_version: 3.1 +multus_version: 2.0 multus_url: "https://github.com/intel/multus-cni/releases/download/v{{ multus_version }}/multus-cni_v{{ multus_version }}_linux_amd64.tar.gz" #multus_source_type: "source" #multus_version: def72938cd2fb272eb3a6f64a8162b1049404357 |