-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcontainer_env.go
More file actions
130 lines (118 loc) · 3.29 KB
/
container_env.go
File metadata and controls
130 lines (118 loc) · 3.29 KB
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
package etcd
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
// buildContainerEnv constructs all environment variables for etcd clustering in
// StatefulSets. This combines pod identity, etcd config, and cluster peer
// discovery details.
func buildContainerEnv(
etcdName, namespace string,
replicas int32,
serviceName string,
) []corev1.EnvVar {
envVars := make([]corev1.EnvVar, 0)
// Add pod identity variables from downward API
envVars = append(envVars, buildPodIdentityEnv()...)
// Add etcd configuration variables
envVars = append(envVars, buildEtcdConfigEnv(etcdName, serviceName, namespace)...)
// Add the initial cluster peer list
clusterPeerList := buildEtcdClusterPeerList(etcdName, serviceName, namespace, replicas)
envVars = append(envVars, corev1.EnvVar{
Name: "ETCD_INITIAL_CLUSTER",
Value: clusterPeerList,
})
return envVars
}
// buildPodIdentityEnv creates environment variables for pod name and namespace.
// These are required for etcd to construct its advertise URLs in StatefulSets,
// and this association of both Pod name and namespace are common.
//
// Ref: https://etcd.io/docs/latest/op-guide/clustering/
func buildPodIdentityEnv() []corev1.EnvVar {
return []corev1.EnvVar{
{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
{
Name: "POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
}
}
// buildEtcdConfigEnv creates etcd configuration environment variables.
// These configure etcd's network endpoints and cluster formation.
//
// Ref: https://etcd.io/docs/latest/op-guide/configuration/
func buildEtcdConfigEnv(etcdName, serviceName, namespace string) []corev1.EnvVar {
return []corev1.EnvVar{
{
Name: "ETCD_NAME",
Value: "$(POD_NAME)",
},
{
Name: "ETCD_DATA_DIR",
Value: "/var/lib/etcd",
},
{
Name: "ETCD_LISTEN_CLIENT_URLS",
Value: "http://0.0.0.0:2379",
},
{
Name: "ETCD_LISTEN_PEER_URLS",
Value: "http://0.0.0.0:2380",
},
{
Name: "ETCD_ADVERTISE_CLIENT_URLS",
Value: fmt.Sprintf(
"http://$(POD_NAME).%s.$(POD_NAMESPACE).svc.cluster.local:2379",
serviceName,
),
},
{
Name: "ETCD_INITIAL_ADVERTISE_PEER_URLS",
Value: fmt.Sprintf(
"http://$(POD_NAME).%s.$(POD_NAMESPACE).svc.cluster.local:2380",
serviceName,
),
},
{
Name: "ETCD_INITIAL_CLUSTER_STATE",
Value: "new",
},
{
Name: "ETCD_INITIAL_CLUSTER_TOKEN",
Value: etcdName,
},
}
}
// buildEtcdClusterPeerList generates the initial cluster member list for
// bootstrap. This tells each etcd member about all other members during cluster
// formation.
//
// Format: member-0=http://member-0.service.ns.svc.cluster.local:2380,...
//
// Ref: https://etcd.io/docs/latest/op-guide/clustering/#static
func buildEtcdClusterPeerList(etcdName, serviceName, namespace string, replicas int32) string {
if replicas < 0 {
return ""
}
peers := make([]string, 0, replicas)
for i := range replicas {
podName := fmt.Sprintf("%s-%d", etcdName, i)
peerURL := fmt.Sprintf("%s=http://%s.%s.%s.svc.cluster.local:2380",
podName, podName, serviceName, namespace)
peers = append(peers, peerURL)
}
return strings.Join(peers, ",")
}