Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from utilitywarehouse/prepend-init
Browse files Browse the repository at this point in the history
Add prependInitContainers option
  • Loading branch information
ribbybibby authored Jul 27, 2021
2 parents 9d20c6e + 71b3bbd commit bf30cc3
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 24 deletions.
3 changes: 3 additions & 0 deletions docs/sidecar-configuration-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ inherits: "some-sidecar.yaml"
# https://medium.com/@marko.luksa/delaying-application-start-until-sidecar-is-ready-2ec2d21a7b74
prependContainers: false

# prependInitContainers is the same as above, but for initContainers
prependInitContainers: false

containers:
# we inject a nginx container
- name: sidecar-nginx
Expand Down
25 changes: 13 additions & 12 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ var (

// InjectionConfig is a specific instance of a injected config, for a given annotation
type InjectionConfig struct {
Name string `json:"name"`
Inherits string `json:"inherits"`
Containers []corev1.Container `json:"containers"`
Volumes []corev1.Volume `json:"volumes"`
Environment []corev1.EnvVar `json:"env"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts"`
HostAliases []corev1.HostAlias `json:"hostAliases"`
HostNetwork bool `json:"hostNetwork"`
HostPID bool `json:"hostPID"`
InitContainers []corev1.Container `json:"initContainers"`
ServiceAccountName string `json:"serviceAccountName"`
PrependContainers bool `json:"prependContainers"`
Name string `json:"name"`
Inherits string `json:"inherits"`
Containers []corev1.Container `json:"containers"`
Volumes []corev1.Volume `json:"volumes"`
Environment []corev1.EnvVar `json:"env"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts"`
HostAliases []corev1.HostAlias `json:"hostAliases"`
HostNetwork bool `json:"hostNetwork"`
HostPID bool `json:"hostPID"`
InitContainers []corev1.Container `json:"initContainers"`
ServiceAccountName string `json:"serviceAccountName"`
PrependContainers bool `json:"prependContainers"`
PrependInitContainers bool `json:"prependInitContainers"`

version string
}
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ var (
InitContainerCount: 0,
PrependContainers: true,
},
"prepend-init-containers": testhelper.ConfigExpectation{
Name: "prepend-init-containers",
Version: "latest",
Path: fixtureSidecarsDir + "/prepend-init-containers.yaml",
EnvCount: 0,
ContainerCount: 0,
VolumeCount: 0,
VolumeMountCount: 0,
HostAliasCount: 0,
InitContainerCount: 2,
PrependInitContainers: true,
},
}
)

Expand Down
23 changes: 12 additions & 11 deletions internal/pkg/testing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ type ConfigExpectation struct {
// version is the parsed version string, or "latest" if omitted
Version string
// Path is the path to the YAML to load the sidecar yaml from
Path string
EnvCount int
ContainerCount int
VolumeCount int
VolumeMountCount int
HostAliasCount int
HostNetwork bool
HostPID bool
InitContainerCount int
ServiceAccount string
PrependContainers bool
Path string
EnvCount int
ContainerCount int
VolumeCount int
VolumeMountCount int
HostAliasCount int
HostNetwork bool
HostPID bool
InitContainerCount int
ServiceAccount string
PrependContainers bool
PrependInitContainers bool

// LoadError is an error, if any, that is expected during load
LoadError error
Expand Down
7 changes: 6 additions & 1 deletion pkg/server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,12 @@ func createPatch(pod *corev1.Pod, inj *config.InjectionConfig, annotations map[s
// this mutates inj.InitContainers with our environment vars
mutatedInjectedInitContainers := mergeEnvVars(inj.Environment, inj.InitContainers)
mutatedInjectedInitContainers = mergeVolumeMounts(inj.VolumeMounts, mutatedInjectedInitContainers)
patch = append(patch, appendContainers(pod.Spec.InitContainers, mutatedInjectedInitContainers, "/spec/initContainers")...)
// then, add containers to the patch
if inj.PrependInitContainers {
patch = append(patch, prependContainers(pod.Spec.InitContainers, mutatedInjectedInitContainers, "/spec/initContainers")...)
} else {
patch = append(patch, appendContainers(pod.Spec.InitContainers, mutatedInjectedInitContainers, "/spec/initContainers")...)
}
}

{ // container injections
Expand Down
3 changes: 3 additions & 0 deletions pkg/server/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
obj7v2 = "test/fixtures/k8s/object7-v2.yaml"
obj7v3 = "test/fixtures/k8s/object7-badrequestformat.yaml"
obj8 = "test/fixtures/k8s/object8.yaml"
obj9 = "test/fixtures/k8s/object9.yaml"
badSidecar = "test/fixtures/k8s/bad-sidecar.yaml"

// tests to check config loading of sidecars
Expand All @@ -54,6 +55,7 @@ var (
{configuration: obj7v2, expectedSidecar: "init-containers:v2"},
{configuration: obj7v3, expectedSidecar: "", expectedError: ErrRequestedSidecarNotFound},
{configuration: obj8, expectedSidecar: "prepend-containers:latest"},
{configuration: obj9, expectedSidecar: "prepend-init-containers:latest"},
{configuration: badSidecar, expectedSidecar: "", expectedError: ErrRequestedSidecarNotFound},
}

Expand All @@ -63,6 +65,7 @@ var (
{name: "sidecar-test-1", allowed: true, patchExpected: true},
{name: "env-override", allowed: true, patchExpected: true},
{name: "prepend-containers", allowed: true, patchExpected: true},
{name: "prepend-init-containers", allowed: true, patchExpected: true},
{name: "service-account", allowed: true, patchExpected: true},
{name: "service-account-already-set", allowed: true, patchExpected: true},
{name: "service-account-set-default", allowed: true, patchExpected: true},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"op": "add",
"path": "/spec/initContainers",
"value": [
{
"image": "foo:69",
"name": "sidecar-existing-vm",
"ports": [
{
"containerPort": 420
}
],
"resources": {}
}
]
},
{
"op": "add",
"path": "/spec/initContainers/0",
"value": {
"image": "nginx:1.12.2",
"imagePullPolicy": "IfNotPresent",
"name": "sidecar-add-vm",
"ports": [
{
"containerPort": 80
}
],
"resources": {}
}
},
{
"op": "add",
"path": "/metadata/annotations/injector.unittest.com~1status",
"value": "injected"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# this is an AdmissionRequest object
# https://godoc.org/k8s.io/api/admission/v1#AdmissionRequest
object:
metadata:
annotations:
injector.unittest.com/request: "prepend-init-containers"
spec:
initContainers: []
4 changes: 4 additions & 0 deletions test/fixtures/k8s/object9.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: object8
namespace: unittest
annotations:
"injector.unittest.com/request": "prepend-init-containers"
12 changes: 12 additions & 0 deletions test/fixtures/sidecars/prepend-init-containers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: prepend-init-containers
initContainers:
- name: sidecar-add-vm
image: nginx:1.12.2
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
- name: sidecar-existing-vm
image: foo:69
ports:
- containerPort: 420
prependInitContainers: true

0 comments on commit bf30cc3

Please sign in to comment.