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

Inherit all environment variables from ES container in initContainers #5962

Merged
merged 5 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/controller/common/defaults/pod_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func NewPodTemplateBuilder(base corev1.PodTemplateSpec, containerName string) *P
return builder.setDefaults()
}

// getContainer retrieves the main Container from the pod template
func (b *PodTemplateBuilder) getContainer() *corev1.Container {
// MainContainer retrieves the main Container from the pod template
pebrc marked this conversation as resolved.
Show resolved Hide resolved
func (b *PodTemplateBuilder) MainContainer() *corev1.Container {
for i, c := range b.PodTemplate.Spec.Containers {
if c.Name == b.containerName {
return &b.PodTemplate.Spec.Containers[i]
Expand All @@ -68,13 +68,13 @@ func (b *PodTemplateBuilder) getContainer() *corev1.Container {
}

func (b *PodTemplateBuilder) setContainerDefaulter() {
b.containerDefaulter = container.NewDefaulter(b.getContainer())
b.containerDefaulter = container.NewDefaulter(b.MainContainer())
}

// setDefaults sets up a default Container in the pod template,
// and disables service account token auto mount.
func (b *PodTemplateBuilder) setDefaults() *PodTemplateBuilder {
userContainer := b.getContainer()
userContainer := b.MainContainer()
if userContainer == nil {
// create the default Container if not provided by the user
b.PodTemplate.Spec.Containers = append(b.PodTemplate.Spec.Containers, corev1.Container{Name: b.containerName})
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/elasticsearch/nodespec/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func BuildPodTemplateSpec(
WithVolumes(volumes...).
WithVolumeMounts(volumeMounts...).
WithInitContainers(initContainers...).
WithInitContainerDefaults(corev1.EnvVar{Name: settings.HeadlessServiceName, Value: headlessServiceName}).
// inherit all env vars from main containers to allow Elasticsearch tools that read ES config to work in initContainers
WithInitContainerDefaults(builder.MainContainer().Env...).
barkbay marked this conversation as resolved.
Show resolved Hide resolved
WithPreStopHook(*NewPreStopHook())

builder, err = stackmon.WithMonitoring(ctx, client, builder, es)
Expand Down
19 changes: 16 additions & 3 deletions pkg/controller/elasticsearch/nodespec/podspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package nodespec

import (
"context"
"path"
"sort"
"testing"

Expand All @@ -23,6 +24,8 @@ import (
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/volume"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/elasticsearch/initcontainer"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/elasticsearch/settings"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/elasticsearch/user"
esvolume "github.com/elastic/cloud-on-k8s/v2/pkg/controller/elasticsearch/volume"
"github.com/elastic/cloud-on-k8s/v2/pkg/utils/k8s"
"github.com/elastic/cloud-on-k8s/v2/pkg/utils/pointer"
)
Expand Down Expand Up @@ -241,11 +244,21 @@ func TestBuildPodTemplateSpec(t *testing.T) {
initContainers, err := initcontainer.NewInitContainers(transportCertificatesVolume(sampleES.Name), nil, nil)
require.NoError(t, err)
// init containers should be patched with volume and inherited env vars and image
headlessSvcEnvVar := corev1.EnvVar{Name: "HEADLESS_SERVICE_NAME", Value: "name-es-nodeset-1"}
// init container env vars come in a slightly different order than main container ones which is an artefact of how the pod template builder works
initContainerEnv := defaults.ExtendPodDownwardEnvVars(
[]corev1.EnvVar{
{Name: "my-env", Value: "my-value"},
{Name: settings.EnvProbePasswordPath, Value: path.Join(esvolume.ProbeUserSecretMountPath, user.ProbeUserName)},
{Name: settings.EnvProbeUsername, Value: user.ProbeUserName},
{Name: settings.EnvReadinessProbeProtocol, Value: sampleES.Spec.HTTP.Protocol()},
{Name: settings.HeadlessServiceName, Value: HeadlessServiceName(esv1.StatefulSet(sampleES.Name, nodeSet.Name))},
{Name: "NSS_SDB_USE_CACHE", Value: "no"},
}...,
)
esDockerImage := "docker.elastic.co/elasticsearch/elasticsearch:7.2.0"
for i := range initContainers {
initContainers[i].Image = esDockerImage
initContainers[i].Env = append(initContainers[i].Env, headlessSvcEnvVar)
initContainers[i].Env = initContainerEnv
initContainers[i].VolumeMounts = append(initContainers[i].VolumeMounts, volumeMounts...)
initContainers[i].Resources = DefaultResources
}
Expand Down Expand Up @@ -288,7 +301,7 @@ func TestBuildPodTemplateSpec(t *testing.T) {
InitContainers: append(initContainers, corev1.Container{
Name: "additional-init-container",
Image: esDockerImage,
Env: defaults.ExtendPodDownwardEnvVars(headlessSvcEnvVar),
Env: initContainerEnv,
VolumeMounts: volumeMounts,
Resources: DefaultResources, // inherited from main container
}),
Expand Down