-
Notifications
You must be signed in to change notification settings - Fork 253
fix clusterprovision creation race #518
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package clusterdeployment | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/util/workqueue" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
|
|
||
| hivev1 "github.com/openshift/hive/pkg/apis/hive/v1alpha1" | ||
| ) | ||
|
|
||
| func (r *ReconcileClusterDeployment) watchClusterProvisions(c controller.Controller) error { | ||
| handler := &clusterProvisionEventHandler{ | ||
| EnqueueRequestForOwner: handler.EnqueueRequestForOwner{ | ||
| IsController: true, | ||
| OwnerType: &hivev1.ClusterDeployment{}, | ||
| }, | ||
| reconciler: r, | ||
| } | ||
| return c.Watch(&source.Kind{Type: &hivev1.ClusterProvision{}}, handler) | ||
| } | ||
|
|
||
| var _ handler.EventHandler = &clusterProvisionEventHandler{} | ||
|
|
||
| type clusterProvisionEventHandler struct { | ||
| handler.EnqueueRequestForOwner | ||
| reconciler *ReconcileClusterDeployment | ||
| } | ||
|
|
||
| // Create implements handler.EventHandler | ||
| func (h *clusterProvisionEventHandler) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) { | ||
| h.reconciler.logger.Info("ClusterProvision created") | ||
| h.reconciler.trackClusterProvisionAdd(e.Object) | ||
| h.EnqueueRequestForOwner.Create(e, q) | ||
| } | ||
|
|
||
| // resolveControllerRef returns the controller referenced by a ControllerRef, | ||
| // or nil if the ControllerRef could not be resolved to a matching controller | ||
| // of the correct Kind. | ||
| func (r *ReconcileClusterDeployment) resolveControllerRef(namespace string, controllerRef *metav1.OwnerReference) *hivev1.ClusterDeployment { | ||
| // We can't look up by UID, so look up by Name and then verify UID. | ||
| // Don't even try to look up by Name if it's the wrong Kind. | ||
| if controllerRef.Kind != controllerKind.Kind { | ||
| return nil | ||
| } | ||
| cd := &hivev1.ClusterDeployment{} | ||
| if err := r.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: controllerRef.Name}, cd); err != nil { | ||
| return nil | ||
| } | ||
| if cd.UID != controllerRef.UID { | ||
| // The controller we found with this Name is not the same one that the | ||
| // ControllerRef points to. | ||
| return nil | ||
| } | ||
| return cd | ||
| } | ||
|
|
||
| // When a clusterprovision is created, update the expectations of the clusterdeployment that owns the clusterprovision. | ||
| func (r *ReconcileClusterDeployment) trackClusterProvisionAdd(obj interface{}) { | ||
| provision := obj.(*hivev1.ClusterProvision) | ||
| if provision.DeletionTimestamp != nil { | ||
| // on a restart of the controller, it's possible a new object shows up in a state that | ||
| // is already pending deletion. Prevent the object from being a creation observation. | ||
| return | ||
| } | ||
|
|
||
| // If it has a ControllerRef, that's all that matters. | ||
| if controllerRef := metav1.GetControllerOf(provision); controllerRef != nil { | ||
| cd := r.resolveControllerRef(provision.Namespace, controllerRef) | ||
| if cd == nil { | ||
| return | ||
| } | ||
| cdKey := types.NamespacedName{Namespace: cd.Namespace, Name: cd.Name}.String() | ||
| r.expectations.CreationObserved(cdKey) | ||
| } | ||
| } |
Oops, something went wrong.
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.
Something that's confusing me a little bit here, it looks like the intent here is to expect the creation of the cluster provision, but the expectation itself is using the cluster deployment's name. Should this be the provision's name? Or is that an issue with the random component of their names.
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.
The expectations are set up to expect a creation on behalf of the clusterdeployment. The key is always the resource being reconciled, and not the resource being created.
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.
gotcha thanks.