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

Add AllowOverwrite for some clients #941

Merged
merged 1 commit into from
Dec 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (m *MockManager) Start(context.Context) error {
func fakeService() *securitypolicy.SecurityPolicyService {
c := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(c)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

service := &securitypolicy.SecurityPolicyService{
Service: common.Service{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import (
func fakeService() *securitypolicy.SecurityPolicyService {
c := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(c)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

service := &securitypolicy.SecurityPolicyService{
Service: common.Service{
Expand Down
11 changes: 7 additions & 4 deletions pkg/nsx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ func (ck *NSXHealthChecker) CheckNSXHealth(req *http.Request) error {
}

func restConnector(c *Cluster) client.Connector {
connector, _ := c.NewRestConnector()
return connector
return c.NewRestConnector()
}

func restConnectorAllowOverwrite(c *Cluster) client.Connector {
return c.NewRestConnectorAllowOverwrite()
}

func GetClient(cf *config.NSXOperatorConfig) *Client {
Expand Down Expand Up @@ -177,13 +180,13 @@ func GetClient(cf *config.NSXOperatorConfig) *Client {
staticRouteClient := vpcs.NewStaticRoutesClient(restConnector(cluster))
natRulesClient := nat.NewNatRulesClient(restConnector(cluster))
vpcGroupClient := vpcs.NewGroupsClient(restConnector(cluster))
portClient := subnets.NewPortsClient(restConnector(cluster))
portClient := subnets.NewPortsClient(restConnectorAllowOverwrite(cluster))
portStateClient := ports.NewStateClient(restConnector(cluster))
ipPoolClient := subnets.NewIpPoolsClient(restConnector(cluster))
ipAllocationClient := ip_pools.NewIpAllocationsClient(restConnector(cluster))
subnetsClient := vpcs.NewSubnetsClient(restConnector(cluster))
subnetStatusClient := subnets.NewStatusClient(restConnector(cluster))
ipAddressAllocationClient := vpcs.NewIpAddressAllocationsClient(restConnector(cluster))
ipAddressAllocationClient := vpcs.NewIpAddressAllocationsClient(restConnectorAllowOverwrite(cluster))
vpcLBSClient := vpcs.NewVpcLbsClient(restConnector(cluster))
vpcLbVirtualServersClient := vpcs.NewVpcLbVirtualServersClient(restConnector(cluster))
vpcLbPoolsClient := vpcs.NewVpcLbPoolsClient(restConnector(cluster))
Expand Down
37 changes: 37 additions & 0 deletions pkg/nsx/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package nsx
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/search"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/ratelimiter"
Expand Down Expand Up @@ -172,3 +174,38 @@ func TestSRGetClient(t *testing.T) {
fmt.Printf("b is %v \n", b[2])

}
func TestRestConnectorAllowOverwrite(t *testing.T) {
resVersion := `{
"node_version": "3.1.3.3.0.18844962",
"product_version": "3.1.3.3.0.18844959"
}`
resHealth := `{
"healthy" : true,
"components_health" : "MANAGER:UP, SEARCH:UP, UI:UP, NODE_MGMT:UP"
}`
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("header %v", r.Header)
w.WriteHeader(http.StatusOK)
if strings.Contains(r.URL.Path, "search") {
allowOverwrite, _ := r.Header["X-Allow-Overwrite"]
assert.Equal(t, "True", allowOverwrite[0])
}
if strings.Contains(r.URL.Path, "reverse-proxy/node/health") {
w.Write(([]byte(resHealth)))
} else {
w.Write([]byte(resVersion))
}
}))
defer ts.Close()
thumbprint := []string{"123"}
index := strings.Index(ts.URL, "//")
a := ts.URL[index+2:]
config := NewConfig(a, "admin", "passw0rd", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, thumbprint)
cluster, _ := NewCluster(config)
nsxVersion, err := cluster.GetVersion()
assert.Equal(t, err, nil)
assert.Equal(t, nsxVersion.NodeVersion, "3.1.3.3.0.18844962")

client := search.NewQueryClient(cluster.NewRestConnectorAllowOverwrite())
client.List("search", nil, nil, nil, nil, nil)
}
18 changes: 14 additions & 4 deletions pkg/nsx/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,23 @@ func (cluster *Cluster) CreateServerUrl(host string, scheme string) string {
}

// NewRestConnector creates a RestConnector used for SDK client.
// HeaderConfig is used to use http header for request, it could be ignored if no extra header needed.
func (cluster *Cluster) NewRestConnector() (policyclient.Connector, *HeaderConfig) {
func (cluster *Cluster) NewRestConnector() policyclient.Connector {
nsxtUrl := cluster.CreateServerUrl(cluster.endpoints[0].Host(), cluster.endpoints[0].Scheme())
connector := policyclient.NewConnector(nsxtUrl, policyclient.UsingRest(nil), policyclient.WithHttpClient(cluster.client))
header := CreateHeaderConfig(false, false, cluster.config.AllowOverwriteHeader)
connector.NewExecutionContext()
return connector, header
return connector
}
func SetAllowOverwriteHeader(req *http.Request) error {
// Set the header X-Allow-Overwrite to True
req.Header.Set("X-Allow-Overwrite", "True")
return nil
}
func (cluster *Cluster) NewRestConnectorAllowOverwrite() policyclient.Connector {
nsxtUrl := cluster.CreateServerUrl(cluster.endpoints[0].Host(), cluster.endpoints[0].Scheme())
policyclient.WithRequestProcessors()
connector := policyclient.NewConnector(nsxtUrl, policyclient.UsingRest(nil), policyclient.WithHttpClient(cluster.client), policyclient.WithRequestProcessors(SetAllowOverwriteHeader))
connector.NewExecutionContext()
return connector
}

func (cluster *Cluster) UsingEnvoy() bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestCluster_NewRestConnector(t *testing.T) {
thumbprint := []string{"123"}
config := NewConfig(a, "admin", "passw0rd", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, thumbprint)
c, _ := NewCluster(config)
con, _ := c.NewRestConnector()
con := c.NewRestConnector()
assert.NotNil(t, con)
}

Expand Down
64 changes: 0 additions & 64 deletions pkg/nsx/headerconfig.go

This file was deleted.

53 changes: 0 additions & 53 deletions pkg/nsx/headerconfig_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/nsx/services/common/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var filterTag = func(v []model.Tag) []string {
func Test_InitializeResourceStore(t *testing.T) {
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

service := Service{
NSXClient: &nsx.Client{
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/ipaddressallocation/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func createService(t *testing.T) (*vpc.VPCService, *gomock.Controller, *mocks.Mo
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})

cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

mockCtrl := gomock.NewController(t)
mockVpcclient := mocks.NewMockVpcsClient(mockCtrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func createIPAddressAllocationService(t *testing.T) (*IPAddressAllocationService
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})

cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

mockCtrl := gomock.NewController(t)
mockVPCIPAddressAllocationclient := mocks.NewMockIPAddressAllocationClient(mockCtrl)
Expand Down
6 changes: 3 additions & 3 deletions pkg/nsx/services/securitypolicy/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func Test_KeyFunc(t *testing.T) {
func Test_InitializeRuleStore(t *testing.T) {
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

service := SecurityPolicyService{
Service: common.Service{
Expand Down Expand Up @@ -201,7 +201,7 @@ func Test_InitializeRuleStore(t *testing.T) {
func Test_InitializeGroupStore(t *testing.T) {
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

service := SecurityPolicyService{
Service: common.Service{
Expand Down Expand Up @@ -252,7 +252,7 @@ func Test_InitializeGroupStore(t *testing.T) {
func Test_InitializeSecurityPolicyStore(t *testing.T) {
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

service := SecurityPolicyService{
Service: common.Service{
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/securitypolicy/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (f fakeVPCGroupClient) Update(orgIdParam string, projectIdParam string, vpc
func fakeSecurityPolicyService() *SecurityPolicyService {
c := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(c)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()
fakeService := &SecurityPolicyService{
Service: common.Service{
NSXClient: &nsx.Client{
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/staticroute/staticroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func createService(t *testing.T) (*StaticRouteService, *gomock.Controller, *mock
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})

cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

mockCtrl := gomock.NewController(t)
mockStaticRouteclient := mocks.NewMockStaticRoutesClient(mockCtrl)
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/subnet/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Test_KeyFunc(t *testing.T) {
func Test_InitializeSubnetStore(t *testing.T) {
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

subnetCacheIndexer := cache.NewIndexer(keyFunc, cache.Indexers{
common.TagScopeSubnetCRUID: subnetIndexFunc,
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/subnet/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func fakeService() *SubnetService {
c := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(c)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()
service := &SubnetService{
Service: common.Service{
NSXClient: &nsx.Client{
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/vpc/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Test_filterTag(t *testing.T) {
func Test_InitializeVPCStore(t *testing.T) {
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})
cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()
vpcCacheIndexer := cache.NewIndexer(keyFunc, cache.Indexers{})
vpcStore := &VPCStore{ResourceStore: common.ResourceStore{
Indexer: vpcCacheIndexer,
Expand Down
2 changes: 1 addition & 1 deletion pkg/nsx/services/vpc/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func createService(t *testing.T) (*VPCService, *gomock.Controller, *mocks.MockVp
config2 := nsx.NewConfig("localhost", "1", "1", []string{}, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, nil, nil, []string{})

cluster, _ := nsx.NewCluster(config2)
rc, _ := cluster.NewRestConnector()
rc := cluster.NewRestConnector()

mockCtrl := gomock.NewController(t)
mockVpcclient := mocks.NewMockVpcsClient(mockCtrl)
Expand Down
Loading