Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#698 from TaoZou1/refactor
Browse files Browse the repository at this point in the history
Remove NSXResourcePath from staticroute/subnetset status
  • Loading branch information
TaoZou1 authored Aug 20, 2024
2 parents 19e5011 + e4f3ea9 commit 549c109
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 30 deletions.
3 changes: 0 additions & 3 deletions build/yaml/crd/vpc/crd.nsx.vmware.com_staticroutes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ spec:
- type
type: object
type: array
nsxResourcePath:
type: string
required:
- conditions
- nsxResourcePath
type: object
type: object
served: true
Expand Down
2 changes: 0 additions & 2 deletions build/yaml/crd/vpc/crd.nsx.vmware.com_subnetsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ spec:
items:
type: string
type: array
nsxResourcePath:
type: string
type: object
type: array
type: object
Expand Down
3 changes: 1 addition & 2 deletions pkg/apis/vpc/v1alpha1/staticroute_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ type NextHop struct {

// StaticRouteStatus defines the observed state of StaticRoute.
type StaticRouteStatus struct {
Conditions []StaticRouteCondition `json:"conditions"`
NSXResourcePath string `json:"nsxResourcePath"`
Conditions []StaticRouteCondition `json:"conditions"`
}

// +genclient
Expand Down
1 change: 0 additions & 1 deletion pkg/apis/vpc/v1alpha1/subnetset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type SubnetSetSpec struct {

// SubnetInfo defines the observed state of a single Subnet of a SubnetSet.
type SubnetInfo struct {
NSXResourcePath string `json:"nsxResourcePath,omitempty"`
NetworkAddresses []string `json:"networkAddresses,omitempty"`
GatewayAddresses []string `json:"gatewayAddresses,omitempty"`
DHCPServerAddresses []string `json:"DHCPServerAddresses,omitempty"`
Expand Down
4 changes: 1 addition & 3 deletions pkg/nsx/services/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ func (service *SubnetService) UpdateSubnetSetStatus(obj *v1alpha1.SubnetSet) err
if err != nil {
return err
}
subnetInfo := v1alpha1.SubnetInfo{
NSXResourcePath: *subnet.Path,
}
subnetInfo := v1alpha1.SubnetInfo{}
for _, status := range statusList {
subnetInfo.NetworkAddresses = append(subnetInfo.NetworkAddresses, *status.NetworkAddress)
subnetInfo.GatewayAddresses = append(subnetInfo.GatewayAddresses, *status.GatewayAddress)
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"github.com/vmware-tanzu/nsx-operator/pkg/client/clientset/versioned"

"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/test/e2e/providers"
)
Expand Down Expand Up @@ -694,6 +696,32 @@ func deleteYAML(filename string, ns string) error {
return nil
}

// queryResource is used to query resource by tags, not handling pagination
// tags should be present in pairs, the first tag is the scope, the second tag is the value
// caller should transform the response to the expected resource type
func (data *TestData) queryResource(resourceType string, tags []string) (model.SearchResponse, error) {
tagScopeClusterKey := strings.Replace(common.TagScopeNamespace, "/", "\\/", -1)
tagScopeClusterValue := strings.Replace(tags[0], ":", "\\:", -1)
tagParam := fmt.Sprintf("tags.scope:%s AND tags.tag:%s", tagScopeClusterKey, tagScopeClusterValue)
resourceParam := fmt.Sprintf("%s:%s", common.ResourceType, resourceType)
queryParam := resourceParam + " AND " + tagParam
if len(tags) >= 2 {
tagscope := strings.Replace(tags[0], "/", "\\/", -1)
tagtag := strings.Replace(tags[1], ":", "\\:", -1)
tagParam = fmt.Sprintf("tags.scope:%s AND tags.tag:%s", tagscope, tagtag)
queryParam = resourceParam + " AND " + tagParam
}
queryParam += " AND marked_for_delete:false"
var cursor *string
var pageSize int64 = 500
response, err := data.nsxClient.QueryClient.List(queryParam, cursor, nil, &pageSize, nil, nil)
if err != nil {
log.Printf("Error when querying resource %s: %v", resourceType, err)
return model.SearchResponse{}, err
}
return response, nil
}

func (data *TestData) waitForResourceExist(namespace string, resourceType string, key string, value string, shouldExist bool) error {
err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, defaultTimeout, false, func(ctx context.Context) (bool, error) {
exist := true
Expand Down
58 changes: 39 additions & 19 deletions test/e2e/nsx_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)
Expand Down Expand Up @@ -75,6 +77,32 @@ func TestSubnetSet(t *testing.T) {
t.Run("case=SubnetCIDR", SubnetCIDR)
}

func transSearchResponsetoSubnet(response model.SearchResponse) []model.VpcSubnet {
var resources []model.VpcSubnet
if response.Results == nil {
return resources
}
for _, result := range response.Results {
obj, err := common.NewConverter().ConvertToGolang(result, model.VpcSubnetBindingType())
if err != nil {
log.Printf("Failed to convert to golang subnet: %v", err)
return resources
}
if subnet, ok := obj.(model.VpcSubnet); ok {
resources = append(resources, subnet)
}
}
return resources
}

func fetchSubnet(t *testing.T, subnetSet *v1alpha1.SubnetSet) model.VpcSubnet {
tags := []string{common.TagScopeSubnetSetCRUID, string(subnetSet.UID)}
results, err := testData.queryResource(common.ResourceTypeSubnet, tags)
assertNil(t, err)
subnets := transSearchResponsetoSubnet(results)
assertTrue(t, len(subnets) > 0, "No NSX subnet found")
return subnets[0]
}
func defaultSubnetSet(t *testing.T) {
// 1. Check whether default-vm-subnetset and default-pod-subnetset are created.
err := testData.waitForCRReadyOrDeleted(defaultTimeout, SubnetSetCRType, E2ENamespace, common.DefaultVMSubnetSet, Ready)
Expand All @@ -98,11 +126,8 @@ func defaultSubnetSet(t *testing.T) {
assertNil(t, err)
assert.NotEmpty(t, subnetSet.Status.Subnets, "No Subnet info in SubnetSet")
// 4. Check NSX subnet allocation.
subnetPath := subnetSet.Status.Subnets[0].NSXResourcePath
vpcInfo, err := common.ParseVPCResourcePath(subnetPath)
assertNil(t, err, "Failed to parse VPC resource path %s", subnetPath)
vpcSubnet, err := testData.nsxClient.SubnetsClient.Get(vpcInfo.OrgID, vpcInfo.ProjectID, vpcInfo.VPCID, vpcInfo.ID)
assertNil(t, err, "Failed to get VPC subnet %s", vpcInfo.ID)
networkAddress := subnetSet.Status.Subnets[0].NetworkAddresses
assertTrue(t, len(networkAddress) > 0, "No network address in SubnetSet")

// 5. Check adding NSX subnet tags.
ns, err := testData.clientset.CoreV1().Namespaces().Get(context.TODO(), E2ENamespace, v1.GetOptions{})
Expand All @@ -112,49 +137,47 @@ func defaultSubnetSet(t *testing.T) {
ns, err = testData.clientset.CoreV1().Namespaces().Update(context.TODO(), ns, v1.UpdateOptions{})
time.Sleep(5 * time.Second)
assertNil(t, err)
vpcSubnet, err = testData.nsxClient.SubnetsClient.Get(vpcInfo.OrgID, vpcInfo.ProjectID, vpcInfo.VPCID, vpcInfo.ID)
assertNil(t, err)

vpcSubnet := fetchSubnet(t, subnetSet)
found := false
for _, tag := range vpcSubnet.Tags {
if *tag.Scope == labelKey && *tag.Tag == labelValue {
found = true
break
}
}
assertTrue(t, found, "Failed to add tags for NSX subnet %s", vpcInfo.ID)
assertTrue(t, found, "Failed to add tags for NSX subnet %s", *(vpcSubnet.Id))

// 6. Check updating NSX subnet tags.
labelValue = "update"
ns.Labels[labelKey] = labelValue
ns, err = testData.clientset.CoreV1().Namespaces().Update(context.TODO(), ns, v1.UpdateOptions{})
time.Sleep(5 * time.Second)
assertNil(t, err)
vpcSubnet, err = testData.nsxClient.SubnetsClient.Get(vpcInfo.OrgID, vpcInfo.ProjectID, vpcInfo.VPCID, vpcInfo.ID)
assertNil(t, err)
vpcSubnet = fetchSubnet(t, subnetSet)
found = false
for _, tag := range vpcSubnet.Tags {
if *tag.Scope == labelKey && *tag.Tag == labelValue {
found = true
break
}
}
assertTrue(t, found, "Failed to update tags for NSX subnet %s", vpcInfo.ID)
assertTrue(t, found, "Failed to update tags for NSX subnet %s", *(vpcSubnet.Id))

// 7. Check deleting NSX subnet tags.
delete(ns.Labels, labelKey)
_, err = testData.clientset.CoreV1().Namespaces().Update(context.TODO(), ns, v1.UpdateOptions{})
time.Sleep(5 * time.Second)
assertNil(t, err)
vpcSubnet, err = testData.nsxClient.SubnetsClient.Get(vpcInfo.OrgID, vpcInfo.ProjectID, vpcInfo.VPCID, vpcInfo.ID)
assertNil(t, err)
vpcSubnet = fetchSubnet(t, subnetSet)
found = false
for _, tag := range vpcSubnet.Tags {
if *tag.Scope == labelKey {
found = true
break
}
}
assertFalse(t, found, "Failed to delete tags for NSX subnet %s", vpcInfo.ID)
assertFalse(t, found, "Failed to delete tags for NSX subnet %s", *(vpcSubnet.Id))
}

func UserSubnetSet(t *testing.T) {
Expand Down Expand Up @@ -210,11 +233,8 @@ func UserSubnetSet(t *testing.T) {
}

// 5. Check NSX subnet allocation.
subnetPath := subnetSet.Status.Subnets[0].NSXResourcePath
vpcInfo, err := common.ParseVPCResourcePath(subnetPath)
assertNil(t, err, "Failed to parse VPC resource path %s", subnetPath)
_, err = testData.nsxClient.SubnetsClient.Get(vpcInfo.OrgID, vpcInfo.ProjectID, vpcInfo.VPCID, vpcInfo.ID)
assertNil(t, err, "Failed to get VPC subnet %s", vpcInfo.ID)
networkaddress := subnetSet.Status.Subnets[0].NetworkAddresses
assertTrue(t, len(networkaddress) > 0, "No network address in SubnetSet")
}
}

Expand Down

0 comments on commit 549c109

Please sign in to comment.