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

Fix readiness probe #2272

Merged
merged 7 commits into from
Dec 17, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/controller/common/volume/downward_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package volume

import (
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/volume"
corev1 "k8s.io/api/core/v1"
)

var downwardApiVolume = corev1.Volume{
Name: volume.DownwardApiVolumeName,
VolumeSource: corev1.VolumeSource{
DownwardAPI: &corev1.DownwardAPIVolumeSource{
Items: []corev1.DownwardAPIVolumeFile{
{
Path: volume.LabelsFile,
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.labels",
},
},
},
},
},
}

var downwardApiVolumeMount = corev1.VolumeMount{
Name: volume.DownwardApiVolumeName,
MountPath: volume.DownwardApiMountPath,
ReadOnly: true,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I feel like these don't need to be global variables (even though they're only package-private). Maybe they can be directly returned from the method calls of the DownwardApi object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is to avoid to recreate what must be a constant everytime a Pod spec is created. I understand that it may sound like a very tiny optimization though 😐

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I don't feel strongly about it. Happy to leave it as is.


type DownwardApi struct{}
barkbay marked this conversation as resolved.
Show resolved Hide resolved
barkbay marked this conversation as resolved.
Show resolved Hide resolved

func (DownwardApi) Name() string {
return volume.DownwardApiVolumeName
}

func (DownwardApi) Volume() corev1.Volume {
return downwardApiVolume
}

func (DownwardApi) VolumeMount() corev1.VolumeMount {
return downwardApiVolumeMount
}
37 changes: 30 additions & 7 deletions pkg/controller/elasticsearch/nodespec/readiness_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,25 @@ func NewReadinessProbe() *corev1.Probe {

const ReadinessProbeScriptConfigKey = "readiness-probe-script.sh"
const ReadinessProbeScript = `#!/usr/bin/env bash
# Consider a node to be healthy if it responds to a simple GET on "/_cat/nodes?local"

# fail should be called as a last resort to help the user to understand why the probe failed
function fail {
timestamp=$(date --iso-8601=seconds)
echo "{\"timestamp\": \"${timestamp}\", \"message\": \"readiness probe failed\", "$1"}" | tee /proc/1/fd/1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever :-) But should it be /fd/2 for stderr?

exit 1
}

labels="` + volume.DownwardApiMountPath + "/" + volume.LabelsFile + `"

if [[ ! -f "${labels}" ]]; then
fail "\"reason\": \"${labels} does not exist\""
fi

# get Elasticsearch version from the downward API
version=$(grep "elasticsearch.k8s.elastic.co/version" ${labels} | cut -d '=' -f 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could be safer to use the VersionLabelName constant:

VersionLabelName = "elasticsearch.k8s.elastic.co/version"

# remove quotes
version=$(echo "${version}" | tr -d '"')

CURL_TIMEOUT=${CURL_TIMEOUT:=3}

# Check if PROBE_PASSWORD_PATH is set, otherwise fall back to its former name in 1.0.0.beta-1: PROBE_PASSWORD_FILE
Expand All @@ -46,14 +64,19 @@ else
BASIC_AUTH=''
fi

# request Elasticsearch
ENDPOINT="${READINESS_PROBE_PROTOCOL:-https}://127.0.0.1:9200/_cat/nodes?local"
# request Elasticsearch on /
ENDPOINT="${READINESS_PROBE_PROTOCOL:-https}://127.0.0.1:9200/"
status=$(curl -o /dev/null -w "%{http_code}" --max-time $CURL_TIMEOUT -XGET -s -k ${BASIC_AUTH} $ENDPOINT)
curl_rc=$?

if [[ ${curl_rc} -ne 0 ]]; then
fail "\"curl_rc\": \"${curl_rc}\""
fi

# ready if status code 200
if [[ $status == "200" ]]; then
exit 0
# ready if status code 200, 503 is tolerable if ES version is 6.x
if [[ ${status} == "200" ]] || [[ ${status} == "503" && ${version:0:2} == "6." ]]; then
exit 0
else
exit 1
fail " \"status\": \"${status}\", \"version\":\"${version}\" "
fi
`
4 changes: 4 additions & 0 deletions pkg/controller/elasticsearch/nodespec/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
esvolume "github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/volume"
)

var downwardApiVolume = volume.DownwardApi{}

func buildVolumes(esName string, nodeSpec esv1.NodeSet, keystoreResources *keystore.Resources) ([]corev1.Volume, []corev1.VolumeMount) {

configVolume := settings.ConfigSecretVolume(esv1.StatefulSet(esName, nodeSpec.Name))
Expand Down Expand Up @@ -70,6 +72,7 @@ func buildVolumes(esName string, nodeSpec esv1.NodeSet, keystoreResources *keyst
httpCertificatesVolume.Volume(),
scriptsVolume.Volume(),
configVolume.Volume(),
downwardApiVolume.Volume(),
)...)
if keystoreResources != nil {
volumes = append(volumes, keystoreResources.Volume)
Expand All @@ -86,6 +89,7 @@ func buildVolumes(esName string, nodeSpec esv1.NodeSet, keystoreResources *keyst
httpCertificatesVolume.VolumeMount(),
scriptsVolume.VolumeMount(),
configVolume.VolumeMount(),
downwardApiVolume.VolumeMount(),
)

return volumes, volumeMounts
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/elasticsearch/volume/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ const (

ScriptsVolumeName = "elastic-internal-scripts"
ScriptsVolumeMountPath = "/mnt/elastic-internal/scripts"

DownwardApiVolumeName = "downward-api"
DownwardApiMountPath = "/mnt/elastic-internal/downward-api"
LabelsFile = "labels"
)