forked from pingcap/tidb-operator
/
utils.go
214 lines (191 loc) · 7.13 KB
/
utils.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
206
207
208
209
210
211
212
213
214
// Copyright 2018 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package member
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"github.com/pingcap/tidb-operator/pkg/controller"
apps "k8s.io/api/apps/v1beta1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
)
const (
// LastAppliedConfigAnnotation is annotation key of last applied configuration
LastAppliedConfigAnnotation = "pingcap.com/last-applied-configuration"
// ImagePullBackOff is the pod state of image pull failed
ImagePullBackOff = "ImagePullBackOff"
// ErrImagePull is the pod state of image pull failed
ErrImagePull = "ErrImagePull"
)
func timezoneMountVolume() (corev1.VolumeMount, corev1.Volume) {
return corev1.VolumeMount{Name: "timezone", MountPath: "/etc/localtime", ReadOnly: true},
corev1.Volume{
Name: "timezone",
VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: "/etc/localtime"}},
}
}
func annotationsMountVolume() (corev1.VolumeMount, corev1.Volume) {
m := corev1.VolumeMount{Name: "annotations", ReadOnly: true, MountPath: "/etc/podinfo"}
v := corev1.Volume{
Name: "annotations",
VolumeSource: corev1.VolumeSource{
DownwardAPI: &corev1.DownwardAPIVolumeSource{
Items: []corev1.DownwardAPIVolumeFile{
{
Path: "annotations",
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.annotations"},
},
},
},
},
}
return m, v
}
// statefulSetIsUpgrading confirms whether the statefulSet is upgrading phase
func statefulSetIsUpgrading(set *apps.StatefulSet) bool {
if set.Status.ObservedGeneration == nil {
return false
}
if set.Status.CurrentRevision != set.Status.UpdateRevision {
return true
}
if set.Generation > *set.Status.ObservedGeneration && *set.Spec.Replicas == set.Status.Replicas {
return true
}
return false
}
// SetLastAppliedConfigAnnotation set last applied config info to Statefulset's annotation
func SetLastAppliedConfigAnnotation(set *apps.StatefulSet) error {
setApply, err := encode(set.Spec)
if err != nil {
return err
}
if set.Annotations == nil {
set.Annotations = map[string]string{}
}
set.Annotations[LastAppliedConfigAnnotation] = setApply
templateApply, err := encode(set.Spec.Template.Spec)
if err != nil {
return err
}
if set.Spec.Template.Annotations == nil {
set.Spec.Template.Annotations = map[string]string{}
}
set.Spec.Template.Annotations[LastAppliedConfigAnnotation] = templateApply
return nil
}
// GetLastAppliedConfig get last applied config info from Statefulset's annotation
func GetLastAppliedConfig(set *apps.StatefulSet) (*apps.StatefulSetSpec, *corev1.PodSpec, error) {
specAppliedConfig, ok := set.Annotations[LastAppliedConfigAnnotation]
if !ok {
return nil, nil, fmt.Errorf("statefulset:[%s/%s] not found spec's apply config", set.GetNamespace(), set.GetName())
}
spec := &apps.StatefulSetSpec{}
err := json.Unmarshal([]byte(specAppliedConfig), spec)
if err != nil {
return nil, nil, err
}
podSpecAppliedConfig, ok := set.Spec.Template.Annotations[LastAppliedConfigAnnotation]
if !ok {
return nil, nil, fmt.Errorf("statefulset:[%s/%s] not found template spec's apply config", set.GetNamespace(), set.GetName())
}
podSpec := &corev1.PodSpec{}
err = json.Unmarshal([]byte(podSpecAppliedConfig), podSpec)
if err != nil {
return nil, nil, err
}
return spec, podSpec, nil
}
func encode(obj interface{}) (string, error) {
b, err := json.Marshal(obj)
if err != nil {
return "", err
}
return string(b), nil
}
// statefulSetEqual compares the new Statefulset's spec with old Statefulset's last applied config
func statefulSetEqual(new apps.StatefulSet, old apps.StatefulSet) bool {
oldConfig := apps.StatefulSetSpec{}
if lastAppliedConfig, ok := old.Annotations[LastAppliedConfigAnnotation]; ok {
err := json.Unmarshal([]byte(lastAppliedConfig), &oldConfig)
if err != nil {
glog.Errorf("unmarshal Statefulset: [%s/%s]'s applied config failed,error: %v", old.GetNamespace(), old.GetName(), err)
return false
}
return apiequality.Semantic.DeepEqual(oldConfig.Replicas, new.Spec.Replicas) &&
apiequality.Semantic.DeepEqual(oldConfig.Template, new.Spec.Template) &&
apiequality.Semantic.DeepEqual(oldConfig.UpdateStrategy, new.Spec.UpdateStrategy)
}
return false
}
// templateEqual compares the new podTemplateSpec's spec with old podTemplateSpec's last applied config
func templateEqual(new corev1.PodTemplateSpec, old corev1.PodTemplateSpec) bool {
oldConfig := corev1.PodSpec{}
if lastAppliedConfig, ok := old.Annotations[LastAppliedConfigAnnotation]; ok {
err := json.Unmarshal([]byte(lastAppliedConfig), &oldConfig)
if err != nil {
glog.Errorf("unmarshal PodTemplate: [%s/%s]'s applied config failed,error: %v", old.GetNamespace(), old.GetName(), err)
return false
}
return apiequality.Semantic.DeepEqual(oldConfig, new.Spec)
}
return false
}
// SetServiceLastAppliedConfigAnnotation set last applied config info to Service's annotation
func SetServiceLastAppliedConfigAnnotation(svc *corev1.Service) error {
svcApply, err := encode(svc.Spec)
if err != nil {
return err
}
if svc.Annotations == nil {
svc.Annotations = map[string]string{}
}
svc.Annotations[LastAppliedConfigAnnotation] = svcApply
return nil
}
// serviceEqual compares the new Service's spec with old Service's last applied config
func serviceEqual(new, old *corev1.Service) (bool, error) {
oldSpec := corev1.ServiceSpec{}
if lastAppliedConfig, ok := old.Annotations[LastAppliedConfigAnnotation]; ok {
err := json.Unmarshal([]byte(lastAppliedConfig), &oldSpec)
if err != nil {
glog.Errorf("unmarshal ServiceSpec: [%s/%s]'s applied config failed,error: %v", old.GetNamespace(), old.GetName(), err)
return false, err
}
return apiequality.Semantic.DeepEqual(oldSpec, new.Spec), nil
}
return false, nil
}
// setUpgradePartition set statefulSet's rolling update partition
func setUpgradePartition(set *apps.StatefulSet, upgradeOrdinal int32) {
set.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateStatefulSetStrategy{Partition: &upgradeOrdinal}
}
func imagePullFailed(pod *corev1.Pod) bool {
for _, container := range pod.Status.ContainerStatuses {
if container.State.Waiting != nil && container.State.Waiting.Reason != "" &&
(container.State.Waiting.Reason == ImagePullBackOff || container.State.Waiting.Reason == ErrImagePull) {
return true
}
}
return false
}
func tikvPodName(tcName string, ordinal int32) string {
return fmt.Sprintf("%s-%d", controller.TiKVMemberName(tcName), ordinal)
}
func pdPodName(tcName string, ordinal int32) string {
return fmt.Sprintf("%s-%d", controller.PDMemberName(tcName), ordinal)
}
func tidbPodName(tcName string, ordinal int32) string {
return fmt.Sprintf("%s-%d", controller.TiDBMemberName(tcName), ordinal)
}