From 09bec85eb7d53e0919de34279c67382708fb0e31 Mon Sep 17 00:00:00 2001 From: Rokibul Hasan Date: Fri, 6 Sep 2024 18:46:15 +0600 Subject: [PATCH] Fix image listing function (#476) Signed-off-by: Rokibul Hasan --- .../apps.k8s.appscode.com/v1/petsets.yaml | 2 +- .../apps/v1/daemonsets.yaml | 2 +- .../apps/v1/deployments.yaml | 2 +- .../apps/v1/replicasets.yaml | 2 +- .../apps/v1/statefulsets.yaml | 2 +- .../batch/v1/cronjobs.yaml | 2 +- .../batch/v1/jobs.yaml | 2 +- .../batch/v1beta1/cronjobs.yaml | 2 +- .../core/v1/pods.yaml | 2 +- .../core/v1/replicationcontrollers.yaml | 2 +- pkg/tableconvertor/templates.go | 60 ++++++++++++++----- 11 files changed, 54 insertions(+), 26 deletions(-) diff --git a/hub/resourcetabledefinitions/apps.k8s.appscode.com/v1/petsets.yaml b/hub/resourcetabledefinitions/apps.k8s.appscode.com/v1/petsets.yaml index 6a51d9484..e7fa04f5d 100644 --- a/hub/resourcetabledefinitions/apps.k8s.appscode.com/v1/petsets.yaml +++ b/hub/resourcetabledefinitions/apps.k8s.appscode.com/v1/petsets.yaml @@ -61,7 +61,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Service Name pathTemplate: '{{ .spec.serviceName }}' priority: 1 diff --git a/hub/resourcetabledefinitions/apps/v1/daemonsets.yaml b/hub/resourcetabledefinitions/apps/v1/daemonsets.yaml index 94472e26f..b537d3728 100644 --- a/hub/resourcetabledefinitions/apps/v1/daemonsets.yaml +++ b/hub/resourcetabledefinitions/apps/v1/daemonsets.yaml @@ -73,7 +73,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/hub/resourcetabledefinitions/apps/v1/deployments.yaml b/hub/resourcetabledefinitions/apps/v1/deployments.yaml index cf81f8e62..69e794ff8 100644 --- a/hub/resourcetabledefinitions/apps/v1/deployments.yaml +++ b/hub/resourcetabledefinitions/apps/v1/deployments.yaml @@ -61,7 +61,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/hub/resourcetabledefinitions/apps/v1/replicasets.yaml b/hub/resourcetabledefinitions/apps/v1/replicasets.yaml index b66bc63ac..59aea694e 100644 --- a/hub/resourcetabledefinitions/apps/v1/replicasets.yaml +++ b/hub/resourcetabledefinitions/apps/v1/replicasets.yaml @@ -49,7 +49,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/hub/resourcetabledefinitions/apps/v1/statefulsets.yaml b/hub/resourcetabledefinitions/apps/v1/statefulsets.yaml index a54cb1f84..855a2b733 100644 --- a/hub/resourcetabledefinitions/apps/v1/statefulsets.yaml +++ b/hub/resourcetabledefinitions/apps/v1/statefulsets.yaml @@ -61,7 +61,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Service Name pathTemplate: '{{ .spec.serviceName }}' priority: 1 diff --git a/hub/resourcetabledefinitions/batch/v1/cronjobs.yaml b/hub/resourcetabledefinitions/batch/v1/cronjobs.yaml index 1cb55b580..b2a299527 100644 --- a/hub/resourcetabledefinitions/batch/v1/cronjobs.yaml +++ b/hub/resourcetabledefinitions/batch/v1/cronjobs.yaml @@ -45,7 +45,7 @@ spec: - name: Images pathTemplate: '{{ .spec.jobTemplate.spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.jobTemplate.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.jobTemplate.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.jobTemplate.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/hub/resourcetabledefinitions/batch/v1/jobs.yaml b/hub/resourcetabledefinitions/batch/v1/jobs.yaml index a508bd3c2..1f8c5a4a8 100644 --- a/hub/resourcetabledefinitions/batch/v1/jobs.yaml +++ b/hub/resourcetabledefinitions/batch/v1/jobs.yaml @@ -45,7 +45,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/hub/resourcetabledefinitions/batch/v1beta1/cronjobs.yaml b/hub/resourcetabledefinitions/batch/v1beta1/cronjobs.yaml index 61ba1f96e..71a9a84ab 100644 --- a/hub/resourcetabledefinitions/batch/v1beta1/cronjobs.yaml +++ b/hub/resourcetabledefinitions/batch/v1beta1/cronjobs.yaml @@ -45,7 +45,7 @@ spec: - name: Images pathTemplate: '{{ .spec.jobTemplate.spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.jobTemplate.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.jobTemplate.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.jobTemplate.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/hub/resourcetabledefinitions/core/v1/pods.yaml b/hub/resourcetabledefinitions/core/v1/pods.yaml index 33f4d8cc8..5db627432 100644 --- a/hub/resourcetabledefinitions/core/v1/pods.yaml +++ b/hub/resourcetabledefinitions/core/v1/pods.yaml @@ -54,7 +54,7 @@ spec: - name: Images pathTemplate: '{{ .spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Restart Policy pathTemplate: '{{ .spec.restartPolicy }}' priority: 1 diff --git a/hub/resourcetabledefinitions/core/v1/replicationcontrollers.yaml b/hub/resourcetabledefinitions/core/v1/replicationcontrollers.yaml index 35ecfa8c6..344a4e811 100644 --- a/hub/resourcetabledefinitions/core/v1/replicationcontrollers.yaml +++ b/hub/resourcetabledefinitions/core/v1/replicationcontrollers.yaml @@ -49,7 +49,7 @@ spec: - name: Images pathTemplate: '{{ .spec.template.spec.containers | k8s_container_images }}' priority: 2 - type: string + type: object - name: Security Context pathTemplate: '{ "runAsUser": "{{ jp "{.spec.template.spec.securityContext.runAsUser}" . }}", "runAsGroup": "{{ jp "{.spec.template.spec.securityContext.runAsGroup}" . }}","fsGroup": "{{ jp "{.spec.template.spec.securityContext.fsGroup}" . }}" }' priority: 1 diff --git a/pkg/tableconvertor/templates.go b/pkg/tableconvertor/templates.go index f6d15e72a..59d0ad5fc 100644 --- a/pkg/tableconvertor/templates.go +++ b/pkg/tableconvertor/templates.go @@ -17,7 +17,6 @@ limitations under the License. package tableconvertor import ( - "bytes" "fmt" "strconv" "strings" @@ -328,27 +327,56 @@ func promNamespaceSelectorFn(data interface{}) (string, error) { } func containerImagesFn(data interface{}) (string, error) { - var containers []core.Container + var images []map[string]string + appendImage := func(name, image string) { + images = append(images, map[string]string{ + "container": name, + "image": image, + }) + } + if s, ok := data.(string); ok && s != "" { - err := json.Unmarshal([]byte(s), &containers) - if err != nil { - return "", err + var containers []map[string]interface{} + if err := json.Unmarshal([]byte(s), &containers); err != nil { + return "", fmt.Errorf("failed to unmarshal containers JSON: %w", err) + } + for _, container := range containers { + if name, ok := container["name"].(string); ok { + if image, ok := container["image"].(string); ok { + appendImage(name, image) + } + } } - } else if _, ok := data.(map[string]interface{}); ok { - err := meta_util.DecodeObject(data, &containers) - if err != nil { - return "", err + } else if m, ok := data.(map[string]interface{}); ok { + if containerList, ok := m["containers"].([]interface{}); ok { + for _, c := range containerList { + if container, ok := c.(map[string]interface{}); ok { + if name, ok := container["name"].(string); ok { + if image, ok := container["image"].(string); ok { + appendImage(name, image) + } + } + } + } + } + } else if slice, ok := data.([]interface{}); ok { + for _, item := range slice { + if container, ok := item.(map[string]interface{}); ok { + if name, ok := container["name"].(string); ok { + if image, ok := container["image"].(string); ok { + appendImage(name, image) + } + } + } } } - var imagesBuffer bytes.Buffer - for i, container := range containers { - imagesBuffer.WriteString(container.Image) - if i != len(containers)-1 { - imagesBuffer.WriteString(",") - } + imagesJSON, err := json.Marshal(images) + if err != nil { + return "", err } - return imagesBuffer.String(), nil + + return string(imagesJSON), nil } func durationFn(start interface{}, end ...interface{}) (string, error) {