Skip to content

Commit

Permalink
Fix image listing function (#476)
Browse files Browse the repository at this point in the history
Signed-off-by: Rokibul Hasan <[email protected]>
  • Loading branch information
RokibulHasan7 authored Sep 6, 2024
1 parent ace8c94 commit 09bec85
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/apps/v1/daemonsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/apps/v1/deployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/apps/v1/replicasets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/apps/v1/statefulsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/batch/v1/cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/batch/v1/jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/batch/v1beta1/cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcetabledefinitions/core/v1/pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 44 additions & 16 deletions pkg/tableconvertor/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package tableconvertor

import (
"bytes"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 09bec85

Please sign in to comment.