Skip to content

Commit

Permalink
add imagePullPolicy column support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetb authored Feb 19, 2022
1 parent bfc6e5e commit 4a0ef25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
}
rootCmd.Flags().BoolP("all-namespaces", "A", false, "if present, list images in all namespaces.")
rootCmd.Flags().StringP("namespace", "n", "", "if present, list images in the specified namespace only. Use current namespace as fallback.")
rootCmd.Flags().StringP("columns", "c", "1,2,3", "specify the columns to display, separated by comma. [0:Namespace, 1:PodName, 2:ContainerName, 3:ContainerImage]")
rootCmd.Flags().StringP("columns", "c", "1,2,3", "specify the columns to display, separated by comma. [0:Namespace, 1:PodName, 2:ContainerName, 3:ContainerImage, 4:ImagePullPolicy]")
rootCmd.Flags().StringP("kubeconfig", "k", "", "path to the kubeconfig file to use for CLI requests.")
rootCmd.Flags().StringP("output-format", "o", "table", "output format. [json|table]")
rootCmd.Flags().String("context", "", "The name of the kubeconfig context to use.")
Expand Down
41 changes: 25 additions & 16 deletions kubectl_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
)

const (
gotemplate = `go-template={{range .items}} {{.metadata.namespace}} {{","}} {{.metadata.name}} {{","}} {{range .spec.containers}} {{.name}} {{","}} {{.image}} {{"\n"}} {{end}} {{range .spec.initContainers}} {{"(init)"}} {{.name}} {{","}} {{.image}} {{"\n"}} {{end}} {{end}}`
gotemplate = `go-template={{range .items}} {{.metadata.namespace}} {{","}} {{.metadata.name}} {{","}} {{range .spec.containers}} {{.name}} {{","}} {{.image}} {{","}} {{.imagePullPolicy}} {{"\n"}} {{end}} {{range .spec.initContainers}} {{"(init)"}} {{.name}} {{","}} {{.image}} {{","}} {{.imagePullPolicy}} {{"\n"}} {{end}} {{end}}`

namespace = "Namespace"
podName = "PodName"
containerName = "ContainerName"
containerImage = "ContainerImage"
namespace = "Namespace"
podName = "PodName"
containerName = "ContainerName"
containerImage = "ContainerImage"
imagePullPolicy = "ImagePullPolicy"
)

// KubeImage is the representation of a container image used in the cluster.
Expand Down Expand Up @@ -45,10 +46,11 @@ func NewKubeImage(regx *regexp.Regexp, allNamespace bool, namespace, columns, ku

// ImageEntity is the representation of an entity to be displayed.
type ImageEntity struct {
Namespace string
PodName string
ContainerName string
ContainerImage string
Namespace string
PodName string
ContainerName string
ContainerImage string
ImagePullPolicy string
}

func (ie *ImageEntity) format(columns []string) []string {
Expand All @@ -63,6 +65,8 @@ func (ie *ImageEntity) format(columns []string) []string {
result = append(result, ie.ContainerName)
case containerImage:
result = append(result, ie.ContainerImage)
case imagePullPolicy:
result = append(result, ie.ImagePullPolicy)
}
}
return result
Expand Down Expand Up @@ -112,6 +116,8 @@ func (ki *KubeImage) Columns() []string {
result = append(result, containerName)
case "3":
result = append(result, containerImage)
case "4":
result = append(result, imagePullPolicy)
}
}
return result
Expand Down Expand Up @@ -153,14 +159,16 @@ func (ki *KubeImage) run() {
switch len(items) {
case 1:
continue
case 2:
case 3:
entity.ContainerName = items[0]
entity.ContainerImage = items[1]
case 4:
entity.ImagePullPolicy = items[2]
case 5:
entity.Namespace = items[0]
entity.PodName = items[1]
entity.ContainerName = items[2]
entity.ContainerImage = items[3]
entity.ImagePullPolicy = items[4]
}
entities = append(entities, entity)
}
Expand Down Expand Up @@ -227,14 +235,15 @@ func (ki *KubeImage) Render(format string) {
case "json":
{
type PodRecord struct {
Namespace string
Pod string
Container string
Image string
Namespace string
Pod string
Container string
Image string
ImagePullPolicy string
}
var rec []PodRecord
for _, v := range ki.entities {
rec = append(rec, PodRecord{v.Namespace, v.PodName, v.ContainerName, v.ContainerImage})
rec = append(rec, PodRecord{v.Namespace, v.PodName, v.ContainerName, v.ContainerImage, v.ImagePullPolicy})
}
output, err := json.Marshal(rec)
if err != nil {
Expand Down

0 comments on commit 4a0ef25

Please sign in to comment.