Skip to content

Commit

Permalink
Configmap/Secrets Optional (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
shraykay authored Mar 5, 2018
1 parent a827357 commit 1057505
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pipeline/builder/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,28 @@ func (mp *ManifestParser) deploymentContainers(dep *appsv1.Deployment, scaffold

if vf := env.ValueFrom; vf != nil {
if vf.ConfigMapKeyRef != nil {
if vf.ConfigMapKeyRef.Optional == nil {
vf.ConfigMapKeyRef.Optional = newFalse()
}

e.EnvSource = &types.EnvSource{
ConfigMapSource: &types.ConfigMapSource{
ConfigMapName: vf.ConfigMapKeyRef.Name,
Key: vf.ConfigMapKeyRef.Key,
Optional: *vf.ConfigMapKeyRef.Optional,
},
}
}

if vf.SecretKeyRef != nil {
if vf.SecretKeyRef.Optional == nil {
vf.SecretKeyRef.Optional = newFalse()
}
e.EnvSource = &types.EnvSource{
SecretSource: &types.SecretSource{
Key: vf.SecretKeyRef.Key,
SecretName: vf.SecretKeyRef.Name,
Optional: *vf.SecretKeyRef.Optional,
},
}
}
Expand Down Expand Up @@ -280,3 +289,8 @@ func spinnakerProbeHandler(probe *corev1.Probe) *types.Probe {
Handler: h,
}
}

func newFalse() *bool {
b := false
return &b
}
30 changes: 30 additions & 0 deletions pipeline/builder/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,37 @@ func TestContainersFromManifests(t *testing.T) {
assert.Equal(t, "dummy-ref", container.EnvFrom[0].ConfigMapSource.Name)
assert.Equal(t, "some-prefix", container.EnvFrom[0].Prefix)
})
t.Run("EnvFrom sources are copied in", func(t *testing.T) {
file := filepath.Join(wd, "testdata", "deployment.envfrom.yml")
parser := builder.NewManfifestParser(&config.Pipeline{})
group, err := parser.ContainersFromScaffold(scaffoldMock{
manifest: file,
})
require.NoError(t, err)

container := group.Containers[0]
require.Len(t, container.EnvFrom, 1)
require.NotNil(t, container.EnvFrom[0].ConfigMapSource)
assert.Equal(t, "dummy-ref", container.EnvFrom[0].ConfigMapSource.Name)
assert.Equal(t, "some-prefix", container.EnvFrom[0].Prefix)
})
t.Run("Optional Configmaps/secrets are copied in", func(t *testing.T) {
file := filepath.Join(wd, "testdata", "deployment.optional.yml")
parser := builder.NewManfifestParser(&config.Pipeline{})
group, err := parser.ContainersFromScaffold(scaffoldMock{
manifest: file,
})
require.NoError(t, err)

container := group.Containers[0]
require.Len(t, container.EnvVars, 3)
require.NotNil(t, container.EnvVars[0].EnvSource.SecretSource.Optional)
require.NotNil(t, container.EnvVars[1].EnvSource.ConfigMapSource.Optional)
require.NotNil(t, container.EnvVars[2].EnvSource.ConfigMapSource.Optional)
assert.Equal(t, false, container.EnvVars[0].EnvSource.SecretSource.Optional)
assert.Equal(t, true, container.EnvVars[1].EnvSource.ConfigMapSource.Optional)
assert.Equal(t, false, container.EnvVars[2].EnvSource.ConfigMapSource.Optional)
})
t.Run("LivenessProbe is copied in the correct format", func(t *testing.T) {
file := filepath.Join(wd, "testdata", "deployment.probes.yml")
parser := builder.NewManfifestParser(&config.Pipeline{})
Expand Down
37 changes: 37 additions & 0 deletions pipeline/builder/testdata/deployment.optional.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: example
namespace: fake-namespace
annotations:
fake-annotation-1: "Hello"
fake-annotation-2: "World"
spec:
template:
metadata:
labels:
app: example
spec:
containers:
- command:
- echo
- hello
env:
- name: PEPPER
valueFrom:
secretKeyRef:
name: secrets
key: pepper
optional: false
- name: SALT
valueFrom:
configMapKeyRef:
key: configs
name: salt
optional: true
- name: EXISTENCE
valueFrom:
configMapKeyRef:
key: configs
name: mundane
image: bird.word/latest
2 changes: 2 additions & 0 deletions pipeline/builder/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ type EnvSource struct {
type SecretSource struct {
SecretName string `json:"secretName"`
Key string `json:"key"`
Optional bool `json:"optional"`
}

// ConfigMapSource is a env var from a config map in k8s
type ConfigMapSource struct {
ConfigMapName string `json:"configMapName"`
Key string `json:"key"`
Optional bool `json:"optional"`
}

// ImageDescription is used to tell spinnaker which image to use for a stage
Expand Down
6 changes: 6 additions & 0 deletions test-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ spec:
configMapKeyRef:
key: ADMIN_PASSWORD
name: super-secret-password
optional: true
- name: ADMIN_USERNAME
valueFrom:
secretKeyRef:
key: ADMIN_USERNAME
name: super-secret-username
# this image doesn't really matter, its going to be replaced, but its not to show intent
image: your.registry.land/org/example:latest
imagePullPolicy: IfNotPresent
Expand Down

0 comments on commit 1057505

Please sign in to comment.