From a36ad8773171c9b7ec08934c7a7f7d92b14dcb93 Mon Sep 17 00:00:00 2001 From: Zhiying Lin Date: Tue, 7 Jan 2025 11:04:36 +0800 Subject: [PATCH] feat: support weight 0 --- pkg/common/defaulter/trafficmanagerbackend.go | 19 +++++++ .../defaulter/trafficmanagerbackend_test.go | 57 +++++++++++++++++++ .../hub/trafficmanagerbackend/controller.go | 12 ++++ .../controller_integration_test.go | 22 +++++++ .../trafficmanager/validator/profile.go | 2 +- test/e2e/traffic_manager_test.go | 20 +++++++ 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 pkg/common/defaulter/trafficmanagerbackend.go create mode 100644 pkg/common/defaulter/trafficmanagerbackend_test.go diff --git a/pkg/common/defaulter/trafficmanagerbackend.go b/pkg/common/defaulter/trafficmanagerbackend.go new file mode 100644 index 00000000..43cbfd19 --- /dev/null +++ b/pkg/common/defaulter/trafficmanagerbackend.go @@ -0,0 +1,19 @@ +/* +Copyright (c) Microsoft Corporation. +Licensed under the MIT license. +*/ + +package defaulter + +import ( + "k8s.io/utils/ptr" + + fleetnetv1beta1 "go.goms.io/fleet-networking/api/v1beta1" +) + +// SetDefaultsTrafficManagerBackend sets the default values for TrafficManagerBackend. +func SetDefaultsTrafficManagerBackend(obj *fleetnetv1beta1.TrafficManagerBackend) { + if obj.Spec.Weight == nil { + obj.Spec.Weight = ptr.To(int64(1)) + } +} diff --git a/pkg/common/defaulter/trafficmanagerbackend_test.go b/pkg/common/defaulter/trafficmanagerbackend_test.go new file mode 100644 index 00000000..a10c6328 --- /dev/null +++ b/pkg/common/defaulter/trafficmanagerbackend_test.go @@ -0,0 +1,57 @@ +/* +Copyright (c) Microsoft Corporation. +Licensed under the MIT license. +*/ + +package defaulter + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "k8s.io/utils/ptr" + + fleetnetv1beta1 "go.goms.io/fleet-networking/api/v1beta1" +) + +func TestSetDefaultsTrafficManagerBackend(t *testing.T) { + tests := []struct { + name string + obj *fleetnetv1beta1.TrafficManagerBackend + want *fleetnetv1beta1.TrafficManagerBackend + }{ + { + name: "TrafficManagerBackend with nil weight", + obj: &fleetnetv1beta1.TrafficManagerBackend{ + Spec: fleetnetv1beta1.TrafficManagerBackendSpec{}, + }, + want: &fleetnetv1beta1.TrafficManagerBackend{ + Spec: fleetnetv1beta1.TrafficManagerBackendSpec{ + Weight: ptr.To(int64(1)), + }, + }, + }, + { + name: "TrafficManagerBackend with values", + obj: &fleetnetv1beta1.TrafficManagerBackend{ + Spec: fleetnetv1beta1.TrafficManagerBackendSpec{ + Weight: ptr.To(int64(100)), + }, + }, + want: &fleetnetv1beta1.TrafficManagerBackend{ + Spec: fleetnetv1beta1.TrafficManagerBackendSpec{ + Weight: ptr.To(int64(100)), + }, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + SetDefaultsTrafficManagerBackend(tc.obj) + if diff := cmp.Diff(tc.want, tc.obj); diff != "" { + t.Errorf("SetDefaultsTrafficManagerBackend() mismatch (-want +got):\n%s", diff) + } + }) + } +} diff --git a/pkg/controllers/hub/trafficmanagerbackend/controller.go b/pkg/controllers/hub/trafficmanagerbackend/controller.go index 10ff51e1..333d1b0b 100644 --- a/pkg/controllers/hub/trafficmanagerbackend/controller.go +++ b/pkg/controllers/hub/trafficmanagerbackend/controller.go @@ -36,6 +36,7 @@ import ( fleetnetv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1" fleetnetv1beta1 "go.goms.io/fleet-networking/api/v1beta1" "go.goms.io/fleet-networking/pkg/common/azureerrors" + "go.goms.io/fleet-networking/pkg/common/defaulter" "go.goms.io/fleet-networking/pkg/common/objectmeta" "go.goms.io/fleet-networking/pkg/controllers/hub/trafficmanagerprofile" ) @@ -121,6 +122,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco return ctrl.Result{}, controller.NewUpdateIgnoreConflictError(err) } } + // TODO: replace the following with defaulter wehbook + defaulter.SetDefaultsTrafficManagerBackend(backend) return r.handleUpdate(ctx, backend) } @@ -244,6 +247,15 @@ func (r *Reconciler) handleUpdate(ctx context.Context, backend *fleetnetv1beta1. klog.V(2).InfoS("Found the serviceImport", "trafficManagerBackend", backendKObj, "serviceImport", klog.KObj(serviceImport), "clusters", serviceImport.Status.Clusters) + if *backend.Spec.Weight == 0 { + klog.V(2).InfoS("Weight is 0, deleting all the endpoints", "trafficManagerBackend", backendKObj) + if err := r.cleanupEndpoints(ctx, backend, atmProfile); err != nil { + return ctrl.Result{}, err + } + setTrueCondition(backend, nil) + return ctrl.Result{}, r.updateTrafficManagerBackendStatus(ctx, backend) + } + desiredEndpointsMaps, invalidServicesMaps, err := r.validateExportedServiceForServiceImport(ctx, backend, serviceImport) if err != nil || (desiredEndpointsMaps == nil && invalidServicesMaps == nil) { // We don't need to requeue not found internalServiceExport(err == nil and desiredEndpointsMaps == nil && invalidServicesMaps == nil) diff --git a/pkg/controllers/hub/trafficmanagerbackend/controller_integration_test.go b/pkg/controllers/hub/trafficmanagerbackend/controller_integration_test.go index 29f50e47..2a9da1fe 100644 --- a/pkg/controllers/hub/trafficmanagerbackend/controller_integration_test.go +++ b/pkg/controllers/hub/trafficmanagerbackend/controller_integration_test.go @@ -973,6 +973,28 @@ var _ = Describe("Test TrafficManagerBackend Controller", func() { validator.ValidateTrafficManagerBackendConsistently(ctx, k8sClient, &want) }) + It("Updating weight to 0", func() { + Expect(k8sClient.Get(ctx, backendNamespacedName, backend)).Should(Succeed(), "failed to get trafficManagerBackend") + backend.Spec.Weight = ptr.To(int64(0)) + Expect(k8sClient.Update(ctx, backend)).Should(Succeed(), "failed to update trafficManagerBackend") + }) + + It("Validating trafficManagerBackend", func() { + want := fleetnetv1beta1.TrafficManagerBackend{ + ObjectMeta: metav1.ObjectMeta{ + Name: backendName, + Namespace: testNamespace, + Finalizers: []string{objectmeta.TrafficManagerBackendFinalizer}, + }, + Spec: backend.Spec, + Status: fleetnetv1beta1.TrafficManagerBackendStatus{ + Conditions: buildTrueCondition(backend.Generation), + }, + } + validator.ValidateTrafficManagerBackend(ctx, k8sClient, &want) + validator.ValidateTrafficManagerBackendConsistently(ctx, k8sClient, &want) + }) + It("Deleting trafficManagerBackend", func() { err := k8sClient.Delete(ctx, backend) Expect(err).Should(Succeed(), "failed to delete trafficManagerBackend") diff --git a/test/common/trafficmanager/validator/profile.go b/test/common/trafficmanager/validator/profile.go index 571f7a5b..8ddb06d9 100644 --- a/test/common/trafficmanager/validator/profile.go +++ b/test/common/trafficmanager/validator/profile.go @@ -24,7 +24,7 @@ import ( ) const ( - timeout = time.Second * 90 // need more time to create azure resources + timeout = time.Second * 120 // need more time to create azure resources interval = time.Millisecond * 250 // duration used by consistently duration = time.Second * 30 diff --git a/test/e2e/traffic_manager_test.go b/test/e2e/traffic_manager_test.go index b2d83468..c0118996 100644 --- a/test/e2e/traffic_manager_test.go +++ b/test/e2e/traffic_manager_test.go @@ -565,6 +565,26 @@ var _ = Describe("Test exporting service via Azure traffic manager", Ordered, fu atmProfile = buildDesiredATMProfile(profile, status.Endpoints) atmValidator.ValidateProfile(ctx, atmProfileName, atmProfile) }) + + It("Updating the weight to 0", func() { + By("Updating the trafficManagerBackend spec") + Eventually(func() error { + if err := hubClient.Get(ctx, backendName, &backend); err != nil { + return err + } + backend.Spec.Weight = ptr.To(int64(0)) + return hubClient.Update(ctx, &backend) + }, framework.PollTimeout, framework.PollInterval).Should(Succeed(), "Failed to update the trafficManagerBackend") + + By("Validating the trafficManagerBackend status") + status := validator.ValidateTrafficManagerBackendIfAcceptedAndIgnoringEndpointName(ctx, hubClient, backendName, true, nil) + validator.ValidateTrafficManagerBackendStatusAndIgnoringEndpointNameConsistently(ctx, hubClient, backendName, status) + + By("Validating the Azure traffic manager profile") + atmProfile = buildDesiredATMProfile(profile, status.Endpoints) + atmProfile.Properties.Endpoints = append(atmProfile.Properties.Endpoints, extraTrafficManagerEndpoint) + atmValidator.ValidateProfile(ctx, atmProfileName, atmProfile) + }) }) })