forked from tigera/operator
/
node.go
546 lines (508 loc) · 17.8 KB
/
node.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright (c) 2019 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 render
import (
operator "github.com/tigera/operator/pkg/apis/operator/v1"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
)
// Node creates the node daemonset and other resources for the daemonset to operate normally.
func Node(cr *operator.Installation, openshift bool) Component {
return &nodeComponent{cr: cr, openshift: openshift}
}
type nodeComponent struct {
cr *operator.Installation
openshift bool
}
func (c *nodeComponent) Objects() []runtime.Object {
return []runtime.Object{
c.nodeServiceAccount(),
c.nodeRole(),
c.nodeRoleBinding(),
c.nodeCNIConfigMap(),
c.nodeDaemonset(),
}
}
func (c *nodeComponent) Ready() bool {
return true
}
// nodeServiceAccount creates the node's service account.
func (c *nodeComponent) nodeServiceAccount() *v1.ServiceAccount {
return &v1.ServiceAccount{
TypeMeta: metav1.TypeMeta{Kind: "ServiceAccount", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "calico-node",
Namespace: calicoNamespace,
},
}
}
// nodeRoleBinding creates a clusterrolebinding giving the node service account the required permissions to operate.
func (c *nodeComponent) nodeRoleBinding() *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRoleBinding", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "calico-node",
Labels: map[string]string{},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: "calico-node",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: "calico-node",
Namespace: calicoNamespace,
},
},
}
}
// nodeRole creates the clusterrole containing policy rules that allow the node daemonset to operate normally.
func (c *nodeComponent) nodeRole() *rbacv1.ClusterRole {
role := &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRole", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "calico-node",
Labels: map[string]string{},
},
Rules: []rbacv1.PolicyRule{
{
// The CNI plugin needs to get pods, nodes, namespaces.
APIGroups: []string{""},
Resources: []string{"pods", "nodes", "namespaces"},
Verbs: []string{"get"},
},
{
// Used to discover Typha endpoints and service IPs for advertisement.
APIGroups: []string{""},
Resources: []string{"endpoints", "services"},
Verbs: []string{"watch", "list", "get"},
},
{
// Some information is stored on the node status.
APIGroups: []string{""},
Resources: []string{"nodes/status"},
Verbs: []string{"patch", "update"},
},
{
// For enforcing network policies.
APIGroups: []string{"networking.k8s.io"},
Resources: []string{"networkpolicies"},
Verbs: []string{"watch", "list"},
},
{
// Metadata from these are used in conjunction with network policy.
APIGroups: []string{""},
Resources: []string{"pods", "namespaces", "serviceaccounts"},
Verbs: []string{"watch", "list"},
},
{
// Calico patches the allocated IP onto the pod.
APIGroups: []string{""},
Resources: []string{"pods/status"},
Verbs: []string{"patch"},
},
{
// For monitoring Calico-specific configuration.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{
"bgpconfigurations",
"bgppeers",
"blockaffinities",
"clusterinformations",
"felixconfigurations",
"globalnetworkpolicies",
"globalnetworksets",
"hostendpoints",
"ipamblocks",
"ippools",
"networkpolicies",
"networksets",
},
Verbs: []string{"get", "list", "watch"},
},
{
// For migration code in calico/node startup only. Remove when the migration
// code is removed from node.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{
"globalbgpconfigs",
"globalfelixconfigs",
},
Verbs: []string{"get", "list", "watch"},
},
{
// Calico creates some configuration on startup.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{
"clusterinformations",
"felixconfigurations",
"ippools",
},
Verbs: []string{"create", "update"},
},
{
// Calico monitors nodes for some networking configuration.
APIGroups: []string{""},
Resources: []string{"nodes"},
Verbs: []string{"get", "list", "watch"},
},
{
// Most IPAM resources need full CRUD permissions so we can allocate and
// release IP addresses for pods.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{
"blockaffinities",
"ipamblocks",
"ipamhandles",
},
Verbs: []string{"get", "list", "create", "update", "delete"},
},
{
// But, we only need to be able to query for IPAM config.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{"ipamconfigs"},
Verbs: []string{"get"},
},
{
// confd (and in some cases, felix) watches block affinities for route aggregation.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{"blockaffinities"},
Verbs: []string{"watch"},
},
},
}
if c.cr.Spec.Variant == operator.TigeraSecureEnterprise {
extraRules := []rbacv1.PolicyRule{
{
// Tigera Secure needs to be able to read licenses, tiers, and config.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{
"licensekeys",
"remoteclusterconfigurations",
"tiers",
},
Verbs: []string{"get", "list", "watch"},
},
{
// Tigera Secure creates some tiers on startup.
APIGroups: []string{"crd.projectcalico.org"},
Resources: []string{
"tiers",
},
Verbs: []string{"create"},
},
}
role.Rules = append(role.Rules, extraRules...)
}
return role
}
// nodeCNIConfigMap returns a config map containing the CNI network config to be installed on each node.
func (c *nodeComponent) nodeCNIConfigMap() *v1.ConfigMap {
var config = `{
"name": "k8s-pod-network",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "calico",
"datastore_type": "kubernetes",
"mtu": 1440,
"ipam": {
"type": "calico-ipam"
},
"policy": {
"type": "k8s"
},
"kubernetes": {
"kubeconfig": "__KUBECONFIG_FILEPATH__"
}
},
{
"type": "portmap",
"snat": true,
"capabilities": {"portMappings": true}
}
]
}`
return &v1.ConfigMap{
TypeMeta: metav1.TypeMeta{Kind: "ConfigMap", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "cni-config",
Namespace: calicoNamespace,
Labels: map[string]string{},
},
Data: map[string]string{
"config": config,
},
}
}
// nodeDaemonset creates the node damonset.
func (c *nodeComponent) nodeDaemonset() *apps.DaemonSet {
var terminationGracePeriod int64 = 0
ds := apps.DaemonSet{
TypeMeta: metav1.TypeMeta{Kind: "DaemonSet", APIVersion: "apps/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "calico-node",
Namespace: calicoNamespace,
},
Spec: apps.DaemonSetSpec{
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k8s-app": "calico-node"}},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"k8s-app": "calico-node",
},
},
Spec: v1.PodSpec{
NodeSelector: map[string]string{},
Tolerations: c.nodeTolerations(),
ImagePullSecrets: c.cr.Spec.ImagePullSecrets,
ServiceAccountName: "calico-node",
TerminationGracePeriodSeconds: &terminationGracePeriod,
HostNetwork: true,
InitContainers: []v1.Container{c.cniContainer(), c.flexVolumeContainer()},
Containers: []v1.Container{c.nodeContainer()},
Volumes: c.nodeVolumes(),
},
},
UpdateStrategy: apps.DaemonSetUpdateStrategy{
RollingUpdate: &apps.RollingUpdateDaemonSet{
MaxUnavailable: c.cr.Spec.Components.Node.MaxUnavailable,
},
},
},
}
setCriticalPod(&(ds.Spec.Template))
return &ds
}
// nodeTolerations creates the node's tolerations.
func (c *nodeComponent) nodeTolerations() []v1.Toleration {
tolerations := []v1.Toleration{
{Operator: v1.TolerationOpExists, Effect: v1.TaintEffectNoSchedule},
{Operator: v1.TolerationOpExists, Effect: v1.TaintEffectNoExecute},
{Operator: v1.TolerationOpExists, Key: "CriticalAddonsOnly"},
}
// Merge in any user-supplied overrides.
tolerations = setCustomTolerations(tolerations, c.cr.Spec.Components.Node.Tolerations)
return tolerations
}
// nodeVolumes creates the node's volumes.
func (c *nodeComponent) nodeVolumes() []v1.Volume {
fileOrCreate := v1.HostPathFileOrCreate
dirOrCreate := v1.HostPathDirectoryOrCreate
volumes := []v1.Volume{
{Name: "lib-modules", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/lib/modules"}}},
{Name: "var-run-calico", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/var/run/calico"}}},
{Name: "var-lib-calico", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/var/lib/calico"}}},
{Name: "xtables-lock", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/run/xtables.lock", Type: &fileOrCreate}}},
{Name: "cni-bin-dir", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: c.cr.Spec.CNIBinDir}}},
{Name: "cni-net-dir", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: c.cr.Spec.CNINetDir}}},
{Name: "policysync", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/var/run/nodeagent", Type: &dirOrCreate}}},
}
// Override with Tigera-specific config.
if c.cr.Spec.Variant == operator.TigeraSecureEnterprise {
// Add volume for calico logs.
calicoLogVol := v1.Volume{
Name: "var-log-calico",
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/var/log/calico", Type: &dirOrCreate}},
}
volumes = append(volumes, calicoLogVol)
}
flexVolumePluginsPath := "/usr/libexec/kubernetes/kubelet-plugins/volume/exec/"
// In OpenShift 4.x, the location for flexvolume plugins has changed.
// See: https://bugzilla.redhat.com/show_bug.cgi?id=1667606#c5
if c.openshift {
flexVolumePluginsPath = "/etc/kubernetes/kubelet-plugins/volume/exec/"
}
// Create and append flexvolume
flexVolume := v1.Volume{
Name: "flexvol-driver-host",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{Path: flexVolumePluginsPath + "nodeagent~uds", Type: &dirOrCreate},
},
}
volumes = append(volumes, flexVolume)
volumes = setCustomVolumes(volumes, c.cr.Spec.Components.Node.ExtraVolumes)
volumes = setCustomVolumes(volumes, c.cr.Spec.Components.CNI.ExtraVolumes)
return volumes
}
// cniContainer creates the node's init container that installs CNI.
func (c *nodeComponent) cniContainer() v1.Container {
// Determine environment to pass to the CNI init container.
cniEnv := c.cniEnvvars()
cniVolumeMounts := []v1.VolumeMount{
{MountPath: "/host/opt/cni/bin", Name: "cni-bin-dir"},
{MountPath: "/host/etc/cni/net.d", Name: "cni-net-dir"},
}
// Merge in any user-supplied overrides.
cniEnv = setCustomEnv(cniEnv, c.cr.Spec.Components.CNI.ExtraEnv)
cniVolumeMounts = setCustomVolumeMounts(cniVolumeMounts, c.cr.Spec.Components.CNI.ExtraVolumeMounts)
return v1.Container{
Name: "install-cni",
Image: constructImage(CNIImageName, c.cr.Spec.Registry),
Command: []string{"/install-cni.sh"},
Env: cniEnv,
VolumeMounts: cniVolumeMounts,
}
}
// flexVolumeContainer creates the node's init container that installs the Unix Domain Socket to allow Dikastes
// to communicate with Felix over the Policy Sync API.
func (c *nodeComponent) flexVolumeContainer() v1.Container {
flexVolumeMounts := []v1.VolumeMount{
{MountPath: "/host/driver", Name: "flexvol-driver-host"},
}
return v1.Container{
Name: "flexvol-driver",
Image: constructImage(FlexVolumeImageName, c.cr.Spec.Registry),
VolumeMounts: flexVolumeMounts,
}
}
// cniEnvvars creates the CNI container's envvars.
func (c *nodeComponent) cniEnvvars() []v1.EnvVar {
return []v1.EnvVar{
{Name: "CNI_CONF_NAME", Value: "10-calico.conflist"},
{Name: "SLEEP", Value: "false"},
{Name: "CNI_NET_DIR", Value: c.cr.Spec.CNINetDir},
{
Name: "CNI_NETWORK_CONFIG",
ValueFrom: &v1.EnvVarSource{
ConfigMapKeyRef: &v1.ConfigMapKeySelector{
Key: "config",
LocalObjectReference: v1.LocalObjectReference{
Name: "cni-config",
},
},
},
},
}
}
// nodeContainer creates the main node container.
func (c *nodeComponent) nodeContainer() v1.Container {
lp, rp := c.nodeLivenessReadinessProbes()
isPrivileged := true
// Select which image to use.
image := constructImage(NodeImageNameCalico, c.cr.Spec.Registry)
if c.cr.Spec.Variant == operator.TigeraSecureEnterprise {
image = constructImage(NodeImageNameTigera, c.cr.Spec.Registry)
}
return v1.Container{
Name: "calico-node",
Image: image,
Resources: c.nodeResources(),
SecurityContext: &v1.SecurityContext{Privileged: &isPrivileged},
Env: c.nodeEnvVars(),
VolumeMounts: c.nodeVolumeMounts(),
LivenessProbe: lp,
ReadinessProbe: rp,
}
}
// nodeResources creates the node's resource requirements.
func (c *nodeComponent) nodeResources() v1.ResourceRequirements {
res := v1.ResourceRequirements{}
if len(c.cr.Spec.Components.Node.Resources.Limits) > 0 || len(c.cr.Spec.Components.Node.Resources.Requests) > 0 {
res.Requests = c.cr.Spec.Components.Node.Resources.Requests
res.Limits = c.cr.Spec.Components.Node.Resources.Limits
}
return res
}
// nodeVolumeMounts creates the node's volume mounts.
func (c *nodeComponent) nodeVolumeMounts() []v1.VolumeMount {
nodeVolumeMounts := []v1.VolumeMount{
{MountPath: "/lib/modules", Name: "lib-modules", ReadOnly: true},
{MountPath: "/run/xtables.lock", Name: "xtables-lock"},
{MountPath: "/var/run/calico", Name: "var-run-calico"},
{MountPath: "/var/lib/calico", Name: "var-lib-calico"},
{MountPath: "/var/run/nodeagent", Name: "policysync"},
}
if c.cr.Spec.Variant == operator.TigeraSecureEnterprise {
extraNodeMounts := []v1.VolumeMount{
{MountPath: "/var/log/calico", Name: "var-log-calico"},
}
nodeVolumeMounts = append(nodeVolumeMounts, extraNodeMounts...)
}
nodeVolumeMounts = setCustomVolumeMounts(nodeVolumeMounts, c.cr.Spec.Components.Node.ExtraVolumeMounts)
return nodeVolumeMounts
}
// nodeEnvVars creates the node's envvars.
func (c *nodeComponent) nodeEnvVars() []v1.EnvVar {
nodeEnv := []v1.EnvVar{
{Name: "DATASTORE_TYPE", Value: "kubernetes"},
{Name: "WAIT_FOR_DATASTORE", Value: "true"},
{Name: "CALICO_NETWORKING_BACKEND", Value: "bird"},
{Name: "CLUSTER_TYPE", Value: "k8s,bgp,operator"},
{Name: "IP", Value: "autodetect"},
{Name: "CALICO_IPV4POOL_CIDR", Value: c.cr.Spec.IPPools[0].CIDR},
{Name: "CALICO_IPV4POOL_IPIP", Value: "Always"},
{Name: "CALICO_DISABLE_FILE_LOGGING", Value: "true"},
{Name: "FELIX_IPINIPMTU", Value: "1440"},
{Name: "FELIX_DEFAULTENDPOINTTOHOSTACTION", Value: "ACCEPT"},
{Name: "FELIX_IPV6SUPPORT", Value: "false"},
{Name: "FELIX_HEALTHENABLED", Value: "true"},
{
Name: "NODENAME",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{FieldPath: "spec.nodeName"},
},
},
}
if c.cr.Spec.Variant == operator.TigeraSecureEnterprise {
extraNodeEnv := []v1.EnvVar{
{Name: "FELIX_PROMETHEUSREPORTERENABLED", Value: "true"},
{Name: "FELIX_PROMETHEUSREPORTERPORT", Value: "9081"},
{Name: "FELIX_FLOWLOGSFILEENABLED", Value: "true"},
{Name: "FELIX_FLOWLOGSFILEINCLUDELABELS", Value: "true"},
{Name: "FELIX_FLOWLOGSFILEINCLUDEPOLICIES", Value: "true"},
{Name: "FELIX_FLOWLOGSENABLENETWORKSETS", Value: "true"},
}
nodeEnv = append(nodeEnv, extraNodeEnv...)
}
if c.openshift {
// For Openshift, we need special configuration since our default port is already in use.
nodeEnv = append(nodeEnv, v1.EnvVar{Name: "FELIX_HEALTHPORT", Value: "9199"})
// Use iptables in nftables mode.
nodeEnv = append(nodeEnv, v1.EnvVar{Name: "FELIX_IPTABLESBACKEND", Value: "NFT"})
}
nodeEnv = setCustomEnv(nodeEnv, c.cr.Spec.Components.Node.ExtraEnv)
return nodeEnv
}
// nodeLivenessReadinessProbes creates the node's liveness and readiness probes.
func (c *nodeComponent) nodeLivenessReadinessProbes() (*v1.Probe, *v1.Probe) {
// Determine liveness and readiness configuration for node.
livenessPort := intstr.FromInt(9099)
readinessCmd := []string{"/bin/calico-node", "-bird-ready", "-felix-ready"}
if c.openshift {
// For Openshift, we need special configuration since our default port is already in use.
// Additionally, since the node readiness probe doesn't yet support
// custom ports, we need to disable felix readiness for now.
livenessPort = intstr.FromInt(9199)
readinessCmd = []string{"/bin/calico-node", "-bird-ready"}
}
lp := &v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Host: "localhost",
Path: "/liveness",
Port: livenessPort,
},
},
}
rp := &v1.Probe{
Handler: v1.Handler{Exec: &v1.ExecAction{Command: readinessCmd}},
}
return lp, rp
}