Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#470 from jwsui/remove-tag
Browse files Browse the repository at this point in the history
Remove SubnetTypeCR tag
  • Loading branch information
TaoZou1 authored Jan 9, 2024
2 parents c1f9b6f + b96e19b commit 4f9dd4a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 41 deletions.
12 changes: 7 additions & 5 deletions pkg/controllers/subnet/subnet_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,19 @@ func (r *SubnetReconciler) GarbageCollector(cancel chan bool, timeout time.Durat
return
case <-time.After(timeout):
}
nsxSubnetList := r.Service.ListSubnetCreatedByCR()
if len(nsxSubnetList) == 0 {
continue
}

crdSubnetList := &v1alpha1.SubnetList{}
err := r.Client.List(ctx, crdSubnetList)
if err != nil {
log.Error(err, "failed to list subnet CR")
continue
}
var nsxSubnetList []model.VpcSubnet
for _, subnet := range crdSubnetList.Items {
nsxSubnetList = append(nsxSubnetList, r.Service.ListSubnetCreatedBySubnet(string(subnet.UID))...)
}
if len(nsxSubnetList) == 0 {
continue
}

crdSubnetIDs := sets.NewString()
for _, sr := range crdSubnetList.Items {
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/subnet/subnet_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestSubnetReconciler_GarbageCollector(t *testing.T) {
}
// Subnet doesn't have TagScopeSubnetSetCRId (not belong to SubnetSet)
// gc collect item "2345", local store has more item than k8s cache
patch := gomonkey.ApplyMethod(reflect.TypeOf(service), "ListSubnetCreatedByCR", func(_ *subnet.SubnetService) []model.VpcSubnet {
a := []model.VpcSubnet{}
patch := gomonkey.ApplyMethod(reflect.TypeOf(service), "ListSubnetCreatedBySubnet", func(_ *subnet.SubnetService, uid string) []model.VpcSubnet {
var a []model.VpcSubnet
id1 := "2345"
a = append(a, model.VpcSubnet{Id: &id1})
id2 := "1234"
Expand Down Expand Up @@ -70,8 +70,8 @@ func TestSubnetReconciler_GarbageCollector(t *testing.T) {

// local store has same item as k8s cache
patch.Reset()
patch.ApplyMethod(reflect.TypeOf(service), "ListSubnetCreatedByCR", func(_ *subnet.SubnetService) []model.VpcSubnet {
a := []model.VpcSubnet{}
patch.ApplyMethod(reflect.TypeOf(service), "ListSubnetCreatedBySubnet", func(_ *subnet.SubnetService, uid string) []model.VpcSubnet {
var a []model.VpcSubnet
id := "1234"
a = append(a, model.VpcSubnet{Id: &id})
return a
Expand All @@ -95,14 +95,14 @@ func TestSubnetReconciler_GarbageCollector(t *testing.T) {

// local store has no item
patch.Reset()
patch.ApplyMethod(reflect.TypeOf(service), "ListSubnetCreatedByCR", func(_ *subnet.SubnetService) []model.VpcSubnet {
patch.ApplyMethod(reflect.TypeOf(service), "ListSubnetCreatedBySubnet", func(_ *subnet.SubnetService, uid string) []model.VpcSubnet {
return []model.VpcSubnet{}
})
patch.ApplyMethod(reflect.TypeOf(service), "DeleteSubnet", func(_ *subnet.SubnetService, subnet model.VpcSubnet) error {
assert.FailNow(t, "should not be called")
return nil
})
k8sClient.EXPECT().List(ctx, srList).Return(nil).Times(0)
k8sClient.EXPECT().List(ctx, srList).Return(nil).Times(1)
go func() {
time.Sleep(1 * time.Second)
cancel <- true
Expand Down
6 changes: 4 additions & 2 deletions pkg/controllers/subnetset/subnetset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ func (r *SubnetSetReconciler) GarbageCollector(cancel chan bool, timeout time.Du
log.Error(err, "failed to list SubnetSet CR")
continue
}

nsxSubnetList := r.Service.ListSubnetCreatedBySubnetSet()
var nsxSubnetList []model.VpcSubnet
for _, subnetSet := range subnetSetList.Items {
nsxSubnetList = append(nsxSubnetList, r.Service.ListSubnetCreatedBySubnetSet(string(subnetSet.UID))...)
}
if len(nsxSubnetList) == 0 {
continue
}
Expand Down
1 change: 0 additions & 1 deletion pkg/nsx/services/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const (
LabelDefaultPodSubnetSet string = "Pod"
DefaultPodSubnetSet string = "pod-default"
DefaultVMSubnetSet string = "vm-default"
TagScopeSubnetCRType string = "nsx-op/subnet_type"
TagScopeSubnetCRUID string = "nsx-op/subnet_uid"
TagScopeSubnetCRName string = "nsx-op/subnet_name"
TagScopeSubnetSetCRName string = "nsx-op/subnetset_name"
Expand Down
11 changes: 0 additions & 11 deletions pkg/nsx/services/subnet/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ func subnetIndexFunc(obj interface{}) ([]string, error) {
}
}

// subnetTypeIndexFunc is used to filter out NSX Subnets which are tagged with subnetcr type.
// TODO, change it to use "nsx-op/subnetset_cr_uid" and "nsx-op/subnet_cr_uid"
func subnetTypeIndexFunc(obj interface{}) ([]string, error) {
switch o := obj.(type) {
case model.VpcSubnet:
return filterTag(o.Tags, common.TagScopeSubnetCRType), nil
default:
return nil, errors.New("subnetIndexFunc doesn't support unknown type")
}
}

// subnetIndexFunc is used to filter out NSX Subnets which are tagged with CR UID.
func subnetSetIndexFunc(obj interface{}) ([]string, error) {
switch o := obj.(type) {
Expand Down
18 changes: 4 additions & 14 deletions pkg/nsx/services/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func InitializeSubnetService(service common.Service) (*SubnetService, error) {
ResourceStore: common.ResourceStore{
Indexer: cache.NewIndexer(keyFunc, cache.Indexers{
common.TagScopeSubnetCRUID: subnetIndexFunc,
common.TagScopeSubnetCRType: subnetTypeIndexFunc,
common.TagScopeSubnetSetCRUID: subnetSetIndexFunc,
}),
BindingType: model.VpcSubnetBindingType(),
Expand Down Expand Up @@ -174,21 +173,12 @@ func (service *SubnetService) DeleteSubnet(nsxSubnet model.VpcSubnet) error {
return nil
}

func (service *SubnetService) ListSubnetCreatedByCR() []model.VpcSubnet {
return service.listSubnetByCRType("subnet")
func (service *SubnetService) ListSubnetCreatedBySubnet(id string) []model.VpcSubnet {
return service.SubnetStore.GetByIndex(common.TagScopeSubnetCRUID, id)
}

func (service *SubnetService) listSubnetByCRType(crType string) []model.VpcSubnet {
subnets := service.SubnetStore.GetByIndex(common.TagScopeSubnetCRType, crType)
subnetList := []model.VpcSubnet{}
for _, subnet := range subnets {
subnetList = append(subnetList, subnet)
}
return subnetList
}

func (service *SubnetService) ListSubnetCreatedBySubnetSet() []model.VpcSubnet {
return service.listSubnetByCRType("subnetset")
func (service *SubnetService) ListSubnetCreatedBySubnetSet(id string) []model.VpcSubnet {
return service.SubnetStore.GetByIndex(common.TagScopeSubnetSetCRUID, id)
}

func (service *SubnetService) ListSubnetSetID(ctx context.Context) sets.String {
Expand Down
2 changes: 0 additions & 2 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,9 @@ func BuildBasicTags(cluster string, obj interface{}, namespaceID types.UID) []mo
case *v1alpha1.Subnet:
tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetCRName), Tag: String(i.ObjectMeta.Name)})
tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetCRUID), Tag: String(string(i.UID))})
tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetCRType), Tag: String(SubnetTypeSubnet)})
case *v1alpha1.SubnetSet:
tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetSetCRName), Tag: String(i.ObjectMeta.Name)})
tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetSetCRUID), Tag: String(string(i.UID))})
tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetCRType), Tag: String(SubnetTypeSubnetSet)})
case *v1alpha1.SubnetPort:
tags = append(tags, model.Tag{Scope: String(common.TagScopeVMNamespace), Tag: String(i.ObjectMeta.Namespace)})
isVmSubnetPort = true
Expand Down

0 comments on commit 4f9dd4a

Please sign in to comment.