Skip to content

Commit e647fb3

Browse files
ductrung-nguyenNGUYEN Duc Trung
authored andcommitted
feat: implement prefix-based metadata filtering for StatefulSet and Pod Template
1 parent e3f19b9 commit e647fb3

18 files changed

Lines changed: 3275 additions & 18 deletions
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2018-2022 Splunk Inc. All rights reserved.
2+
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package common
17+
18+
import (
19+
"context"
20+
"reflect"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/log"
24+
)
25+
26+
// MergeStatefulSetMetaUpdates compares and merges StatefulSet ObjectMeta (labels and annotations).
27+
// This does NOT trigger pod restarts since it only touches StatefulSet-level metadata.
28+
// Returns true if there were any changes.
29+
func MergeStatefulSetMetaUpdates(ctx context.Context, current, revised *metav1.ObjectMeta, name string) bool {
30+
reqLogger := log.FromContext(ctx)
31+
scopedLog := reqLogger.WithName("MergeStatefulSetMetaUpdates").WithValues("name", name)
32+
result := false
33+
34+
// Check Annotations - normalize nil to empty map for comparison
35+
currentAnnotations := current.Annotations
36+
if currentAnnotations == nil {
37+
currentAnnotations = map[string]string{}
38+
}
39+
revisedAnnotations := revised.Annotations
40+
if revisedAnnotations == nil {
41+
revisedAnnotations = map[string]string{}
42+
}
43+
if !reflect.DeepEqual(currentAnnotations, revisedAnnotations) {
44+
scopedLog.Info("StatefulSet Annotations differ",
45+
"current", current.Annotations,
46+
"revised", revised.Annotations)
47+
current.Annotations = revised.Annotations
48+
result = true
49+
}
50+
51+
// Check Labels - normalize nil to empty map for comparison
52+
currentLabels := current.Labels
53+
if currentLabels == nil {
54+
currentLabels = map[string]string{}
55+
}
56+
revisedLabels := revised.Labels
57+
if revisedLabels == nil {
58+
revisedLabels = map[string]string{}
59+
}
60+
if !reflect.DeepEqual(currentLabels, revisedLabels) {
61+
scopedLog.Info("StatefulSet Labels differ",
62+
"current", current.Labels,
63+
"revised", revised.Labels)
64+
current.Labels = revised.Labels
65+
result = true
66+
}
67+
68+
return result
69+
}

0 commit comments

Comments
 (0)