Skip to content

Commit

Permalink
Get shared VPC for shared ns
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpang-vmware committed Dec 6, 2023
1 parent 60e508c commit 39ede90
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 23 deletions.
108 changes: 108 additions & 0 deletions pkg/mock/vpcclient/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/nsx/services/ippool/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/agiledragon/gomonkey"
"github.com/stretchr/testify/assert"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha2"
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestIPPoolService_BuildIPPool(t *testing.T) {
}
vpcStore := &vpc.VPCStore{ResourceStore: resourceStore}
commonctl.ServiceMediator.VPCService = &vpc.VPCService{VpcStore: vpcStore}
patch := gomonkey.ApplyMethod(reflect.TypeOf(vpcStore), "GetVPCsByNamespace", func(vpcStore *vpc.VPCStore,
patch := gomonkey.ApplyMethod(reflect.TypeOf(commonctl.ServiceMediator.VPCService), "GetVPCsByNamespace", func(vpcService *vpc.VPCService,
ns string,
) []model.Vpc {
return vpcinfolist
Expand Down
8 changes: 4 additions & 4 deletions pkg/nsx/services/ippool/ippool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestIPPoolService_acquireCidr(t *testing.T) {
}
vpcStore := &vpc.VPCStore{ResourceStore: resourceStore}
commonctl.ServiceMediator.VPCService = &vpc.VPCService{VpcStore: vpcStore}
patches := gomonkey.ApplyMethod(reflect.TypeOf(vpcStore), "GetVPCsByNamespace", func(_ *vpc.VPCStore, ns string) []model.Vpc {
patches := gomonkey.ApplyMethod(reflect.TypeOf(commonctl.ServiceMediator.VPCService), "GetVPCsByNamespace", func(_ *vpc.VPCService, ns string) []model.Vpc {
id := "vpc-1"
return []model.Vpc{{Path: common.String("/orgs/default/projects/project-1/vpcs/vpc-1"), Id: &id}}
})
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestIPPoolService_AcquireRealizedSubnetIP(t *testing.T) {
}
vpcStore := &vpc.VPCStore{ResourceStore: resourceStore}
commonctl.ServiceMediator.VPCService = &vpc.VPCService{VpcStore: vpcStore}
patches := gomonkey.ApplyMethod(reflect.TypeOf(vpcStore), "GetVPCsByNamespace", func(_ *vpc.VPCStore, ns string) []model.Vpc {
patches := gomonkey.ApplyMethod(reflect.TypeOf(commonctl.ServiceMediator.VPCService), "GetVPCsByNamespace", func(_ *vpc.VPCService, ns string) []model.Vpc {
id := "vpc-1"
return []model.Vpc{{Path: common.String("/orgs/default/projects/project-1/vpcs/vpc-1"), Id: &id}}
})
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestIPPoolService_CRUDResource(t *testing.T) {
}
vpcStore := &vpc.VPCStore{ResourceStore: resourceStore}
commonctl.ServiceMediator.VPCService = &vpc.VPCService{VpcStore: vpcStore}
patches := gomonkey.ApplyMethod(reflect.TypeOf(vpcStore), "GetVPCsByNamespace", func(_ *vpc.VPCStore, ns string) []model.Vpc {
patches := gomonkey.ApplyMethod(reflect.TypeOf(commonctl.ServiceMediator.VPCService), "GetVPCsByNamespace", func(_ *vpc.VPCService, ns string) []model.Vpc {
id := "vpc-1"
return []model.Vpc{{Path: common.String("/orgs/default/projects/project-1/vpcs/vpc-1"), Id: &id}}
})
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestIPPoolService_CreateOrUpdateIPPool(t *testing.T) {
}
vpcStore := &vpc.VPCStore{ResourceStore: resourceStore}
commonctl.ServiceMediator.VPCService = &vpc.VPCService{VpcStore: vpcStore}
patch = gomonkey.ApplyMethod(reflect.TypeOf(vpcStore), "GetVPCsByNamespace", func(vpcStore *vpc.VPCStore,
patch = gomonkey.ApplyMethod(reflect.TypeOf(commonctl.ServiceMediator.VPCService), "GetVPCsByNamespace", func(vpcService *vpc.VPCService,
ns string) []model.Vpc {
return vpcinfo
})
Expand Down
36 changes: 35 additions & 1 deletion pkg/nsx/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/realizestate"
"github.com/vmware-tanzu/nsx-operator/pkg/util"
)

const (
Expand Down Expand Up @@ -194,7 +195,12 @@ func InitializeVPC(service common.Service) (*VPCService, error) {
}

func (s *VPCService) GetVPCsByNamespace(namespace string) []model.Vpc {
return s.VpcStore.GetVPCsByNamespace(namespace)
sns, err := s.getSharedVPCNamespaceFromNS(namespace)
if err != nil {
log.Error(err, "Failed to get namespace.")
return nil
}
return s.VpcStore.GetVPCsByNamespace(util.If(sns == "", namespace, sns).(string))
}

func (s *VPCService) ListVPC() []model.Vpc {
Expand Down Expand Up @@ -321,6 +327,34 @@ func (s *VPCService) CreatOrUpdatePrivateIPBlock(obj *v1alpha1.VPC, nc VPCNetwor
return path, nil
}

func (s *VPCService) getSharedVPCNamespaceFromNS(ns string) (string, error) {
obj := &v1.Namespace{}
if err := s.Client.Get(ctx, types.NamespacedName{
Name: ns,
Namespace: ns,
}, obj); err != nil {
log.Error(err, "failed to fetch namespace", "Namespace", ns)
return "", err
}

annos := obj.Annotations
// If no annotaion on ns, then this is not a shared VPC ns
if len(annos) == 0 {
return "", nil
}

// If no annotation nsx.vmware.com/vpc_name on ns, this is not a shared vpc
ncName, exist := annos[common.AnnotationVPCName]
if !exist {
return "", nil
}

// Retrieve the shared vpc namespace from annotation
shared_ns := strings.Split(ncName, "/")[0]

return shared_ns, nil
}

func (s *VPCService) getNetworkconfigNameFromNS(ns string) (string, error) {
obj := &v1.Namespace{}
if err := s.Client.Get(ctx, types.NamespacedName{
Expand Down
Loading

0 comments on commit 39ede90

Please sign in to comment.