Skip to content

Commit

Permalink
Fix race condition on chosing a cluster-init node
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jun 21, 2021
1 parent 8b1b121 commit 4848c21
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func getServerURL(ctx context.Context, nodeClient corev1interface.NodeInterface,
func setClusterAnnotation(ctx context.Context, clustersClient dynamic.NamespaceableResourceInterface, adminName string) error {
cluster, err := clustersClient.Get(ctx, "local", v1.GetOptions{})
if err != nil {
return errors.Errorf("Local cluster is not ready yet")
return fmt.Errorf("Local cluster is not ready yet (get local cluster: %w)", err)
}
if adminName == "" {
return errors.Errorf("User is not set yet")
Expand Down
11 changes: 5 additions & 6 deletions pkg/rancher/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ var defaultValues = map[string]interface{}{
"ingress": map[string]interface{}{
"enabled": false,
},
"features": "multi-cluster-management=false,legacy=false",
"rancherImageTag": "master-head",
"replicas": "-1",
"tls": "external",
"hostPort": 8443,
"noDefaultAdmin": true,
"features": "multi-cluster-management=false",
"replicas": -1,
"tls": "external",
"hostPort": 8443,
"noDefaultAdmin": true,
}

func GetRancherValues(dataDir string) string {
Expand Down
43 changes: 43 additions & 0 deletions pkg/resources/resources.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package resources

import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"strings"

Expand All @@ -19,6 +22,38 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

func writeCattleID(id string) error {
if err := os.MkdirAll("/etc/rancher", 0755); err != nil {
return fmt.Errorf("mkdir /etc/rancher: %w", err)
}
if err := os.MkdirAll("/etc/rancher/agent", 0700); err != nil {
return fmt.Errorf("mkdir /etc/rancher/agent: %w", err)
}
return ioutil.WriteFile("/etc/rancher/agent/cattle-id", []byte(id), 0400)
}

func getCattleID() (string, error) {
data, err := ioutil.ReadFile("/etc/rancher/agent/cattle-id")
if os.IsNotExist(err) {
} else if err != nil {
return "", err
}
id := strings.TrimSpace(string(data))
if id == "" {
id, err = randomtoken.Generate()
if err != nil {
return "", err
}
return id, writeCattleID(id)
}
return id, nil
}

func machineRequestSecretName(name string) string {
hash := sha256.Sum256([]byte(name))
return "custom-" + hex.EncodeToString(hash[:])[:12]
}

func ToBootstrapFile(config *config.Config, path string) (*applyinator.File, error) {
nodeName := config.NodeName
if nodeName == "" {
Expand All @@ -42,6 +77,11 @@ func ToBootstrapFile(config *config.Config, path string) (*applyinator.File, err
}
}

id, err := getCattleID()
if err != nil {
return nil, err
}

return ToFile(append(config.BootstrapResources, v1.GenericMap{
Data: map[string]interface{}{
"kind": "Node",
Expand All @@ -68,6 +108,9 @@ func ToBootstrapFile(config *config.Config, path string) (*applyinator.File, err
"metadata": map[string]interface{}{
"name": "local",
"namespace": "fleet-local",
"labels": map[string]interface{}{
"rke.cattle.io/init-node-machine-id": id,
},
},
"spec": map[string]interface{}{
"kubernetesVersion": k8sVersion,
Expand Down

0 comments on commit 4848c21

Please sign in to comment.