forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_proxy_migration.go
220 lines (177 loc) · 7.31 KB
/
kube_proxy_migration.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
215
216
217
218
219
220
/*
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 upgrades
import (
"fmt"
"time"
apps "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const (
defaultTestTimeout = time.Duration(5 * time.Minute)
clusterAddonLabelKey = "k8s-app"
clusterComponentKey = "component"
kubeProxyLabelName = "kube-proxy"
)
// KubeProxyUpgradeTest tests kube-proxy static pods -> DaemonSet upgrade path.
type KubeProxyUpgradeTest struct {
}
func (KubeProxyUpgradeTest) Name() string { return "[sig-network] kube-proxy-upgrade" }
// Setup verifies kube-proxy static pods is running before uprgade.
func (t *KubeProxyUpgradeTest) Setup(f *framework.Framework) {
By("Waiting for kube-proxy static pods running and ready")
Expect(waitForKubeProxyStaticPodsRunning(f.ClientSet)).NotTo(HaveOccurred())
}
// Test validates if kube-proxy is migrated from static pods to DaemonSet.
func (t *KubeProxyUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
c := f.ClientSet
// Block until upgrade is done.
By("Waiting for upgrade to finish")
<-done
By("Waiting for kube-proxy static pods disappear")
Expect(waitForKubeProxyStaticPodsDisappear(c)).NotTo(HaveOccurred())
By("Waiting for kube-proxy DaemonSet running and ready")
Expect(waitForKubeProxyDaemonSetRunning(c)).NotTo(HaveOccurred())
}
// Teardown does nothing.
func (t *KubeProxyUpgradeTest) Teardown(f *framework.Framework) {
}
// KubeProxyDowngradeTest tests kube-proxy DaemonSet -> static pods downgrade path.
type KubeProxyDowngradeTest struct {
}
func (KubeProxyDowngradeTest) Name() string { return "[sig-network] kube-proxy-downgrade" }
// Setup verifies kube-proxy DaemonSet is running before uprgade.
func (t *KubeProxyDowngradeTest) Setup(f *framework.Framework) {
By("Waiting for kube-proxy DaemonSet running and ready")
Expect(waitForKubeProxyDaemonSetRunning(f.ClientSet)).NotTo(HaveOccurred())
}
// Test validates if kube-proxy is migrated from DaemonSet to static pods.
func (t *KubeProxyDowngradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
c := f.ClientSet
// Block until upgrade is done.
By("Waiting for upgrade to finish")
<-done
By("Waiting for kube-proxy DaemonSet disappear")
Expect(waitForKubeProxyDaemonSetDisappear(c)).NotTo(HaveOccurred())
By("Waiting for kube-proxy static pods running and ready")
Expect(waitForKubeProxyStaticPodsRunning(c)).NotTo(HaveOccurred())
}
// Teardown does nothing.
func (t *KubeProxyDowngradeTest) Teardown(f *framework.Framework) {
}
func waitForKubeProxyStaticPodsRunning(c clientset.Interface) error {
framework.Logf("Waiting up to %v for kube-proxy static pods running", defaultTestTimeout)
condition := func() (bool, error) {
pods, err := getKubeProxyStaticPods(c)
if err != nil {
framework.Logf("Failed to get kube-proxy static pods: %v", err)
return false, nil
}
numberSchedulableNodes := len(framework.GetReadySchedulableNodesOrDie(c).Items)
numberkubeProxyPods := 0
for _, pod := range pods.Items {
if pod.Status.Phase == v1.PodRunning {
numberkubeProxyPods = numberkubeProxyPods + 1
}
}
if numberkubeProxyPods != numberSchedulableNodes {
framework.Logf("Expect %v kube-proxy static pods running, got %v running, %v in total", numberSchedulableNodes, numberkubeProxyPods, len(pods.Items))
return false, nil
}
return true, nil
}
if err := wait.PollImmediate(5*time.Second, defaultTestTimeout, condition); err != nil {
return fmt.Errorf("error waiting for kube-proxy static pods running: %v", err)
}
return nil
}
func waitForKubeProxyStaticPodsDisappear(c clientset.Interface) error {
framework.Logf("Waiting up to %v for kube-proxy static pods disappear", defaultTestTimeout)
condition := func() (bool, error) {
pods, err := getKubeProxyStaticPods(c)
if err != nil {
framework.Logf("Failed to get kube-proxy static pods: %v", err)
return false, nil
}
if len(pods.Items) != 0 {
framework.Logf("Expect kube-proxy static pods to disappear, got %v pods", len(pods.Items))
return false, nil
}
return true, nil
}
if err := wait.PollImmediate(5*time.Second, defaultTestTimeout, condition); err != nil {
return fmt.Errorf("error waiting for kube-proxy static pods disappear: %v", err)
}
return nil
}
func waitForKubeProxyDaemonSetRunning(c clientset.Interface) error {
framework.Logf("Waiting up to %v for kube-proxy DaemonSet running", defaultTestTimeout)
condition := func() (bool, error) {
daemonSets, err := getKubeProxyDaemonSet(c)
if err != nil {
framework.Logf("Failed to get kube-proxy DaemonSet: %v", err)
return false, nil
}
if len(daemonSets.Items) != 1 {
framework.Logf("Expect only one kube-proxy DaemonSet, got %v", len(daemonSets.Items))
return false, nil
}
numberSchedulableNodes := len(framework.GetReadySchedulableNodesOrDie(c).Items)
numberkubeProxyPods := int(daemonSets.Items[0].Status.NumberAvailable)
if numberkubeProxyPods != numberSchedulableNodes {
framework.Logf("Expect %v kube-proxy DaemonSet pods running, got %v", numberSchedulableNodes, numberkubeProxyPods)
return false, nil
}
return true, nil
}
if err := wait.PollImmediate(5*time.Second, defaultTestTimeout, condition); err != nil {
return fmt.Errorf("error waiting for kube-proxy DaemonSet running: %v", err)
}
return nil
}
func waitForKubeProxyDaemonSetDisappear(c clientset.Interface) error {
framework.Logf("Waiting up to %v for kube-proxy DaemonSet disappear", defaultTestTimeout)
condition := func() (bool, error) {
daemonSets, err := getKubeProxyDaemonSet(c)
if err != nil {
framework.Logf("Failed to get kube-proxy DaemonSet: %v", err)
return false, nil
}
if len(daemonSets.Items) != 0 {
framework.Logf("Expect kube-proxy DaemonSet to disappear, got %v DaemonSet", len(daemonSets.Items))
return false, nil
}
return true, nil
}
if err := wait.PollImmediate(5*time.Second, defaultTestTimeout, condition); err != nil {
return fmt.Errorf("error waiting for kube-proxy DaemonSet disappear: %v", err)
}
return nil
}
func getKubeProxyStaticPods(c clientset.Interface) (*v1.PodList, error) {
label := labels.SelectorFromSet(labels.Set(map[string]string{clusterComponentKey: kubeProxyLabelName}))
listOpts := metav1.ListOptions{LabelSelector: label.String()}
return c.CoreV1().Pods(metav1.NamespaceSystem).List(listOpts)
}
func getKubeProxyDaemonSet(c clientset.Interface) (*apps.DaemonSetList, error) {
label := labels.SelectorFromSet(labels.Set(map[string]string{clusterAddonLabelKey: kubeProxyLabelName}))
listOpts := metav1.ListOptions{LabelSelector: label.String()}
return c.AppsV1().DaemonSets(metav1.NamespaceSystem).List(listOpts)
}