Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#1661 from saschagrunert/config-mul…
Browse files Browse the repository at this point in the history
…tiple-set

Fix `crictl config --set` if the YAML defines entries multiple times
  • Loading branch information
k8s-ci-robot authored Oct 28, 2024
2 parents f4d03d4 + b6b10eb commit 4cea203
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/common/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ func setConfigOption(configName, configValue string, yamlData *yaml.Node) {
for indx := 0; indx < contentLen-1; {
name := yamlData.Content[0].Content[indx].Value
if name == configName {
// Set the value, even if we have the option defined multiple times.
yamlData.Content[0].Content[indx+1].Value = configValue
foundOption = true
break
}
indx += 2
}
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,42 @@ pull-image-on-create: false
disable-pull-on-run: false
`))
})

It("should succeed to get the right value if duplicate entries are defined", func() {
_, err := configFile.WriteString(`
timeout: 20
timeout: 5
timeout: 10
`)
Expect(err).NotTo(HaveOccurred())

t.CrictlExpectSuccess("--config "+configFile.Name()+" config --get timeout", "10")
})

It("should succeed to set duplicate entries", func() {
_, err := configFile.WriteString(`
timeout: 20
timeout: 5
timeout: 10
`)
Expect(err).NotTo(HaveOccurred())

t.CrictlExpectSuccess("--config "+configFile.Name()+" config --set timeout=30", "")

cfgContent, err := os.ReadFile(configFile.Name())
Expect(err).NotTo(HaveOccurred())

Expect(string(cfgContent)).To(Equal(
`timeout: 30
timeout: 30
timeout: 30
runtime-endpoint: ""
image-endpoint: ""
debug: false
pull-image-on-create: false
disable-pull-on-run: false
`))

t.CrictlExpectSuccess("--config "+configFile.Name()+" config --get timeout", "30")
})
})

0 comments on commit 4cea203

Please sign in to comment.