This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathpod_util.go
146 lines (125 loc) · 3.8 KB
/
pod_util.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
// 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"
api "github.com/coreos/etcd-operator/pkg/apis/etcd/v1beta2"
"github.com/coreos/etcd-operator/pkg/util/etcdutil"
"k8s.io/api/core/v1"
)
const (
etcdVolumeName = "etcd-data"
)
func etcdVolumeMounts() []v1.VolumeMount {
return []v1.VolumeMount{
{Name: etcdVolumeName, MountPath: etcdVolumeMountDir},
}
}
func etcdContainer(cmd []string, repo, version string) v1.Container {
c := v1.Container{
Command: cmd,
Name: "etcd",
Image: ImageName(repo, version),
Ports: []v1.ContainerPort{
{
Name: "server",
ContainerPort: int32(2380),
Protocol: v1.ProtocolTCP,
},
{
Name: "client",
ContainerPort: int32(EtcdClientPort),
Protocol: v1.ProtocolTCP,
},
},
VolumeMounts: etcdVolumeMounts(),
}
return c
}
func containerWithProbes(c v1.Container, lp *v1.Probe, rp *v1.Probe) v1.Container {
c.LivenessProbe = lp
c.ReadinessProbe = rp
return c
}
func containerWithRequirements(c v1.Container, r v1.ResourceRequirements) v1.Container {
c.Resources = r
return c
}
func newEtcdProbe(isSecure bool) *v1.Probe {
// etcd pod is alive only if a linearizable get succeeds.
cmd := "ETCDCTL_API=3 etcdctl get foo"
if isSecure {
tlsFlags := fmt.Sprintf("--cert=%[1]s/%[2]s --key=%[1]s/%[3]s --cacert=%[1]s/%[4]s", operatorEtcdTLSDir, etcdutil.CliCertFile, etcdutil.CliKeyFile, etcdutil.CliCAFile)
cmd = fmt.Sprintf("ETCDCTL_API=3 etcdctl --endpoints=https://localhost:%d %s get foo", EtcdClientPort, tlsFlags)
}
return &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"/bin/sh", "-ec", cmd},
},
},
InitialDelaySeconds: 10,
TimeoutSeconds: 10,
PeriodSeconds: 60,
FailureThreshold: 3,
}
}
func applyPodPolicy(clusterName string, pod *v1.Pod, policy *api.PodPolicy) {
if policy == nil {
return
}
if policy.Affinity != nil {
pod.Spec.Affinity = policy.Affinity
}
if len(policy.NodeSelector) != 0 {
pod = PodWithNodeSelector(pod, policy.NodeSelector)
}
if len(policy.Tolerations) != 0 {
pod.Spec.Tolerations = policy.Tolerations
}
mergeLabels(pod.Labels, policy.Labels)
for i := range pod.Spec.Containers {
pod.Spec.Containers[i] = containerWithRequirements(pod.Spec.Containers[i], policy.Resources)
if pod.Spec.Containers[i].Name == "etcd" {
pod.Spec.Containers[i].Env = append(pod.Spec.Containers[i].Env, policy.EtcdEnv...)
}
}
for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i] = containerWithRequirements(pod.Spec.InitContainers[i], policy.Resources)
}
for key, value := range policy.Annotations {
pod.ObjectMeta.Annotations[key] = value
}
}
// IsPodReady returns false if the Pod Status is nil
func IsPodReady(pod *v1.Pod) bool {
condition := getPodReadyCondition(&pod.Status)
return condition != nil && condition.Status == v1.ConditionTrue
}
func getPodReadyCondition(status *v1.PodStatus) *v1.PodCondition {
for i := range status.Conditions {
if status.Conditions[i].Type == v1.PodReady {
return &status.Conditions[i]
}
}
return nil
}
func PodSpecToPrettyJSON(pod *v1.Pod) (string, error) {
bytes, err := json.MarshalIndent(pod.Spec, "", " ")
if err != nil {
return "", err
}
return string(bytes), nil
}