-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
pod_name.go
94 lines (77 loc) · 2.17 KB
/
pod_name.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package util
import (
"fmt"
"hash/fnv"
"os"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
const (
maxK8sResourceNameLength = 253
k8sNamingHashLength = 10
)
// PodNameVersion stores which type of pod names should be used.
// v1 represents the node id.
// v2 is the combination of a node id and template name.
type PodNameVersion string
const (
// PodNameV1 is the v1 name that uses node ids for pod names
PodNameV1 PodNameVersion = "v1"
// PodNameV2 is the v2 name that uses node id combined with
// the template name
PodNameV2 PodNameVersion = "v2"
DefaultPodNameVersion PodNameVersion = PodNameV2
)
// String stringifies the pod name version
func (v PodNameVersion) String() string {
return string(v)
}
// GetPodNameVersion returns the pod name version to be used
func GetPodNameVersion() PodNameVersion {
switch os.Getenv("POD_NAMES") {
case "v2":
return PodNameV2
case "v1":
return PodNameV1
default:
return DefaultPodNameVersion
}
}
// GeneratePodName return a deterministic pod name
func GeneratePodName(workflowName, nodeName, templateName, nodeID string, version PodNameVersion) string {
if version == PodNameV1 {
return nodeID
}
if workflowName == nodeName {
return workflowName
}
prefix := fmt.Sprintf("%s-%s", workflowName, templateName)
prefix = ensurePodNamePrefixLength(prefix)
h := fnv.New32a()
_, _ = h.Write([]byte(nodeName))
return fmt.Sprintf("%s-%v", prefix, h.Sum32())
}
func ensurePodNamePrefixLength(prefix string) string {
maxPrefixLength := maxK8sResourceNameLength - k8sNamingHashLength
if len(prefix) > maxPrefixLength-1 {
return prefix[0 : maxPrefixLength-1]
}
return prefix
}
// GetWorkflowPodNameVersion gets the pod name version from the annotation of a
// given workflow
func GetWorkflowPodNameVersion(wf *v1alpha1.Workflow) PodNameVersion {
annotations := wf.GetAnnotations()
version, ok := annotations[common.AnnotationKeyPodNameVersion]
if !ok {
return GetPodNameVersion()
}
switch version {
case PodNameV1.String():
return PodNameV1
case PodNameV2.String():
return PodNameV2
default:
return DefaultPodNameVersion
}
}