forked from coreos/etcd-operator
/
self_hosted.go
115 lines (103 loc) · 3.5 KB
/
self_hosted.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
// Copyright 2016 The etcd-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8sutil
import (
"encoding/json"
"fmt"
"path"
"strings"
"github.com/coreos/etcd-operator/pkg/spec"
"k8s.io/client-go/pkg/api/meta/metatypes"
"k8s.io/client-go/pkg/api/v1"
)
const (
shouldCheckpointAnnotation = "checkpointer.alpha.coreos.com/checkpoint" // = "true"
)
var (
envPodIP = v1.EnvVar{
Name: "MY_POD_IP",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "status.podIP",
},
},
}
)
func PodWithAddMemberInitContainer(p *v1.Pod, endpoints []string, name string, peerURLs []string, cs spec.ClusterSpec) *v1.Pod {
containerSpec := []v1.Container{
{
Name: "add-member",
Image: EtcdImageName(cs.Version),
Command: []string{
"/bin/sh", "-c",
fmt.Sprintf("ETCDCTL_API=3 etcdctl --endpoints=%s member add %s --peer-urls=%s", strings.Join(endpoints, ","), name, strings.Join(peerURLs, ",")),
},
Env: []v1.EnvVar{envPodIP},
},
}
b, err := json.Marshal(containerSpec)
if err != nil {
panic(err)
}
p.Annotations[v1.PodInitContainersBetaAnnotationKey] = string(b)
return p
}
func NewSelfHostedEtcdPod(name string, initialCluster []string, clusterName, ns, state, token string, cs spec.ClusterSpec, owner metatypes.OwnerReference) *v1.Pod {
selfHostedDataDir := path.Join(etcdVolumeMountDir, ns+"-"+name)
commands := fmt.Sprintf("/usr/local/bin/etcd --data-dir=%s --name=%s --initial-advertise-peer-urls=http://$(MY_POD_IP):2380 "+
"--listen-peer-urls=http://$(MY_POD_IP):2380 --listen-client-urls=http://$(MY_POD_IP):2379 --advertise-client-urls=http://$(MY_POD_IP):2379 "+
"--initial-cluster=%s --initial-cluster-state=%s",
selfHostedDataDir, name, strings.Join(initialCluster, ","), state)
if state == "new" {
commands = fmt.Sprintf("%s --initial-cluster-token=%s", commands, token)
}
c := etcdContainer(commands, cs.Version)
if cs.Pod != nil {
c = containerWithRequirements(c, cs.Pod.Resources)
}
c.Env = []v1.EnvVar{envPodIP}
pod := &v1.Pod{
ObjectMeta: v1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app": "etcd",
"etcd_node": name,
"etcd_cluster": clusterName,
},
Annotations: map[string]string{
shouldCheckpointAnnotation: "true",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{c},
// Self-hosted etcd pod need to endure node restart.
// If we set it to Never, the pod won't restart. If etcd won't come up, nor does other k8s API components.
RestartPolicy: v1.RestartPolicyAlways,
HostNetwork: true,
Volumes: []v1.Volume{{
Name: "etcd-data",
// TODO: configurable mount host path.
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: etcdVolumeMountDir}},
}},
},
}
SetEtcdVersion(pod, cs.Version)
pod = PodWithAntiAffinity(pod, clusterName)
if cs.Pod != nil && len(cs.Pod.NodeSelector) != 0 {
pod = PodWithNodeSelector(pod, cs.Pod.NodeSelector)
}
addOwnerRefToObject(pod.GetObjectMeta(), owner)
return pod
}