From 0f030a062550e68e7cd38f4715b7e4e7c215137f Mon Sep 17 00:00:00 2001 From: chengzw Date: Wed, 23 Aug 2023 21:27:54 +0800 Subject: [PATCH] use contents of the script Configmap to generate config hash --- .../elasticsearch/nodespec/podspec.go | 27 ++++++++++++++++--- .../elasticsearch/nodespec/podspec_test.go | 20 +++++++------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pkg/controller/elasticsearch/nodespec/podspec.go b/pkg/controller/elasticsearch/nodespec/podspec.go index b7a8008b67e..f62feb946fd 100644 --- a/pkg/controller/elasticsearch/nodespec/podspec.go +++ b/pkg/controller/elasticsearch/nodespec/podspec.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "hash/fnv" + "sort" "strings" corev1 "k8s.io/api/core/v1" @@ -98,7 +99,7 @@ func BuildPodTemplateSpec( if err := client.Get(context.Background(), types.NamespacedName{Namespace: es.Namespace, Name: esv1.ScriptsConfigMap(es.Name)}, esScripts); err != nil { return corev1.PodTemplateSpec{}, err } - annotations := buildAnnotations(es, cfg, keystoreResources, esScripts.ResourceVersion) + annotations := buildAnnotations(es, cfg, keystoreResources, getScriptsConfigMapContent(esScripts)) // Attempt to detect if the default data directory is mounted in a volume. // If not, it could be a bug, a misconfiguration, or a custom storage configuration that requires the user to @@ -189,7 +190,7 @@ func buildAnnotations( es esv1.Elasticsearch, cfg settings.CanonicalConfig, keystoreResources *keystore.Resources, - scriptsVersion string, + scriptsContent string, ) map[string]string { // start from our defaults annotations := map[string]string{ @@ -199,8 +200,8 @@ func buildAnnotations( configHash := fnv.New32a() // hash of the ES config to rotate the pod on config changes hash.WriteHashObject(configHash, cfg) - // hash of the scripts' version to rotate the pod if the scripts have changed - _, _ = configHash.Write([]byte(scriptsVersion)) + // hash of the scripts' content to rotate the pod if the scripts have changed + _, _ = configHash.Write([]byte(scriptsContent)) if es.HasDownwardNodeLabels() { // list of node labels expected on the pod to rotate the pod when the list is updated @@ -245,3 +246,21 @@ func enableLog4JFormatMsgNoLookups(builder *defaults.PodTemplateBuilder) { } } } + +// Get contents of the script ConfigMap to generate config hash, excluding the suspended_pods.txt, as it may change over time. +func getScriptsConfigMapContent(cm *corev1.ConfigMap) string { + var builder strings.Builder + var keys []string + + for k := range cm.Data { + if k != "suspended_pods.txt" { + keys = append(keys, k) + } + } + sort.Strings(keys) + + for _, k := range keys { + builder.WriteString(cm.Data[k]) + } + return builder.String() +} diff --git a/pkg/controller/elasticsearch/nodespec/podspec_test.go b/pkg/controller/elasticsearch/nodespec/podspec_test.go index 5c344960919..3db0a01ebae 100644 --- a/pkg/controller/elasticsearch/nodespec/podspec_test.go +++ b/pkg/controller/elasticsearch/nodespec/podspec_test.go @@ -297,7 +297,7 @@ func TestBuildPodTemplateSpec(t *testing.T) { "pod-template-label-name": "pod-template-label-value", }, Annotations: map[string]string{ - "elasticsearch.k8s.elastic.co/config-hash": "3893049321", + "elasticsearch.k8s.elastic.co/config-hash": "533641620", "pod-template-annotation-name": "pod-template-annotation-value", "co.elastic.logs/module": "elasticsearch", }, @@ -364,7 +364,7 @@ func Test_buildAnnotations(t *testing.T) { cfg map[string]interface{} esAnnotations map[string]string keystoreResources *keystore.Resources - scriptsVersion string + scriptsContent string } tests := []struct { name string @@ -410,15 +410,15 @@ func Test_buildAnnotations(t *testing.T) { }, }, { - name: "With keystore and scripts version", + name: "With keystore and scripts content", args: args{ keystoreResources: &keystore.Resources{ Version: "42", }, - scriptsVersion: "84", + scriptsContent: "scripts content", }, expectedAnnotations: map[string]string{ - "elasticsearch.k8s.elastic.co/config-hash": "1607725946", + "elasticsearch.k8s.elastic.co/config-hash": "99942575", }, }, { @@ -427,10 +427,10 @@ func Test_buildAnnotations(t *testing.T) { keystoreResources: &keystore.Resources{ Version: "43", }, - scriptsVersion: "84", + scriptsContent: "scripts content", }, expectedAnnotations: map[string]string{ - "elasticsearch.k8s.elastic.co/config-hash": "1624503565", + "elasticsearch.k8s.elastic.co/config-hash": "83164956", }, }, { @@ -439,10 +439,10 @@ func Test_buildAnnotations(t *testing.T) { keystoreResources: &keystore.Resources{ Version: "42", }, - scriptsVersion: "85", + scriptsContent: "another scripts content", }, expectedAnnotations: map[string]string{ - "elasticsearch.k8s.elastic.co/config-hash": "3194693445", + "elasticsearch.k8s.elastic.co/config-hash": "1050348692", }, }, } @@ -453,7 +453,7 @@ func Test_buildAnnotations(t *testing.T) { require.NoError(t, err) cfg, err := settings.NewMergedESConfig(es.Name, ver, corev1.IPv4Protocol, es.Spec.HTTP, *es.Spec.NodeSets[0].Config) require.NoError(t, err) - got := buildAnnotations(es, cfg, tt.args.keystoreResources, tt.args.scriptsVersion) + got := buildAnnotations(es, cfg, tt.args.keystoreResources, tt.args.scriptsContent) for expectedAnnotation, expectedValue := range tt.expectedAnnotations { actualValue, exists := got[expectedAnnotation]