-
Notifications
You must be signed in to change notification settings - Fork 407
/
revert.go
352 lines (309 loc) · 12.5 KB
/
revert.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright 2020 The OpenYurt 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 revert
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
nodeutil "k8s.io/kubernetes/pkg/controller/util/node"
"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/openyurtio/openyurt/pkg/yurtctl/constants"
"github.com/openyurtio/openyurt/pkg/yurtctl/lock"
enutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/edgenode"
kubeutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/kubernetes"
strutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/strings"
)
// RevertOptions has the information required by the revert operation
type RevertOptions struct {
clientSet *kubernetes.Clientset
YurtctlServantImage string
PodMainfestPath string
KubeadmConfPath string
}
// NewConvertOptions creates a new RevertOptions
func NewRevertOptions() *RevertOptions {
return &RevertOptions{}
}
// NewRevertCmd generates a new revert command
func NewRevertCmd() *cobra.Command {
ro := NewRevertOptions()
cmd := &cobra.Command{
Use: "revert",
Short: "Reverts the yurt cluster back to a Kubernetes cluster",
Run: func(cmd *cobra.Command, _ []string) {
if err := ro.Complete(cmd.Flags()); err != nil {
klog.Fatalf("fail to complete the revert option: %s", err)
}
if err := ro.RunRevert(); err != nil {
klog.Fatalf("fail to revert yurt to kubernetes: %s", err)
}
},
}
cmd.AddCommand(NewRevertEdgeNodeCmd())
cmd.Flags().String("yurtctl-servant-image",
"openyurt/yurtctl-servant:latest",
"The yurtctl-servant image.")
cmd.Flags().String("kubeadm-conf-path",
"/etc/systemd/system/kubelet.service.d/10-kubeadm.conf",
"The path to kubelet service conf that is used by kubelet component to join the cluster on the edge node.")
return cmd
}
// Complete completes all the required options
func (ro *RevertOptions) Complete(flags *pflag.FlagSet) error {
ycsi, err := flags.GetString("yurtctl-servant-image")
if err != nil {
return err
}
ro.YurtctlServantImage = ycsi
kcp, err := flags.GetString("kubeadm-conf-path")
if err != nil {
return err
}
ro.KubeadmConfPath = kcp
podManifestPath, err := enutil.GetPodManifestPath(ro.KubeadmConfPath)
if err != nil {
return err
}
ro.PodMainfestPath = podManifestPath
ro.clientSet, err = kubeutil.GenClientSet(flags)
if err != nil {
return err
}
return nil
}
// RunRevert reverts the target Yurt cluster back to a standard Kubernetes cluster
func (ro *RevertOptions) RunRevert() (err error) {
if err = lock.AcquireLock(ro.clientSet); err != nil {
return
}
defer func() {
if releaseLockErr := lock.ReleaseLock(ro.clientSet); releaseLockErr != nil {
klog.Error(releaseLockErr)
}
}()
klog.V(4).Info("successfully acquire the lock")
// 1. check the server version
if err = kubeutil.ValidateServerVersion(ro.clientSet); err != nil {
return
}
klog.V(4).Info("the server version is valid")
// 1.1. get kube-controller-manager HA nodes
kcmNodeNames, err := kubeutil.GetKubeControllerManagerHANodes(ro.clientSet)
if err != nil {
return
}
// 1.2. check the state of worker nodes
nodeLst, err := ro.clientSet.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
return
}
for _, node := range nodeLst.Items {
isEdgeNode, ok := node.Labels[projectinfo.GetEdgeWorkerLabelKey()]
if ok && isEdgeNode == "true" || strutil.IsInStringLst(kcmNodeNames, node.GetName()) {
_, condition := nodeutil.GetNodeCondition(&node.Status, v1.NodeReady)
if condition == nil || condition.Status != v1.ConditionTrue {
klog.Errorf("Cannot do the revert, the status of worker or kube-controller-manager node: %s is not 'Ready'.", node.Name)
return
}
}
}
klog.V(4).Info("the status of worker nodes and kube-controller-manager nodes are satisfied")
// 2. remove labels from nodes
var edgeNodeNames []string
for _, node := range nodeLst.Items {
isEdgeNode, ok := node.Labels[projectinfo.GetEdgeWorkerLabelKey()]
if ok && isEdgeNode == "true" {
// cache edge nodes, we need to run servant job on each edge node later
edgeNodeNames = append(edgeNodeNames, node.GetName())
}
if ok && isEdgeNode == "false" {
// remove the label for both the cloud node
delete(node.Labels, projectinfo.GetEdgeWorkerLabelKey())
if _, err = ro.clientSet.CoreV1().Nodes().Update(context.Background(), &node, metav1.UpdateOptions{}); err != nil {
return
}
}
}
klog.Info("label openyurt.io/is-edge-worker is removed")
// 3. remove the yurt controller manager
if err = ro.clientSet.AppsV1().Deployments("kube-system").
Delete(context.Background(), "yurt-controller-manager", metav1.DeleteOptions{
PropagationPolicy: &kubeutil.PropagationPolicy,
}); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("fail to remove yurt controller manager: %s", err)
return
}
klog.Info("yurt controller manager is removed")
// 3.1 remove the serviceaccount for yurt-controller-manager
if err = ro.clientSet.CoreV1().ServiceAccounts("kube-system").
Delete(context.Background(), "yurt-controller-manager", metav1.DeleteOptions{
PropagationPolicy: &kubeutil.PropagationPolicy,
}); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("fail to remove serviceaccount for yurt controller manager: %s", err)
return
}
klog.Info("serviceaccount for yurt controller manager is removed")
// 3.2 remove the clusterrole for yurt-controller-manager
if err = ro.clientSet.RbacV1().ClusterRoles().
Delete(context.Background(), "yurt-controller-manager", metav1.DeleteOptions{
PropagationPolicy: &kubeutil.PropagationPolicy,
}); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("fail to remove clusterrole for yurt controller manager: %s", err)
return
}
klog.Info("clusterrole for yurt controller manager is removed")
// 3.3 remove the clusterrolebinding for yurt-controller-manager
if err = ro.clientSet.RbacV1().ClusterRoleBindings().
Delete(context.Background(), "yurt-controller-manager", metav1.DeleteOptions{
PropagationPolicy: &kubeutil.PropagationPolicy,
}); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("fail to remove clusterrolebinding for yurt controller manager: %s", err)
return
}
klog.Info("clusterrolebinding for yurt controller manager is removed")
// 4. remove the yurt-tunnel agent
if err = removeYurtTunnelAgent(ro.clientSet); err != nil {
klog.Errorf("fail to remove the yurt tunnel agent: %s", err)
return
}
// 5. remove the yurt-tunnel server
if err = removeYurtTunnelServer(ro.clientSet); err != nil {
klog.Errorf("fail to remove the yurt tunnel server: %s", err)
return
}
// 6. enable node-controller
if err = kubeutil.RunServantJobs(ro.clientSet,
map[string]string{
"action": "enable",
"yurtctl_servant_image": ro.YurtctlServantImage,
"pod_manifest_path": ro.PodMainfestPath,
},
kcmNodeNames); err != nil {
klog.Errorf("fail to run EnableNodeControllerJobs: %s", err)
return
}
klog.Info("complete enabling node-controller")
// 7. remove yurt-hub and revert kubelet service
if err = kubeutil.RunServantJobs(ro.clientSet,
map[string]string{
"action": "revert",
"yurtctl_servant_image": ro.YurtctlServantImage,
"kubeadm_conf_path": ro.KubeadmConfPath,
},
edgeNodeNames); err != nil {
klog.Errorf("fail to revert edge node: %s", err)
return
}
klog.Info("complete removing yurt-hub and resetting kubelet service")
// 7.1. remove yut-hub k8s config, roleBinding role
err = kubeutil.DeleteYurthubSetting(ro.clientSet)
if err != nil {
klog.Error("DeleteYurthubSetting err: ", err)
return err
}
klog.Info("delete yurthub clusterrole and clusterrolebinding")
return
}
func removeYurtTunnelServer(client *kubernetes.Clientset) error {
// 1. remove the DaemonSet
if err := client.AppsV1().
Deployments(constants.YurttunnelNamespace).
Delete(context.Background(), constants.YurttunnelServerComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the daemonset/%s: %s",
constants.YurttunnelServerComponentName, err)
}
klog.V(4).Infof("deployment/%s is deleted", constants.YurttunnelServerComponentName)
// 2.1 remove the Service
if err := client.CoreV1().Services(constants.YurttunnelNamespace).
Delete(context.Background(), constants.YurttunnelServerSvcName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the service/%s: %s",
constants.YurttunnelServerSvcName, err)
}
klog.V(4).Infof("service/%s is deleted", constants.YurttunnelServerSvcName)
// 2.2 remove the internal Service(type=ClusterIP)
if err := client.CoreV1().Services(constants.YurttunnelNamespace).
Delete(context.Background(), constants.YurttunnelServerInternalSvcName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the service/%s: %s",
constants.YurttunnelServerInternalSvcName, err)
}
klog.V(4).Infof("service/%s is deleted", constants.YurttunnelServerInternalSvcName)
// 3. remove the ClusterRoleBinding
if err := client.RbacV1().ClusterRoleBindings().
Delete(context.Background(), constants.YurttunnelServerComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the clusterrolebinding/%s: %s",
constants.YurttunnelServerComponentName, err)
}
klog.V(4).Infof("clusterrolebinding/%s is deleted", constants.YurttunnelServerComponentName)
// 4. remove the SerivceAccount
if err := client.CoreV1().ServiceAccounts(constants.YurttunnelNamespace).
Delete(context.Background(), constants.YurttunnelServerComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the serviceaccount/%s: %s",
constants.YurttunnelServerComponentName, err)
}
klog.V(4).Infof("serviceaccount/%s is deleted", constants.YurttunnelServerComponentName)
// 5. remove the ClusterRole
if err := client.RbacV1().ClusterRoles().
Delete(context.Background(), constants.YurttunnelServerComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the clusterrole/%s: %s",
constants.YurttunnelServerComponentName, err)
}
// 6. remove the ConfigMap
if err := client.CoreV1().ConfigMaps(constants.YurttunnelNamespace).
Delete(context.Background(), constants.YurttunnelServerCmName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the configmap/%s: %s",
constants.YurttunnelServerCmName, err)
}
klog.V(4).Infof("clusterrole/%s is deleted", constants.YurttunnelServerComponentName)
return nil
}
func removeYurtTunnelAgent(client *kubernetes.Clientset) error {
// 1. remove the DaemonSet
if err := client.AppsV1().
DaemonSets(constants.YurttunnelNamespace).
Delete(context.Background(), constants.YurttunnelAgentComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the daemonset/%s: %s",
constants.YurttunnelAgentComponentName, err)
}
klog.V(4).Infof("daemonset/%s is deleted", constants.YurttunnelAgentComponentName)
// 2. remove the ClusterRoleBinding
if err := client.RbacV1().ClusterRoleBindings().
Delete(context.Background(), constants.YurttunnelAgentComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the clusterrolebinding/%s: %s",
constants.YurttunnelAgentComponentName, err)
}
klog.V(4).Infof("clusterrolebinding/%s is deleted", constants.YurttunnelAgentComponentName)
// 3. remove the ClusterRole
if err := client.RbacV1().ClusterRoles().
Delete(context.Background(), constants.YurttunnelAgentComponentName,
metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("fail to delete the clusterrole/%s: %s",
constants.YurttunnelAgentComponentName, err)
}
klog.V(4).Infof("clusterrole/%s is deleted", constants.YurttunnelAgentComponentName)
return nil
}