Skip to content

Commit f2c17b6

Browse files
PunakshiPunakshi Chaandpchaand
authored
MESH-6858: Improve VS deletion logic (#957) (#432)
* MESH-6858- improve VirtualService deletion handling in virtualservice_handler --------- ### Checklist 🚨 Please review this repository's [contribution guidelines](./CONTRIBUTING.md). - [ ] I've read and agree to the project's contribution guidelines. - [ ] I'm requesting to **pull a topic/feature/bugfix branch**. - [ ] I checked that my code additions will pass code linting checks and unit tests. - [ ] I updated unit and integration tests (if applicable). - [ ] I'm ready to notify the team of this contribution. ### Description What does this change do and why? [Link to related ISSUE] Thank you! Co-authored-by: Punakshi Chaand <[email protected]> Co-authored-by: pchaand <[email protected]>
1 parent bd5f3d6 commit f2c17b6

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

admiral/pkg/clusters/serviceentry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ func deleteAdditionalEndpoints(ctxLogger *logrus.Entry, ctx context.Context, rc
23202320
return nil
23212321
}
23222322

2323-
err = deleteVirtualService(ctx, vsToDelete, namespace, rc)
2323+
err = deleteVirtualService(ctx, vsToDelete.Name, namespace, rc)
23242324
if err != nil {
23252325
ctxLogger.Errorf(LogErrFormat, "Delete", "VirtualService", vsToDelete.Name, rc.ClusterID, err)
23262326
return err

admiral/pkg/clusters/virtualservice_handler.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package clusters
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"regexp"
78
"strings"
@@ -21,6 +22,16 @@ import (
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
)
2324

25+
type IsVSAlreadyDeletedErr struct {
26+
msg string
27+
}
28+
29+
var vsAlreadyDeletedMsg = "either VirtualService was already deleted, or it never existed"
30+
31+
func (e *IsVSAlreadyDeletedErr) Error() string {
32+
return vsAlreadyDeletedMsg
33+
}
34+
2435
// NewVirtualServiceHandler returns a new instance of VirtualServiceHandler after verifying
2536
// the required properties are set correctly
2637
func NewVirtualServiceHandler(remoteRegistry *RemoteRegistry, clusterID string) (*VirtualServiceHandler, error) {
@@ -445,9 +456,10 @@ func syncVirtualServiceToDependentCluster(
445456
// Best effort delete for existing virtual service with old name
446457
_ = rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Delete(ctx, virtualService.Name, metav1.DeleteOptions{})
447458

448-
err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Delete(ctx, vSName, metav1.DeleteOptions{})
459+
err := deleteVirtualService(ctx, vSName, syncNamespace, rc)
449460
if err != nil {
450-
if k8sErrors.IsNotFound(err) {
461+
var vsAlreadyDeletedErr *IsVSAlreadyDeletedErr
462+
if errors.As(err, &vsAlreadyDeletedErr) {
451463
ctxLogger.Infof(LogFormat, "Delete", "VirtualService", vSName, cluster, "Either VirtualService was already deleted, or it never existed")
452464
return nil
453465
}
@@ -572,9 +584,10 @@ func syncVirtualServiceToRemoteCluster(
572584
// Best effort delete for existing virtual service with old name
573585
_ = rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Delete(ctx, virtualService.Name, metav1.DeleteOptions{})
574586

575-
err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Delete(ctx, vSName, metav1.DeleteOptions{})
587+
err := deleteVirtualService(ctx, vSName, syncNamespace, rc)
576588
if err != nil {
577-
if k8sErrors.IsNotFound(err) {
589+
var vsAlreadyDeletedErr *IsVSAlreadyDeletedErr
590+
if errors.As(err, &vsAlreadyDeletedErr) {
578591
ctxLogger.Infof(LogFormat, "Delete", common.VirtualServiceResourceType, vSName, cluster, "Either VirtualService was already deleted, or it never existed")
579592
return nil
580593
}
@@ -814,16 +827,19 @@ func createVirtualServiceSkeleton(vs networkingV1Alpha3.VirtualService, name str
814827
return &v1alpha3.VirtualService{Spec: vs, ObjectMeta: metaV1.ObjectMeta{Name: name, Namespace: namespace}}
815828
}
816829

817-
func deleteVirtualService(ctx context.Context, exist *v1alpha3.VirtualService, namespace string, rc *RemoteController) error {
818-
if exist == nil {
819-
return fmt.Errorf("the VirtualService passed was nil")
830+
func deleteVirtualService(ctx context.Context, vsName string, namespace string, rc *RemoteController) error {
831+
err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(namespace).Delete(ctx, vsName, metaV1.DeleteOptions{})
832+
if err == nil {
833+
return nil
820834
}
821-
err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(namespace).Delete(ctx, exist.Name, metaV1.DeleteOptions{})
822-
if err != nil {
835+
if k8sErrors.IsNotFound(err) {
836+
err = rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(namespace).Delete(ctx, strings.ToLower(vsName), metaV1.DeleteOptions{})
837+
if err == nil {
838+
return nil
839+
}
823840
if k8sErrors.IsNotFound(err) {
824-
return fmt.Errorf("either VirtualService was already deleted, or it never existed")
841+
return &IsVSAlreadyDeletedErr{vsAlreadyDeletedMsg}
825842
}
826-
return err
827843
}
828-
return nil
844+
return err
829845
}

admiral/pkg/clusters/virtualservice_handler_test.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,49 +1488,64 @@ func TestDeleteVirtualService(t *testing.T) {
14881488
Hosts: []string{"stage.test00.foo", "stage.test00.bar"},
14891489
},
14901490
}
1491+
barVS := &apiNetworkingV1Alpha3.VirtualService{
1492+
ObjectMeta: metaV1.ObjectMeta{
1493+
Name: "stage.test00.bar-vs",
1494+
},
1495+
Spec: networkingV1Alpha3.VirtualService{
1496+
Hosts: []string{"stage.test00.foo", "stage.test00.bar"},
1497+
},
1498+
}
14911499

14921500
validIstioClient := istioFake.NewSimpleClientset()
14931501
validIstioClient.NetworkingV1alpha3().VirtualServices(namespace).Create(ctx, fooVS, metaV1.CreateOptions{})
1502+
validIstioClient.NetworkingV1alpha3().VirtualServices(namespace).Create(ctx, barVS, metaV1.CreateOptions{})
14941503

14951504
testcases := []struct {
14961505
name string
1497-
virtualService *apiNetworkingV1Alpha3.VirtualService
1506+
virtualServiceName string
14981507
rc *RemoteController
14991508
expectedError error
15001509
expectedDeletedVSName string
15011510
}{
15021511
{
1503-
name: "Given virtualservice to delete, when nil VS is passed, the func should return an error",
1504-
virtualService: nil,
1505-
expectedError: fmt.Errorf("the VirtualService passed was nil"),
1512+
name: "Given virtualservice to delete, when VS passed does not exists, the func should return an error",
1513+
virtualServiceName: "vs-does-not-exists",
1514+
expectedError: fmt.Errorf("either VirtualService was already deleted, or it never existed"),
1515+
rc: &RemoteController{
1516+
VirtualServiceController: &istio.VirtualServiceController{
1517+
IstioClient: validIstioClient,
1518+
},
1519+
},
15061520
},
15071521
{
1508-
name: "Given virtualservice to delete, when VS passed does not exists, the func should return an error",
1509-
virtualService: &apiNetworkingV1Alpha3.VirtualService{ObjectMeta: metaV1.ObjectMeta{Name: "vs-does-not-exists"}},
1510-
expectedError: fmt.Errorf("either VirtualService was already deleted, or it never existed"),
1522+
name: "Given virtualservice to delete, when VS exists, the func should delete the VS and not return any error",
1523+
virtualServiceName: "stage.test00.foo-vs",
1524+
expectedError: nil,
15111525
rc: &RemoteController{
15121526
VirtualServiceController: &istio.VirtualServiceController{
15131527
IstioClient: validIstioClient,
15141528
},
15151529
},
1530+
expectedDeletedVSName: "stage.test00.foo-vs",
15161531
},
15171532
{
1518-
name: "Given virtualservice to delete, when VS exists, the func should delete the VS and not return any error",
1519-
virtualService: fooVS,
1520-
expectedError: nil,
1533+
name: "Given virtualservice to delete, when VS exists with capital in name, the func should delete the VS and not return any error",
1534+
virtualServiceName: "stage.test00.Bar-vs",
1535+
expectedError: nil,
15211536
rc: &RemoteController{
15221537
VirtualServiceController: &istio.VirtualServiceController{
15231538
IstioClient: validIstioClient,
15241539
},
15251540
},
1526-
expectedDeletedVSName: "stage.test00.foo-vs",
1541+
expectedDeletedVSName: "stage.test00.bar-vs",
15271542
},
15281543
}
15291544

15301545
for _, tc := range testcases {
15311546
t.Run(tc.name, func(t *testing.T) {
15321547

1533-
err := deleteVirtualService(ctx, tc.virtualService, namespace, tc.rc)
1548+
err := deleteVirtualService(ctx, tc.virtualServiceName, namespace, tc.rc)
15341549

15351550
if err != nil && tc.expectedError != nil {
15361551
if !strings.Contains(err.Error(), tc.expectedError.Error()) {

0 commit comments

Comments
 (0)