Skip to content

Commit a334db7

Browse files
feat: implement compare annotations
It allows to specify ignores values. Note that additional values are ignored anyways, this ignore annotation only handles different values or additional annotations.
1 parent 94b9a2e commit a334db7

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

pkg/configuration/base/pod.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package base
33
import (
44
"context"
55
"fmt"
6-
"reflect"
7-
86
"github.com/jenkinsci/kubernetes-operator/api/v1alpha2"
97
"github.com/jenkinsci/kubernetes-operator/pkg/configuration/backuprestore"
108
"github.com/jenkinsci/kubernetes-operator/pkg/configuration/base/resources"
119
"github.com/jenkinsci/kubernetes-operator/pkg/notifications/event"
1210
"github.com/jenkinsci/kubernetes-operator/pkg/notifications/reason"
1311
"github.com/jenkinsci/kubernetes-operator/version"
12+
"reflect"
13+
"slices"
1414

1515
stackerr "github.com/pkg/errors"
1616
corev1 "k8s.io/api/core/v1"
@@ -82,7 +82,7 @@ func (r *JenkinsBaseConfigurationReconciler) checkForPodRecreation(currentJenkin
8282
currentJenkinsMasterPod.Labels, r.Configuration.Jenkins.Spec.Master.Labels))
8383
}
8484

85-
if !compareMap(r.Configuration.Jenkins.Spec.Master.Annotations, currentJenkinsMasterPod.ObjectMeta.Annotations) {
85+
if !r.compareAnnotations(currentJenkinsMasterPod) {
8686
messages = append(messages, "Jenkins pod annotations have changed")
8787
verbose = append(verbose, fmt.Sprintf("Jenkins pod annotations have changed, actual '%+v' required '%+v'",
8888
currentJenkinsMasterPod.ObjectMeta.Annotations, r.Configuration.Jenkins.Spec.Master.Annotations))
@@ -146,6 +146,20 @@ func (r *JenkinsBaseConfigurationReconciler) checkForPodRecreation(currentJenkin
146146
return reason.NewPodRestart(reason.OperatorSource, messages, verbose...)
147147
}
148148

149+
func (r *JenkinsBaseConfigurationReconciler) compareAnnotations(currentJenkinsMasterPod corev1.Pod) bool {
150+
ignoredAnnotations := r.Jenkins.Spec.Lifecycle.Ignore.IgnoredAnnotations
151+
annotations := r.Configuration.Jenkins.Spec.Master.Annotations
152+
153+
res := make(map[string]string)
154+
for key, val := range annotations {
155+
if slices.Contains(ignoredAnnotations, key) {
156+
continue
157+
}
158+
res[key] = val
159+
}
160+
return compareMap(res, currentJenkinsMasterPod.ObjectMeta.Annotations)
161+
}
162+
149163
func (r *JenkinsBaseConfigurationReconciler) ensureJenkinsMasterPod(meta metav1.ObjectMeta) (reconcile.Result, error) {
150164
userAndPasswordHash, err := r.calculateUserAndPasswordHash()
151165
if err != nil {

pkg/configuration/base/reconcile_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,114 @@ func TestCompareContainerVolumeMounts(t *testing.T) {
9797
})
9898
}
9999

100+
func TestCompareAnnotations(t *testing.T) {
101+
type testCase struct {
102+
name string
103+
jenkinsAnnotations map[string]string
104+
ignoredAnnotations []string
105+
podAnnotations map[string]string
106+
expectedShouldMatch bool
107+
}
108+
109+
runTest := func(t *testing.T, tc testCase) {
110+
111+
t.Run(tc.name, func(t *testing.T) {
112+
jenkins := &v1alpha2.Jenkins{
113+
Spec: v1alpha2.JenkinsSpec{
114+
Master: v1alpha2.JenkinsMaster{
115+
Annotations: tc.jenkinsAnnotations,
116+
},
117+
},
118+
}
119+
120+
if len(tc.ignoredAnnotations) > 0 {
121+
jenkins.Spec.Lifecycle = v1alpha2.JenkinsLifecycle{
122+
Ignore: v1alpha2.JenkinsLifecycleIgnore{
123+
IgnoredAnnotations: tc.ignoredAnnotations,
124+
},
125+
}
126+
}
127+
128+
pod := corev1.Pod{
129+
ObjectMeta: metav1.ObjectMeta{
130+
Annotations: tc.podAnnotations,
131+
},
132+
}
133+
134+
reconciler := New(configuration.Configuration{Jenkins: jenkins}, client.JenkinsAPIConnectionSettings{})
135+
result := reconciler.compareAnnotations(pod)
136+
137+
assert.Equal(t, tc.expectedShouldMatch, result,
138+
"Expected compareAnnotations to return %v but got %v", tc.expectedShouldMatch, result)
139+
})
140+
}
141+
142+
testCases := []testCase{
143+
{
144+
name: "no annotation - additional annotations - not different",
145+
jenkinsAnnotations: map[string]string{},
146+
ignoredAnnotations: nil,
147+
podAnnotations: map[string]string{"one": "two"},
148+
expectedShouldMatch: true,
149+
},
150+
{
151+
name: "one additional annotation - change not ignored - not different",
152+
jenkinsAnnotations: map[string]string{"one": "two"},
153+
ignoredAnnotations: nil,
154+
podAnnotations: map[string]string{"one": "two", "additional": "annotation"},
155+
expectedShouldMatch: true,
156+
},
157+
{
158+
159+
name: "annotations different - different",
160+
jenkinsAnnotations: map[string]string{"one": "two"},
161+
ignoredAnnotations: nil,
162+
podAnnotations: map[string]string{"two": "three"},
163+
expectedShouldMatch: false,
164+
},
165+
166+
{
167+
name: "annotations different - ignored - not different",
168+
jenkinsAnnotations: map[string]string{"one": "two"},
169+
ignoredAnnotations: []string{"one"},
170+
podAnnotations: map[string]string{"two": "three"},
171+
expectedShouldMatch: true,
172+
},
173+
{
174+
name: "one annotation different - change not ignored - different",
175+
jenkinsAnnotations: map[string]string{"one": "two"},
176+
ignoredAnnotations: nil,
177+
podAnnotations: map[string]string{"one": "different"},
178+
expectedShouldMatch: false,
179+
},
180+
{
181+
name: "one annotation different - change ignored - not different",
182+
jenkinsAnnotations: map[string]string{"one": "two"},
183+
ignoredAnnotations: []string{"one"},
184+
podAnnotations: map[string]string{"one": "different"},
185+
expectedShouldMatch: true,
186+
},
187+
{
188+
name: "one additional annotation - different",
189+
jenkinsAnnotations: map[string]string{"one": "two", "ignore": "me"},
190+
ignoredAnnotations: nil,
191+
podAnnotations: map[string]string{"one": "two", "ignore": "this"},
192+
expectedShouldMatch: false,
193+
},
194+
{
195+
name: "one additional annotation - change ignored - not different",
196+
jenkinsAnnotations: map[string]string{"one": "two", "ignore": "me"},
197+
ignoredAnnotations: []string{"ignore"},
198+
podAnnotations: map[string]string{"one": "two", "ignore": "this"},
199+
expectedShouldMatch: true,
200+
},
201+
}
202+
203+
for _, tc := range testCases {
204+
runTest(t, tc)
205+
}
206+
}
207+
100208
func TestCompareVolumes(t *testing.T) {
101209
t.Run("defaults", func(t *testing.T) {
102210
jenkins := &v1alpha2.Jenkins{}

0 commit comments

Comments
 (0)