Skip to content

Commit dda32ee

Browse files
authored
Do not write elasticsearch.k8s.elastic.co/managed-remote-clusters when not necessary (#8932)
1 parent cb0d001 commit dda32ee

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/controller/elasticsearch/remotecluster/annotations.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ func annotateWithCreatedRemoteClusters(ctx context.Context, c k8s.Client, es esv
5959
}
6060

6161
sort.Strings(annotation)
62-
es.Annotations[ManagedRemoteClustersAnnotationName] = strings.Join(annotation, ",")
62+
expected := strings.Join(annotation, ",")
63+
current, ok := es.Annotations[ManagedRemoteClustersAnnotationName]
6364

64-
return c.Update(ctx, &es)
65+
if !ok || current != expected {
66+
es.Annotations[ManagedRemoteClustersAnnotationName] = expected
67+
return c.Update(ctx, &es)
68+
}
69+
return nil
6570
}

pkg/controller/elasticsearch/remotecluster/elasticsearch_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
"k8s.io/client-go/tools/record"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
1415

1516
commonv1 "github.com/elastic/cloud-on-k8s/v3/pkg/apis/common/v1"
1617
esv1 "github.com/elastic/cloud-on-k8s/v3/pkg/apis/elasticsearch/v1"
@@ -121,6 +122,7 @@ func TestUpdateSettings(t *testing.T) {
121122
name string
122123
args args
123124
wantAnnotation string
125+
wantKubernetesAPIUpdateCalled int
124126
wantRequeue bool
125127
wantErr bool
126128
wantGetRemoteClusterSettingsCalled bool
@@ -139,6 +141,7 @@ func TestUpdateSettings(t *testing.T) {
139141
),
140142
},
141143
wantRequeue: false,
144+
wantKubernetesAPIUpdateCalled: 0,
142145
wantGetRemoteClusterSettingsCalled: false,
143146
wantUpdateRemoteClusterSettingsCalled: false,
144147
},
@@ -153,6 +156,7 @@ func TestUpdateSettings(t *testing.T) {
153156
map[string]string{"foo": "bar", ManagedRemoteClustersAnnotationName: ""},
154157
),
155158
},
159+
wantKubernetesAPIUpdateCalled: 1, // Annotation should be removed
156160
wantRequeue: false,
157161
wantGetRemoteClusterSettingsCalled: true,
158162
wantUpdateRemoteClusterSettingsCalled: false,
@@ -168,6 +172,7 @@ func TestUpdateSettings(t *testing.T) {
168172
map[string]string{ManagedRemoteClustersAnnotationName: "ns2-es2"},
169173
),
170174
},
175+
wantKubernetesAPIUpdateCalled: 1,
171176
wantRequeue: false,
172177
wantGetRemoteClusterSettingsCalled: true,
173178
wantUpdateRemoteClusterSettingsCalled: false,
@@ -188,6 +193,7 @@ func TestUpdateSettings(t *testing.T) {
188193
),
189194
},
190195
wantAnnotation: "ns2-es2",
196+
wantKubernetesAPIUpdateCalled: 1,
191197
wantGetRemoteClusterSettingsCalled: true,
192198
wantUpdateRemoteClusterSettingsCalled: true,
193199
wantSettings: esclient.RemoteClustersSettings{
@@ -219,6 +225,7 @@ func TestUpdateSettings(t *testing.T) {
219225
wantGetRemoteClusterSettingsCalled: true,
220226
wantUpdateRemoteClusterSettingsCalled: true,
221227
wantAnnotation: "ns1-es2",
228+
wantKubernetesAPIUpdateCalled: 1,
222229
wantSettings: esclient.RemoteClustersSettings{
223230
PersistentSettings: &esclient.SettingsGroup{
224231
Cluster: esclient.RemoteClusters{
@@ -259,6 +266,7 @@ func TestUpdateSettings(t *testing.T) {
259266
wantGetRemoteClusterSettingsCalled: true,
260267
wantUpdateRemoteClusterSettingsCalled: true,
261268
wantAnnotation: "ns2-es2",
269+
wantKubernetesAPIUpdateCalled: 0,
262270
wantSettings: esclient.RemoteClustersSettings{
263271
PersistentSettings: &esclient.SettingsGroup{
264272
Cluster: esclient.RemoteClusters{
@@ -298,6 +306,7 @@ func TestUpdateSettings(t *testing.T) {
298306
},
299307
wantRequeue: false,
300308
wantAnnotation: "ns2-es2",
309+
wantKubernetesAPIUpdateCalled: 1,
301310
wantGetRemoteClusterSettingsCalled: true,
302311
wantUpdateRemoteClusterSettingsCalled: true,
303312
wantSettings: esclient.RemoteClustersSettings{
@@ -343,6 +352,7 @@ func TestUpdateSettings(t *testing.T) {
343352
wantGetRemoteClusterSettingsCalled: true,
344353
wantUpdateRemoteClusterSettingsCalled: true,
345354
wantAnnotation: "ns1-es2,ns1-es3",
355+
wantKubernetesAPIUpdateCalled: 1, // Add ns1-es3
346356
wantSettings: esclient.RemoteClustersSettings{
347357
PersistentSettings: &esclient.SettingsGroup{
348358
Cluster: esclient.RemoteClusters{
@@ -466,6 +476,7 @@ func TestUpdateSettings(t *testing.T) {
466476
wantGetRemoteClusterSettingsCalled: true,
467477
wantUpdateRemoteClusterSettingsCalled: true,
468478
wantAnnotation: "ns1-es2,ns1-es4,ns1-es5",
479+
wantKubernetesAPIUpdateCalled: 1,
469480
wantSettings: esclient.RemoteClustersSettings{
470481
PersistentSettings: &esclient.SettingsGroup{
471482
Cluster: esclient.RemoteClusters{
@@ -481,7 +492,7 @@ func TestUpdateSettings(t *testing.T) {
481492
}
482493
for _, tt := range tests {
483494
t.Run(tt.name, func(t *testing.T) {
484-
client := k8s.NewFakeClient(tt.args.es)
495+
client := trackUpdateCalls(k8s.NewFakeClient(tt.args.es))
485496
shouldRequeue, err := UpdateSettings(
486497
context.Background(),
487498
client,
@@ -504,6 +515,8 @@ func TestUpdateSettings(t *testing.T) {
504515
assert.False(t, annotationExists)
505516
}
506517

518+
assert.Equal(t, tt.wantKubernetesAPIUpdateCalled, client.updateCallCount, "Kubernetes API Update call count mismatch")
519+
507520
// Check the requeue result
508521
assert.Equal(t, tt.wantRequeue, shouldRequeue)
509522
assert.Equal(t, tt.wantGetRemoteClusterSettingsCalled, tt.args.esClient.getRemoteClusterSettingsCalled)
@@ -515,3 +528,17 @@ func TestUpdateSettings(t *testing.T) {
515528
})
516529
}
517530
}
531+
532+
func trackUpdateCalls(c client.Client) *trackUpdateCallsClient {
533+
return &trackUpdateCallsClient{Client: c}
534+
}
535+
536+
type trackUpdateCallsClient struct {
537+
client.Client
538+
updateCallCount int
539+
}
540+
541+
func (t *trackUpdateCallsClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
542+
t.updateCallCount++
543+
return t.Client.Update(ctx, obj, opts...)
544+
}

0 commit comments

Comments
 (0)