Skip to content

Commit

Permalink
fix: call GatherEnvironmentInfo once in NodeAddToClusterMulti to avoi…
Browse files Browse the repository at this point in the history
…d race condition causing error (#1411)
  • Loading branch information
ligfx authored Jul 10, 2024
1 parent 35dbaa4 commit 0bedbc2
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions pkg/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ import (

// NodeAddToCluster adds a node to an existing cluster
func NodeAddToCluster(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, cluster *k3d.Cluster, createNodeOpts k3d.NodeCreateOpts) error {
targetClusterName := cluster.Name
cluster, err := ClusterGet(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("Failed to find specified cluster '%s': %w", targetClusterName, err)
}
if createNodeOpts.EnvironmentInfo == nil {
targetClusterName := cluster.Name
cluster, err := ClusterGet(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("Failed to find specified cluster '%s': %w", targetClusterName, err)
}

envInfo, err := GatherEnvironmentInfo(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("error gathering cluster environment info required to properly create the node: %w", err)
}
envInfo, err := GatherEnvironmentInfo(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("error gathering cluster environment info required to properly create the node: %w", err)
}

createNodeOpts.EnvironmentInfo = envInfo
createNodeOpts.EnvironmentInfo = envInfo
}

// networks: ensure that cluster network is on index 0
networks := []string{cluster.Network.Name}
Expand Down Expand Up @@ -106,7 +108,7 @@ func NodeAddToCluster(ctx context.Context, runtime runtimes.Runtime, node *k3d.N
}

// get node details
srcNode, err = NodeGet(ctx, runtime, srcNode)
srcNode, err := NodeGet(ctx, runtime, srcNode)
if err != nil {
return err
}
Expand Down Expand Up @@ -262,7 +264,7 @@ func NodeAddToCluster(ctx context.Context, runtime runtimes.Runtime, node *k3d.N
Runtime: runtime,
Command: []string{
"sh", "-c",
fmt.Sprintf("echo '%s %s' >> /etc/hosts", envInfo.HostGateway.String(), k3d.DefaultK3dInternalHostRecord),
fmt.Sprintf("echo '%s %s' >> /etc/hosts", createNodeOpts.EnvironmentInfo.HostGateway.String(), k3d.DefaultK3dInternalHostRecord),
},
Retries: 0,
Description: fmt.Sprintf("Inject /etc/hosts record for %s", k3d.DefaultK3dInternalHostRecord),
Expand Down Expand Up @@ -333,6 +335,17 @@ func NodeAddToClusterMulti(ctx context.Context, runtime runtimes.Runtime, nodes
defer cancel()
}

targetClusterName := cluster.Name
cluster, err := ClusterGet(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("Failed to find specified cluster '%s': %w", targetClusterName, err)
}

createNodeOpts.EnvironmentInfo, err = GatherEnvironmentInfo(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("error gathering cluster environment info required to properly create the node: %w", err)
}

nodeWaitGroup, ctx := errgroup.WithContext(ctx)
for _, node := range nodes {
currentNode := node
Expand Down

0 comments on commit 0bedbc2

Please sign in to comment.