8000 Allow optional ConfigMap / Secrets by theobarberbany · Pull Request #61 · wave-k8s/wave · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow optional ConfigMap / Secrets #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/controller/deployment/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ var _ = Describe("Deployment controller Suite", func() {

m.UpdateWithFunc(deployment, removeContainer2).Should(Succeed())
waitForDeploymentReconciled(deployment)
waitForDeploymentReconciled(deployment)

// Get the updated Deployment
m.Get(deployment, timeout).Should(Succeed())
Expand Down
12 changes: 8 additions & 4 deletions pkg/core/children.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ func getChildNamesByType(obj podController) (map[string]configMetadata, map[stri
// and Secrets
for _, vol := range obj.GetPodTemplate().Spec.Volumes {
if cm := vol.VolumeSource.ConfigMap; cm != nil {
configMaps[cm.Name] = configMetadata{required: true, allKeys: true}
configMaps[cm.Name] = configMetadata{required: isRequired(cm.Optional), allKeys: true}
}
if s := vol.VolumeSource.Secret; s != nil {
secrets[s.SecretName] = configMetadata{required: true, allKeys: true}
secrets[s.SecretName] = configMetadata{required: isRequired(s.Optional), allKeys: true}
}
}

Expand All @@ -117,10 +117,10 @@ func getChildNamesByType(obj podController) (map[string]configMetadata, map[stri
for _, container := range obj.GetPodTemplate().Spec.Containers {
for _, env := range container.EnvFrom {
if cm := env.ConfigMapRef; cm != nil {
configMaps[cm.Name] = configMetadata{required: true, allKeys: true}
configMaps[cm.Name] = configMetadata{required: isRequired(cm.Optional), allKeys: true}
}
if s := env.SecretRef; s != nil {
secrets[s.Name] = configMetadata{required: true, allKeys: true}
secrets[s.Name] = configMetadata{required: isRequired(s.Optional), allKeys: true}
}
}
}
Expand All @@ -142,6 +142,10 @@ func getChildNamesByType(obj podController) (map[string]configMetadata, map[stri
return configMaps, secrets
}

func isRequired(b *bool) bool {
return b == nil || !*b
}

// parseConfigMapKeyRef updates the metadata for a ConfigMap to include the keys specified in this ConfigMapKeySelector
func parseConfigMapKeyRef(metadata configMetadata, cm *corev1.ConfigMapKeySelector) configMetadata {
if !metadata.allKeys {
Expand Down
42 changes: 39 additions & 3 deletions pkg/core/children_test.go
}))
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,22 @@ var _ = Describe("Wave children Suite", func() {
Expect(configMaps).To(HaveKeyWithValue(cm1.GetName(), configMetadata{required: true, allKeys: true}))
})

It("optional ConfigMaps referenced in Volumes are returned as optional", func() {
Expect(configMaps).To(HaveKeyWithValue("volume-optional", configMetadata{required: false, allKeys: true}))
})

It("optional Secrets referenced in Volumes are returned as optional", func() {
Expect(secrets).To(HaveKeyWithValue("volume-optional", configMetadata{required: false, allKeys: true}))
})

It("returns ConfigMaps referenced in EnvFrom", func() {
Expect(configMaps).To(HaveKeyWithValue(cm2.GetName(), configMetadata{required: true, allKeys: true}))
})

It("optional ConfigMaps referenced in EnvFrom are returned as optional", func() {
Expect(configMaps).To(HaveKeyWithValue("envfrom-optional", configMetadata{required: false, allKeys: true}))
})

It("returns ConfigMaps referenced in Env", func() {
Expect(configMaps).To(HaveKeyWithValue(cm3.GetName(), configMetadata{
required: true,
Expand All @@ -232,6 +244,16 @@ var _ = Describe("Wave children Suite", func() {
}))
})

It("returns ConfigMaps referenced in Env as optional correctly", func() {
Expect(configMaps).To(HaveKeyWithValue("env-optional", configMetadata{
required: false,
allKeys: false,
keys: map[string]struct{}{
"key2": {},
},
}))
})

It("returns Secrets referenced in Volumes", func() {
Expect(secrets).To(HaveKeyWithValue(s1.GetName(), configMetadata{required: true, allKeys: true}))
})
Expand All @@ -240,8 +262,12 @@ var _ = Describe("Wave children Suite", func() {
Expect(secrets).To(HaveKeyWithValue(s2.GetName(), configMetadata{required: true, allKeys: true}))
})

It("optional Secrets referenced in EnvFrom are returned as optional", func() {
Expect(secrets).To(HaveKeyWithValue("envfrom-optional", configMetadata{required: false, allKeys: true}))
})

It("returns Secrets referenced in Env", func() {
Expect(configMaps).To(HaveKeyWithValue(s3.GetName(), configMetadata{
Expect(secrets).To(HaveKeyWithValue(s3.GetName(), configMetadata{
required: true,
allKeys: false,
keys: map[string]struct{}{
Expand All @@ -252,9 +278,19 @@ var _ = Describe("Wave children Suite", func() {
}))
})

It("returns secrets referenced in Env as optional correctly", func() {
Expect(secrets).To(HaveKeyWithValue("env-optional", configMetadata{
required: false,
allKeys: false,
keys: map[string]struct{}{
"key2": {},
},
})

It("does not return extra children", func() {
Expect(configMaps).To(HaveLen(4))
Expect(secrets).To(HaveLen(4))
Expect(configMaps).To(HaveLen(7))
Expect(secrets).To(HaveLen(7))
})
})

Expand Down
60 changes: 60 additions & 0 deletions test/utils/test_objects.go
6D4E
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ var ExampleDeployment = &appsv1.Deployment{
},
},
},
{
Name: "secret-optional",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "volume-optional",
Optional: &trueValue,
},
},
},
{
Name: "configmap1",
VolumeSource: corev1.VolumeSource{
Expand All @@ -63,6 +72,17 @@ var ExampleDeployment = &appsv1.Deployment{
},
},
},
{
Name: "configmap-optional",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "volume-optional",
},
Optional: &trueValue,
},
},
},
},
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -181,19 +201,47 @@ var ExampleDeployment = &appsv1.Deployment{
},
},
},
{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "envfrom-optional",
},
Optional: &trueValue,
},
},
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "example1",
},
},
},
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "envfrom-optional",
},
Optional: &trueValue,
},
},
},
},
{
Name: "container2",
Image: "container2",
Env: []corev1.EnvVar{
{
Name: "env_optional_key2",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "env-optional",
},
Key: "key2",
Optional: &trueValue,
},
},
},
{
Name: "example3_key2",
ValueFrom: &corev1.EnvVarSource{
Expand All @@ -216,6 +264,18 @@ var ExampleDeployment = &appsv1.Deployment{
},
},
},
{
Name: "env_optional_secret_key2",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "env-optional",
},
Key: "key2",
Optional: &trueValue,
},
},
},
},
EnvFrom: []corev1.EnvFromSource{
{
Expand Down
< 302E div hidden> Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
0