-
Notifications
You must be signed in to change notification settings - Fork 159
/
names.go
205 lines (162 loc) · 5.9 KB
/
names.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package naming
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
scyllav1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func ManualRef(namespace, name string) string {
return fmt.Sprintf("%s/%s", namespace, name)
}
func ObjRef(obj metav1.Object) string {
namespace := obj.GetNamespace()
if len(namespace) == 0 {
return obj.GetName()
}
return ManualRef(obj.GetNamespace(), obj.GetName())
}
func ObjRefWithUID(obj metav1.Object) string {
return fmt.Sprintf("%s(UID=%s)", ObjRef(obj), obj.GetUID())
}
func StatefulSetNameForRack(r scyllav1.RackSpec, c *scyllav1.ScyllaCluster) string {
return fmt.Sprintf("%s-%s-%s", c.Name, c.Spec.Datacenter.Name, r.Name)
}
func ServiceNameFromPod(pod *corev1.Pod) string {
// Pod and Service has the same name
return pod.Name
}
func AgentAuthTokenSecretName(clusterName string) string {
return fmt.Sprintf("%s-auth-token", clusterName)
}
func MemberServiceName(r scyllav1.RackSpec, c *scyllav1.ScyllaCluster, idx int) string {
return fmt.Sprintf("%s-%d", StatefulSetNameForRack(r, c), idx)
}
func ServiceDNSName(service string, c *scyllav1.ScyllaCluster) string {
return fmt.Sprintf("%s.%s", service, CrossNamespaceServiceNameForCluster(c))
}
func HeadlessServiceNameForCluster(c *scyllav1.ScyllaCluster) string {
return fmt.Sprintf("%s-client", c.Name)
}
func PodDisruptionBudgetName(c *scyllav1.ScyllaCluster) string {
return c.Name
}
func CrossNamespaceServiceNameForCluster(c *scyllav1.ScyllaCluster) string {
return fmt.Sprintf("%s.%s.svc", HeadlessServiceNameForCluster(c), c.Namespace)
}
func ManagerClusterName(c *scyllav1.ScyllaCluster) string {
return c.Namespace + "/" + c.Name
}
func PVCNameForPod(podName string) string {
return fmt.Sprintf("%s-%s", PVCTemplateName, podName)
}
func PVCNameForService(svcName string) string {
return PVCNameForPod(svcName)
}
func PVCNamePrefixForScyllaCluster(scName string) string {
return fmt.Sprintf("%s-%s-", PVCTemplateName, scName)
}
func PVCNameForStatefulSet(stsName string, ordinal int32) string {
return fmt.Sprintf("%s-%s-%d", PVCTemplateName, stsName, ordinal)
}
// IndexFromName attempts to get the index from a name using the
// naming convention <name>-<index>.
func IndexFromName(n string) (int32, error) {
// index := svc.Name[strings.LastIndex(svc.Name, "-") + 1 : len(svc.Name)]
delimIndex := strings.LastIndex(n, "-")
if delimIndex == -1 {
return -1, errors.New(fmt.Sprintf("didn't find '-' delimiter in string %s", n))
}
index, err := strconv.Atoi(n[delimIndex+1:])
if err != nil {
return -1, errors.New(fmt.Sprintf("couldn't convert '%s' to a number", n[delimIndex+1:]))
}
return int32(index), nil
}
// ImageToVersion strips version part from container image.
func ImageToVersion(image string) (string, error) {
parts := strings.Split(image, ":")
if len(parts) != 2 || len(parts[1]) == 0 {
return "", errors.New(fmt.Sprintf("Invalid image name: %s", image))
}
return parts[1], nil
}
// FindScyllaContainer returns Scylla container from given list.
func FindScyllaContainer(containers []corev1.Container) (int, error) {
return FindContainerWithName(containers, ScyllaContainerName)
}
// FindSidecarInjectorContainer returns sidecar injector container from given list.
func FindSidecarInjectorContainer(containers []corev1.Container) (int, error) {
return FindContainerWithName(containers, SidecarInjectorContainerName)
}
// FindContainerWithName returns container having
func FindContainerWithName(containers []corev1.Container, name string) (int, error) {
for idx := range containers {
if containers[idx].Name == name {
return idx, nil
}
}
return 0, errors.Errorf(" '%s' container not found", name)
}
// ScyllaVersion returns version of Scylla container.
func ScyllaVersion(containers []corev1.Container) (string, error) {
idx, err := FindScyllaContainer(containers)
if err != nil {
return "", errors.Wrap(err, "find scylla container")
}
version, err := ImageToVersion(containers[idx].Image)
if err != nil {
return "", errors.Wrap(err, "parse scylla container version")
}
return version, nil
}
// SidecarVersion returns version of sidecar container.
func SidecarVersion(containers []corev1.Container) (string, error) {
idx, err := FindSidecarInjectorContainer(containers)
if err != nil {
return "", errors.Wrap(err, "find sidecar container")
}
version, err := ImageToVersion(containers[idx].Image)
if err != nil {
return "", errors.Wrap(err, "parse sidecar container version")
}
return version, nil
}
func PerftuneResultName(uid string) string {
return fmt.Sprintf("%s-%s", PerftuneJobPrefixName, uid)
}
func GetTuningConfigMapNameForPod(pod *corev1.Pod) string {
return fmt.Sprintf("nodeconfig-podinfo-%s", pod.UID)
}
func MemberServiceAccountNameForScyllaCluster(scName string) string {
return fmt.Sprintf("%s-member", scName)
}
func GetScyllaClusterRootCASecretName(scName string) string {
return fmt.Sprintf("%s-root-ca", scName)
}
func GetScyllaClusterLocalClientCAName(scName string) string {
return fmt.Sprintf("%s-local-client-ca", scName)
}
func GetScyllaClusterLocalUserAdminCertName(scName string) string {
return fmt.Sprintf("%s-local-user-admin", scName)
}
func GetScyllaClusterLocalServingCAName(scName string) string {
return fmt.Sprintf("%s-local-serving-ca", scName)
}
func GetScyllaClusterLocalServingCertName(scName string) string {
return fmt.Sprintf("%s-local-serving-certs", scName)
}
func GetScyllaClusterLocalAdminCQLConnectionConfigsName(scName string) string {
return fmt.Sprintf("%s-local-cql-connection-configs-admin", scName)
}
func GetProtocolSubDomain(protocol, domain string) string {
return fmt.Sprintf("%s.%s", protocol, domain)
}
func GetCQLProtocolSubDomain(domain string) string {
return GetProtocolSubDomain(CQLProtocolDNSLabel, domain)
}
func GetCQLHostIDSubDomain(hostID, domain string) string {
return fmt.Sprintf("%s.%s", hostID, GetCQLProtocolSubDomain(domain))
}