diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-07-17 16:55:00 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-08-07 13:06:51 -0700 |
commit | b75115b0678a1034ffc1d1c8fee40c7f5b995c97 (patch) | |
tree | 3e2b96eef05d58c913f53a9cbbcc40e2bfd7ce4d /src/k8splugin/internal/plugin/helpers.go | |
parent | 99d4574f6385666f21d5c31fd0e9046a2ab509ef (diff) |
Add custom label to track created resources
Create a custom label on created resources
Also, create it on pods where pods are being
created.
This will help us later for filtering and querying
pods and resources.
Issue-ID: MULTICLOUD-675
Change-Id: I4b4fce7b67f9f27559d99dcca94a9191b96cb7c6
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/internal/plugin/helpers.go')
-rw-r--r-- | src/k8splugin/internal/plugin/helpers.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/k8splugin/internal/plugin/helpers.go b/src/k8splugin/internal/plugin/helpers.go index 26e0f467..b5c9109c 100644 --- a/src/k8splugin/internal/plugin/helpers.go +++ b/src/k8splugin/internal/plugin/helpers.go @@ -17,14 +17,18 @@ package plugin import ( + "encoding/json" "log" "strings" utils "github.com/onap/multicloud-k8s/src/k8splugin/internal" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/config" "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm" pkgerrors "github.com/pkg/errors" + 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/schema" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -45,6 +49,9 @@ type KubernetesConnector interface { // GetStandardClient returns the standard client that can be used to handle // standard kubernetes kinds GetStandardClient() kubernetes.Interface + + //GetInstanceID returns the InstanceID for tracking during creation + GetInstanceID() string } // Reference is the interface that is implemented @@ -90,3 +97,54 @@ func GetPluginByKind(kind string) (Reference, error) { return pluginImpl, nil } + +// TagPodsIfPresent finds the PodTemplateSpec from any workload +// object that contains it and changes the spec to include the tag label +func TagPodsIfPresent(unstruct *unstructured.Unstructured, tag string) { + + spec, ok := unstruct.Object["spec"].(map[string]interface{}) + if !ok { + 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) + if err != nil { + 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 + if labels == nil { + labels = map[string]string{} + } + labels[config.GetConfiguration().KubernetesLabelName] = tag +} |