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 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
1 change: 1 addition & 0 deletions docs/operating-eck/upgrading-eck.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Upgrading the operator results in a one-time update to existing managed resource
* 2.1
* 2.2
* 2.4
* 2.5

If you have a very large Elasticsearch cluster or multiple Elastic Stack deployments, this rolling restart might be disruptive or inconvenient. To have more control over when the pods belonging to a particular deployment should be restarted, you can <<{p}-exclude-resource,add an annotation>> to the corresponding resources to temporarily exclude them from being managed by the operator. When the time is convenient, you can remove the annotation and let the rolling restart go through.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,6 @@ spec:
cluster.routing.allocation.awareness.attributes: k8s_node_name,zone
podTemplate:
spec:
# The initContainers section is only required if using secureSettings in conjunction with zone awareness.
# initContainers:
# - name: elastic-internal-init-keystore
# env:
# - name: ZONE
# valueFrom:
# fieldRef:
# fieldPath: metadata.annotations['topology.kubernetes.io/zone']
containers:
- name: elasticsearch
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,3 @@ data:
----

Check <<{p}-snapshots,How to create automated snapshots>> for an example use case.

WARNING: If using <<{p}-advanced-node-scheduling>> in conjunction with secure settings, you have to add an `initContainers` section to the `podTemplate` to ensure all the required environment variables exist for the initialization of the keystore. Refer to <<{p}-availability-zone-awareness-example,Zone Awareness>> for a complete example.
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 or nil if not found.
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