Skip to content

Commit

Permalink
Add initContainers to PodSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
pepov committed Jan 20, 2021
1 parent 210c7fe commit b10e47c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/types/base_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
| volumes | []corev1.Volume | No | - | |
| priorityClassName | string | No | - | |
| containers | []ContainerBase | No | - | |
| initContainers | []ContainerBase | No | - | |
| imagePullSecrets | []corev1.LocalObjectReference | No | - | |
### DeploymentBase
| Variable Name | Type | Required | Default | Description |
Expand Down
11 changes: 11 additions & 0 deletions pkg/types/base_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type PodSpecBase struct {
Volumes []corev1.Volume `json:"volumes,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
PriorityClassName string `json:"priorityClassName,omitempty"`
Containers []ContainerBase `json:"containers,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
InitContainers []ContainerBase `json:"initContainers,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
}

Expand Down Expand Up @@ -245,6 +246,16 @@ func (base *PodSpecBase) Override(spec corev1.PodSpec) corev1.PodSpec {
}
}
}
if len(base.InitContainers) > 0 {
for _, baseContainer := range base.InitContainers {
for o, originalContainer := range spec.InitContainers {
if baseContainer.Name == originalContainer.Name {
spec.InitContainers[o] = baseContainer.Override(originalContainer)
break
}
}
}
}
return spec
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/types/base_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,45 @@ func TestPodSpecOverride(t *testing.T) {
},
},
},
{
name: "initcontainers merged properly",
base: &types.PodSpecBase{
InitContainers: []types.ContainerBase{
{
Name: "override",
Image: "override-image",
},
{
Name: "new", // this one does not exist in the original
Image: "new-image",
},
},
},
spec: v12.PodSpec{
InitContainers: []v12.Container{
{
Name: "override", // this one will be overridden
Image: "original",
},
{
Name: "old", // this one will be unmodified
Image: "old-image",
},
},
},
want: v12.PodSpec{
InitContainers: []v12.Container{
{
Name: "override",
Image: "override-image",
},
{
Name: "old",
Image: "old-image",
},
},
},
},
}
for _, tt := range tests {
tt := tt
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b10e47c

Please sign in to comment.