From 872968c420215370621d8e37583d9d7dc9878579 Mon Sep 17 00:00:00 2001 From: Michael Kuc Date: Tue, 17 Oct 2023 07:37:51 +0000 Subject: [PATCH] fix: show CronJob properties Show the properties of CronJobs when running `kustomize cfg tree` with the `--image` flag (or other resource flag). --- cmd/config/internal/commands/tree.go | 15 ++++ cmd/config/internal/commands/tree_test.go | 105 ++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/cmd/config/internal/commands/tree.go b/cmd/config/internal/commands/tree.go index 7504bda91..bea5769d3 100644 --- a/cmd/config/internal/commands/tree.go +++ b/cmd/config/internal/commands/tree.go @@ -101,12 +101,14 @@ func (r *TreeRunner) runE(c *cobra.Command, args []string) error { fields = append(fields, newField("spec", "containers", "[name=.*]", "name"), newField("spec", "template", "spec", "containers", "[name=.*]", "name"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "name"), ) } if r.images || (r.all && !c.Flag("image").Changed) { fields = append(fields, newField("spec", "containers", "[name=.*]", "image"), newField("spec", "template", "spec", "containers", "[name=.*]", "image"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "image"), ) } @@ -114,18 +116,21 @@ func (r *TreeRunner) runE(c *cobra.Command, args []string) error { fields = append(fields, newField("spec", "containers", "[name=.*]", "command"), newField("spec", "template", "spec", "containers", "[name=.*]", "command"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "command"), ) } if r.args || (r.all && !c.Flag("args").Changed) { fields = append(fields, newField("spec", "containers", "[name=.*]", "args"), newField("spec", "template", "spec", "containers", "[name=.*]", "args"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "args"), ) } if r.env || (r.all && !c.Flag("env").Changed) { fields = append(fields, newField("spec", "containers", "[name=.*]", "env"), newField("spec", "template", "spec", "containers", "[name=.*]", "env"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "env"), ) } @@ -138,12 +143,14 @@ func (r *TreeRunner) runE(c *cobra.Command, args []string) error { fields = append(fields, newField("spec", "containers", "[name=.*]", "resources"), newField("spec", "template", "spec", "containers", "[name=.*]", "resources"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "resources"), ) } if r.ports || (r.all && !c.Flag("ports").Changed) { fields = append(fields, newField("spec", "containers", "[name=.*]", "ports"), newField("spec", "template", "spec", "containers", "[name=.*]", "ports"), + newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "ports"), newField("spec", "ports"), ) } @@ -176,6 +183,14 @@ func newField(val ...string) kio.TreeWriterField { } } + if strings.HasPrefix(strings.Join(val, "."), "spec.jobTemplate.spec.template.spec.containers") { + return kio.TreeWriterField{ + Name: "spec.jobTemplate.spec.template.spec.containers", + PathMatcher: yaml.PathMatcher{Path: val, StripComments: true}, + SubName: val[len(val)-1], + } + } + if strings.HasPrefix(strings.Join(val, "."), "spec.containers") { return kio.TreeWriterField{ Name: "spec.containers", diff --git a/cmd/config/internal/commands/tree_test.go b/cmd/config/internal/commands/tree_test.go index 1399870f8..72278c5d7 100644 --- a/cmd/config/internal/commands/tree_test.go +++ b/cmd/config/internal/commands/tree_test.go @@ -556,3 +556,108 @@ spec: return } } + +// TestTreeCommand_images tests that the image flag returns the image specified +// by various workloads. +func TestTreeCommand_images(t *testing.T) { + d := t.TempDir() + cwd, err := os.Getwd() + if !assert.NoError(t, err) { + t.FailNow() + } + + if !assert.NoError(t, os.Chdir(d)) { + return + } + + t.Cleanup(func() { + if !assert.NoError(t, os.Chdir(cwd)) { + t.FailNow() + } + }) + + err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(` +kind: Deployment +metadata: + name: deployment +spec: + template: + spec: + containers: + - name: test-deployment + image: docker.io/bash:alpine3.18 +--- +kind: StatefulSet +metadata: + name: statefulset +spec: + template: + spec: + containers: + - name: test-statefulset + image: gcr.io/distroless/static-debian12:nonroot +--- +kind: Job +metadata: + name: job +spec: + template: + spec: + containers: + - name: job + image: tagless +--- +kind: CronJob +metadata: + name: cronjob +spec: + jobTemplate: + spec: + template: + spec: + containers: + - name: test-cronjob + image: local-image:v1.0.0 +--- +kind: Service +metadata: + name: service +spec: + selector: + app: nginx +`), 0600) + if !assert.NoError(t, err) { + return + } + + // fmt the files + b := &bytes.Buffer{} + r := commands.GetTreeRunner("") + r.Command.SetArgs([]string{"--image"}) + r.Command.SetOut(b) + if !assert.NoError(t, r.Command.Execute()) { + return + } + + if !assert.Equal(t, `. +├── [f1.yaml] CronJob cronjob +│ └── spec.jobTemplate.spec.template.spec.containers +│ └── 0 +│ └── image: local-image:v1.0.0 +├── [f1.yaml] Deployment deployment +│ └── spec.template.spec.containers +│ └── 0 +│ └── image: docker.io/bash:alpine3.18 +├── [f1.yaml] Job job +│ └── spec.template.spec.containers +│ └── 0 +│ └── image: tagless +├── [f1.yaml] Service service +└── [f1.yaml] StatefulSet statefulset + └── spec.template.spec.containers + └── 0 + └── image: gcr.io/distroless/static-debian12:nonroot +`, b.String()) { + return + } +}