diff options
author | Manjunath Ranganathaiah <manjunath.ranganathaiah@intel.com> | 2020-06-26 22:22:13 +0000 |
---|---|---|
committer | Ritu Sood <ritu.sood@intel.com> | 2020-06-27 20:11:33 -0700 |
commit | 8f338f5fb69c509439eb7f9ac7cfe7bd47849f8b (patch) | |
tree | 5839a7e861033ec259da8bcf31e496997b6143fe /src/rsync/pkg | |
parent | 8e446beb55c68314896f16cb3105816cacc05f84 (diff) |
Remove config and yaml file creation
Signed-off-by: Manjunath Ranganathaiah <manjunath.ranganathaiah@intel.com>
Issue-ID: MULTICLOUD-1005
Change-Id: Iaa8b70f38cf1fc1f89cf2d95fbe37c242fc44f65
Diffstat (limited to 'src/rsync/pkg')
-rw-r--r-- | src/rsync/pkg/app/client.go | 40 | ||||
-rw-r--r-- | src/rsync/pkg/context/context.go | 52 | ||||
-rw-r--r-- | src/rsync/pkg/internal/utils.go | 15 | ||||
-rw-r--r-- | src/rsync/pkg/resource/resource.go | 8 |
4 files changed, 32 insertions, 83 deletions
diff --git a/src/rsync/pkg/app/client.go b/src/rsync/pkg/app/client.go index fb57d46b..49997ed0 100644 --- a/src/rsync/pkg/app/client.go +++ b/src/rsync/pkg/app/client.go @@ -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") } diff --git a/src/rsync/pkg/context/context.go b/src/rsync/pkg/context/context.go index 67e589c9..e5da1296 100644 --- a/src/rsync/pkg/context/context.go +++ b/src/rsync/pkg/context/context.go @@ -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") } diff --git a/src/rsync/pkg/internal/utils.go b/src/rsync/pkg/internal/utils.go index 59ff6df8..270edba5 100644 --- a/src/rsync/pkg/internal/utils.go +++ b/src/rsync/pkg/internal/utils.go @@ -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 { diff --git a/src/rsync/pkg/resource/resource.go b/src/rsync/pkg/resource/resource.go index 9d715697..8b45c341 100644 --- a/src/rsync/pkg/resource/resource.go +++ b/src/rsync/pkg/resource/resource.go @@ -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") } |