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

Refactor manager cluster resource #992

Merged
merged 1 commit into from
Oct 18, 2023
Merged

Conversation

annakhm
Copy link
Collaborator

@annakhm annakhm commented Oct 5, 2023

Main changes:

  1. Resolve provider host address, if needed, rather than relying on comparing host names in cluster config and in provider config
  2. Rely on nodes IP version rather than host IP version, which is unknown when not specified in provider config
  3. Refactor Read function to update only Computed fields in state

@annakhm annakhm requested review from 2ez4szliu and ksamoray October 5, 2023 02:49
needIPv4 := (net.ParseIP(ip)).To4() != nil

for _, hostIP := range hostIPs {
isIPv4 := (net.ParseIP(hostIP)).To4() != nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are 100% sure hostIp is always a valid Ip address? otherwise we might first need to check ParseIP result.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We validate hostIP in resolveHostIPs, which is happening before this call


for _, hostIP := range hostIPs {
isIPv4 := (net.ParseIP(hostIP)).To4() != nil
if needIPv4 == isIPv4 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good but I find some difficulties to read it. It's not easy to understand that if needIPv4=False and isIPv4=False then we want to return an IPv6 address?

Do you agree or is it just me getting older?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a bit tricky to read, I agree, I've added a comment

if (entity.IpAddress != nil) && (*entity.IpAddress == address) {
return true
}
if (entity.Ipv6Address != nil) && (*entity.Ipv6Address == address) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we convert string to IPs to avoid equality check to fail due to differences in Ipv6 short/full representation?

for i := 0; i < maxRetries; i++ {
clusterConfig, err := client.Get()
if err != nil {
return "", "", "", handleReadError(d, "Cluster Config", "", err)
return "", "", hostIPs, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this fails after the first iteration we might return a non empty host IP list.
Is that expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ok since error is always checked, and other return values are disregarded when error is present

}

log.Printf("[DEBUG]: Host resolved to IP addresses %v", hostIPs)
}
clusterID := *clusterConfig.ClusterId
nodes := clusterConfig.Nodes
node := nodes[0]
apiListenAddr := node.ApiListenAddr
if apiListenAddr != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be better to first do this check, and only if it succeeds collect hostIPs? It seems while we wait for ApiListenAddr to populate we will keep repeating resolveHostIPs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolveHostIPs only happens on first iteration, if len(hostIPs) == 0, so it shouldn't be repeated..

}

func getHostIPFromClusterConfig(client interface{}, clusterConfig nsxModel.ClusterConfig) (string, error) {
func resolveHostIPs(client interface{}) ([]string, error) {
c := client.(nsxtClients)
host := c.Host
host = strings.TrimPrefix(host, "https://")
// Check if host is ip address or fqdn, if it's ip address then we are all set,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the approach in resolveHostIPs I think this comment section is no longer needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comment, thanks

@annakhm annakhm force-pushed the refactor-manager-cluster branch from 9fdbf6b to 2e7171f Compare October 10, 2023 22:30
Main changes:
1. Resolve provider host address, if needed, rather than relying
on comparing host names in cluster config and in provider config
2. Rely on nodes IP version rather than host IP version, which
is unknown when not specified in provider config
3. Refactor Read function to update only Computed fields in state

Signed-off-by: Anna Khmelnitsky <[email protected]>
@annakhm annakhm force-pushed the refactor-manager-cluster branch from 2e7171f to 7b23d00 Compare October 11, 2023 20:01
Copy link
Collaborator

@ksamoray ksamoray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, if no other comments from reviewers, it's good to go.

@annakhm annakhm merged commit 8e24ef4 into master Oct 18, 2023
2 checks passed
@annakhm annakhm deleted the refactor-manager-cluster branch February 5, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants