forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_auto_repairs.go
166 lines (147 loc) · 5.65 KB
/
node_auto_repairs.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
/*
Copyright 2017 The Kubernetes 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 lifecycle
import (
"fmt"
"os/exec"
"strings"
"time"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
"github.com/golang/glog"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const (
defaultTimeout = 3 * time.Minute
repairTimeout = 20 * time.Minute
)
var _ = SIGDescribe("Node Auto Repairs [Slow] [Disruptive]", func() {
f := framework.NewDefaultFramework("lifecycle")
var c clientset.Interface
var originalNodes map[string]string
var nodeCount int
BeforeEach(func() {
framework.SkipUnlessProviderIs("gke")
c = f.ClientSet
nodeCount = 0
originalNodes = make(map[string]string)
for _, groupName := range strings.Split(framework.TestContext.CloudConfig.NodeInstanceGroup, ",") {
glog.Infof("Processing group %s", groupName)
nodes, err := framework.GetGroupNodes(groupName)
framework.ExpectNoError(err)
for _, node := range nodes {
nodeReady, err := isNodeReady(c, node)
framework.ExpectNoError(err)
Expect(nodeReady).To(Equal(true))
originalNodes[groupName] = node
nodeCount++
}
}
glog.Infof("Number of nodes %d", nodeCount)
})
AfterEach(func() {
framework.SkipUnlessProviderIs("gke")
By(fmt.Sprintf("Restoring initial size of the cluster"))
for groupName, nodeName := range originalNodes {
nodeReady, err := isNodeReady(c, nodeName)
framework.ExpectNoError(err)
if !nodeReady {
framework.ExpectNoError(recreateNode(nodeName, groupName))
}
}
framework.ExpectNoError(framework.WaitForAllNodesSchedulable(c, defaultTimeout))
})
It("should repair node [Feature:NodeAutoRepairs]", func() {
framework.SkipUnlessProviderIs("gke")
framework.ExpectNoError(enableAutoRepair("default-pool"))
defer disableAutoRepair("default-pool")
readyNodes := getReadyNodes(c)
Expect(len(readyNodes)).NotTo(Equal(0))
nodeName := readyNodes[0].Name
framework.ExpectNoError(stopKubeletOnNode(nodeName))
By("Wait till node is unready.")
Expect(framework.WaitForNodeToBeNotReady(c, nodeName, defaultTimeout)).To(Equal(true))
By("Wait till node is repaired.")
Expect(framework.WaitForNodeToBeReady(c, nodeName, repairTimeout)).To(Equal(true))
})
})
func execCmd(args ...string) *exec.Cmd {
glog.Infof("Executing: %s", strings.Join(args, " "))
return exec.Command(args[0], args[1:]...)
}
func getReadyNodes(c clientset.Interface) []v1.Node {
nodeList := framework.GetReadySchedulableNodesOrDie(c)
return nodeList.Items
}
func enableAutoRepair(nodePool string) error {
glog.Infof("Using gcloud to enable auto repair for pool %s", nodePool)
output, err := execCmd("gcloud", "beta", "container", "node-pools", "update", nodePool,
"--enable-autorepair",
"--project="+framework.TestContext.CloudConfig.ProjectID,
"--zone="+framework.TestContext.CloudConfig.Zone,
"--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput()
if err != nil {
glog.Errorf("Failed to enable auto repair: %s", string(output))
return fmt.Errorf("failed to enable auto repair: %v", err)
}
return nil
}
func disableAutoRepair(nodePool string) error {
glog.Infof("Using gcloud to disable auto repair for pool %s", nodePool)
output, err := execCmd("gcloud", "beta", "container", "node-pools", "update", nodePool,
"--no-enable-autorepair",
"--project="+framework.TestContext.CloudConfig.ProjectID,
"--zone="+framework.TestContext.CloudConfig.Zone,
"--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput()
if err != nil {
glog.Errorf("Failed to disable auto repair: %s", string(output))
return fmt.Errorf("failed to disable auto repair: %v", err)
}
return nil
}
func stopKubeletOnNode(node string) error {
glog.Infof("Using gcloud to stop Kublet on node %s", node)
output, err := execCmd("gcloud", "compute", "ssh", node,
"--command=sudo systemctl stop kubelet-monitor.service && sudo systemctl stop kubelet.service",
"--zone="+framework.TestContext.CloudConfig.Zone).CombinedOutput()
if err != nil {
glog.Errorf("Failed to stop Kubelet: %v", string(output))
return fmt.Errorf("failed to stop Kubelet: %v", err)
}
return nil
}
func recreateNode(nodeName string, groupName string) error {
glog.Infof("Using gcloud to recreate node %s", nodeName)
//gcloud compute instance-groups managed recreate-instances gke-oneoff-default-pool-e4383993-grp --instances=gke-oneoff-default-pool-e4383993-81jm --zone=us-central1-c
output, err := execCmd("gcloud", "compute", "instance-groups", "managed", "recreate-instances", groupName,
"--instances="+nodeName,
"--zone="+framework.TestContext.CloudConfig.Zone).CombinedOutput()
if err != nil {
glog.Errorf("Failed to recreate node: %s", string(output))
return fmt.Errorf("failed to recreate node: %v", err)
}
return nil
}
func isNodeReady(c clientset.Interface, nodeName string) (bool, error) {
glog.Infof("Check if node %s is ready ", nodeName)
node, err := c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
if err != nil {
return false, err
}
result := framework.IsNodeConditionSetAsExpected(node, v1.NodeReady, true)
glog.Infof("Node %s is ready: %t", nodeName, result)
return result, nil
}