Skip to content

Commit 027f4aa

Browse files
committed
RegisteredLimit Actuator mutability
On-behalf-of: SAP nils.gondermann@sap.com
1 parent 60061a7 commit 027f4aa

4 files changed

Lines changed: 70 additions & 10 deletions

File tree

api/v1alpha1/registeredlimit_types.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ type RegisteredLimitResourceSpec struct {
3333
// +kubebuilder:validation:MinLength:=1
3434
// +kubebuilder:validation:MaxLength:=255
3535
// +required
36-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="resourceName is immutable"
3736
ResourceName string `json:"resourceName,omitempty"`
3837

3938
// defaultLimit is limit of the specified resource in the given context.
4039
// +kubebuilder:validation:Minimum=-1
4140
// +kubebuilder:validation:Maximum=2147483647
4241
// +required
43-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="defaultLimit is immutable"
4442
DefaultLimit *int32 `json:"defaultLimit,omitempty"`
4543
}
4644

config/crd/bases/openstack.k-orc.cloud_registeredlimits.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ spec:
162162
maximum: 2147483647
163163
minimum: -1
164164
type: integer
165-
x-kubernetes-validations:
166-
- message: defaultLimit is immutable
167-
rule: self == oldSelf
168165
description:
169166
description: description is a human-readable description for the
170167
resource.
@@ -176,9 +173,6 @@ spec:
176173
maxLength: 255
177174
minLength: 1
178175
type: string
179-
x-kubernetes-validations:
180-
- message: resourceName is immutable
181-
rule: self == oldSelf
182176
serviceRef:
183177
description: serviceRef is a reference to the ORC Service which
184178
this resource is associated with.

internal/controllers/registeredlimit/actuator.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ func (actuator registeredlimitActuator) updateResource(ctx context.Context, obj
190190
updateOpts := registeredlimits.UpdateOpts{}
191191

192192
handleDescriptionUpdate(&updateOpts, resource, osResource)
193-
194-
// TODO(scaffolding): add handler for all fields supporting mutability
193+
handleResourceNameUpdate(&updateOpts, resource, osResource)
194+
handleDefaultLimitUpdate(&updateOpts, resource, osResource)
195195

196196
needsUpdate, err := needsUpdate(updateOpts)
197197
if err != nil {
@@ -236,6 +236,20 @@ func handleDescriptionUpdate(updateOpts *registeredlimits.UpdateOpts, resource *
236236
}
237237
}
238238

239+
func handleResourceNameUpdate(updateOpts *registeredlimits.UpdateOpts, resource *resourceSpecT, osResource *osResourceT) {
240+
resourceName := resource.ResourceName
241+
if osResource.ResourceName != resourceName {
242+
updateOpts.ResourceName = resourceName
243+
}
244+
}
245+
246+
func handleDefaultLimitUpdate(updateOpts *registeredlimits.UpdateOpts, resource *resourceSpecT, osResource *osResourceT) {
247+
defaultLimit := int(*resource.DefaultLimit)
248+
if osResource.DefaultLimit != defaultLimit {
249+
updateOpts.DefaultLimit = &defaultLimit
250+
}
251+
}
252+
239253
func (actuator registeredlimitActuator) GetResourceReconcilers(ctx context.Context, orcObject orcObjectPT, osResource *osResourceT, controller interfaces.ResourceController) ([]resourceReconciler, progress.ReconcileStatus) {
240254
return []resourceReconciler{
241255
actuator.updateResource,

internal/controllers/registeredlimit/actuator_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,60 @@ func TestNeedsUpdate(t *testing.T) {
5252
}
5353
}
5454

55+
func TestHandleResourceNameUpdate(t *testing.T) {
56+
testCases := []struct {
57+
name string
58+
newValue *string
59+
existingValue string
60+
expectChange bool
61+
}{
62+
{name: "Identical", newValue: ptr.To("same-resourceName"), existingValue: "same-resourceName", expectChange: false},
63+
{name: "Different", newValue: ptr.To("new-resourceName"), existingValue: "same-resourceName", expectChange: true},
64+
}
65+
66+
for _, tt := range testCases {
67+
t.Run(tt.name, func(t *testing.T) {
68+
resource := &orcv1alpha1.RegisteredLimitResourceSpec{ResourceName: ptr.Deref(tt.newValue, "")}
69+
osResource := &osResourceT{ResourceName: tt.existingValue}
70+
71+
updateOpts := registeredlimits.UpdateOpts{}
72+
handleResourceNameUpdate(&updateOpts, resource, osResource)
73+
74+
got, _ := needsUpdate(updateOpts)
75+
if got != tt.expectChange {
76+
t.Errorf("Expected change: %v, got: %v", tt.expectChange, got)
77+
}
78+
})
79+
}
80+
}
81+
82+
func TestHandleDefaultLimitUpdate(t *testing.T) {
83+
testCases := []struct {
84+
name string
85+
newValue int
86+
existingValue int
87+
expectChange bool
88+
}{
89+
{name: "Identical", newValue: 1, existingValue: 1, expectChange: false},
90+
{name: "Different", newValue: 2, existingValue: 1, expectChange: true},
91+
}
92+
93+
for _, tt := range testCases {
94+
t.Run(tt.name, func(t *testing.T) {
95+
resource := &orcv1alpha1.RegisteredLimitResourceSpec{DefaultLimit: ptr.To(int32(tt.newValue))}
96+
osResource := &osResourceT{DefaultLimit: tt.existingValue}
97+
98+
updateOpts := registeredlimits.UpdateOpts{}
99+
handleDefaultLimitUpdate(&updateOpts, resource, osResource)
100+
101+
got, _ := needsUpdate(updateOpts)
102+
if got != tt.expectChange {
103+
t.Errorf("Expected change: %v, got: %v", tt.expectChange, got)
104+
}
105+
})
106+
}
107+
}
108+
55109
func TestHandleDescriptionUpdate(t *testing.T) {
56110
ptrToDescription := ptr.To[string]
57111
testCases := []struct {

0 commit comments

Comments
 (0)