fix: call GatherEnvironmentInfo once in NodeAddToClusterMulti to avoid race condition causing error (#1411)

pull/1469/head
Michael Maltese 2 months ago committed by GitHub
parent 35dbaa4ded
commit 0bedbc2611
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      pkg/client/node.go

@ -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}
@ -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
}
@ -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),
@ -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

Loading…
Cancel
Save