Skip to content

Commit 40a8a6c

Browse files
black-dragon74mergify[bot]
authored andcommitted
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 66244a4 commit 40a8a6c

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed

internal/controller/csiaddons/pvc_new_controller.go

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

106106
// Reconcile for dependent features
107107
// Reconcile - Key rotation
108+
keyRotationName := fmt.Sprintf("%s-keyrotation", pvc.Name)
108109
keyRotationSched := sc.Annotations[utils.KrcJobScheduleTimeAnnotation]
109110
keyRotationEnabled := sc.Annotations[utils.KrEnableAnnotation]
110111
keyRotationChild := &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{
111112
ObjectMeta: metav1.ObjectMeta{
112-
Name: fmt.Sprintf("%s-keyrotation", pvc.Name),
113+
Name: keyRotationName,
113114
Namespace: pvc.Namespace,
114115
},
115116
}
116117

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

121133
// Reconcile - Reclaim space
134+
reclaimSpaceName := fmt.Sprintf("%s-reclaimspace", pvc.Name)
122135
reclaimSpaceSched := sc.Annotations[utils.RsCronJobScheduleTimeAnnotation]
123-
relciamSpaceChild := &csiaddonsv1alpha1.ReclaimSpaceCronJob{
136+
reclaimSpaceChild := &csiaddonsv1alpha1.ReclaimSpaceCronJob{
124137
ObjectMeta: metav1.ObjectMeta{
125-
Name: fmt.Sprintf("%s-reclaimspace", pvc.Name),
138+
Name: reclaimSpaceName,
126139
Namespace: pvc.Namespace,
127140
},
128141
}
129142

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

internal/controller/utils/predicates.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package utils
1919
import (
2020
"context"
2121

22+
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
23+
2224
corev1 "k8s.io/api/core/v1"
2325
storagev1 "k8s.io/api/storage/v1"
2426
"k8s.io/apimachinery/pkg/types"
@@ -106,6 +108,18 @@ func SetupPVCControllerIndexers(mgr ctrl.Manager) error {
106108
return []string{*pvc.Spec.StorageClassName}
107109
},
108110
},
111+
// FIXME: Remove this shim in later releases
112+
{
113+
obj: &csiaddonsv1alpha1.ReclaimSpaceCronJob{},
114+
field: JobOwnerKey,
115+
indexFn: ExtractOwnerNameFromPVCObj[*csiaddonsv1alpha1.ReclaimSpaceCronJob],
116+
},
117+
// FIXME: Remove this shim in later releases
118+
{
119+
obj: &csiaddonsv1alpha1.EncryptionKeyRotationCronJob{},
120+
field: JobOwnerKey,
121+
indexFn: ExtractOwnerNameFromPVCObj[*csiaddonsv1alpha1.EncryptionKeyRotationCronJob],
122+
},
109123
}
110124

111125
for _, index := range indices {

internal/controller/utils/reclaimspace_keyrotation.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ limitations under the License.
1717
package utils
1818

1919
import (
20+
"context"
2021
"errors"
2122

2223
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
2324

25+
"github.com/go-logr/logr"
26+
"k8s.io/apimachinery/pkg/api/meta"
2427
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
ctrl "sigs.k8s.io/controller-runtime"
2529
"sigs.k8s.io/controller-runtime/pkg/client"
2630
)
2731

@@ -128,3 +132,56 @@ func ExtractOwnerNameFromPVCObj[T client.Object](rawObj client.Object) []string
128132

129133
return []string{owner.Name}
130134
}
135+
136+
func CleanOldJobs(
137+
ctx context.Context,
138+
c client.Client,
139+
log logr.Logger,
140+
req ctrl.Request,
141+
objList client.ObjectList,
142+
expectedName string,
143+
) (bool, error) {
144+
// We need to find all the CronJobs in the namespace of the PVC which
145+
// are owned by this controller and remove it if it doesn't match the expected name
146+
shouldRequeue := false
147+
148+
if err := c.List(ctx, objList, client.InNamespace(req.Namespace), client.MatchingFields{JobOwnerKey: req.Name}); client.IgnoreNotFound(err) != nil {
149+
return shouldRequeue, err
150+
}
151+
152+
items, err := meta.ExtractList(objList)
153+
if err != nil {
154+
return shouldRequeue, err
155+
}
156+
157+
for _, item := range items {
158+
obj, ok := item.(client.Object)
159+
if !ok {
160+
// As long as objList is a k8s object
161+
// we will never hit this
162+
continue
163+
}
164+
objName := obj.GetName()
165+
166+
// Only delete what we might have created
167+
if owner := metav1.GetControllerOf(obj); owner == nil ||
168+
owner.Kind != "PersistentVolumeClaim" ||
169+
owner.Name != req.Name {
170+
log.Info("Found an object without any owner", "jobName", objName)
171+
172+
continue
173+
}
174+
175+
// If the name does not match, delete the resource
176+
if objName != expectedName {
177+
if err := c.Delete(ctx, obj); client.IgnoreNotFound(err) != nil {
178+
return shouldRequeue, err
179+
}
180+
181+
shouldRequeue = true
182+
log.Info("Deleted old job", "jobName", objName)
183+
}
184+
}
185+
186+
return shouldRequeue, nil
187+
}

0 commit comments

Comments
 (0)