-
Notifications
You must be signed in to change notification settings - Fork 38
feat: add rollout controller skeleton #378
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
pkg/controllers/clusterresourcerollout/clusterresourcerollout_controller.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT license. | ||
| */ | ||
|
|
||
| package clusterresourcerollout | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/client-go/tools/record" | ||
| "k8s.io/klog/v2" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
||
| fleetv1 "go.goms.io/fleet/apis/v1" | ||
| ) | ||
|
|
||
| // Reconciler reconciles the active ClusterResourceSnapshot object. | ||
| type Reconciler struct { | ||
| client.Client | ||
| recorder record.EventRecorder | ||
| } | ||
|
|
||
| // Reconcile rollouts the resources by updating/deleting the clusterResourceBindings. | ||
| func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| name := req.NamespacedName | ||
| crs := fleetv1.ClusterResourceSnapshot{} | ||
| crsKRef := klog.KRef(name.Namespace, name.Name) | ||
|
|
||
| startTime := time.Now() | ||
| klog.V(2).InfoS("Reconciliation starts", "clusterResourceSnapshot", crsKRef) | ||
| defer func() { | ||
| latency := time.Since(startTime).Milliseconds() | ||
| klog.V(2).InfoS("Reconciliation ends", "clusterResourceSnapshot", crsKRef, "latency", latency) | ||
| }() | ||
|
|
||
| if err := r.Client.Get(ctx, name, &crs); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| klog.V(4).InfoS("Ignoring NotFound clusterResourceSnapshot", "clusterResourceSnapshot", crsKRef) | ||
| return ctrl.Result{}, nil | ||
| } | ||
| klog.ErrorS(err, "Failed to get clusterResourceSnapshot", "clusterResourceSnapshot", crsKRef) | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
||
| if crs.ObjectMeta.DeletionTimestamp != nil { | ||
| return r.handleDelete(ctx, &crs) | ||
| } | ||
|
|
||
| // register finalizer | ||
| if !controllerutil.ContainsFinalizer(&crs, fleetv1.ClusterResourceSnapshotFinalizer) { | ||
| controllerutil.AddFinalizer(&crs, fleetv1.ClusterResourceSnapshotFinalizer) | ||
| if err := r.Update(ctx, &crs); err != nil { | ||
| klog.ErrorS(err, "Failed to add mcs finalizer", "clusterResourceSnapshot", crsKRef) | ||
| return ctrl.Result{}, err | ||
| } | ||
| } | ||
| return r.handleUpdate(ctx, &crs) | ||
| } | ||
|
|
||
| func (r *Reconciler) handleDelete(_ context.Context, _ *fleetv1.ClusterResourceSnapshot) (ctrl.Result, error) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| func (r *Reconciler) handleUpdate(_ context.Context, _ *fleetv1.ClusterResourceSnapshot) (ctrl.Result, error) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| r.recorder = mgr.GetEventRecorderFor("clusterResourceRollout") | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| For(&fleetv1.ClusterResourceSnapshot{}). | ||
| Complete(r) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, I remember that ResourceSnapshot has a reference to PolicySnapshot, correct?
Additionally, since we cannot tell whether a PolicySnapshot is the latest from looking at the name, we have to talk to the API server (or actually, cache) any way.
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.
yeah, but we only store the resourceSnapshot name here, but not include the policySnapshot name. :(
We will get the latest policySnapshot name by calling API server.
This field is to quickly figure out which policySnapshot the resourceSnapsht of the binding points to.
otherwise, we need to call API server by GET resourceSnapshot.
Uh oh!
There was an error while loading. Please reload this page.
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, but the client in use will query the cache for the GET ResourceSnapshot call, right? It will never reach API server. Besides, doesn't the rollout controller have to retrieve ResourceSnapshot any way?
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 the controller needs to get the ResourceSnapshot? If so, then I am not sure we need this field as the policySnapshotName is in the ResourceSnapshot.
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.
nope, it does not need to query the resourceSnapshot for each binding.
The PolicySnapshotName will be used in two places:
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.
Please see the message in chat. We don't need this field for saving trips to API server (the trip will not happen because of caches), but we do need it for a different reason.