-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
controller: add PersistentVolumeClaimController #93
Conversation
I'll add documentation for this by tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks quite reasonable. Missing update to the documentation under doc/reclaimspace.md
config/rbac/role.yaml
Outdated
resources: | ||
- persistentvolumeclaims | ||
verbs: | ||
- create |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why create,delete and update permission is required?
config/rbac/role.yaml
Outdated
- persistentvolumeclaims/finalizers | ||
verbs: | ||
- update | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- persistentvolumeclaims/status | ||
verbs: | ||
- get | ||
- patch | ||
- update |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here why this permission is required?
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *PersistentVolumeClaimReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &csiaddonsv1alpha1.ReclaimSpaceCronJob{}, jobOwnerKey, func(rawObj client.Object) []string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split this into multiple lines for better reading
err = r.deleteChildCronJobs(ctx, &logger, &req) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one PVC will have only one Cron job why this is required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one PVC will have only one Cron job why this is required?
yes, recreating Cron job each time simplifies the logic for a lot of scenarios.
This will also help in handling deletion when schedule is removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i still need to understand this one. what are childCronJobs referred here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i still need to understand this one. what are childCronJobs referred here?
Just the cronjob created for this PVC previously.
Each reconcile will delete old cronjob and create new one with new name.
Using saved name led to lot of complexity.
_, nameFound := annotations[rsCronJobNameAnnotation] | ||
if nameFound { | ||
delete(annotations, rsCronJobNameAnnotation) | ||
pvc.SetAnnotations(annotations) | ||
err = r.Client.Update(ctx, pvc) | ||
if err != nil { | ||
logger.Error(err, "Failed to update annotation") | ||
|
||
return ctrl.Result{}, err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please explain a bit more why this block is required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please explain a bit more why this block is required?
when schedule annotation is removed, we are checking if name annotation exists and removing it (since we added it).
pvc.SetAnnotations(annotations) | ||
err = r.Client.Update(ctx, pvc) | ||
if err != nil { | ||
logger.Error(err, "Failed to update annotation") | ||
|
||
return ctrl.Result{}, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set annotation only after creating the cronJob?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
"testing" | ||
|
||
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/v1alpha1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line separation
doc changes are required with examples/usage. |
befbf09
to
be8eced
Compare
updated permissions, using patch, added doc and refactored the code |
What happens if the PVC get recreated with same name @Rakshith-R ? |
err = r.deleteChildCronJobs(ctx, &logger, &req) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i still need to understand this one. what are childCronJobs referred here?
} | ||
|
||
annotations := pvc.GetAnnotations() | ||
schedule, scheduleFound := getScheduleFromAnnotation(&logger, annotations) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more question. if the annotations are found but the cronjob CR is not created are we going to recreate it with same name or a new one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more question. if the annotations are found but the cronjob CR is not created are we going to recreate it with same name or a new one?
new name each time, using the same name causes pre-condition error
since name/namespace remains same while uid changes for other controllers.
docs/reclaimspace.md
Outdated
to PersistentVolumeClaim object. | ||
|
||
``` | ||
$ kubectl get pvc pvc-1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use meaningful name instead of pvc-1, pvc-sample and pvc-1 names are not matching
docs/reclaimspace.md
Outdated
$ kubectl annotate pvc pvc-sample "reclaimspace.csiaddons.openshift.io/schedule=@midnight" | ||
persistentvolumeclaim/pvc-sample annotated | ||
|
||
$ k get reclaimspacecronjobs.csiaddons.openshift.io |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k get to kubectl get
docs/reclaimspace.md
Outdated
$ kubectl annotate pvc pvc-sample "reclaimspace.csiaddons.openshift.io/schedule=*/1 * * * *" --overwrite=true | ||
persistentvolumeclaim/pvc-sample annotated | ||
|
||
$ k get reclaimspacecronjobs.csiaddons.openshift.io |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k get to kubectl get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any hiccups if the PVC get recreated with the same name?
Due to controllerOwnerRef, previous child cronjobs will get deleted |
220258c
to
3e71ef7
Compare
Pull request has been modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a better naming suggestion at the moment but PersistentVolumeClaimController
doesn't sound great.
} | ||
|
||
// create cronjob name | ||
rsCronJobName := fmt.Sprintf("%s-%v", pvc.Name, time.Now().Unix()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be moved into a new function getCronJobName()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be moved into a new function getCronJobName()?
done, names it generateCronJobName()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well you forget to use it :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣 its used now,
} | ||
|
||
// add cronjob name in annotations. | ||
patch := []byte(fmt.Sprintf(`{"metadata":{"annotations":{%q:%q}}}`, rsCronJobNameAnnotation, rsCronJobName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be name it annotations instead of patch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be name it annotations instead of patch?
annotations is already taken, just patch sounds fine to me
return ctrl.Result{}, err | ||
} | ||
|
||
logger.Info("Successfully created reclaimSpaceCronJob") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line before return?
} | ||
// reconcile only if schedule annotation is found. | ||
_, ok := e.Object.GetAnnotations()[rsCronJobScheduleTimeAnnotation] | ||
return ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line here too!
// reconcile only if schedule annotation between old and new objects have changed. | ||
oldSchdeule, oldOk := e.ObjectOld.GetAnnotations()[rsCronJobScheduleTimeAnnotation] | ||
newSchdeule, newOk := e.ObjectNew.GetAnnotations()[rsCronJobScheduleTimeAnnotation] | ||
return oldOk != newOk || oldSchdeule != newSchdeule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line here too!
|
||
return defaultSchedule, true | ||
} | ||
return schedule, true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line
} | ||
_, err := cron.ParseStandard(schedule) | ||
if err != nil { | ||
logger.Info(fmt.Sprintf("Parsing given schedule %q failed, using default schedule %q", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like logger.Warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like logger.Warning?
Should be fine for now, there is no default logger.Warning,
We may add it the future.
// Reconcile is part of the main kubernetes reconciliation loop which aims to | ||
// move the current state of the cluster closer to the desired state. | ||
// This is triggered when `reclaimspace.csiaddons.openshift/schedule` annotation | ||
// is found on newly created PVC or if there is a change in valude of the annotation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/valude/value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done @pkalever PTAL
3e71ef7
to
2612f1c
Compare
😆 me too, check the pr description. We can always come back and change it, this is completely internal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I didn't block this PR for the naming part, but for the rest of the comments.
You have one more to take care of for now i.e use generateCronJobName
2612f1c
to
50644f3
Compare
done 👍 thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, the only thing I don't like is the Controller name as mentioned before.
How about PVCAnnotationsController?
making some changes to update child cronjob instead of recreating them |
50644f3
to
a86c379
Compare
Pull request has been modified.
2ebeffb
to
d87ed84
Compare
PTAL @csi-addons/kubernetes-csi-addons-reviewers @csi-addons/kubernetes-csi-addons-contributors |
d87ed84
to
ca9ae69
Compare
logger.Error(err, "Failed to delete child reclaimSpaceCronJob", | ||
"ReclaimSpaceCronJobName", job.Name) | ||
|
||
return fmt.Errorf("Failed to delete child reclaimSpaceCronJob: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use %w
return childjob name also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
%w
return childjob name also?
done
ca9ae69
to
611422e
Compare
@Mergifyio rebase |
This controller watches for annotation on PVC, if matches with `reclaimspace.csiaddons.openshift.io/schedule` which can specify a schedule, a reclaimspace cron job cr is created for that PVC witha generated name having pvc-name as prefix. Signed-off-by: Rakshith R <[email protected]>
✅ Branch has been successfully rebased |
611422e
to
e344894
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, Thanks!
|
||
return ok | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this not require the update
permission for the PVC/annotations? patch
is there, is it not cleaner to patch it in any case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Watch perm is enough to check for events.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sure. Thanks!
Syncing latest changes from main for kubernetes-csi-addons
This controller watches for annotation on PVC,
if matches with
reclaimspace.csiaddons.openshift.io/schedule
which can specify a schedule, a reclaimspace cron job cr
is created for that PVC witha generated name having pvc-name
as prefix.
Signed-off-by: Rakshith R [email protected]
[open to changing name of controller 😉 ]