/
config_controller.go
398 lines (367 loc) · 12.5 KB
/
config_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
/*
* Copyright 2022- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
multinicv1 "github.com/foundation-model-stack/multi-nic-cni/api/v1"
"github.com/foundation-model-stack/multi-nic-cni/controllers/vars"
"github.com/foundation-model-stack/multi-nic-cni/plugin"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"os"
)
const configFinalizer = "finalizers.config.multinic.fms.io"
var (
OPERATOR_NAMESPACE string = getOperatorNamespace()
ConfigReady bool = false
// referred by daemon watcher
DaemonName string = vars.DaemonLabelValue
)
func getOperatorNamespace() string {
key := "OPERATOR_NAMESPACE"
val, found := os.LookupEnv(key)
if !found {
return vars.DefaultOperatorNamespace
}
return val
}
// ConfigReconciler reconciles a Config object
// - if Config is deleted, delete daemon
// - create CNI daemonset, and NetworkAttachmentDefinition watcher if not exist
// - otherwise, update the existing daemonset and restart NetworkAttachmentDefinition watcher
type ConfigReconciler struct {
client.Client
*kubernetes.Clientset
*rest.Config
*CIDRHandler
*plugin.NetAttachDefHandler
Scheme *runtime.Scheme
}
//+kubebuilder:rbac:groups=multinic.fms.io,resources=configs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=multinic.fms.io,resources=configs/status,verbs=get;update;patch
func (r *ConfigReconciler) getDefaultConfigSpec() multinicv1.ConfigSpec {
daemonEnv := corev1.EnvVar{
Name: "DAEMON_PORT",
Value: fmt.Sprintf("%d", vars.DefaultDaemonPort),
}
routeEnv := corev1.EnvVar{
Name: "RT_TABLE_PATH",
Value: "/opt/rt_tables",
}
env := []corev1.EnvVar{daemonEnv, routeEnv}
binMnt := multinicv1.HostPathMount{
Name: "cnibin",
PodCNIPath: "/host/opt/cni/bin",
HostCNIPath: r.getCNIHostPath(),
}
devPluginMnt := multinicv1.HostPathMount{
Name: "device-plugin",
PodCNIPath: "/var/lib/kubelet/device-plugins",
HostCNIPath: "/var/lib/kubelet/device-plugins",
}
routeMnt := multinicv1.HostPathMount{
Name: "rt-tables",
PodCNIPath: "/opt/rt_tables",
HostCNIPath: "/etc/iproute2/rt_tables",
}
hostPathMounts := []multinicv1.HostPathMount{binMnt, devPluginMnt, routeMnt}
resources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("50Mi"),
},
}
var privileged bool = true
securityContext := &corev1.SecurityContext{
Privileged: &privileged,
}
daemonSpec := multinicv1.DaemonSpec{
Image: vars.DefaultDaemonImage,
Env: env,
HostPathMounts: hostPathMounts,
Resources: resources,
SecurityContext: securityContext,
DaemonPort: vars.DefaultDaemonPort,
}
spec := multinicv1.ConfigSpec{
CNIType: vars.DefaultCNIType,
IPAMType: vars.DefaultIPAMType,
Daemon: daemonSpec,
JoinPath: vars.DefaultJoinPath,
InterfacePath: vars.DefaultInterfacePath,
AddRoutePath: vars.DefaultAddRoutePath,
DeleteRoutePath: vars.DefaultDeleteRoutePath,
}
return spec
}
func (r *ConfigReconciler) CreateDefaultDaemonConfig() error {
objMeta := metav1.ObjectMeta{
Name: DaemonName,
}
cfg := &multinicv1.Config{
ObjectMeta: objMeta,
Spec: r.getDefaultConfigSpec(),
}
err := r.Client.Create(context.TODO(), cfg)
return err
}
func (r *ConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = vars.ConfigLog.WithValues("config", req.NamespacedName)
instance := &multinicv1.Config{}
err := r.Client.Get(ctx, req.NamespacedName, instance)
if err != nil {
// - if Config is deleted, delete daemon
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Return and don't requeue
return ctrl.Result{}, nil
}
vars.ConfigLog.V(7).Info(fmt.Sprintf("Cannot get #%v ", err))
// Error reading the object - requeue the request.
return ctrl.Result{RequeueAfter: vars.LongReconcileTime}, nil
}
// Add finalizer to instance
if !controllerutil.ContainsFinalizer(instance, configFinalizer) {
controllerutil.AddFinalizer(instance, configFinalizer)
err = r.Update(ctx, instance)
if err != nil {
return ctrl.Result{}, err
}
}
// If config is deleted, delete corresponding daemonset
is_deleted := instance.GetDeletionTimestamp() != nil
if is_deleted {
if controllerutil.ContainsFinalizer(instance, configFinalizer) {
if err = r.callFinalizer(vars.ConfigLog, req.NamespacedName.Name); err != nil {
return ctrl.Result{}, err
}
controllerutil.RemoveFinalizer(instance, configFinalizer)
err := r.Client.Update(ctx, instance)
if err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
r.UpdateConfigBySpec(&instance.Spec)
if !ConfigReady {
r.CIDRHandler.SyncAllPendingCustomCR(r.NetAttachDefHandler)
vars.ConfigLog.Info("Set ConfigReady")
ConfigReady = true
// initial run
r.CIDRHandler.UpdateCIDRs()
}
dsName := instance.GetName()
_, err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Get(context.TODO(), dsName, metav1.GetOptions{})
daemonset := r.newCNIDaemonSet(r.Clientset, dsName, instance.Spec.Daemon)
if err != nil {
// - create CNI daemonset
if errors.IsNotFound(err) {
SetDaemonConnector(instance.Spec)
_, err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Create(context.TODO(), daemonset, metav1.CreateOptions{})
vars.ConfigLog.V(7).Info(fmt.Sprintf("Create new multi-nic daemonset #%s (cni=%s,ipam=%s), err=%v ", dsName, instance.Spec.CNIType, instance.Spec.IPAMType, err))
} else {
vars.ConfigLog.Info(fmt.Sprintf("Cannot get daemonset #%v ", err))
}
} else {
// - otherwise, update the existing daemonset
SetDaemonConnector(instance.Spec)
_, err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Update(context.TODO(), daemonset, metav1.UpdateOptions{})
vars.ConfigLog.V(7).Info(fmt.Sprintf("Update multi-nic daemonset #%s, err=%v ", dsName, err))
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&multinicv1.Config{}).
Complete(r)
}
// UpdateConfigBySpec updates configuration variables defined in the spec
func (r *ConfigReconciler) UpdateConfigBySpec(spec *multinicv1.ConfigSpec) {
// set configurations
if spec.UrgentReconcileSeconds > 0 {
vars.ConfigLog.Info(fmt.Sprintf("Configure UrgentReconcileSeconds = %d", spec.UrgentReconcileSeconds))
vars.UrgentReconcileTime = time.Duration(spec.UrgentReconcileSeconds) * time.Second
}
if spec.NormalReconcileMinutes > 0 {
vars.ConfigLog.Info(fmt.Sprintf("Configure NormalReconcileMinutes = %d", spec.NormalReconcileMinutes))
vars.NormalReconcileTime = time.Duration(spec.NormalReconcileMinutes) * time.Minute
}
if spec.LongReconcileMinutes > 0 {
vars.ConfigLog.Info(fmt.Sprintf("Configure LongReconcileMinutes = %d", spec.LongReconcileMinutes))
vars.LongReconcileTime = time.Duration(spec.LongReconcileMinutes) * time.Minute
}
if spec.ContextTimeoutMinutes > 0 {
vars.ConfigLog.Info(fmt.Sprintf("Configure ContextTimeoutMinutes = %d", spec.ContextTimeoutMinutes))
vars.ContextTimeout = time.Duration(spec.ContextTimeoutMinutes) * time.Minute
}
if spec.LogLevel >= 1 && spec.LogLevel <= 127 {
if !vars.ConfigLog.V(spec.LogLevel).Enabled() {
vars.ConfigLog.Info(fmt.Sprintf("Configure LogLevel = %d", spec.LogLevel))
intLevel := -1 * spec.LogLevel
zaplevel := zapcore.Level(int8(intLevel))
vars.ZapOpts.Level = zaplevel
vars.SetLog()
}
}
vars.DaemonPort = spec.Daemon.DaemonPort
vars.TargetCNI = spec.CNIType
}
// newCNIDaemonSet creates new CNI daemonset
func (r *ConfigReconciler) newCNIDaemonSet(client *kubernetes.Clientset, name string, daemonSpec multinicv1.DaemonSpec) *appsv1.DaemonSet {
labels := map[string]string{vars.DeamonLabelKey: vars.DaemonLabelValue}
// prepare container port
containerPort := corev1.ContainerPort{ContainerPort: int32(daemonSpec.DaemonPort)}
mnts := daemonSpec.HostPathMounts
vmnts := []corev1.VolumeMount{}
volumes := []corev1.Volume{}
for _, mnt := range mnts {
// prepare volume mounting
vmnt := corev1.VolumeMount{
Name: mnt.Name,
MountPath: mnt.PodCNIPath,
}
hostSource := &corev1.HostPathVolumeSource{Path: mnt.HostCNIPath}
volume := corev1.Volume{
Name: mnt.Name,
VolumeSource: corev1.VolumeSource{
HostPath: hostSource,
},
}
vmnts = append(vmnts, vmnt)
volumes = append(volumes, volume)
}
// hostName environment
hostNameVar := corev1.EnvVar{
Name: vars.NodeNameKey,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
}
daemonSpec.Env = append(daemonSpec.Env, hostNameVar)
// prepare secret
secrets := []corev1.LocalObjectReference{}
if daemonSpec.ImagePullSecret != "" {
secret := corev1.LocalObjectReference{
Name: daemonSpec.ImagePullSecret,
}
secrets = append(secrets, secret)
}
// prepare container
container := corev1.Container{
Name: name,
Image: daemonSpec.Image,
Ports: []corev1.ContainerPort{
containerPort,
},
EnvFrom: daemonSpec.EnvFrom,
Env: daemonSpec.Env,
Resources: daemonSpec.Resources,
VolumeMounts: vmnts,
ImagePullPolicy: corev1.PullPolicy(daemonSpec.ImagePullPolicy),
SecurityContext: daemonSpec.SecurityContext,
}
return &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: OPERATOR_NAMESPACE,
},
Spec: appsv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
Spec: corev1.PodSpec{
PriorityClassName: "system-cluster-critical",
HostNetwork: true,
ServiceAccountName: vars.ServiceAccountName,
NodeSelector: daemonSpec.NodeSelector,
Tolerations: daemonSpec.Tolerations,
Containers: []corev1.Container{
container,
},
Volumes: volumes,
ImagePullSecrets: secrets,
},
},
},
}
}
func (r *ConfigReconciler) getCNIHostPath() string {
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
// find multus pod
labels := fmt.Sprintf("%s=%s", vars.MultusLabelKey, vars.MultusLabelValue)
listOptions := metav1.ListOptions{
LabelSelector: labels,
Limit: 1,
}
multusPods, err := r.Clientset.CoreV1().Pods(metav1.NamespaceAll).List(ctx, listOptions)
if err != nil {
return vars.DefaultCNIHostPath
}
for _, multusPod := range multusPods.Items {
volumes := multusPod.Spec.Volumes
for _, volume := range volumes {
if volume.Name == vars.CNIBinVolumeName {
if volume.HostPath != nil {
cniPath := volume.HostPath.Path
vars.ConfigLog.Info(fmt.Sprintf("Find Multus CNI path: %s", cniPath))
return cniPath
} else {
// hostpath is not defined
return vars.DefaultCNIHostPath
}
}
}
}
return vars.DefaultCNIHostPath
}
// callFinalizer deletes all CIDRs, waits for all ippools deleted, deletes CNI deamonset, and stops NetworkAttachmentDefinition watcher
func (r *ConfigReconciler) callFinalizer(reqLogger logr.Logger, dsName string) error {
reqLogger.Info(fmt.Sprintf("Finalize %s", dsName))
// delete all CIDRs
cidrMap, err := r.CIDRHandler.ListCIDR()
if err == nil {
for cidrName, cidr := range cidrMap {
err := r.CIDRHandler.DeleteCIDR(cidr)
if err != nil {
reqLogger.V(3).Info(fmt.Sprintf("Failed to delete CIDR %s: %v", cidrName, err))
}
}
}
// wait for all ippools deleted
for {
if err != nil || r.CIDRHandler.IPPoolHandler.SafeCache.GetSize() == 0 {
break
}
reqLogger.V(5).Info(fmt.Sprintf("%d ippools left, wait...", r.CIDRHandler.IPPoolHandler.SafeCache.GetSize()))
time.Sleep(1 * time.Second)
}
// delete CNI deamonset
err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Delete(context.TODO(), dsName, metav1.DeleteOptions{})
ConfigReady = false
if err != nil {
vars.ConfigLog.Info(fmt.Sprintf("Failed to finalize %s: %v", dsName, err))
}
return nil
}