Skip to content
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

Add finalizer for propagation policy #4836

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 66 additions & 30 deletions pkg/detector/detector.go
whitewindmills marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (d *ResourceDetector) Start(ctx context.Context) error {
Version: policyv1alpha1.GroupVersion.Version,
Resource: "propagationpolicies",
}
policyHandler := fedinformer.NewHandlerOnEvents(d.OnPropagationPolicyAdd, d.OnPropagationPolicyUpdate, d.OnPropagationPolicyDelete)
policyHandler := fedinformer.NewHandlerOnEvents(d.OnPropagationPolicyAdd, d.OnPropagationPolicyUpdate, nil)
d.InformerManager.ForResource(propagationPolicyGVR, policyHandler)
d.propagationPolicyLister = d.InformerManager.Lister(propagationPolicyGVR)

Expand All @@ -148,7 +148,7 @@ func (d *ResourceDetector) Start(ctx context.Context) error {
Version: policyv1alpha1.GroupVersion.Version,
Resource: "clusterpropagationpolicies",
}
clusterPolicyHandler := fedinformer.NewHandlerOnEvents(d.OnClusterPropagationPolicyAdd, d.OnClusterPropagationPolicyUpdate, d.OnClusterPropagationPolicyDelete)
clusterPolicyHandler := fedinformer.NewHandlerOnEvents(d.OnClusterPropagationPolicyAdd, d.OnClusterPropagationPolicyUpdate, nil)
d.InformerManager.ForResource(clusterPropagationPolicyGVR, clusterPolicyHandler)
d.clusterPropagationPolicyLister = d.InformerManager.Lister(clusterPropagationPolicyGVR)

Expand Down Expand Up @@ -377,6 +377,11 @@ func (d *ResourceDetector) LookForMatchedPolicy(object *unstructured.Unstructure
klog.Errorf("Failed to convert PropagationPolicy from unstructured object: %v", err)
return nil, err
}

if !policy.DeletionTimestamp.IsZero() {
klog.V(4).Infof("Propagation policy(%s/%s) cannot match any resource template because it's being deleted.", policy.Namespace, policy.Name)
continue
}
policyList = append(policyList, policy)
}

Expand All @@ -403,6 +408,11 @@ func (d *ResourceDetector) LookForMatchedClusterPolicy(object *unstructured.Unst
klog.Errorf("Failed to convert ClusterPropagationPolicy from unstructured object: %v", err)
return nil, err
}

if !policy.DeletionTimestamp.IsZero() {
klog.V(4).Infof("Cluster propagation policy(%s) cannot match any resource template because it's being deleted.", policy.Name)
continue
}
policyList = append(policyList, policy)
}

Expand Down Expand Up @@ -981,17 +991,6 @@ func (d *ResourceDetector) OnPropagationPolicyUpdate(oldObj, newObj interface{})
}
}

// OnPropagationPolicyDelete handles object delete event and push the object to queue.
func (d *ResourceDetector) OnPropagationPolicyDelete(obj interface{}) {
key, err := ClusterWideKeyFunc(obj)
if err != nil {
return
}

klog.V(2).Infof("Delete PropagationPolicy(%s)", key)
d.policyReconcileWorker.Add(key)
}

// ReconcilePropagationPolicy handles PropagationPolicy resource changes.
// When adding a PropagationPolicy, the detector will pick the objects in waitingObjects list that matches the policy and
// put the object to queue.
Expand All @@ -1007,19 +1006,43 @@ func (d *ResourceDetector) ReconcilePropagationPolicy(key util.QueueKey) error {
unstructuredObj, err := d.propagationPolicyLister.Get(ckey.NamespaceKey())
if err != nil {
if apierrors.IsNotFound(err) {
klog.Infof("PropagationPolicy(%s) has been removed.", ckey.NamespaceKey())
return d.HandlePropagationPolicyDeletion(ckey.Namespace, ckey.Name)
return nil
}
klog.Errorf("Failed to get PropagationPolicy(%s): %v", ckey.NamespaceKey(), err)
return err
}

klog.Infof("PropagationPolicy(%s) has been added.", ckey.NamespaceKey())
propagationObject := &policyv1alpha1.PropagationPolicy{}
if err = helper.ConvertToTypedObject(unstructuredObj, propagationObject); err != nil {
klog.Errorf("Failed to convert PropagationPolicy(%s) from unstructured object: %v", ckey.NamespaceKey(), err)
return err
}

if !propagationObject.DeletionTimestamp.IsZero() {
klog.Infof("PropagationPolicy(%s) is being deleted.", ckey.NamespaceKey())
if err = d.HandlePropagationPolicyDeletion(propagationObject.Namespace, propagationObject.Name); err != nil {
return err
}
if controllerutil.RemoveFinalizer(propagationObject, util.PropagationPolicyControllerFinalizer) {
if err = d.Client.Update(context.TODO(), propagationObject); err != nil {
klog.Errorf("Failed to remove finalizer for PropagationPolicy(%s), err: %v", ckey.NamespaceKey(), err)
return err
}
}
return nil
}

// TODO(whitewindmills): In order to adapt to the upgrade scenario, we temporarily add finalizer here.
// Transplant it to karmada-webhook in the next release. More info: https://github.com/karmada-io/karmada/pull/4836#discussion_r1568186728.
if controllerutil.AddFinalizer(propagationObject, util.PropagationPolicyControllerFinalizer) {
whitewindmills marked this conversation as resolved.
Show resolved Hide resolved
if err = d.Client.Update(context.TODO(), propagationObject); err != nil {
klog.Errorf("Failed to add finalizer for PropagationPolicy(%s), err: %v", ckey.NamespaceKey(), err)
return err
}
return nil
}

klog.Infof("PropagationPolicy(%s) has been added or updated.", ckey.NamespaceKey())
return d.HandlePropagationPolicyCreationOrUpdate(propagationObject)
}

Expand Down Expand Up @@ -1091,17 +1114,6 @@ func (d *ResourceDetector) OnClusterPropagationPolicyUpdate(oldObj, newObj inter
}
}

// OnClusterPropagationPolicyDelete handles object delete event and push the object to queue.
func (d *ResourceDetector) OnClusterPropagationPolicyDelete(obj interface{}) {
key, err := ClusterWideKeyFunc(obj)
if err != nil {
return
}

klog.V(2).Infof("Delete ClusterPropagationPolicy(%s)", key)
d.clusterPolicyReconcileWorker.Add(key)
}

// ReconcileClusterPropagationPolicy handles ClusterPropagationPolicy resource changes.
// When adding a ClusterPropagationPolicy, the detector will pick the objects in waitingObjects list that matches the policy and
// put the object to queue.
Expand All @@ -1117,20 +1129,44 @@ func (d *ResourceDetector) ReconcileClusterPropagationPolicy(key util.QueueKey)
unstructuredObj, err := d.clusterPropagationPolicyLister.Get(ckey.NamespaceKey())
if err != nil {
if apierrors.IsNotFound(err) {
klog.Infof("ClusterPropagationPolicy(%s) has been removed.", ckey.NamespaceKey())
return d.HandleClusterPropagationPolicyDeletion(ckey.Name)
return nil
}

klog.Errorf("Failed to get ClusterPropagationPolicy(%s): %v", ckey.NamespaceKey(), err)
return err
}

klog.Infof("Policy(%s) has been added", ckey.NamespaceKey())
propagationObject := &policyv1alpha1.ClusterPropagationPolicy{}
if err = helper.ConvertToTypedObject(unstructuredObj, propagationObject); err != nil {
klog.Errorf("Failed to convert ClusterPropagationPolicy(%s) from unstructured object: %v", ckey.NamespaceKey(), err)
return err
}

if !propagationObject.DeletionTimestamp.IsZero() {
klog.Infof("ClusterPropagationPolicy(%s) is being deleted.", ckey.NamespaceKey())
if err = d.HandleClusterPropagationPolicyDeletion(propagationObject.Name); err != nil {
return err
}
if controllerutil.RemoveFinalizer(propagationObject, util.ClusterPropagationPolicyControllerFinalizer) {
if err = d.Client.Update(context.TODO(), propagationObject); err != nil {
klog.Errorf("Failed to remove finalizer for ClusterPropagationPolicy(%s), err: %v", ckey.NamespaceKey(), err)
return err
}
}
return nil
}

// TODO(whitewindmills): In order to adapt to the upgrade scenario, we temporarily add finalizer here.
// Transplant it to karmada-webhook in the next release. More info: https://github.com/karmada-io/karmada/pull/4836#discussion_r1568186728.
if controllerutil.AddFinalizer(propagationObject, util.ClusterPropagationPolicyControllerFinalizer) {
if err = d.Client.Update(context.TODO(), propagationObject); err != nil {
klog.Errorf("Failed to add finalizer for ClusterPropagationPolicy(%s), err: %v", ckey.NamespaceKey(), err)
return err
}
whitewindmills marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

klog.Infof("ClusterPropagationPolicy(%s) has been added or updated.", ckey.NamespaceKey())
return d.HandleClusterPropagationPolicyCreationOrUpdate(propagationObject)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ const (

// MCSControllerFinalizer is added to Cluster to ensure service work is deleted before itself is deleted.
MCSControllerFinalizer = "karmada.io/multiclusterservice-controller"

// PropagationPolicyControllerFinalizer is added to PropagationPolicy to ensure the related resources have been unbound before itself is deleted.
PropagationPolicyControllerFinalizer = "karmada.io/propagation-policy-controller"

// ClusterPropagationPolicyControllerFinalizer is added to ClusterPropagationPolicy to ensure the related resources have been unbound before itself is deleted.
ClusterPropagationPolicyControllerFinalizer = "karmada.io/cluster-propagation-policy-controller"
)

const (
Expand Down
Loading