Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions cmd/hub-net-controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func main() {
Client: mgr.GetClient(),
ProfilesClient: profilesClient,
EndpointsClient: endpointsClient,
Recorder: mgr.GetEventRecorderFor(trafficmanagerbackend.ControllerName),
// serviceImport controller has already enabled the internalServiceExportIndexer.
// Therefore, no need to setup it again.
}).SetupWithManager(ctx, mgr, true); err != nil {
Expand Down
23 changes: 21 additions & 2 deletions pkg/controllers/hub/trafficmanagerbackend/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -52,6 +53,9 @@ func init() {
}

const (
// ControllerName is the name of the TrafficManagerBackend controller.
ControllerName = "trafficmanagerbackend-controller"

trafficManagerBackendProfileFieldKey = ".spec.profile.name"
trafficManagerBackendBackendFieldKey = ".spec.backend.name"
// fields name used to filter resources
Expand All @@ -70,6 +74,11 @@ const (
// The cluster name length should be restricted to <= 63 characters.
// The endpoint name must contain no more than 260 characters, excluding the following characters "< > * % $ : \ ? + /".
AzureResourceEndpointNameFormat = "%s%s#%s"

backendEventReasonAzureAPIError = "AzureAPIError"
backendEventReasonAccepted = "Accepted"
backendEventReasonParticalAccepted = "PartiallyAccepted"
backendEventReasonDeleted = "Deleted"
)

var (
Expand Down Expand Up @@ -97,6 +106,7 @@ type Reconciler struct {

ProfilesClient *armtrafficmanager.ProfilesClient
EndpointsClient *armtrafficmanager.EndpointsClient
Recorder record.EventRecorder
}

//+kubebuilder:rbac:groups=networking.fleet.azure.com,resources=trafficmanagerbackends,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -163,9 +173,11 @@ func (r *Reconciler) handleDelete(ctx context.Context, backend *fleetnetv1beta1.

if controllerutil.ContainsFinalizer(backend, objectmeta.TrafficManagerBackendFinalizer) {
if err := r.deleteAzureTrafficManagerEndpoints(ctx, backend); err != nil {
r.Recorder.Eventf(backend, corev1.EventTypeWarning, backendEventReasonAzureAPIError, "Failed to delete Azure Traffic Manager endpoints: %v", err)
klog.ErrorS(err, "Failed to delete Azure Traffic Manager endpoints", "trafficManagerBackend", backendKObj)
return ctrl.Result{}, err
}
r.Recorder.Eventf(backend, corev1.EventTypeNormal, backendEventReasonDeleted, "Deleted Azure Traffic Manager endpoints")
controllerutil.RemoveFinalizer(backend, objectmeta.TrafficManagerBackendFinalizer)
needUpdate = true
}
Expand Down Expand Up @@ -284,8 +296,10 @@ func (r *Reconciler) handleUpdate(ctx context.Context, backend *fleetnetv1beta1.
if *backend.Spec.Weight == 0 {
klog.V(2).InfoS("Weight is 0, deleting all the endpoints", "trafficManagerBackend", backendKObj)
if err := r.cleanupEndpoints(ctx, profile.Spec.ResourceGroup, backend, atmProfile); err != nil {
r.Recorder.Eventf(backend, corev1.EventTypeWarning, backendEventReasonAzureAPIError, "Failed to delete Azure Traffic Manager endpoints: %v", err)
Comment thread
zhiying-lin marked this conversation as resolved.
return ctrl.Result{}, err
}
r.Recorder.Eventf(backend, corev1.EventTypeNormal, backendEventReasonAccepted, "Successfully removed all endpoints from Azure Traffic Manager due to zero weight")
setTrueCondition(backend, nil)
return ctrl.Result{}, r.updateTrafficManagerBackendStatus(ctx, backend)
}
Expand Down Expand Up @@ -319,7 +333,7 @@ func (r *Reconciler) handleUpdate(ctx context.Context, backend *fleetnetv1beta1.
} else {
var invalidEndpointErrMessage string
if len(badEndpointsErr) > 0 {
invalidEndpointErrMessage = fmt.Sprintf("%v endpoint(s) failed to be created/updated in the Azure Traffic Manager, for example, %v; ", len(badEndpointsErr), badEndpointsErr[0])
invalidEndpointErrMessage = fmt.Sprintf("%d endpoint(s) failed to be created/updated in the Azure Traffic Manager, for example, %v; ", len(badEndpointsErr), badEndpointsErr[0])
}
if len(invalidServicesMaps) > 0 {
for clusterID, invalidServiceErr := range invalidServicesMaps {
Expand Down Expand Up @@ -379,6 +393,8 @@ func (r *Reconciler) validateAzureTrafficManagerProfile(ctx context.Context, bac
profileKObj := klog.KObj(profile)
getRes, getErr := r.ProfilesClient.Get(ctx, profile.Spec.ResourceGroup, atmProfileName, nil)
if getErr != nil {
klog.ErrorS(getErr, "Failed to get Azure Traffic Manager profile", "resourceGroup", profile.Spec.ResourceGroup, "trafficManagerBackend", backendKObj, "trafficManagerProfile", profileKObj, "atmProfileName", atmProfileName)
Comment thread
zhiying-lin marked this conversation as resolved.
r.Recorder.Eventf(backend, corev1.EventTypeWarning, backendEventReasonAzureAPIError, "Failed to get Azure Traffic Manager profile %q under %q: %v", atmProfileName, profile.Spec.ResourceGroup, getErr)
if azureerrors.IsNotFound(getErr) {
// We've already checked the TrafficManagerProfile condition before getting Azure resource.
// It may happen when
Expand All @@ -391,7 +407,6 @@ func (r *Reconciler) validateAzureTrafficManagerProfile(ctx context.Context, bac
setFalseCondition(backend, nil, fmt.Sprintf("Azure Traffic Manager profile %q under %q is not found", atmProfileName, profile.Spec.ResourceGroup))
return nil, r.updateTrafficManagerBackendStatus(ctx, backend)
}
klog.V(2).InfoS("Failed to get Azure Traffic Manager profile", "resourceGroup", profile.Spec.ResourceGroup, "trafficManagerBackend", backendKObj, "trafficManagerProfile", profileKObj, "atmProfileName", atmProfileName)
setUnknownCondition(backend, fmt.Sprintf("Failed to get the Azure Traffic Manager profile %q under %q: %v", atmProfileName, profile.Spec.ResourceGroup, getErr))
if err := r.updateTrafficManagerBackendStatus(ctx, backend); err != nil {
return nil, err
Expand All @@ -410,6 +425,7 @@ func (r *Reconciler) validateServiceImportAndCleanupEndpointsIfInvalid(ctx conte
if apierrors.IsNotFound(getServiceImportErr) {
klog.V(2).InfoS("NotFound serviceImport and starting deleting any stale endpoints", "trafficManagerBackend", backendKObj, "serviceImport", backend.Spec.Backend.Name)
if err := r.cleanupEndpoints(ctx, resourceGroup, backend, azureProfile); err != nil {
r.Recorder.Eventf(backend, corev1.EventTypeWarning, backendEventReasonAzureAPIError, "Failed to delete stale endpoints for an invalid serviceImport: %v", err)
klog.ErrorS(err, "Failed to delete stale endpoints for an invalid serviceImport", "trafficManagerBackend", backendKObj, "serviceImport", backend.Spec.Backend.Name)
return nil, err
}
Expand Down Expand Up @@ -659,6 +675,7 @@ func (r *Reconciler) updateTrafficManagerEndpointsAndUpdateStatusIfUnknown(ctx c
continue
}
klog.ErrorS(deleteErr, "Failed to delete the Azure Traffic Manager endpoint", "resourceGroup", resourceGroup, "trafficManagerBackend", backendKObj, "atmProfile", profile.Name, "atmEndpoint", endpointName)
r.Recorder.Eventf(backend, corev1.EventTypeWarning, backendEventReasonAzureAPIError, "Failed to delete Azure Traffic Manager endpoint %q: %v", endpointName, deleteErr)
setUnknownCondition(backend, fmt.Sprintf("Failed to cleanup the existing %q for %q: %v", endpointName, *profile.Name, deleteErr))
if err := r.updateTrafficManagerBackendStatus(ctx, backend); err != nil {
return nil, nil, err
Expand All @@ -683,6 +700,7 @@ func (r *Reconciler) updateTrafficManagerEndpointsAndUpdateStatusIfUnknown(ctx c
endpointName := *endpoint.Endpoint.Name
res, updateErr := r.EndpointsClient.CreateOrUpdate(ctx, resourceGroup, *profile.Name, armtrafficmanager.EndpointTypeAzureEndpoints, endpointName, endpoint.Endpoint, nil)
if updateErr != nil {
r.Recorder.Eventf(backend, corev1.EventTypeWarning, backendEventReasonAzureAPIError, "Failed to create or update Azure Traffic Manager endpoint %q: %v", endpointName, updateErr)
if !errors.As(updateErr, &responseError) {
klog.ErrorS(updateErr, "Failed to send the createOrUpdate request", "resourceGroup", resourceGroup, "trafficManagerBackend", backendKObj, "atmProfile", *profile.Name, "atmEndpoint", endpointName)
return nil, nil, updateErr
Expand All @@ -700,6 +718,7 @@ func (r *Reconciler) updateTrafficManagerEndpointsAndUpdateStatusIfUnknown(ctx c
}
return nil, nil, updateErr
}
r.Recorder.Eventf(backend, corev1.EventTypeNormal, backendEventReasonAccepted, "Successfully created or updated Azure Traffic Manager endpoint %q", endpointName)
klog.V(2).InfoS("Created or updated Traffic Manager endpoint", "resourceGroup", resourceGroup, "trafficManagerBackend", backendKObj, "atmProfile", profile.Name, "atmEndpoint", endpointName)
acceptedEndpoints = append(acceptedEndpoints, buildAcceptedEndpointStatus(&res.Endpoint, endpoint))
}
Expand Down
Loading