aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin/plugins/generic
diff options
context:
space:
mode:
authorvamshi.nemalikonda <vn00480215@techmahindra.com>2020-07-24 12:05:51 +0000
committerRitu Sood <Ritu.Sood@intel.com>2020-08-26 15:59:47 +0000
commit9c942a11c14836630ba528b75bdcb2790045b91f (patch)
tree55404d7b7d8a4189c0eed99de39cb42e3a630341 /src/k8splugin/plugins/generic
parent16ca82713faf6678d4b7055130768541f86ea20c (diff)
Improve configure API to support k8s resource updations
Code fore review. Issue-ID: MULTICLOUD-1124 Change-Id: I6bb9786660f6760c15008132f1c254e7f9b39561 Signed-off-by: vamshi.nemalikonda <vn00480215@techmahindra.com> Improve configure API to support k8s resource updations create resource enabled. Issue-ID: MULTICLOUD-1124 Change-Id: I6bb9786660f6760c15008132f1c254e7f9b39561 Signed-off-by: vamshi.nemalikonda <vn00480215@techmahindra.com> Improve configure API to support k8s resource updations code improvements. Issue-ID: MULTICLOUD-1124 Change-Id: I6bb9786660f6760c15008132f1c254e7f9b39561 Signed-off-by: vamshi.nemalikonda <vn00480215@techmahindra.com> Improve configure API to support k8s resource updations fixing unit test failures. Issue-ID: MULTICLOUD-1124 Change-Id: I6bb9786660f6760c15008132f1c254e7f9b39561 Signed-off-by: vamshi.nemalikonda <vn00480215@techmahindra.com>
Diffstat (limited to 'src/k8splugin/plugins/generic')
-rw-r--r--src/k8splugin/plugins/generic/plugin.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/k8splugin/plugins/generic/plugin.go b/src/k8splugin/plugins/generic/plugin.go
index 0711466f..aa503097 100644
--- a/src/k8splugin/plugins/generic/plugin.go
+++ b/src/k8splugin/plugins/generic/plugin.go
@@ -91,6 +91,62 @@ func (g genericPlugin) Create(yamlFilePath string, namespace string, client plug
return createdObj.GetName(), nil
}
+// Update deployment object in a specific Kubernetes cluster
+func (g genericPlugin) Update(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
+ if namespace == "" {
+ namespace = "default"
+ }
+
+ //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)
+ if err != nil {
+ return "", pkgerrors.Wrap(err, "Decode deployment object error")
+ }
+
+ dynClient := client.GetDynamicClient()
+ mapper := client.GetMapper()
+
+ gvk := unstruct.GroupVersionKind()
+ mapping, err := mapper.RESTMapping(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}, gvk.Version)
+ if err != nil {
+ return "", pkgerrors.Wrap(err, "Mapping kind to resource error")
+ }
+
+ //Add the tracking label to all resources created here
+ labels := unstruct.GetLabels()
+ //Check if labels exist for this object
+ if labels == nil {
+ labels = map[string]string{}
+ }
+ labels[config.GetConfiguration().KubernetesLabelName] = client.GetInstanceID()
+ unstruct.SetLabels(labels)
+
+ // This checks if the resource we are creating has a podSpec in it
+ // Eg: Deployment, StatefulSet, Job etc..
+ // If a PodSpec is found, the label will be added to it too.
+ plugin.TagPodsIfPresent(unstruct, client.GetInstanceID())
+
+ gvr := mapping.Resource
+ var updatedObj *unstructured.Unstructured
+
+ switch mapping.Scope.Name() {
+ case meta.RESTScopeNameNamespace:
+ updatedObj, err = dynClient.Resource(gvr).Namespace(namespace).Update(unstruct, metav1.UpdateOptions{})
+ case meta.RESTScopeNameRoot:
+ updatedObj, err = dynClient.Resource(gvr).Update(unstruct, metav1.UpdateOptions{})
+ default:
+ return "", pkgerrors.New("Got an unknown RESTSCopeName for mapping: " + gvk.String())
+ }
+
+ if err != nil {
+ return "", pkgerrors.Wrap(err, "Update object error")
+ }
+
+ return updatedObj.GetName(), nil
+}
+
// Get an existing resource hosted in a specific Kubernetes cluster
func (g genericPlugin) Get(resource helm.KubernetesResource,
namespace string, client plugin.KubernetesConnector) (string, error) {