From 68745fdd2bf0a8a289cf35d7a48535e6dcd32cc4 Mon Sep 17 00:00:00 2001 From: gran Date: Thu, 20 Jun 2024 11:03:36 +0800 Subject: [PATCH] [VPC] Add NSX LBS path to VPCInfo and use LB options in NsxConfig Signed-off-by: gran --- ...x.vmware.com_vpcnetworkconfigurations.yaml | 9 +- .../v1alpha1/vpcnetworkconfiguration_types.go | 2 + .../v1alpha1/vpcnetworkconfiguration_types.go | 2 + pkg/config/config.go | 20 +++++ pkg/config/config_test.go | 84 +++++++++++++++++++ 5 files changed, 115 insertions(+), 2 deletions(-) diff --git a/build/yaml/crd/nsx.vmware.com_vpcnetworkconfigurations.yaml b/build/yaml/crd/nsx.vmware.com_vpcnetworkconfigurations.yaml index 4911e9a63..9640e2666 100644 --- a/build/yaml/crd/nsx.vmware.com_vpcnetworkconfigurations.yaml +++ b/build/yaml/crd/nsx.vmware.com_vpcnetworkconfigurations.yaml @@ -103,8 +103,9 @@ spec: maxLength: 8 type: string vpc: - description: NSX path of the VPC the Namespace associated with. If - vpc is set, only defaultIPv4SubnetSize and defaultSubnetAccessMode + description: |- + NSX path of the VPC the Namespace associated with. + If vpc is set, only defaultIPv4SubnetSize and defaultSubnetAccessMode take effect, other fields are ignored. type: string type: object @@ -125,6 +126,10 @@ spec: name: description: VPC name. type: string + nsxLoadBalancerPath: + description: NSXLoadBalancerPath is the NSX Policy path for + the NSX Load Balancer. + type: string required: - name type: object diff --git a/pkg/apis/nsx.vmware.com/v1alpha1/vpcnetworkconfiguration_types.go b/pkg/apis/nsx.vmware.com/v1alpha1/vpcnetworkconfiguration_types.go index 23355f5f9..08d5ee7fb 100644 --- a/pkg/apis/nsx.vmware.com/v1alpha1/vpcnetworkconfiguration_types.go +++ b/pkg/apis/nsx.vmware.com/v1alpha1/vpcnetworkconfiguration_types.go @@ -65,6 +65,8 @@ type VPCInfo struct { Name string `json:"name"` // AVISESubnetPath is the NSX Policy Path for the AVI SE Subnet. AVISESubnetPath string `json:"lbSubnetPath,omitempty"` + // NSXLoadBalancerPath is the NSX Policy path for the NSX Load Balancer. + NSXLoadBalancerPath string `json:"nsxLoadBalancerPath,omitempty"` } // +genclient diff --git a/pkg/apis/v1alpha1/vpcnetworkconfiguration_types.go b/pkg/apis/v1alpha1/vpcnetworkconfiguration_types.go index 23355f5f9..08d5ee7fb 100644 --- a/pkg/apis/v1alpha1/vpcnetworkconfiguration_types.go +++ b/pkg/apis/v1alpha1/vpcnetworkconfiguration_types.go @@ -65,6 +65,8 @@ type VPCInfo struct { Name string `json:"name"` // AVISESubnetPath is the NSX Policy Path for the AVI SE Subnet. AVISESubnetPath string `json:"lbSubnetPath,omitempty"` + // NSXLoadBalancerPath is the NSX Policy path for the NSX Load Balancer. + NSXLoadBalancerPath string `json:"nsxLoadBalancerPath,omitempty"` } // +genclient diff --git a/pkg/config/config.go b/pkg/config/config.go index 2710b69c9..29a3c60c5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -11,6 +11,7 @@ import ( "fmt" "os" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" "go.uber.org/zap" ini "gopkg.in/ini.v1" @@ -117,6 +118,10 @@ type NsxConfig struct { EnvoyHost string `ini:"envoy_host"` EnvoyPort int `ini:"envoy_port"` LicenseValidationInterval int `ini:"license_validation_interval"` + UseAVILoadBalancer bool `ini:"use_avi_lb"` + UseNSXLoadBalancer *bool `ini:"use_native_loadbalancer"` + RelaxNSXLBScaleValication bool `ini:"relax_scale_validation"` + NSXLBSize string `ini:"service_size"` } type K8sConfig struct { @@ -406,3 +411,18 @@ func (coeConfig *CoeConfig) validate() error { func (nsxConfig *NsxConfig) ValidateConfigFromCmd() error { return nsxConfig.validate(true) } + +func (nsxConfig *NsxConfig) NSXLBEnabled() bool { + if nsxConfig.UseAVILoadBalancer == false && (nsxConfig.UseNSXLoadBalancer == nil || *nsxConfig.UseNSXLoadBalancer == true) { + return true + } + return false +} + +func (nsxConfig *NsxConfig) GetNSXLBSize() string { + lbsSize := nsxConfig.NSXLBSize + if lbsSize == "" { + lbsSize = model.LBService_SIZE_SMALL + } + return lbsSize +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 6ca245042..afacbf018 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -9,7 +9,9 @@ import ( "os" "testing" + "github.com/openlyinc/pointy" "github.com/stretchr/testify/assert" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" ) func TestConfig_VCConfig(t *testing.T) { @@ -176,3 +178,85 @@ func TestNSXOperatorConfig_GetCACert(t *testing.T) { }) } } + +func TestNsxConfig_NSXLBEnabled(t *testing.T) { + type fields struct { + UseAVILB bool + UseNativeLoadBalancer *bool + } + tests := []struct { + name string + fields fields + want bool + }{{ + name: "avilb", + fields: fields{ + UseAVILB: true, + UseNativeLoadBalancer: nil, + }, + want: false, + }, { + name: "nsxlbnil", + fields: fields{ + UseAVILB: false, + UseNativeLoadBalancer: nil, + }, + want: true, + }, { + name: "nsxlbtrue", + fields: fields{ + UseAVILB: false, + UseNativeLoadBalancer: pointy.Bool(true), + }, + want: true, + }, { + name: "nsxlbfalse", + fields: fields{ + UseAVILB: false, + UseNativeLoadBalancer: pointy.Bool(false), + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + nsxConfig := &NsxConfig{ + UseAVILoadBalancer: tt.fields.UseAVILB, + UseNSXLoadBalancer: tt.fields.UseNativeLoadBalancer, + } + assert.Equalf(t, tt.want, nsxConfig.NSXLBEnabled(), "NSXLBEnabled()") + }) + } +} + +func TestNsxConfig_GetServiceSize(t *testing.T) { + type fields struct { + ServiceSize string + } + tests := []struct { + name string + fields fields + want string + }{{ + name: "default", + fields: fields{ + ServiceSize: "", + }, + want: model.LBService_SIZE_SMALL, + }, { + name: "large", + fields: fields{ + ServiceSize: model.LBService_SIZE_LARGE, + }, + want: model.LBService_SIZE_LARGE, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + nsxConfig := &NsxConfig{ + NSXLBSize: tt.fields.ServiceSize, + } + assert.Equalf(t, tt.want, nsxConfig.GetNSXLBSize(), "GetNSXLBSize()") + }) + } +}