func()

in pkg/controllers/member/endpointslice/controller.go [343:388]


func (r *Reconciler) deleteEndpointSliceExportIfLinked(ctx context.Context, endpointSlice *discoveryv1.EndpointSlice) error {
	fleetUniqueName := endpointSlice.Annotations[objectmeta.ExportedObjectAnnotationUniqueName]

	// Skip the deletion if the unique name assigned as an annotation is not a valid DNS subdomain name; this
	// helps guard against user tampering with the annotation.
	if !isUniqueNameValid(fleetUniqueName) {
		klog.V(2).InfoS("The unique name annotation for exporting the EndpointSlice is not valid; unexport is skipped",
			"endpointSlice", klog.KObj(endpointSlice),
			"uniqueName", fleetUniqueName)
		return nil
	}

	endpointSliceExport := fleetnetv1alpha1.EndpointSliceExport{
		ObjectMeta: metav1.ObjectMeta{
			Namespace: r.HubNamespace,
			Name:      fleetUniqueName,
		},
	}
	endpointSliceExportKey := types.NamespacedName{Namespace: r.HubNamespace, Name: fleetUniqueName}
	err := r.HubClient.Get(ctx, endpointSliceExportKey, &endpointSliceExport)
	switch {
	case errors.IsNotFound(err):
		// It is guaranteed that a unique name annotation is always added before an EndpointSlice is exported; and
		// in some rare occasions it could happen that an EndpointSlice has a unique name annotation present yet has
		// not been exported to the hub cluster. It is an expected behavior and no action is needed on this controller's
		// end.
		return nil
	case err != nil:
		// An unexpected error has occurred.
		return err
	}

	if !isEndpointSliceExportLinkedWithEndpointSlice(&endpointSliceExport, endpointSlice) {
		// The EndpointSliceExport to which the unique name annotation on the EndpointSlice refers is not actually
		// linked with the EndpointSlice. This could happen if direct manipulation forces unique name annotations
		// on two different EndpointSlices to point to the same EndpointSliceExport. In this case the
		// EndpointSliceExport will not be deleted.
		return nil
	}

	if err := r.HubClient.Delete(ctx, &endpointSliceExport); err != nil && !errors.IsNotFound(err) {
		// An unexpected error has occurred.
		return err
	}
	return nil
}