Skip to content

Commit

Permalink
Merge pull request #5387 from Crystalix007/master
Browse files Browse the repository at this point in the history
fix: show CronJob properties
  • Loading branch information
k8s-ci-robot committed Mar 6, 2024
2 parents 33caee5 + 872968c commit 74ba2fb
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/config/internal/commands/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,36 @@ 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"),
)
}

if r.cmd || (r.all && !c.Flag("command").Changed) {
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"),
)
}

Expand All @@ -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"),
)
}
Expand Down Expand Up @@ -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",
Expand Down
105 changes: 105 additions & 0 deletions cmd/config/internal/commands/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 74ba2fb

Please sign in to comment.