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

Use Elasticsearch readiness port #7847

Merged
merged 24 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
8 changes: 8 additions & 0 deletions pkg/apis/elasticsearch/v1/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

package v1

import "github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/version"

// as of 8.2.0 a simplified unauthenticated readiness port is available which takes cluster membership into account
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/advanced-configuration.html#readiness-tcp-port
var MinReadinessPortVersion = version.MinFor(8, 2, 0)

const (
ClusterName = "cluster.name"

Expand All @@ -14,6 +20,8 @@ const (
DiscoverySeedProviders = "discovery.seed_providers" // ES >= 7.X
DiscoverySeedHosts = "discovery.seed_hosts" // ES >= 7.X

ReadinessPort = "readiness.port" // ES >= 8.2.0

NetworkHost = "network.host"
NetworkPublishHost = "network.publish_host"
HTTPPublishHost = "http.publish_host"
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/elasticsearch/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ func ReconcileScriptsConfigMap(ctx context.Context, c k8s.Client, es esv1.Elasti
types.NamespacedName{Namespace: es.Namespace, Name: esv1.ScriptsConfigMap(es.Name)},
k8s.ExtractNamespacedName(&es),
map[string]string{
nodespec.ReadinessProbeScriptConfigKey: nodespec.ReadinessProbeScript,
nodespec.PreStopHookScriptConfigKey: preStopScript,
initcontainer.PrepareFsScriptConfigKey: fsScript,
initcontainer.SuspendScriptConfigKey: initcontainer.SuspendScript,
initcontainer.SuspendedHostsFile: initcontainer.RenderSuspendConfiguration(es),
nodespec.ReadinessProbeScriptConfigKey: nodespec.ReadinessProbeScript,
nodespec.ReadinessPortProbeScriptConfigKey: nodespec.ReadinessPortProbeScript,
nodespec.PreStopHookScriptConfigKey: preStopScript,
initcontainer.PrepareFsScriptConfigKey: fsScript,
initcontainer.SuspendScriptConfigKey: initcontainer.SuspendScript,
initcontainer.SuspendedHostsFile: initcontainer.RenderSuspendConfiguration(es),
},
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/elasticsearch/nodespec/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func BuildPodTemplateSpec(
WithResources(DefaultResources).
WithTerminationGracePeriod(DefaultTerminationGracePeriodSeconds).
WithPorts(defaultContainerPorts).
WithReadinessProbe(*NewReadinessProbe()).
WithReadinessProbe(*NewReadinessProbe(v)).
WithAffinity(DefaultAffinity(es.Name)).
WithEnv(DefaultEnvVars(es.Spec.HTTP, headlessServiceName)...).
WithVolumes(volumes...).
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/elasticsearch/nodespec/podspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func TestBuildPodTemplateSpec(t *testing.T) {
DefaultEnvVars(sampleES.Spec.HTTP, HeadlessServiceName(esv1.StatefulSet(sampleES.Name, nodeSet.Name)))...),
Resources: DefaultResources,
VolumeMounts: volumeMounts,
ReadinessProbe: NewReadinessProbe(),
ReadinessProbe: NewReadinessProbe(ver),
Lifecycle: &corev1.Lifecycle{
PreStop: NewPreStopHook(),
},
Expand Down
22 changes: 20 additions & 2 deletions pkg/controller/elasticsearch/nodespec/readiness_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@ import (

corev1 "k8s.io/api/core/v1"

esv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/http"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/version"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/elasticsearch/label"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/elasticsearch/volume"
)

func NewReadinessProbe() *corev1.Probe {
// as of 8.2.0 a simplified unauthenticated readiness port is available which takes cluster membership into account
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/advanced-configuration.html#readiness-tcp-port

const (
ReadinessPortProbeScriptConfigKey = "readiness-port-script.sh"
// ReadinessPortProbeScript is the simplified readiness check for ES >= 8.2.0 which supports a dedicated TCP check
ReadinessPortProbeScript = `#!/usr/bin/env bash
nc -z -v -w5 127.0.0.1 8080
pebrc marked this conversation as resolved.
Show resolved Hide resolved
`
)

func NewReadinessProbe(v version.Version) *corev1.Probe {
scriptKey := ReadinessPortProbeScriptConfigKey
if v.LE(esv1.MinReadinessPortVersion) {
pebrc marked this conversation as resolved.
Show resolved Hide resolved
scriptKey = ReadinessProbeScriptConfigKey
}

return &corev1.Probe{
FailureThreshold: 3,
InitialDelaySeconds: 10,
Expand All @@ -23,7 +41,7 @@ func NewReadinessProbe() *corev1.Probe {
TimeoutSeconds: 5,
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"bash", "-c", path.Join(volume.ScriptsVolumeMountPath, ReadinessProbeScriptConfigKey)},
Command: []string{"bash", "-c", path.Join(volume.ScriptsVolumeMountPath, scriptKey)},
},
},
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/elasticsearch/settings/merged_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func baseConfig(clusterName string, ver version.Version, ipFamily corev1.IPFamil
cfg[esv1.DiscoverySeedHosts] = []string{}
}

if ver.GTE(esv1.MinReadinessPortVersion) {
cfg[esv1.ReadinessPort] = "8080"
}

return &CanonicalConfig{common.MustCanonicalConfig(cfg)}
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/controller/elasticsearch/settings/merged_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ func TestNewMergedESConfig(t *testing.T) {
require.Equal(t, 1, len(cfg.HasKeys([]string{esv1.XPackLicenseUploadTypes})))
},
},
{
name: "prior to 8.2.0 we should not enable the readiness.port",
version: "8.1.0",
ipFamily: corev1.IPv4Protocol,
cfgData: map[string]interface{}{},
assert: func(cfg CanonicalConfig) {
require.Equal(t, 0, len(cfg.HasKeys([]string{esv1.ReadinessPort})))
},
},
{
name: "starting 8.2.0 we should enable the readiness.port",
version: "8.2.0",
ipFamily: corev1.IPv4Protocol,
cfgData: map[string]interface{}{},
assert: func(cfg CanonicalConfig) {
require.Equal(t, 1, len(cfg.HasKeys([]string{esv1.ReadinessPort})))
},
},
{
name: "user-provided Elasticsearch config overrides should have precedence over ECK config",
version: "7.6.0",
Expand Down