Skip to content
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

Ensure server-config-init and server-config-init-base are in initContainers #572

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti

## unreleased

* [ENHANCEMENT] [#571](https://github.com/k8ssandra/cass-operator/issues/571) Ensure both "server-config-init" as well as "server-config-init-base" are always created in the initContainers if 4.1.x is used.

## v1.17.1

* [CHANGE] [#541](https://github.com/k8ssandra/cass-operator/issues/541) Revert when deployed through OLM, add serviceAccount to Cassandra pods that use nonroot priviledge. This is no longer necessary with 1.17.0 and up.
Expand Down
38 changes: 26 additions & 12 deletions pkg/reconciliation/construct_podtemplatespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,20 @@ func symmetricDifference(list1 []corev1.Volume, list2 []corev1.Volume) []corev1.
func buildInitContainers(dc *api.CassandraDatacenter, rackName string, baseTemplate *corev1.PodTemplateSpec) error {

serverCfg := &corev1.Container{}
foundOverrides := false
configContainer := &corev1.Container{}

serverContainerIndex := -1
configContainerIndex := -1

for i, c := range baseTemplate.Spec.InitContainers {
if c.Name == ServerConfigContainerName {
// Modify the existing container
foundOverrides = true
serverContainerIndex = i
serverCfg = &baseTemplate.Spec.InitContainers[i]
break
}
if c.Name == ServerBaseConfigContainerName {
configContainerIndex = i
configContainer = &baseTemplate.Spec.InitContainers[i]
}
}

Expand Down Expand Up @@ -420,7 +426,6 @@ func buildInitContainers(dc *api.CassandraDatacenter, rackName string, baseTempl

configMounts := []corev1.VolumeMount{serverCfgMount}

var configContainer *corev1.Container
if dc.UseClientImage() {

configBaseMount := corev1.VolumeMount{
Expand All @@ -431,8 +436,10 @@ func buildInitContainers(dc *api.CassandraDatacenter, rackName string, baseTempl
configMounts = append(configMounts, configBaseMount)

// Similar to k8ssandra 1.x, use config-container if use new config-builder replacement
configContainer = &corev1.Container{
Name: ServerBaseConfigContainerName,
if configContainerIndex < 0 {
configContainer = &corev1.Container{
Name: ServerBaseConfigContainerName,
}
}

if configContainer.Image == "" {
Expand Down Expand Up @@ -483,16 +490,23 @@ func buildInitContainers(dc *api.CassandraDatacenter, rackName string, baseTempl

serverCfg.Env = combineEnvSlices(envDefaults, serverCfg.Env)

if !foundOverrides {
// Note that append makes a copy, so we must do this after
// serverCfg has been properly set up.
if dc.UseClientImage() {
baseTemplate.Spec.InitContainers = append(baseTemplate.Spec.InitContainers, *configContainer, *serverCfg)
if dc.UseClientImage() && configContainerIndex < 0 {
if serverContainerIndex >= 0 {
// Set before serverCfg
if serverContainerIndex == 0 {
baseTemplate.Spec.InitContainers = append([]corev1.Container{*configContainer}, baseTemplate.Spec.InitContainers...)
} else {
baseTemplate.Spec.InitContainers = append(baseTemplate.Spec.InitContainers[:serverContainerIndex], append([]corev1.Container{*configContainer}, baseTemplate.Spec.InitContainers[serverContainerIndex:]...)...)
}
} else {
baseTemplate.Spec.InitContainers = append(baseTemplate.Spec.InitContainers, *serverCfg)
baseTemplate.Spec.InitContainers = append(baseTemplate.Spec.InitContainers, *configContainer)
}
}

if serverContainerIndex < 0 {
baseTemplate.Spec.InitContainers = append(baseTemplate.Spec.InitContainers, *serverCfg)
}

return nil
}

Expand Down
166 changes: 166 additions & 0 deletions pkg/reconciliation/construct_podtemplatespec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,172 @@ func TestCassandraDatacenter_buildPodTemplateSpec_clientImage(t *testing.T) {
assert.True(volumesContains(volumes, volumeNameMatcher("vector-lib")))
}

func TestCassandraDatacenter_buildPodTemplateSpec_clientImage_withContainerOverrides(t *testing.T) {
assert := assert.New(t)

// Example from k8ssandra-operator when medusa is used
dc41 := &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "4.1.2",
Racks: []api.Rack{
{
Name: "default",
},
},
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: ServerConfigContainerName,
Resources: corev1.ResourceRequirements{},
},
},
},
},
},
}

spec41, err := buildPodTemplateSpec(dc41, dc41.Spec.Racks[0], false)
assert.NoError(err, "should not have gotten error when building podTemplateSpec")

initContainers := spec41.Spec.InitContainers

assert.Equal(2, len(initContainers))

serverBaseConfigInitContainer := initContainers[0]
assert.Equal(ServerBaseConfigContainerName, serverBaseConfigInitContainer.Name)
assert.Equal(1, len(serverBaseConfigInitContainer.VolumeMounts))
// We use a contains check here because the ordering is not important
assert.True(volumeMountsContains(serverBaseConfigInitContainer.VolumeMounts, volumeMountNameMatcher("server-config-base")))

serverConfigInitContainer := initContainers[1]
assert.Equal(ServerConfigContainerName, serverConfigInitContainer.Name)
assert.Equal(2, len(serverConfigInitContainer.VolumeMounts))
// We use a contains check here because the ordering is not important
assert.True(volumeMountsContains(serverConfigInitContainer.VolumeMounts, volumeMountNameMatcher("server-config")))
assert.True(volumeMountsContains(serverConfigInitContainer.VolumeMounts, volumeMountNameMatcher("server-config-base")))

volumes := spec41.Spec.Volumes
assert.Equal(4, len(volumes))
// We use a contains check here because the ordering is not important
assert.True(volumesContains(volumes, volumeNameMatcher("server-config")))
assert.True(volumesContains(volumes, volumeNameMatcher("server-logs")))
assert.True(volumesContains(volumes, volumeNameMatcher("server-config-base")))
assert.True(volumesContains(volumes, volumeNameMatcher("vector-lib")))

// Give extra containers and swap the orders

dc41 = &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "4.1.2",
Racks: []api.Rack{
{
Name: "default",
},
},
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "my-own-init-container",
},
{
Name: ServerBaseConfigContainerName,
Resources: corev1.ResourceRequirements{},
},
},
},
},
},
}

spec41, err = buildPodTemplateSpec(dc41, dc41.Spec.Racks[0], false)
assert.NoError(err, "should not have gotten error when building podTemplateSpec")
initContainers = spec41.Spec.InitContainers
assert.Equal(3, len(initContainers))

assert.Equal(ServerBaseConfigContainerName, initContainers[1].Name)
assert.Equal(ServerConfigContainerName, initContainers[2].Name)

//

dc41 = &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "4.1.2",
Racks: []api.Rack{
{
Name: "default",
},
},
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: ServerBaseConfigContainerName,
Resources: corev1.ResourceRequirements{},
},
{
Name: "my-own-init-container",
},
},
},
},
},
}

spec41, err = buildPodTemplateSpec(dc41, dc41.Spec.Racks[0], false)
assert.NoError(err, "should not have gotten error when building podTemplateSpec")
initContainers = spec41.Spec.InitContainers
assert.Equal(3, len(initContainers))

assert.Equal(ServerBaseConfigContainerName, initContainers[0].Name)
assert.Equal("my-own-init-container", initContainers[1].Name)
assert.Equal(ServerConfigContainerName, initContainers[2].Name)

//

dc41 = &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "4.1.2",
Racks: []api.Rack{
{
Name: "default",
},
},
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: ServerConfigContainerName,
Resources: corev1.ResourceRequirements{},
},
{
Name: "my-own-init-container",
},
},
},
},
},
}

spec41, err = buildPodTemplateSpec(dc41, dc41.Spec.Racks[0], false)
assert.NoError(err, "should not have gotten error when building podTemplateSpec")
initContainers = spec41.Spec.InitContainers
assert.Equal(3, len(initContainers))

assert.Equal(ServerBaseConfigContainerName, initContainers[0].Name)
assert.Equal(ServerConfigContainerName, initContainers[1].Name)
assert.Equal("my-own-init-container", initContainers[2].Name)
}

func TestCassandraDatacenter_buildContainers_DisableSystemLoggerSidecar(t *testing.T) {
dc := &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ spec:
- name: r1
podTemplateSpec:
spec:
initContainers:
- name: server-config-init
resources: {}
containers:
- args: ["/bin/sh", "-c", "tail -n+1 -F /var/log/cassandra/system.log"]
image: busybox
Expand Down
Loading