-
Notifications
You must be signed in to change notification settings - Fork 94
/
migration_controller.go
496 lines (427 loc) · 18.1 KB
/
migration_controller.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright (c) 2019-2020 Tigera, Inc. All rights reserved.
//
// 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 flannelmigration
import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/projectcalico/kube-controllers/pkg/controllers/controller"
client "github.com/projectcalico/libcalico-go/lib/clientv3"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
uruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
const (
namespaceKubeSystem = "kube-system"
migrationNodeSelectorKey = "projectcalico.org/node-network-during-migration"
migrationNodeInProgressKey = "projectcalico.org/node-flannel-migration-in-progress"
addOnManagerLabelKey = "addonmanager.kubernetes.io/mode"
canalDaemonsetName = "canal"
calicoConfigMapName = "calico-config"
calicoConfigMapMtuKey = "veth_mtu"
migrationConfigMapName = "flannel-migration-config"
migrationConfigMapEnvKey = "flannel_subnet_env"
flannelConfigFile = "run/flannel/subnet.env"
flannelContainerName = "kube-flannel"
)
// Flannel migration controller consists of three major components.
// IPAM Migrator who setups Calico IPAM based on Flannel network configurations.
// Network Migrator who removes Flannel vxlan data plane and allow Calico vxlan network to be setup on nodes.
// Main controller logic controls the entire migration process and handle new node events.
var (
// nodeNetworkFlannel is a map value indicates a node is still part of Flannel vxlan network.
// This is used both as a nodeSelector for Flannel daemonset and a label for a node.
nodeNetworkFlannel = map[string]string{migrationNodeSelectorKey: "flannel"}
// nodeNetworkCalico is a map value indicates a node is becoming part of Calico vxlan network.
// This is used both as a nodeSelector for Calico daemonset and a label for a node.
nodeNetworkCalico = map[string]string{migrationNodeSelectorKey: "calico"}
// nodeNetworkNone is a map value indicates there should be neither Flannel nor Calico running on the node.
nodeNetworkNone = map[string]string{migrationNodeSelectorKey: "none"}
// nodeMigrationInProgress is a map value indicates a node is running network migration.
nodeMigrationInProgress = map[string]string{migrationNodeInProgressKey: "true"}
// Possible Labels for Flannel daemonset pod.
flannelPodLabel = map[string]string{"app": "flannel", "k8s-app": "flannel"}
// Label for Canal daemonset pod.
canalPodLabel = map[string]string{"k8s-app": "canal"}
// Label for Calico daemonset pod.
calicoPodLabel = map[string]string{"k8s-app": "calico-node"}
)
// flannelMigrationController implements the Controller interface.
type flannelMigrationController struct {
ctx context.Context
informer cache.Controller
indexer cache.Indexer
calicoClient client.Interface
k8sClientset *kubernetes.Clientset
// ipamMigrator runs ipam migration process.
ipamMigrator ipamMigrator
// networkMigrator runs network migration process.
networkMigrator *networkMigrator
// List of nodes need to be migrated.
flannelNodes []*v1.Node
// Configurations for migration controller.
config *Config
}
// NewFlannelMigrationController Constructor for Flannel migration controller
func NewFlannelMigrationController(ctx context.Context, k8sClientset *kubernetes.Clientset, calicoClient client.Interface, cfg *Config) controller.Controller {
mc := &flannelMigrationController{
ctx: ctx,
calicoClient: calicoClient,
k8sClientset: k8sClientset,
ipamMigrator: NewIPAMMigrator(ctx, k8sClientset, calicoClient, cfg),
networkMigrator: NewNetworkMigrator(ctx, k8sClientset, calicoClient, cfg),
config: cfg,
}
// Create a Node watcher.
listWatcher := cache.NewListWatchFromClient(k8sClientset.CoreV1().RESTClient(), "nodes", "", fields.Everything())
// Setup event handlers
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
mc.processNewNode(obj.(*v1.Node))
},
}
// Informer handles managing the watch and signals us when nodes are added.
mc.indexer, mc.informer = cache.NewIndexerInformer(listWatcher, &v1.Node{}, 0, handlers, cache.Indexers{})
return mc
}
// Wait before start.
func (c *flannelMigrationController) waitBeforeStart() {
if c.config.DebugWaitBeforeStart > 0 {
time.Sleep(time.Duration(c.config.DebugWaitBeforeStart) * time.Second)
}
}
// Wait before exit.
func (c *flannelMigrationController) waitBeforeExit() {
if c.config.DebugWaitBeforeExit > 0 {
time.Sleep(time.Duration(c.config.DebugWaitBeforeExit) * time.Second)
}
}
// Handle error by simply exit 1. Allow controller to restart.
func (c *flannelMigrationController) HandleError(err error) {
c.waitBeforeExit()
log.Fatalf("Migration controller stopped.")
}
// Migration Completed. Stop controller.
func (c *flannelMigrationController) StopController(msg string) {
log.Infof("%s", msg)
c.waitBeforeExit()
os.Exit(0)
}
// Run starts the migration controller. It does start-of-day preparation
// and then run entire migration process.
func (c *flannelMigrationController) Run(stopCh chan struct{}) {
defer uruntime.HandleCrash()
c.waitBeforeStart()
log.Info("Starting Migration controller")
// Check the status of the cluster to see if we need to migrate.
shouldMigrate, err := c.CheckShouldMigrate()
if err != nil {
log.WithError(err).Errorf("Error checking status, Stopping Migration controller.")
c.HandleError(err)
}
if !shouldMigrate {
c.StopController("No migration needed. Stopping migration controller.")
}
// Start migration process.
// Initialise Calico IPAM before we handle any nodes.
err = c.ipamMigrator.InitialiseIPPoolAndFelixConfig()
if err != nil {
log.WithError(err).Errorf("Error initialising default ipool and Felix configuration.")
c.HandleError(err)
}
// Wait till k8s cache is synced
go c.informer.Run(stopCh)
log.Infof("Waiting to sync with Kubernetes API (Nodes)")
for !c.informer.HasSynced() {
time.Sleep(100 * time.Millisecond)
}
log.Infof("Finished syncing with Kubernetes API (Nodes)")
// Run IPAM migration. Get list of nodes need to be migrated.
c.flannelNodes, err = c.runIpamMigrationForNodes()
if err != nil {
log.WithError(err).Errorf("Error running ipam migration.")
c.HandleError(err)
}
// Add node selector "projectcalico.org/node-network-during-migration==flannel" to Flannel daemonset.
// This would prevent Flannel pod running on any new nodes or a node which has been migrated to Calico network.
d := daemonset(c.config.FlannelDaemonsetName)
err = d.AddNodeSelector(c.k8sClientset, namespaceKubeSystem, nodeNetworkFlannel)
if err != nil {
log.WithError(err).Errorf("Error adding node selector to Flannel daemonset.")
c.HandleError(err)
}
// Start network migration.
err = c.runNetworkMigrationForNodes()
if err != nil {
log.WithError(err).Errorf("Error running network migration.")
c.HandleError(err)
}
// Complete migration process.
err = c.completeMigration()
if err != nil {
log.WithError(err).Errorf("Error completing migration.")
c.HandleError(err)
}
c.StopController("All done. Stopping Migration controller")
}
// For new node, setup Calico IPAM based on node pod CIDR and update node selector.
// This makes sure a new node get Calico installed in the middle of migration process.
func (c *flannelMigrationController) processNewNode(node *v1.Node) {
// Do not process any new node unless existing nodes been processed.
for len(c.flannelNodes) == 0 {
log.Debugf("New node %s skipped.", node.Name)
return
}
// Defensively check node label again to make sure the node has not been processed by anyone.
_, err := getNodeLabelValue(node, migrationNodeSelectorKey)
if err == nil {
// Node got label already. Skip it.
log.Infof("New node %s has been processed.", node.Name)
return
}
log.Infof("Start processing new node %s.", node.Name)
err = c.ipamMigrator.SetupCalicoIPAMForNode(node)
if err != nil {
log.WithError(err).Infof("Error running ipam migration for new node %s. This node has not got Flannel yet. Just need restart to handle it.", node.Name)
log.Fatal("Migration controller will restart and continue...")
return
}
n := k8snode(node.Name)
err = n.addNodeLabels(c.k8sClientset, nodeNetworkCalico)
if err != nil {
log.WithError(err).Fatalf("Error adding node label to enable Calico network for new node %s.", node.Name)
return
}
log.Infof("Complete processing new node %s.", node.Name)
}
// Get Flannel config from subnet.env file by kubectl exec into Flannel/Canal daemonset pod.
// Once a valid config has been read, migration controller need to update migration config map.
// This is because if we just rely on getting config from Flannel/Canal daemonset pod, there are
// chances that at the final stage of migration, all Flannel/Canal daemonset pod has been deleted
// but at the same time migration controller restart itself.
func (c *flannelMigrationController) readAndUpdateFlannelEnvConfig() error {
// Work out the Flannel config by kubectl exec into daemonset pod on controller node.
log.Infof("Trying to read Flannel env config by executing into daemonet pod.")
var podLabel map[string]string
if c.config.IsRunningCanal() {
podLabel = canalPodLabel
} else {
podLabel = flannelPodLabel
}
n := k8snode(c.config.PodNodeName)
data, err := n.execCommandInPod(c.k8sClientset, namespaceKubeSystem, flannelContainerName,
podLabel, "cat", flannelConfigFile)
if err != nil {
return err
}
if err = c.config.ReadFlannelConfig(data); err != nil {
return err
}
if err = c.config.ValidateFlannelConfig(); err != nil {
return err
}
log.WithField("flannelConfig", c.config).Info("Flannel env config parsed successfully.")
// Convert subnet.env content to json string and update flannel-migration-config ConfigMap.
// So that it could be populated into migration controller pod next time it starts.
val := strings.Replace(data, "\n", ";", -1)
err = updateConfigMapValue(c.k8sClientset, namespaceKubeSystem, migrationConfigMapName, migrationConfigMapEnvKey, val)
if err != nil {
return err
}
log.Infof("Flannel subnet.env stored in migration config map: '%s'.", val)
return nil
}
// Check if controller should start migration process.
func (c *flannelMigrationController) CheckShouldMigrate() (bool, error) {
// Check if we are running Canal.
d := daemonset(canalDaemonsetName)
notFound, err := d.CheckNotExists(c.k8sClientset, namespaceKubeSystem)
if err != nil {
return false, err
}
if !notFound {
log.Info("Canal daemonset exists, we are migrating from Canal to Calico.")
c.config.FlannelDaemonsetName = canalDaemonsetName
}
// Check Flannel daemonset.
d = daemonset(c.config.FlannelDaemonsetName)
notFound, err = d.CheckNotExists(c.k8sClientset, namespaceKubeSystem)
if err != nil {
return false, err
}
if notFound {
log.Infof("Daemonset %s not exists, no migration process is needed.", c.config.FlannelDaemonsetName)
return false, nil
}
//Check if addon manager label exists
found, val, err := d.getLabelValue(c.k8sClientset, namespaceKubeSystem, addOnManagerLabelKey)
if err != nil {
return false, err
}
if found {
log.Infof("Daemonset %s got addon manager label set to %s, abort migration process.",
c.config.FlannelDaemonsetName, val)
return false, nil
}
// Check if we need to read and update Flannel subnet.env config.
if !c.config.subnetEnvPopulated() {
err = c.readAndUpdateFlannelEnvConfig()
if err != nil {
return false, err
}
}
// Update calico-config ConfigMap veth_mtu.
// So that it could be populated into calico-node pods.
err = updateConfigMapValue(c.k8sClientset, namespaceKubeSystem, calicoConfigMapName, calicoConfigMapMtuKey, fmt.Sprintf("%d", c.config.FlannelMTU))
if err != nil {
return false, err
}
// Initialise IPAM migrator.
err = c.ipamMigrator.Initialise()
if err != nil {
log.Info("IPAM migrator initialisation failed.")
return false, nil
}
// Initialise network migrator.
err = c.networkMigrator.Initialise()
if err != nil {
log.Info("Network migrator initialisation failed.")
return false, nil
}
return true, nil
}
// Start ipam migration.
// This is to make sure Calico IPAM has been setup for the entire cluster.
// Return a list of nodes which has been processed successfully and should move on to next stage.
// If there is any error, return empty list.
func (c *flannelMigrationController) runIpamMigrationForNodes() ([]*v1.Node, error) {
nodes := []*v1.Node{}
// A node can be in different migration status indicated by the value of labels
// "projectcalico.org/node-network-during-migration" (abbr. network) and "projectcalico.org/node-flannel-migration-in-progress" (abbr. in-progress)
// case 1. No label at all.
// This is the first time migration controller starts. The node is running Flannel.
// Or in rare cases, the node is a new node added between two separate migration processes. e.g. migration controller restarted.
// The controller will not try to distinguish these two scenarios. It regards the new node as if Flannel is running.
// This simplifies the main controller logic and increases robustness.
// case 2. network == flannel with no in-progress label. The node has been identified by previous migration process that Flannel is running.
// case 3. network == none and in-progress == true. The node has completed ipam migration. It started network migration process.
// case 4. network == calico and in-progress == true. The node got flannel network removed but has not completed migration process.
// case 5. network == calico with no in-progress label. The node is running Calico.
//
// The controller will start ipam and network migration for all cases except case 5.
// Work out list of nodes not running Calico. It could happen that all nodes are running Calico and it returns an empty list.
items := c.indexer.List()
var controllerNode *v1.Node
var masterNode *v1.Node
for _, obj := range items {
node := obj.(*v1.Node)
migrationInProgress, _ := getNodeLabelValue(node, migrationNodeInProgressKey)
network, _ := getNodeLabelValue(node, migrationNodeSelectorKey)
if network != "calico" || migrationInProgress == "true" {
if network != "calico" && network != "none" {
// Allow Flannel to run if the node is not starting to run Calico or Flannel network has been removed.
n := k8snode(node.Name)
if err := n.addNodeLabels(c.k8sClientset, nodeNetworkFlannel); err != nil {
log.WithError(err).Errorf("Error adding node label to node %s.", node.Name)
return []*v1.Node{}, err
}
}
addToList := true
// check if migration controller is running on this node.
// If it is, make sure it is the last node we try to process.
if node.Name == c.config.PodNodeName {
log.Infof("Migration controller is running on node %s.", node.Name)
controllerNode = node
addToList = false
}
// check if this node is master node.
// If it is, make sure it is the second last node we try to process.
_, err := getNodeLabelValue(node, "node-role.kubernetes.io/master")
if err == nil {
log.Infof("Master node is %s.", node.Name)
masterNode = node
addToList = false
}
if addToList {
nodes = append(nodes, node)
}
}
}
// Now we have a list of nodes which does not include master node and controllerNode.
// We need to sort the list so that, if controller failed and restarted, it will start
// to process the same node again. This is to prevent migration controller to migrate another
// node without addressing previous failure.
sort.SliceStable(nodes, func(i, j int) bool { return nodes[i].Name < nodes[j].Name })
if masterNode != nil {
log.Infof("Master node %s is last node to be migrated.", masterNode.Name)
nodes = append(nodes, masterNode)
}
if controllerNode != nil {
log.Infof("Controller node %s is last node to be migrated.", controllerNode.Name)
if controllerNode != masterNode {
nodes = append(nodes, controllerNode)
}
}
// At this point, any node would have a "projectcalico.org/node-network-during-migration" label.
// The value is either "flannel" or "calico".
// Start IPAM migration.
err := c.ipamMigrator.MigrateNodes(nodes)
if err != nil {
log.WithError(err).Errorf("Error running ipam migration for nodes.")
return nodes, err
}
return nodes, nil
}
// For each Flannel nodes, run network migration process.
func (c *flannelMigrationController) runNetworkMigrationForNodes() error {
return c.networkMigrator.MigrateNodes(c.flannelNodes)
}
// Complete migration process.
func (c *flannelMigrationController) completeMigration() error {
// Delete Flannel daemonset.
d := daemonset(c.config.FlannelDaemonsetName)
log.Infof("Start deleting %s daemonset.", c.config.FlannelDaemonsetName)
err := d.DeleteForeground(c.k8sClientset, namespaceKubeSystem)
if err != nil {
log.WithError(err).Errorf("Failed to delete Flannel daemonset.")
return err
}
log.Infof("Waiting for %s daemonset to disappear.", c.config.FlannelDaemonsetName)
err = d.WaitForDaemonsetNotFound(c.k8sClientset, namespaceKubeSystem, 1*time.Second, 5*time.Minute)
if err != nil {
log.WithError(err).Errorf("Timeout deleting Flannel daemonset.")
return err
}
// Remove nodeSelector for Calico Daemonet.
d = daemonset(c.config.CalicoDaemonsetName)
log.Infof("Remove node selector for daemonset %s.", c.config.CalicoDaemonsetName)
err = d.RemoveNodeSelector(c.k8sClientset, namespaceKubeSystem, nodeNetworkCalico)
if err != nil {
log.WithError(err).Errorf("Failed to remove node selector for daemonset %s.", c.config.CalicoDaemonsetName)
return err
}
// Remove node labels
err = removeLabelForAllNodes(migrationNodeSelectorKey)
if err != nil {
log.WithError(err).Errorf("Failed to remove node label %s.", migrationNodeSelectorKey)
return err
}
return nil
}