1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package clusterprovider
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
)
const InstancePrefix = "pdr-"
const MaxNameLen = 63
var label = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`)
func InstanceName(cluster string) (string, error) {
if cluster == "" {
return "", errors.New("clusterprovider: a Cluster CR with no name has no instance name")
}
base := strings.ReplaceAll(cluster, ".", "-")
if !label.MatchString(base) {
return "", fmt.Errorf(
"clusterprovider: %q is not usable as an instance name even after normalisation "+
"(dots become dashes, and a DNS-1123 label is [a-z0-9-], starting and ending "+
"alphanumeric)", cluster)
}
if len(InstancePrefix)+len(base) <= MaxNameLen {
return InstancePrefix + base, nil
}
sum := sha256.Sum256([]byte(cluster))
suffix := "-" + hex.EncodeToString(sum[:])[:8]
keep := MaxNameLen - len(InstancePrefix) - len(suffix)
return InstancePrefix + strings.TrimRight(base[:keep], "-") + suffix, nil
}
func ClusterDataDir(stateDir, cluster string) (string, error) {
name, err := InstanceName(cluster)
if err != nil {
return "", err
}
return filepath.Join(stateDir, name), nil
}
const NodesDirName = "nodes"
func NodesDir(stateDir, cluster string) string {
return filepath.Join(stateDir, cluster, NodesDirName)
}
func NodeDataDir(stateDir, cluster, nodeName string) string {
return filepath.Join(NodesDir(stateDir, cluster), nodeName)
}