/
utils.go
172 lines (148 loc) · 4.25 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
// 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 spec
package e2e
import (
"fmt"
"os/exec"
"strings"
"time"
. "github.com/onsi/ginkgo" // revive:disable:dot-imports
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
"github.com/pingcap/tidb-operator/pkg/label"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)
const (
ns = "tidb-cluster-e2e"
helmName = "tidb-cluster-e2e"
operatorNs = "tidb-operator-e2e"
operatorHelmName = "tidb-operator-e2e"
clusterName = "demo-cluster"
testTableName = "demo_table"
testTableVal = "demo_val"
)
var (
cli versioned.Interface
kubeCli kubernetes.Interface
)
func clearOperator() error {
_, err := execCmd(fmt.Sprintf("helm del --purge %s", helmName))
if err != nil && isNotFound(err) {
return err
}
_, err = execCmd(fmt.Sprintf("kubectl delete pvc -n %s --all", ns))
if err != nil {
return err
}
_, err = execCmd(fmt.Sprintf("helm del --purge %s", operatorHelmName))
if err != nil && isNotFound(err) {
return err
}
err = wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
result, err := execCmd(fmt.Sprintf("kubectl get po --output=name -n %s", ns))
if err != nil || result != "" {
return false, nil
}
_, err = execCmd(fmt.Sprintf(`kubectl get pv -l %s=%s,%s=%s --output=name | xargs -I {} \
kubectl patch {} -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'`,
label.NamespaceLabelKey, ns, label.InstanceLabelKey, clusterName))
if err != nil {
logf(err.Error())
}
result, _ = execCmd(fmt.Sprintf("kubectl get pv -l %s=%s,%s=%s 2>/dev/null|grep Released", label.NamespaceLabelKey, ns, label.InstanceLabelKey, clusterName))
if result != "" {
return false, nil
}
return true, nil
})
if err != nil {
return err
}
return nil
}
func installOperator() error {
_, err := execCmd(fmt.Sprintf(
"helm install /charts/tidb-operator -f /tidb-operator-values.yaml -n %s --namespace=%s",
operatorHelmName,
operatorNs))
if err != nil {
return err
}
By("When create a TiDB cluster")
_, err = execCmd(fmt.Sprintf(
"helm install /charts/tidb-cluster -f /tidb-cluster-values.yaml -n %s --namespace=%s",
helmName,
ns))
if err != nil {
return err
}
monitorRestartCount()
return nil
}
func monitorRestartCount() {
maxRestartCount := int32(3)
go func() {
defer GinkgoRecover()
for {
select {
case <-time.After(5 * time.Second):
labelSelector := label.New().Cluster(clusterName)
podList, err := kubeCli.CoreV1().Pods(ns).List(
metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(
labelSelector.Labels(),
).String(),
},
)
if err != nil {
continue
}
for _, pod := range podList.Items {
for _, cs := range pod.Status.ContainerStatuses {
if cs.RestartCount > maxRestartCount {
Fail(fmt.Sprintf("POD: %s/%s's container: %s's restartCount is greater than: %d",
pod.GetNamespace(), pod.GetName(), cs.Name, maxRestartCount))
return
}
}
}
}
}
}()
}
func execCmd(cmdStr string) (string, error) {
logf(fmt.Sprintf("$ %s\n", cmdStr))
result, err := exec.Command("/bin/sh", "-c", cmdStr).CombinedOutput()
resultStr := string(result)
logf(resultStr)
if err != nil {
logf(err.Error())
return resultStr, err
}
return resultStr, nil
}
func nowStamp() string {
return time.Now().Format(time.StampMilli)
}
func log(level string, format string, args ...interface{}) {
fmt.Fprintf(GinkgoWriter, nowStamp()+": "+level+": "+format+"\n", args...)
}
// logf log a message in INFO format
func logf(format string, args ...interface{}) {
log("INFO", format, args...)
}
func isNotFound(err error) bool {
return strings.Contains(err.Error(), "not found")
}