Skip to content

Commit

Permalink
Fix connection initialization flow for joined node
Browse files Browse the repository at this point in the history
When a connector is initialized for the sake of joining the manager
cluster, the endpoint is different from main provider endpoint, thus
global connection init flows such as NSX Version retrieval and licenses
should not be triggered by such connection.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Jan 10, 2024
1 parent 43eedc9 commit 4b57dd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
15 changes: 10 additions & 5 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients)
}

if !isVMC {
err = configureLicenses(getPolicyConnectorForInit(*clients, true), clients.CommonConfig.LicenseKeys)
err = configureLicenses(getStandalonePolicyConnector(*clients, true), clients.CommonConfig.LicenseKeys)
if err != nil {
return err
}
}

err = initNSXVersion(getPolicyConnectorForInit(*clients, true))
err = initNSXVersion(getStandalonePolicyConnector(*clients, true))
if err != nil && isVMC {
// In case version API does not work for VMC, we workaround by testing version-specific APIs
// TODO - remove this when /node/version API works for all auth methods on VMC
Expand Down Expand Up @@ -974,15 +974,19 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return clients, nil
}

// Standard policy connection that initializes global connection settings on demand
func getPolicyConnector(clients interface{}) client.Connector {
return getPolicyConnectorWithHeaders(clients, nil, false, true)
}

func getPolicyConnectorForInit(clients interface{}, withRetry bool) client.Connector {
// Standalone policy connector, possibly for different endpoint,
// for the purpose of special tasks (such as joining manager cluster node)
// Does not initialize global connection settings
func getStandalonePolicyConnector(clients interface{}, withRetry bool) client.Connector {
return getPolicyConnectorWithHeaders(clients, nil, true, withRetry)
}

func getPolicyConnectorWithHeaders(clients interface{}, customHeaders *map[string]string, initFlow bool, withRetry bool) client.Connector {
func getPolicyConnectorWithHeaders(clients interface{}, customHeaders *map[string]string, standaloneFlow bool, withRetry bool) client.Connector {
c := clients.(nsxtClients)

retryFunc := func(retryContext retry.RetryContext) bool {
Expand Down Expand Up @@ -1064,7 +1068,8 @@ func getPolicyConnectorWithHeaders(clients interface{}, customHeaders *map[strin
connector := client.NewConnector(c.Host, connectorOptions...)
// Init NSX version on demand if not done yet
// This is also our indication to apply licenses, in case of delayed connection
if nsxVersion == "" && !initFlow {
// This step is skipped if the connector is for special purpose, or for different endpoint
if nsxVersion == "" && !standaloneFlow {
initNSXVersion(connector)
err := configureLicenses(connector, c.CommonConfig.LicenseKeys)
if err != nil {
Expand Down
11 changes: 4 additions & 7 deletions nsxt/resource_nsxt_manager_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func waitForNodeStatus(d *schema.ResourceData, m interface{}, nodes []NsxCluster
log.Printf("[DEBUG]: API probing for NSX is disabled")
return nil
}
connector := getPolicyConnectorForInit(m, false)
connector := getStandalonePolicyConnector(m, false)
stateConf := getNodeConnectivityStateConf(connector, delay, interval, timeout)
_, err := stateConf.WaitForState()
if err != nil {
Expand All @@ -182,7 +182,7 @@ func waitForNodeStatus(d *schema.ResourceData, m interface{}, nodes []NsxCluster
return err
}
newNsxClients := c.(nsxtClients)
nodeConnector := getPolicyConnectorForInit(newNsxClients, false)
nodeConnector := getStandalonePolicyConnector(newNsxClients, false)
nodeConf := getNodeConnectivityStateConf(nodeConnector, 0, interval, timeout)
_, err = nodeConf.WaitForState()
if err != nil {
Expand Down Expand Up @@ -322,6 +322,7 @@ func getNewNsxtClient(node NsxClusterNode, d *schema.ResourceData, clients inter
return newClients, nil
}


Check failure on line 325 in nsxt/resource_nsxt_manager_cluster.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
func configureNewClient(newClient *nsxtClients, oldClient *nsxtClients, host string, username string, password string) error {
newClient.Host = host
securityCtx, err := getConfiguredSecurityContext(newClient, "", "", "", username, password)
Expand All @@ -330,10 +331,6 @@ func configureNewClient(newClient *nsxtClients, oldClient *nsxtClients, host str
}
newClient.PolicySecurityContext = securityCtx
newClient.PolicyHTTPClient = oldClient.PolicyHTTPClient
err = initNSXVersion(getPolicyConnector(*newClient))
if err != nil {
return fmt.Errorf("Failed to configure new client with host %s: %s", host, err)
}
return nil
}

Expand All @@ -344,7 +341,7 @@ func joinNodeToCluster(clusterID string, certSha256Thumbprint string, guestNode
}
log.Printf("[INFO] Cluster %s. Joining node %s", clusterID, guestNode.IPAddress)
newNsxClients := c.(nsxtClients)
connector := getPolicyConnector(newNsxClients)
connector := getStandalonePolicyConnector(newNsxClients, true)
client := nsx.NewClusterClient(connector)
username, password := getHostCredential(m)
hostIP := getMatchingIPVersion(guestNode.IPAddress, hostIPs)
Expand Down

0 comments on commit 4b57dd4

Please sign in to comment.