Skip to content

Commit 28fdd5b

Browse files
black-dragon74mergify[bot]
authored andcommitted
controller: consider managed state annotation in new pvc controller
This patch adds the missing logic to ignore reconciles for unmanaged resources. Signed-off-by: Niraj Yadav <niryadav@redhat.com>
1 parent 1ce237a commit 28fdd5b

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

internal/controller/csiaddons/pvc_new_controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ func (r *PVCReconiler) reconcileFeature(
185185
}
186186
logger.Info("determined the state to reconcile with", "shouldExist", shouldExist, "exists", exists)
187187

188+
// We allow the user to mark a feature as unmanged by editing an existing resource
189+
// and setting the CSIAddonsStateAnnotation to: unmanaged
190+
// Return and do nothing in such cases
191+
if exists && !utils.IsManagedByController(existingObj) {
192+
logger.Info("The existing object is not managed by the controller, doing nothing for it.", "objName", existingObj.GetName(), "objNamespace", existingObj.GetNamespace())
193+
194+
return nil
195+
}
196+
188197
// Object should not be present in the cluster, garbage collect or return
189198
if !shouldExist {
190199
if exists {

internal/controller/utils/annotations.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package utils
1818

1919
import (
2020
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
21+
"sigs.k8s.io/controller-runtime/pkg/client"
2122
)
2223

2324
var (
@@ -53,3 +54,13 @@ func AnnotationValueChanged(oldAnnotations, newAnnotations map[string]string, ke
5354
}
5455
return false
5556
}
57+
58+
// IsManagedByController checks whether an object is managed by the controller.
59+
// It returns false if the object has the CSIAddonsStateAnnotation set to a value
60+
// other than CSIAddonsStateManaged. If the annotation is missing it returns true.
61+
func IsManagedByController(obj client.Object) bool {
62+
if v, ok := obj.GetAnnotations()[CSIAddonsStateAnnotation]; ok && v != CSIAddonsStateManaged {
63+
return false
64+
}
65+
return true
66+
}

internal/controller/utils/annotations_test.go

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ limitations under the License.
1616

1717
package utils
1818

19-
import "testing"
19+
import (
20+
"testing"
21+
22+
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
23+
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
)
2027

2128
func TestAnnotationValueChanged(t *testing.T) {
2229
tests := []struct {
@@ -86,3 +93,66 @@ func TestAnnotationValueChanged(t *testing.T) {
8693
})
8794
}
8895
}
96+
97+
func TestIsManagedByController(t *testing.T) {
98+
tests := []struct {
99+
name string
100+
obj client.Object
101+
expected bool
102+
}{
103+
{
104+
name: "No annotations",
105+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
106+
ObjectMeta: metav1.ObjectMeta{
107+
Annotations: nil,
108+
},
109+
},
110+
expected: true,
111+
},
112+
{
113+
name: "No CSIAddonsStateAnnotation",
114+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
115+
ObjectMeta: metav1.ObjectMeta{
116+
Annotations: map[string]string{"other": "value"},
117+
},
118+
},
119+
expected: true,
120+
},
121+
{
122+
name: "CSIAddonsStateAnnotation set to managed",
123+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
124+
ObjectMeta: metav1.ObjectMeta{
125+
Annotations: map[string]string{CSIAddonsStateAnnotation: CSIAddonsStateManaged},
126+
},
127+
},
128+
expected: true,
129+
},
130+
{
131+
name: "CSIAddonsStateAnnotation set to unmanaged",
132+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
133+
ObjectMeta: metav1.ObjectMeta{
134+
Annotations: map[string]string{CSIAddonsStateAnnotation: "unmanaged"},
135+
},
136+
},
137+
expected: false,
138+
},
139+
{
140+
name: "CSIAddonsStateAnnotation set to empty string",
141+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Annotations: map[string]string{CSIAddonsStateAnnotation: ""},
144+
},
145+
},
146+
expected: false,
147+
},
148+
}
149+
150+
for _, tt := range tests {
151+
t.Run(tt.name, func(t *testing.T) {
152+
result := IsManagedByController(tt.obj)
153+
if result != tt.expected {
154+
t.Errorf("IsManagedByController() = %v, want %v", result, tt.expected)
155+
}
156+
})
157+
}
158+
}

0 commit comments

Comments
 (0)