Skip to content

Commit 7c168c2

Browse files
controller: add shim to garbage collect old resources
This patch adds a shim layer that is responsible for cleaning up the old resources so that we do not have two CronJobs created for the same underlying PVC. Signed-off-by: Niraj Yadav <niryadav@redhat.com>
1 parent 6d372b4 commit 7c168c2

3 files changed

Lines changed: 99 additions & 4 deletions

File tree

internal/controller/csiaddons/pvc_new_controller.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,53 @@ func (r *PVCReconiler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
108108

109109
// Reconcile for dependent features
110110
// Reconcile - Key rotation
111+
keyRotationName := fmt.Sprintf("%s-keyrotation", pvc.Name)
111112
keyRotationSched := sc.Annotations[utils.KrcJobScheduleTimeAnnotation]
112113
keyRotationEnabled := sc.Annotations[utils.KrEnableAnnotation]
113114
keyRotationChild := &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
114115
ObjectMeta: metav1.ObjectMeta{
115-
Name: fmt.Sprintf("%s-keyrotation", pvc.Name),
116+
Name: keyRotationName,
116117
Namespace: pvc.Namespace,
117118
},
118119
}
119120

121+
// FIXME: This is a shim and should be removed in later releases
122+
// along with the field indexers on child objects
123+
if requeue, err := utils.CleanOldJobs(ctx,
124+
r.Client,
125+
logger,
126+
req,
127+
&csiaddonsv1alpha1.EncryptionKeyRotationCronJobList{},
128+
keyRotationName); err != nil || requeue {
129+
return ctrl.Result{Requeue: requeue}, err
130+
}
131+
120132
if err := r.reoncileFeature(ctx, logger, pvc, keyRotationChild, keyRotationSched, keyRotationEnabled); err != nil {
121133
return ctrl.Result{}, err
122134
}
123135

124136
// Reconcile - Reclaim space
137+
reclaimSpaceName := fmt.Sprintf("%s-reclaimspace", pvc.Name)
125138
reclaimSpaceSched := sc.Annotations[utils.RsCronJobScheduleTimeAnnotation]
126-
relciamSpaceChild := &csiaddonsv1alpha1.ReclaimSpaceCronJob{
139+
reclaimSpaceChild := &csiaddonsv1alpha1.ReclaimSpaceCronJob{
127140
ObjectMeta: metav1.ObjectMeta{
128-
Name: fmt.Sprintf("%s-reclaimspace", pvc.Name),
141+
Name: reclaimSpaceName,
129142
Namespace: pvc.Namespace,
130143
},
131144
}
132145

133-
if err := r.reoncileFeature(ctx, logger, pvc, relciamSpaceChild, reclaimSpaceSched, "true"); err != nil {
146+
// FIXME: This is a shim and should be removed in later releases
147+
// along with the field indexers on child objects
148+
if requeue, err := utils.CleanOldJobs(ctx,
149+
r.Client,
150+
logger,
151+
req,
152+
&csiaddonsv1alpha1.ReclaimSpaceCronJobList{},
153+
reclaimSpaceName); err != nil || requeue {
154+
return ctrl.Result{Requeue: requeue}, err
155+
}
156+
157+
if err := r.reoncileFeature(ctx, logger, pvc, reclaimSpaceChild, reclaimSpaceSched, "true"); err != nil {
134158
return ctrl.Result{}, err
135159
}
136160

internal/controller/utils/predicates.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package utils
33
import (
44
"context"
55

6+
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
7+
68
corev1 "k8s.io/api/core/v1"
79
storagev1 "k8s.io/api/storage/v1"
810
"k8s.io/apimachinery/pkg/types"
@@ -90,6 +92,18 @@ func SetupPVCControllerIndexers(mgr ctrl.Manager) error {
9092
return []string{*pvc.Spec.StorageClassName}
9193
},
9294
},
95+
// FIXME: Remove this shim in later releases
96+
{
97+
obj: &csiaddonsv1alpha1.ReclaimSpaceCronJob{},
98+
field: JobOwnerKey,
99+
indexFn: ExtractOwnerNameFromPVCObj[*csiaddonsv1alpha1.ReclaimSpaceCronJob],
100+
},
101+
// FIXME: Remove this shim in later releases
102+
{
103+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{},
104+
field: JobOwnerKey,
105+
indexFn: ExtractOwnerNameFromPVCObj[*csiaddonsv1alpha1.EncryptionKeyRotationCronJob],
106+
},
93107
}
94108

95109
for _, index := range indices {

internal/controller/utils/reclaimspace_keyrotation_utils.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package utils
22

33
import (
4+
"context"
45
"errors"
56

67
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
78

9+
"github.com/go-logr/logr"
10+
"k8s.io/apimachinery/pkg/api/meta"
811
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
ctrl "sigs.k8s.io/controller-runtime"
913
"sigs.k8s.io/controller-runtime/pkg/client"
1014
)
1115

@@ -105,3 +109,56 @@ func ExtractOwnerNameFromPVCObj[T client.Object](rawObj client.Object) []string
105109

106110
return []string{owner.Name}
107111
}
112+
113+
func CleanOldJobs(
114+
ctx context.Context,
115+
c client.Client,
116+
log logr.Logger,
117+
req ctrl.Request,
118+
objList client.ObjectList,
119+
expectedName string,
120+
) (bool, error) {
121+
// We need to find all the EKRCronJobs in the namespace of the PVC which
122+
// are owned by this controller and remove it if it doesn't match the expected name
123+
shouldRequeue := false
124+
125+
if err := c.List(ctx, objList, client.InNamespace(req.Namespace), client.MatchingFields{JobOwnerKey: req.Name}); client.IgnoreNotFound(err) != nil {
126+
return shouldRequeue, err
127+
}
128+
129+
items, err := meta.ExtractList(objList)
130+
if err != nil {
131+
return shouldRequeue, err
132+
}
133+
134+
for _, item := range items {
135+
obj, ok := item.(client.Object)
136+
if !ok {
137+
// As long as objList is a k8s object
138+
// we will never hit this
139+
continue
140+
}
141+
objName := obj.GetName()
142+
143+
// Only delete what we might have created
144+
if owner := metav1.GetControllerOf(obj); owner == nil ||
145+
owner.Kind != "PersistentVolumeClaim" ||
146+
owner.Name != req.Name {
147+
log.Info("Found an object without any owner", "jobName", objName)
148+
149+
continue
150+
}
151+
152+
// If the name does not match, delete the resource
153+
if obj.GetName() != expectedName {
154+
if err := c.Delete(ctx, obj); client.IgnoreNotFound(err) != nil {
155+
return shouldRequeue, err
156+
}
157+
158+
shouldRequeue = true
159+
log.Info("Deleted old job", "jobName", objName)
160+
}
161+
}
162+
163+
return shouldRequeue, nil
164+
}

0 commit comments

Comments
 (0)