summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKiran Kamineni <kiran.k.kamineni@intel.com>2019-08-08 15:22:43 -0700
committerKiran Kamineni <kiran.k.kamineni@intel.com>2019-08-13 10:13:01 -0700
commita742fc80fc2f56177c004efc8bb7d43b1b004dd9 (patch)
tree58646ba8da3b050b8bf1b70da5052448676208f7 /src
parenta642f3becd14462123691a637a6fe4835a9b945e (diff)
Fix bug in tagging podTemplates
Fix a bug in tagging podTemplates Using the runtime converter to convert unstructured map data to podTemplateSpec now. Issue-ID: MULTICLOUD-675 Change-Id: I661d3c63ae39b3533bb9c0a9934fc507284c7074 Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/k8splugin/internal/plugin/helpers.go35
1 files changed, 11 insertions, 24 deletions
diff --git a/src/k8splugin/internal/plugin/helpers.go b/src/k8splugin/internal/plugin/helpers.go
index b5c9109c..ad785ab7 100644
--- a/src/k8splugin/internal/plugin/helpers.go
+++ b/src/k8splugin/internal/plugin/helpers.go
@@ -17,7 +17,6 @@
package plugin
import (
- "encoding/json"
"log"
"strings"
@@ -29,6 +28,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+ "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
@@ -107,44 +107,31 @@ func TagPodsIfPresent(unstruct *unstructured.Unstructured, tag string) {
log.Println("Error converting spec to map")
return
}
+
template, ok := spec["template"].(map[string]interface{})
if !ok {
log.Println("Error converting template to map")
return
}
- data, err := json.Marshal(template)
- if err != nil {
- log.Println("Error Marshaling Podspec")
- return
- }
-
//Attempt to convert the template to a podtemplatespec.
//This is to check if we have any pods being created.
podTemplateSpec := &corev1.PodTemplateSpec{}
- _, err = podTemplateSpec.MarshalTo(data)
+ err := runtime.DefaultUnstructuredConverter.FromUnstructured(template, podTemplateSpec)
if err != nil {
- log.Println("Did not find a podTemplateSpec" + err.Error())
+ log.Println("Did not find a podTemplateSpec: " + err.Error())
return
}
- //At this point, we know that the data contains a PodTemplateSpec
- metadata, ok := template["metadata"].(map[string]interface{})
- if !ok {
- log.Println("Error converting metadata to map")
- return
- }
-
- //Get the labels map
- labels, ok := metadata["labels"].(map[string]string)
- if !ok {
- log.Println("Error converting labels to map")
- return
- }
-
- //Check if labels exist for this object
+ labels := podTemplateSpec.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[config.GetConfiguration().KubernetesLabelName] = tag
+ podTemplateSpec.SetLabels(labels)
+
+ updatedTemplate, err := runtime.DefaultUnstructuredConverter.ToUnstructured(podTemplateSpec)
+
+ //Set the label
+ spec["template"] = updatedTemplate
}