Skip to content

Commit 0e8458c

Browse files
author
Nont
committed
Preserve ObjectMeta.ResourceVersion
Signed-off-by: Nont <[email protected]>
1 parent 3a20471 commit 0e8458c

File tree

2 files changed

+125
-12
lines changed

2 files changed

+125
-12
lines changed

pkg/webhook/managedresource/validatingadmissionpolicy.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func getValidatingAdmissionPolicy(isHub bool) *admv1.ValidatingAdmissionPolicy {
2525
}
2626

2727
func mutateValidatingAdmissionPolicy(vap *admv1.ValidatingAdmissionPolicy, isHub bool) {
28-
vap.TypeMeta = metav1.TypeMeta{
29-
APIVersion: "admissionregistration.k8s.io/v1",
30-
Kind: "ValidatingAdmissionPolicy",
31-
}
32-
vap.ObjectMeta.Labels = map[string]string{
33-
"fleet.azure.com/managed-by": "arm",
28+
ometa := metav1.ObjectMeta{
29+
Labels: map[string]string{
30+
"fleet.azure.com/managed-by": "arm",
31+
},
32+
ResourceVersion: vap.ResourceVersion,
3433
}
34+
vap.ObjectMeta = ometa
3535
vap.Spec = admv1.ValidatingAdmissionPolicySpec{
3636
MatchConstraints: &admv1.MatchResources{
3737
ObjectSelector: &metav1.LabelSelector{
@@ -108,13 +108,13 @@ func getValidatingAdmissionPolicyBinding() *admv1.ValidatingAdmissionPolicyBindi
108108
}
109109

110110
func mutateValidatingAdmissionPolicyBinding(vapb *admv1.ValidatingAdmissionPolicyBinding) {
111-
vapb.TypeMeta = metav1.TypeMeta{
112-
APIVersion: "admissionregistration.k8s.io/v1",
113-
Kind: "ValidatingAdmissionPolicyBinding",
114-
}
115-
vapb.ObjectMeta.Labels = map[string]string{
116-
"fleet.azure.com/managed-by": "arm",
111+
ometa := metav1.ObjectMeta{
112+
Labels: map[string]string{
113+
"fleet.azure.com/managed-by": "arm",
114+
},
115+
ResourceVersion: vapb.ResourceVersion,
117116
}
117+
vapb.ObjectMeta = ometa
118118
vapb.Spec = admv1.ValidatingAdmissionPolicyBindingSpec{
119119
PolicyName: resourceName,
120120
ValidationActions: []admv1.ValidationAction{

pkg/webhook/managedresource/validatingadmissionpolicy_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/stretchr/testify/assert"
1212
admv1 "k8s.io/api/admissionregistration/v1"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
)
1415

1516
func TestGetValidatingAdmissionPolicy(t *testing.T) {
@@ -50,9 +51,121 @@ func TestGetValidatingAdmissionPolicy(t *testing.T) {
5051
})
5152
}
5253

54+
func TestMutateValidatingAdmissionPolicy(t *testing.T) {
55+
t.Parallel()
56+
57+
tests := []struct {
58+
name string
59+
isHub bool
60+
resourceVersion string
61+
initialLabels map[string]string
62+
}{
63+
{
64+
name: "preserves ResourceVersion and updates labels for member cluster",
65+
isHub: false,
66+
resourceVersion: "12345",
67+
initialLabels: map[string]string{"existing": "label"},
68+
},
69+
{
70+
name: "preserves ResourceVersion and updates labels for hub cluster",
71+
isHub: true,
72+
resourceVersion: "67890",
73+
initialLabels: map[string]string{"old": "value"},
74+
},
75+
{
76+
name: "preserves empty ResourceVersion and sets labels",
77+
isHub: false,
78+
resourceVersion: "",
79+
initialLabels: nil,
80+
},
81+
{
82+
name: "overwrites existing managed label while preserving ResourceVersion",
83+
isHub: false,
84+
resourceVersion: "54321",
85+
initialLabels: map[string]string{"fleet.azure.com/managed-by": "old-value", "other": "label"},
86+
},
87+
}
88+
89+
for _, tt := range tests {
90+
t.Run(tt.name, func(t *testing.T) {
91+
t.Parallel()
92+
93+
vap := &admv1.ValidatingAdmissionPolicy{
94+
ObjectMeta: metav1.ObjectMeta{
95+
Name: "test-policy",
96+
ResourceVersion: tt.resourceVersion,
97+
Labels: tt.initialLabels,
98+
},
99+
}
100+
101+
mutateValidatingAdmissionPolicy(vap, tt.isHub)
102+
103+
assert.Equal(t, tt.resourceVersion, vap.ResourceVersion, "ResourceVersion should be preserved")
104+
assert.Equal(t, "arm", vap.Labels["fleet.azure.com/managed-by"], "managed-by label should be set to 'arm'")
105+
106+
// Verify that only the managed-by label exists (other labels are not preserved)
107+
expectedLabels := map[string]string{
108+
"fleet.azure.com/managed-by": "arm",
109+
}
110+
assert.Equal(t, expectedLabels, vap.Labels, "Only the managed-by label should exist")
111+
})
112+
}
113+
}
114+
53115
func TestGetValidatingAdmissionPolicyBinding(t *testing.T) {
54116
t.Parallel()
55117

56118
vap := getValidatingAdmissionPolicyBinding()
57119
assert.NotNil(t, vap)
58120
}
121+
122+
func TestMutateValidatingAdmissionPolicyBinding(t *testing.T) {
123+
t.Parallel()
124+
125+
tests := []struct {
126+
name string
127+
resourceVersion string
128+
initialLabels map[string]string
129+
}{
130+
{
131+
name: "preserves ResourceVersion and updates labels",
132+
resourceVersion: "12345",
133+
initialLabels: map[string]string{"existing": "label"},
134+
},
135+
{
136+
name: "preserves empty ResourceVersion and sets labels",
137+
resourceVersion: "",
138+
initialLabels: nil,
139+
},
140+
{
141+
name: "overwrites existing managed label while preserving ResourceVersion",
142+
resourceVersion: "67890",
143+
initialLabels: map[string]string{"fleet.azure.com/managed-by": "old-value", "other": "label"},
144+
},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
t.Parallel()
150+
151+
vapb := &admv1.ValidatingAdmissionPolicyBinding{
152+
ObjectMeta: metav1.ObjectMeta{
153+
Name: "test-binding",
154+
ResourceVersion: tt.resourceVersion,
155+
Labels: tt.initialLabels,
156+
},
157+
}
158+
159+
mutateValidatingAdmissionPolicyBinding(vapb)
160+
161+
assert.Equal(t, tt.resourceVersion, vapb.ResourceVersion, "ResourceVersion should be preserved")
162+
assert.Equal(t, "arm", vapb.Labels["fleet.azure.com/managed-by"], "managed-by label should be set to 'arm'")
163+
164+
// Verify that only the managed-by label exists (other labels are not preserved)
165+
expectedLabels := map[string]string{
166+
"fleet.azure.com/managed-by": "arm",
167+
}
168+
assert.Equal(t, expectedLabels, vapb.Labels, "Only the managed-by label should exist")
169+
})
170+
}
171+
}

0 commit comments

Comments
 (0)