Skip to content

Commit

Permalink
add omitempty to Secret in k8s VolumeSource
Browse files Browse the repository at this point in the history
Secret was populating a generated kube as `null`. Add omitempty
so that when the volume source is not a secret, we do not print unnecessary info

resolves #15156

Signed-off-by: Charlie Doern <[email protected]>
  • Loading branch information
cdoern committed Aug 3, 2022
1 parent 549974d commit 7df8d80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/k8s.io/api/core/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ type VolumeSource struct {
// ConfigMap represents a configMap that should populate this volume
// +optional
ConfigMap *ConfigMapVolumeSource `json:"configMap,omitempty"`
Secret *SecretVolumeSource
// Secret represents a secret that should be mounted as a volume
Secret *SecretVolumeSource `json:"secret,omitempty"`
}

// PersistentVolumeClaimVolumeSource references the user's PVC in the same namespace.
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/generate_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,4 +1228,27 @@ USER test1`

Expect(pod.Spec.Containers[0].Env).To(HaveLen(2))
})

It("podman generate kube omit secret if empty", func() {
dir, err := os.MkdirTemp(tempdir, "podman")
Expect(err).Should(BeNil())

defer os.RemoveAll(dir)

podCreate := podmanTest.Podman([]string{"run", "-d", "--pod", "new:" + "noSecretsPod", "--name", "noSecretsCtr", "--volume", dir + ":/foobar", ALPINE})
podCreate.WaitWithDefaultTimeout()
Expect(podCreate).Should(Exit(0))

kube := podmanTest.Podman([]string{"generate", "kube", "noSecretsPod"})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))

Expect(kube.OutputToString()).ShouldNot(ContainSubstring("secret"))

pod := new(v1.Pod)
err = yaml.Unmarshal(kube.Out.Contents(), pod)
Expect(err).To(BeNil())

Expect(pod.Spec.Volumes[0].Secret).To(BeNil())
})
})

0 comments on commit 7df8d80

Please sign in to comment.