Skip to content

Commit

Permalink
Add image field/override to initContainer
Browse files Browse the repository at this point in the history
In order to support having DB migrations run via a separate container
image.

Running the added kuttl test:

```
kubectl kuttl test -v2 bundle/tests/scorecard/kuttl/ --test test-init-container-image
```
  • Loading branch information
kahowell authored and kylape committed Oct 27, 2021
1 parent f97f673 commit 02e279d
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 1 deletion.
4 changes: 4 additions & 0 deletions apis/cloud.redhat.com/v1alpha1/clowdapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
// deployed along with the parent pod and is used to carry out one time
// initialization procedures.
type InitContainer struct {
// Image refers to the container image used to create the init container
// (if different from the primary pod image).
Image string `json:"image,omitempty"`

// A list of commands to run inside the parent Pod.
Command []string `json:"command,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: test-init-container-image
spec:
finalizers:
- kubernetes
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-init-container-image-overridden
namespace: test-init-container-image
spec:
template:
spec:
containers:
- image: k8s.gcr.io/pause:3.2
initContainers:
- image: k8s.gcr.io/pause:3.1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-init-container-image-app
namespace: test-init-container-image
spec:
template:
spec:
containers:
- image: k8s.gcr.io/pause:3.2
initContainers:
- image: k8s.gcr.io/pause:3.2
50 changes: 50 additions & 0 deletions bundle/tests/scorecard/kuttl/test-init-container-image/01-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdEnvironment
metadata:
name: test-init-container-image
spec:
targetNamespace: test-init-container-image
providers:
web:
port: 8000
mode: none
metrics:
port: 9000
mode: none
db:
mode: none
inMemoryDb:
mode: none
kafka:
mode: none
logging:
mode: none
objectStore:
mode: none
resourceDefaults:
limits:
cpu: 400m
memory: 1024Mi
requests:
cpu: 30m
memory: 512Mi
---
apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdApp
metadata:
name: test-init-container-image
namespace: test-init-container-image
spec:
envName: test-init-container-image
deployments:
- name: overridden
podSpec:
image: k8s.gcr.io/pause:3.2
initContainers:
- image: k8s.gcr.io/pause:3.1
- name: app
podSpec:
image: k8s.gcr.io/pause:3.2
initContainers:
- {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
delete:
- apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdEnvironment
name: test-init-container-image
- apiVersion: v1
kind: Namespace
name: test-init-container-image
10 changes: 10 additions & 0 deletions config/crd/bases/cloud.redhat.com_clowdapps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ spec:
- name
type: object
type: array
image:
description: Image refers to the container image used
to create the init container (if different from
the primary pod image).
type: string
inheritEnv:
description: If true, inheirts the environment variables
from the parent pod. specification
Expand Down Expand Up @@ -2692,6 +2697,11 @@ spec:
- name
type: object
type: array
image:
description: Image refers to the container image used
to create the init container (if different from
the primary pod image).
type: string
inheritEnv:
description: If true, inheirts the environment variables
from the parent pod. specification
Expand Down
6 changes: 5 additions & 1 deletion controllers/cloud.redhat.com/providers/deployment/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ func ProcessInitContainers(nn types.NamespacedName, c *core.Container, ics []crd
containerList := make([]core.Container, len(ics))

for i, ic := range ics {
image := c.Image
if ic.Image != "" {
image = ic.Image
}
icStruct := core.Container{
Name: nn.Name + "-init",
Image: c.Image,
Image: image,
Command: ic.Command,
Args: ic.Args,
Resources: c.Resources,
Expand Down
10 changes: 10 additions & 0 deletions deploy-mutate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ objects:
- name
type: object
type: array
image:
description: Image refers to the container image
used to create the init container (if different
from the primary pod image).
type: string
inheritEnv:
description: If true, inheirts the environment variables
from the parent pod. specification
Expand Down Expand Up @@ -2760,6 +2765,11 @@ objects:
- name
type: object
type: array
image:
description: Image refers to the container image
used to create the init container (if different
from the primary pod image).
type: string
inheritEnv:
description: If true, inheirts the environment variables
from the parent pod. specification
Expand Down
10 changes: 10 additions & 0 deletions deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ objects:
- name
type: object
type: array
image:
description: Image refers to the container image
used to create the init container (if different
from the primary pod image).
type: string
inheritEnv:
description: If true, inheirts the environment variables
from the parent pod. specification
Expand Down Expand Up @@ -2760,6 +2765,11 @@ objects:
- name
type: object
type: array
image:
description: Image refers to the container image
used to create the init container (if different
from the primary pod image).
type: string
inheritEnv:
description: If true, inheirts the environment variables
from the parent pod. specification
Expand Down

0 comments on commit 02e279d

Please sign in to comment.