add endpointslice-controller finalizer for Endpointslice Work object

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
changzhen 2024-03-22 10:52:35 +08:00
parent d7db9c5acc
commit 6b8e73e37a
3 changed files with 52 additions and 27 deletions

View File

@ -29,6 +29,7 @@ import (
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
@ -63,12 +64,18 @@ func (c *EndpointSliceController) Reconcile(ctx context.Context, req controllerr
return controllerruntime.Result{}, nil
}
}
return controllerruntime.Result{Requeue: true}, err
return controllerruntime.Result{}, err
}
if !work.DeletionTimestamp.IsZero() {
return controllerruntime.Result{}, nil
err := helper.DeleteEndpointSlice(c.Client, labels.Set{
workv1alpha1.WorkNamespaceLabel: req.Namespace,
workv1alpha1.WorkNameLabel: req.Name,
})
if err != nil {
return controllerruntime.Result{}, err
}
return controllerruntime.Result{}, c.removeFinalizer(work.DeepCopy())
}
// TBD: The work is managed by service-export-controller and endpointslice-collect-controller now,
@ -79,10 +86,20 @@ func (c *EndpointSliceController) Reconcile(ctx context.Context, req controllerr
workv1alpha1.WorkNamespaceLabel: req.Namespace,
workv1alpha1.WorkNameLabel: req.Name,
})
return controllerruntime.Result{}, err
if err != nil {
return controllerruntime.Result{}, err
}
return controllerruntime.Result{}, c.removeFinalizer(work.DeepCopy())
}
return c.collectEndpointSliceFromWork(work)
return controllerruntime.Result{}, c.collectEndpointSliceFromWork(work)
}
func (c *EndpointSliceController) removeFinalizer(work *workv1alpha1.Work) error {
if !controllerutil.RemoveFinalizer(work, util.EndpointSliceControllerFinalizer) {
return nil
}
return c.Client.Update(context.TODO(), work)
}
// SetupWithManager creates a controller and register to controller manager.
@ -94,8 +111,8 @@ func (c *EndpointSliceController) SetupWithManager(mgr controllerruntime.Manager
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
// TBD: We care about the work with label util.MultiClusterServiceNameLabel because the work is
// managed by service-export-controller and endpointslice-collect-controller now, We should delete this after the conflict is fixed.
return (util.GetLabelValue(updateEvent.ObjectNew.GetLabels(), util.ServiceNameLabel) != "" ||
util.GetLabelValue(updateEvent.ObjectNew.GetLabels(), util.MultiClusterServiceNameLabel) != "")
return util.GetLabelValue(updateEvent.ObjectNew.GetLabels(), util.ServiceNameLabel) != "" ||
util.GetLabelValue(updateEvent.ObjectNew.GetLabels(), util.MultiClusterServiceNameLabel) != ""
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
return util.GetLabelValue(deleteEvent.Object.GetLabels(), util.ServiceNameLabel) != ""
@ -107,25 +124,25 @@ func (c *EndpointSliceController) SetupWithManager(mgr controllerruntime.Manager
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.Work{}, builder.WithPredicates(serviceImportPredicateFun)).Complete(c)
}
func (c *EndpointSliceController) collectEndpointSliceFromWork(work *workv1alpha1.Work) (controllerruntime.Result, error) {
func (c *EndpointSliceController) collectEndpointSliceFromWork(work *workv1alpha1.Work) error {
clusterName, err := names.GetClusterName(work.Namespace)
if err != nil {
klog.Errorf("Failed to get cluster name for work %s/%s", work.Namespace, work.Name)
return controllerruntime.Result{Requeue: true}, err
return err
}
for _, manifest := range work.Spec.Workload.Manifests {
unstructObj := &unstructured.Unstructured{}
if err := unstructObj.UnmarshalJSON(manifest.Raw); err != nil {
klog.Errorf("Failed to unmarshal workload, error is: %v", err)
return controllerruntime.Result{Requeue: true}, err
return err
}
endpointSlice := &discoveryv1.EndpointSlice{}
err = helper.ConvertToTypedObject(unstructObj, endpointSlice)
if err != nil {
klog.Errorf("Failed to convert unstructured to typed object: %v", err)
return controllerruntime.Result{Requeue: true}, err
return err
}
desiredEndpointSlice := deriveEndpointSlice(endpointSlice, clusterName)
@ -137,11 +154,11 @@ func (c *EndpointSliceController) collectEndpointSliceFromWork(work *workv1alpha
}
if err = helper.CreateOrUpdateEndpointSlice(c.Client, desiredEndpointSlice); err != nil {
return controllerruntime.Result{Requeue: true}, err
return err
}
}
return controllerruntime.Result{}, nil
return nil
}
func deriveEndpointSlice(original *discoveryv1.EndpointSlice, migratedFrom string) *discoveryv1.EndpointSlice {

View File

@ -149,14 +149,14 @@ func (c *ServiceExportController) RunWorkQueue() {
func (c *ServiceExportController) enqueueReportedEpsServiceExport() {
workList := &workv1alpha1.WorkList{}
err := wait.PollUntilContextCancel(context.TODO(), time.Second, true, func(ctx context.Context) (done bool, err error) {
err := wait.PollUntil(1*time.Second, func() (done bool, err error) {
err = c.List(context.TODO(), workList, client.MatchingLabels{util.PropagationInstruction: util.PropagationInstructionSuppressed})
if err != nil {
klog.Errorf("Failed to list collected EndpointSlices Work from member clusters: %v", err)
return false, nil
}
return true, nil
})
}, context.TODO().Done())
if err != nil {
return
}
@ -508,27 +508,31 @@ func getEndpointSliceWorkMeta(c client.Client, ns string, workName string, endpo
return metav1.ObjectMeta{}, err
}
labels := map[string]string{
util.ServiceNamespaceLabel: endpointSlice.GetNamespace(),
util.ServiceNameLabel: endpointSlice.GetLabels()[discoveryv1.LabelServiceName],
// indicate the Work should be not propagated since it's collected resource.
util.PropagationInstruction: util.PropagationInstructionSuppressed,
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
util.EndpointSliceWorkManagedByLabel: util.ServiceExportKind,
workMeta := metav1.ObjectMeta{
Name: workName,
Namespace: ns,
Finalizers: []string{util.EndpointSliceControllerFinalizer},
Labels: map[string]string{
util.ServiceNamespaceLabel: endpointSlice.GetNamespace(),
util.ServiceNameLabel: endpointSlice.GetLabels()[discoveryv1.LabelServiceName],
// indicate the Work should be not propagated since it's collected resource.
util.PropagationInstruction: util.PropagationInstructionSuppressed,
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
util.EndpointSliceWorkManagedByLabel: util.ServiceExportKind,
},
}
if existWork.Labels == nil || (err != nil && apierrors.IsNotFound(err)) {
workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: labels}
return workMeta, nil
}
labels = util.DedupeAndMergeLabels(labels, existWork.Labels)
workMeta.Labels = util.DedupeAndMergeLabels(workMeta.Labels, existWork.Labels)
if value, ok := existWork.Labels[util.EndpointSliceWorkManagedByLabel]; ok {
controllerSet := sets.New[string]()
controllerSet.Insert(strings.Split(value, ".")...)
controllerSet.Insert(util.ServiceExportKind)
labels[util.EndpointSliceWorkManagedByLabel] = strings.Join(controllerSet.UnsortedList(), ".")
workMeta.Labels[util.EndpointSliceWorkManagedByLabel] = strings.Join(controllerSet.UnsortedList(), ".")
}
workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: labels}
return workMeta, nil
}

View File

@ -98,7 +98,7 @@ const (
// The overrides items should be sorted alphabetically in ascending order by ClusterOverridePolicy's name.
AppliedClusterOverrides = "policy.karmada.io/applied-cluster-overrides"
// EndPointSliceProvisionClusterAnnotation is added to work of the dispatch EndpointSlice in consumption clusters's namespace.
// EndpointSliceProvisionClusterAnnotation is added to work of the dispatch EndpointSlice in consumption clusters' namespace.
EndpointSliceProvisionClusterAnnotation = "endpointslice.karmada.io/provision-cluster"
)
@ -116,6 +116,10 @@ const (
// before ResourceBinding itself is deleted.
BindingControllerFinalizer = "karmada.io/binding-controller"
// EndpointSliceControllerFinalizer is added to Work, which holds EndpointSlice collected from member clusters,
// to ensure related EndpointSlices are deleted before Work itself is deleted.
EndpointSliceControllerFinalizer = "karmada.io/endpointslice-controller"
// MCSEndpointSliceCollectControllerFinalizer is added to mcs to ensure related Works in provider clusters are deleted
MCSEndpointSliceCollectControllerFinalizer = "karmada.io/mcs-endpointslice-collect-controller"