Remove config and yaml file creation 18/109618/2
authorManjunath Ranganathaiah <manjunath.ranganathaiah@intel.com>
Fri, 26 Jun 2020 22:22:13 +0000 (22:22 +0000)
committerRitu Sood <ritu.sood@intel.com>
Sun, 28 Jun 2020 03:11:33 +0000 (20:11 -0700)
Signed-off-by: Manjunath Ranganathaiah <manjunath.ranganathaiah@intel.com>
Issue-ID: MULTICLOUD-1005
Change-Id: Iaa8b70f38cf1fc1f89cf2d95fbe37c242fc44f65

src/rsync/pkg/app/client.go
src/rsync/pkg/context/context.go
src/rsync/pkg/internal/utils.go
src/rsync/pkg/resource/resource.go

index fb57d46..49997ed 100644 (file)
@@ -30,7 +30,6 @@ import (
        "github.com/onap/multicloud-k8s/src/clm/pkg/cluster"
 )
 
-const basePath string = "/tmp/rsync/"
 
 // KubernetesClient encapsulates the different clients' interfaces
 // we need when interacting with a Kubernetes cluster
@@ -44,45 +43,25 @@ type KubernetesClient struct {
 
 // getKubeConfig uses the connectivity client to get the kubeconfig based on the name
 // of the clustername. This is written out to a file.
-func (k *KubernetesClient) getKubeConfig(clustername string, id string) (string, error) {
+func (k *KubernetesClient) getKubeConfig(clustername string, id string) ([]byte, error) {
 
        if !strings.Contains(clustername, "+") {
-               return "", pkgerrors.New("Not a valid cluster name")
+               return nil, pkgerrors.New("Not a valid cluster name")
        }
        strs := strings.Split(clustername, "+")
        if len(strs) != 2 {
-               return "", pkgerrors.New("Not a valid cluster name")
+               return nil, pkgerrors.New("Not a valid cluster name")
        }
        kubeConfig, err := cluster.NewClusterClient().GetClusterContent(strs[0], strs[1])
        if err != nil {
-               return "", pkgerrors.New("Get kubeconfig failed")
+               return nil, pkgerrors.New("Get kubeconfig failed")
        }
 
-       var kubeConfigPath string = basePath + id + "/" + clustername + "/"
-
-       if _, err := os.Stat(kubeConfigPath); os.IsNotExist(err) {
-               err = os.MkdirAll(kubeConfigPath, 0755)
-                       if err != nil {
-                               return "", err
-                       }
-       }
-       kubeConfigPath = kubeConfigPath + "config"
-
-       f, err := os.Create(kubeConfigPath)
-       defer f.Close()
-       if err != nil {
-               return "", err
-       }
        dec, err := base64.StdEncoding.DecodeString(kubeConfig.Kubeconfig)
        if err != nil {
-               return "", err
+               return nil, err
        }
-       _, err = f.Write(dec)
-       if err != nil {
-               return "", err
-       }
-
-       return kubeConfigPath, nil
+       return dec, nil
 }
 
 // init loads the Kubernetes configuation values stored into the local configuration file
@@ -97,15 +76,12 @@ func (k *KubernetesClient) Init(clustername string, iid string) error {
 
        k.instanceID = iid
 
-       configPath, err := k.getKubeConfig(clustername, iid)
+       configData, err := k.getKubeConfig(clustername, iid)
        if err != nil {
                return pkgerrors.Wrap(err, "Get kubeconfig file")
        }
 
-       //Remove kubeconfigfile after the clients are created
-       defer os.Remove(configPath)
-
-       config, err := clientcmd.BuildConfigFromFlags("", configPath)
+       config, err := clientcmd.RESTConfigFromKubeConfig(configData)
        if err != nil {
                return pkgerrors.Wrap(err, "setConfig: Build config from flags raised an error")
        }
index 67e589c..e5da129 100644 (file)
@@ -20,7 +20,6 @@ import (
        "encoding/json"
         "fmt"
         "log"
-        "os"
         "sync"
         "strings"
         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext"
@@ -50,8 +49,6 @@ type instMap struct {
        clusters []clusterInfo
 }
 
-const basePath string = "/tmp/rsync/"
-
 func getInstMap(order string, dependency string, level string) ([]instMap, error) {
 
         if order == "" {
@@ -97,10 +94,10 @@ func deleteResource(clustername string, resname string, respath string) error {
        var gp res.Resource
        err = gp.Delete(respath, resname, "default", c)
        if err != nil {
-               log.Println("Delete resource failed: " +  err.Error())
+               log.Println("Delete resource failed: " +  err.Error() + resname)
                return err
        }
-       log.Println("Resource succesfully deleted")
+       log.Println("Resource succesfully deleted", resname)
        return nil
 
 }
@@ -118,16 +115,16 @@ func createResource(clustername string, resname string, respath string) error {
        var gp res.Resource
        _, err = gp.Create(respath,"default", c)
        if err != nil {
-               log.Println("Create failed: " +  err.Error())
+               log.Println("Create failed: " +  err.Error() + resname)
                return err
        }
-       log.Println("Resource succesfully created")
+       log.Println("Resource succesfully created", resname)
        return nil
 
 }
 
 func terminateResource(ac appcontext.AppContext, resmap instMap, appname string, clustername string) error {
-       var resPath string = basePath + appname + "/" + clustername + "/resources/"
+
        rh, err := ac.GetResourceHandle(appname, clustername, resmap.name)
        if err != nil {
                return err
@@ -139,31 +136,14 @@ func terminateResource(ac appcontext.AppContext, resmap instMap, appname string,
        }
 
        if resval != "" {
-               if _, err := os.Stat(resPath); os.IsNotExist(err) {
-                       err = os.MkdirAll(resPath, 0755)
-                       if err != nil {
-                               return err
-                       }
-               }
-               resPath := resPath + resmap.name + ".yaml"
-               f, err := os.Create(resPath)
-               defer f.Close()
-               if err != nil {
-                       return err
-               }
-               _, err = f.WriteString(resval.(string))
-               if err != nil {
-                       return err
-               }
                result := strings.Split(resmap.name, "+")
                if result[0] == "" {
                        return pkgerrors.Errorf("Resource name is nil")
                }
-               err = deleteResource(clustername, result[0], resPath)
+               err = deleteResource(clustername, result[0], resval.(string))
                if err != nil {
                        return err
                }
-               //defer os.Remove(resPath)
        } else {
                return pkgerrors.Errorf("Resource value is nil")
        }
@@ -173,7 +153,6 @@ func terminateResource(ac appcontext.AppContext, resmap instMap, appname string,
 }
 
 func instantiateResource(ac appcontext.AppContext, resmap instMap, appname string, clustername string) error {
-       var resPath string = basePath + appname + "/" + clustername + "/resources/"
        rh, err := ac.GetResourceHandle(appname, clustername, resmap.name)
        if err != nil {
                return err
@@ -185,31 +164,14 @@ func instantiateResource(ac appcontext.AppContext, resmap instMap, appname strin
        }
 
        if resval != "" {
-               if _, err := os.Stat(resPath); os.IsNotExist(err) {
-                       err = os.MkdirAll(resPath, 0755)
-                       if err != nil {
-                               return err
-                       }
-               }
-               resPath := resPath + resmap.name + ".yaml"
-               f, err := os.Create(resPath)
-               defer f.Close()
-               if err != nil {
-                       return err
-               }
-               _, err = f.WriteString(resval.(string))
-               if err != nil {
-                       return err
-               }
                result := strings.Split(resmap.name, "+")
                if result[0] == "" {
                        return pkgerrors.Errorf("Resource name is nil")
                }
-               err = createResource(clustername, result[0], resPath)
+               err = createResource(clustername, result[0], resval.(string))
                if err != nil {
                        return err
                }
-               //defer os.Remove(resPath)
        } else {
                return pkgerrors.Errorf("Resource value is nil")
        }
index 59ff6df..270edba 100644 (file)
@@ -23,8 +23,8 @@ import (
        "k8s.io/client-go/kubernetes/scheme"
 )
 
-// DecodeYAML reads a YAMl file to extract the Kubernetes object definition
-func DecodeYAML(path string, into runtime.Object) (runtime.Object, error) {
+// DecodeYAMLFile reads a YAMl file to extract the Kubernetes object definition
+func DecodeYAMLFile(path string, into runtime.Object) (runtime.Object, error) {
        if _, err := os.Stat(path); err != nil {
                if os.IsNotExist(err) {
                        return nil, pkgerrors.New("File " + path + " not found")
@@ -47,6 +47,17 @@ func DecodeYAML(path string, into runtime.Object) (runtime.Object, error) {
        return obj, nil
 }
 
+// DecodeYAMLData reads a string to extract the Kubernetes object definition
+func DecodeYAMLData(data string, into runtime.Object) (runtime.Object, error) {
+       decode := scheme.Codecs.UniversalDeserializer().Decode
+       obj, _, err := decode([]byte(data), nil, into)
+       if err != nil {
+               return nil, pkgerrors.Wrap(err, "Deserialize YAML error")
+       }
+
+       return obj, nil
+}
+
 //EnsureDirectory makes sure that the directories specified in the path exist
 //If not, it will create them, if possible.
 func EnsureDirectory(f string) error {
index 9d71569..8b45c34 100644 (file)
@@ -29,7 +29,7 @@ type Resource struct {
 }
 
 // Create deployment object in a specific Kubernetes cluster
-func (r Resource) Create(yamlFilePath string, namespace string, client connector.KubernetesConnector) (string, error) {
+func (r Resource) Create(data string, namespace string, client connector.KubernetesConnector) (string, error) {
        if namespace == "" {
                namespace = "default"
        }
@@ -37,7 +37,7 @@ func (r Resource) Create(yamlFilePath string, namespace string, client connector
        //Decode the yaml file to create a runtime.Object
        unstruct := &unstructured.Unstructured{}
        //Ignore the returned obj as we expect the data in unstruct
-       _, err := utils.DecodeYAML(yamlFilePath, unstruct)
+       _, err := utils.DecodeYAMLData(data, unstruct)
        if err != nil {
                return "", pkgerrors.Wrap(err, "Decode deployment object error")
        }
@@ -85,7 +85,7 @@ func (r Resource) Create(yamlFilePath string, namespace string, client connector
 }
 
 // Delete an existing resource hosted in a specific Kubernetes cluster
-func (r Resource) Delete(yamlFilePath string, resname string, namespace string, client connector.KubernetesConnector) error {
+func (r Resource) Delete(data string, resname string, namespace string, client connector.KubernetesConnector) error {
         if namespace == "" {
                 namespace = "default"
         }
@@ -93,7 +93,7 @@ func (r Resource) Delete(yamlFilePath string, resname string, namespace string,
         //Decode the yaml file to create a runtime.Object
         unstruct := &unstructured.Unstructured{}
         //Ignore the returned obj as we expect the data in unstruct
-        _, err := utils.DecodeYAML(yamlFilePath, unstruct)
+        _, err := utils.DecodeYAMLData(data, unstruct)
         if err != nil {
                 return pkgerrors.Wrap(err, "Decode deployment object error")
         }