Skip to content

Commit

Permalink
feat: support weight 0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiying-lin committed Jan 9, 2025
1 parent 929b956 commit a36ad87
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pkg/common/defaulter/trafficmanagerbackend.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
57 changes: 57 additions & 0 deletions pkg/common/defaulter/trafficmanagerbackend_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
12 changes: 12 additions & 0 deletions pkg/controllers/hub/trafficmanagerbackend/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/common/trafficmanager/validator/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/traffic_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

Expand Down

0 comments on commit a36ad87

Please sign in to comment.