Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix preventTargetPoolToRBSMigration. Properly delete annotation #1741

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions pkg/l4lb/l4lbcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func updateAnnotations(ctx *context.ControllerContext, svc *v1.Service, newL4LBA
return patch.PatchServiceObjectMetadata(ctx.KubeClient.CoreV1(), svc, *newObjectMeta)
}

func deleteAnnotation(ctx *context.ControllerContext, svc *v1.Service, annotationKey string) error {
newObjectMeta := svc.ObjectMeta.DeepCopy()
if _, ok := newObjectMeta.Annotations[annotationKey]; !ok {
return nil
}

klog.V(3).Infof("Removing annotation %s from service %v/%v", annotationKey, svc.Namespace, svc.Name)
delete(newObjectMeta.Annotations, annotationKey)
return patch.PatchServiceObjectMetadata(ctx.KubeClient.CoreV1(), svc, *newObjectMeta)
}

// updateServiceStatus this faction checks if LoadBalancer status changed and patch service if needed.
func updateServiceStatus(ctx *context.ControllerContext, svc *v1.Service, newStatus *v1.LoadBalancerStatus) error {
if helpers.LoadBalancerStatusEqual(&svc.Status.LoadBalancer, newStatus) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/l4lb/l4netlbcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,13 @@ func (lc *L4NetLBController) revertToTargetPool(service *v1.Service) {
}

func (lc *L4NetLBController) deleteRBSAnnotation(service *v1.Service) error {
err := deleteAnnotation(lc.ctx, service, annotations.RBSAnnotationKey)
if err != nil {
return fmt.Errorf("deleteAnnotation(_, %v, %s) returned error %v, want nil", service, annotations.RBSAnnotationKey, err)
}
// update current object annotations, so we do not treat it as RBS after
delete(service.Annotations, annotations.RBSAnnotationKey)
return updateAnnotations(lc.ctx, service, service.Annotations)
return nil
}

// hasRBSForwardingRule checks if services loadbalancer has forwarding rule pointing to backend service
Expand Down
29 changes: 27 additions & 2 deletions pkg/l4lb/l4netlbcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,8 @@ func TestShouldProcessService(t *testing.T) {
}

func TestPreventTargetPoolToRBSMigration(t *testing.T) {
flags.F.MaxIgSize = 1000

testCases := []struct {
desc string
frHook getForwardingRuleHook
Expand Down Expand Up @@ -1155,16 +1157,39 @@ func TestPreventTargetPoolToRBSMigration(t *testing.T) {
controller.ctx.Cloud.Compute().(*cloud.MockGCE).MockForwardingRules.GetHook = testCase.frHook
addNetLBService(controller, svc)

// test only preventTargetPoolToRBSMigration
controller.preventTargetPoolToRBSMigration(svc)
updateNetLBService(controller, svc)

resultSvc, err := controller.ctx.KubeClient.CoreV1().Services(svc.Namespace).Get(context.TODO(), svc.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("controller.ctx.KubeClient.CoreV1().Services(%s).Get(_, %s, _) returned error %v, want nil", svc.Namespace, svc.Name, err)
}
hasRBSAnnotation := controller.hasRBSAnnotation(resultSvc)
if hasRBSAnnotation != testCase.expectRBSAnnotationAfterSync {
t.Errorf("hasRBSAnnotation = %t, testCase.expectRBSAnnotationAfterSync = %t, want equal", hasRBSAnnotation, testCase.expectRBSAnnotationAfterSync)
t.Errorf("After preventTargetPoolToRBSMigration, hasRBSAnnotation = %t, testCase.expectRBSAnnotationAfterSync = %t, want equal", hasRBSAnnotation, testCase.expectRBSAnnotationAfterSync)
}

// test that whole sync process is skipped
svc2 := test.NewL4NetLBRBSServiceMultiplePorts("test-2", []int32{30234})
addNetLBService(controller, svc2)

key, err := common.KeyFunc(svc2)
if err != nil {
t.Fatalf("common.KeyFunc(%v) returned error %v, want nil", svc2, err)
}

err = controller.sync(key)
if err != nil {
t.Fatalf("controller.sync(%s) returned error %v, want nil", key, err)
}

resultSvc, err = controller.ctx.KubeClient.CoreV1().Services(svc2.Namespace).Get(context.TODO(), svc2.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("controller.ctx.KubeClient.CoreV1().Services(%s).Get(_, %s, _) returned error %v, want nil", svc2.Namespace, svc2.Name, err)
}
hasRBSAnnotation = controller.hasRBSAnnotation(resultSvc)
if hasRBSAnnotation != testCase.expectRBSAnnotationAfterSync {
t.Errorf("After sync, hasRBSAnnotation = %t, testCase.expectRBSAnnotationAfterSync = %t, want equal", hasRBSAnnotation, testCase.expectRBSAnnotationAfterSync)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/test/gcehooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ func DeleteHealthCheckResourceInUseErrorHook(ctx context.Context, key *meta.Key,
}

func GetLegacyForwardingRule(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules) (bool, *compute.ForwardingRule, error) {
fwRule := compute.ForwardingRule{Target: "some_target", LoadBalancingScheme: string(cloud.SchemeExternal)}
fwRule := compute.ForwardingRule{Target: "some_target", LoadBalancingScheme: string(cloud.SchemeExternal), NetworkTier: cloud.NetworkTierDefault.ToGCEValue()}
return true, &fwRule, nil
}

func GetRBSForwardingRule(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules) (bool, *compute.ForwardingRule, error) {
fwRule := compute.ForwardingRule{BackendService: "some_rbs", LoadBalancingScheme: string(cloud.SchemeExternal)}
fwRule := compute.ForwardingRule{BackendService: bsUrl, LoadBalancingScheme: string(cloud.SchemeExternal), NetworkTier: cloud.NetworkTierDefault.ToGCEValue()}
return true, &fwRule, nil
}

Expand Down