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

[WIP] Enhancement the returned values of nsx.GetClient #360

Closed
Closed
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
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ func main() {
}

// nsxClient is used to interact with NSX API.
nsxClient := nsx.GetClient(cf)
if nsxClient == nil {
nsxClient, err := nsx.GetClient(cf)
if err != nil {
log.Error(err, "failed to get nsx client")
os.Exit(1)
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package clean

import (
"errors"
"fmt"

"k8s.io/client-go/util/retry"

Expand Down Expand Up @@ -38,10 +37,6 @@ func Clean(cf *config.NSXOperatorConfig) error {
if err := cf.ValidateConfigFromCmd(); err != nil {
return errors.Join(ValidationFailed, err)
}
nsxClient := nsx.GetClient(cf)
if nsxClient == nil {
return GetNSXClientFailed
}
if cleanupService, err := InitializeCleanupService(cf); err != nil {
return errors.Join(InitCleanupServiceFailed, err)
} else if cleanupService.err != nil {
Expand Down Expand Up @@ -72,9 +67,9 @@ func Clean(cf *config.NSXOperatorConfig) error {
func InitializeCleanupService(cf *config.NSXOperatorConfig) (*CleanupService, error) {
cleanupService := NewCleanupService()

nsxClient := nsx.GetClient(cf)
if nsxClient == nil {
return cleanupService, fmt.Errorf("failed to get nsx client")
nsxClient, err := nsx.GetClient(cf)
if err != nil {
return cleanupService, GetNSXClientFailed
}

var commonService = common.Service{
Expand Down
7 changes: 5 additions & 2 deletions pkg/nsx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func restConnector(c *Cluster) *client.RestConnector {
return connector
}

func GetClient(cf *config.NSXOperatorConfig) *Client {
func GetClient(cf *config.NSXOperatorConfig) (*Client, error) {
// Set log level for vsphere-automation-sdk-go
logger := logrus.New()
vspherelog.SetLogger(logger)
Expand Down Expand Up @@ -215,14 +215,17 @@ func GetClient(cf *config.NSXOperatorConfig) *Client {
if !nsxClient.NSXCheckVersion(SecurityPolicy) {
err := errors.New("SecurityPolicy feature support check failed")
log.Error(err, "initial NSX version check for SecurityPolicy got error")
return nil, err
}
if !nsxClient.NSXCheckVersion(ServiceAccount) {
err := errors.New("NSXServiceAccount feature support check failed")
log.Error(err, "initial NSX version check for NSXServiceAccount got error")
return nil, err
}
if !nsxClient.NSXCheckVersion(ServiceAccountRestore) {
err := errors.New("NSXServiceAccountRestore feature support check failed")
log.Error(err, "initial NSX version check for NSXServiceAccountRestore got error")
return nil, err
}
if !nsxClient.NSXCheckVersion(VpcAviRule) {
err := errors.New("VpcAviRule feature support check failed")
Expand All @@ -233,7 +236,7 @@ func GetClient(cf *config.NSXOperatorConfig) *Client {
log.Error(err, "initial NSX version check for ServiceAccountCertRotation got error")
}

return nsxClient
return nsxClient, nil
}

func (client *Client) NSXCheckVersion(feature int) bool {
Expand Down
26 changes: 13 additions & 13 deletions pkg/nsx/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ func TestNSXHealthChecker_CheckNSXHealth(t *testing.T) {
func TestGetClient(t *testing.T) {
cf := config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiUser: "1", NsxApiPassword: "1"}}
cf.VCConfig = &config.VCConfig{}
client := GetClient(&cf)
assert.True(t, client != nil)
_, err := GetClient(&cf)
assert.True(t, err == nil)

cluster := &Cluster{}
patches := gomonkey.ApplyMethod(reflect.TypeOf(cluster), "GetVersion", func(_ *Cluster) (*NsxVersion, error) {
nsxVersion := &NsxVersion{NodeVersion: "3.1.1"}
return nsxVersion, nil
})

client = GetClient(&cf)
client, err := GetClient(&cf)
patches.Reset()
assert.True(t, client != nil)
assert.True(t, err == nil)
securityPolicySupported := client.NSXCheckVersion(SecurityPolicy)
assert.True(t, securityPolicySupported == false)
assert.False(t, client.NSXCheckVersion(ServiceAccount))
Expand All @@ -86,9 +86,9 @@ func TestGetClient(t *testing.T) {
nsxVersion := &NsxVersion{NodeVersion: "3.2.1"}
return nsxVersion, nil
})
client = GetClient(&cf)
client, err = GetClient(&cf)
patches.Reset()
assert.True(t, client != nil)
assert.True(t, err == nil)
securityPolicySupported = client.NSXCheckVersion(SecurityPolicy)
assert.True(t, securityPolicySupported == true)
assert.False(t, client.NSXCheckVersion(ServiceAccount))
Expand All @@ -99,9 +99,9 @@ func TestGetClient(t *testing.T) {
nsxVersion := &NsxVersion{NodeVersion: "4.1.0"}
return nsxVersion, nil
})
client = GetClient(&cf)
client, err = GetClient(&cf)
patches.Reset()
assert.True(t, client != nil)
assert.True(t, err == nil)
securityPolicySupported = client.NSXCheckVersion(SecurityPolicy)
assert.True(t, securityPolicySupported == true)
assert.True(t, client.NSXCheckVersion(ServiceAccount))
Expand All @@ -112,9 +112,9 @@ func TestGetClient(t *testing.T) {
nsxVersion := &NsxVersion{NodeVersion: "4.1.2"}
return nsxVersion, nil
})
client = GetClient(&cf)
client, err = GetClient(&cf)
patches.Reset()
assert.True(t, client != nil)
assert.True(t, err == nil)
securityPolicySupported = client.NSXCheckVersion(SecurityPolicy)
assert.True(t, securityPolicySupported == true)
assert.True(t, client.NSXCheckVersion(ServiceAccount))
Expand All @@ -125,9 +125,9 @@ func TestGetClient(t *testing.T) {
nsxVersion := &NsxVersion{NodeVersion: "4.1.3"}
return nsxVersion, nil
})
client = GetClient(&cf)
client, err = GetClient(&cf)
patches.Reset()
assert.True(t, client != nil)
assert.True(t, err == nil)
securityPolicySupported = client.NSXCheckVersion(SecurityPolicy)
assert.True(t, securityPolicySupported == true)
assert.True(t, client.NSXCheckVersion(ServiceAccount))
Expand All @@ -142,7 +142,7 @@ func IsInstanceOf(objectPtr, typePtr interface{}) bool {
func TestSRGetClient(t *testing.T) {
cf := config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiUser: "admin", NsxApiPassword: "Admin!23Admin", NsxApiManagers: []string{"10.173.82.128"}}}
cf.VCConfig = &config.VCConfig{}
client := GetClient(&cf)
client, _ := GetClient(&cf)
st, error := client.StaticRouteClient.Get("default", "project-1", "vpc-2", "site1")
if error == nil {
fmt.Printf("sr %v\n", *st.ResourceType)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/nsxclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewNSXClient(configFile string) (*NSXClient, error) {
if err != nil {
return nil, err
}
client := nsx.GetClient(cf)
if client == nil {
client, err := nsx.GetClient(cf)
if err != nil {
return nil, fmt.Errorf("failed to get nsx client")
}
nsxClient := &NSXClient{}
Expand Down
Loading