Skip to content

Commit ded0965

Browse files
authored
Merge pull request #102 from sunya-ch/v1.0.4
set stable channel and replaces for bundle V1.0.4 and minor bug fixes
2 parents af6eccf + 7f9a1aa commit ded0965

15 files changed

+156
-116
lines changed

.github/workflows/build_push_controller.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ env:
2424
jobs:
2525
build-push-bundle:
2626
runs-on: ubuntu-latest
27+
needs: build-push-controller
2728
env:
2829
IMAGE_NAME: ghcr.io/${{ github.repository }}-bundle
2930
CHANNELS: stable

.github/workflows/golangci-lint.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ jobs:
1111
with:
1212
go-version: 1.18
1313
- uses: actions/checkout@v3
14+
- name: Tidy
15+
run: |
16+
go mod tidy
1417
- name: golangci-lint
1518
uses: golangci/golangci-lint-action@v3
1619
with:
17-
version: v1.52.2
20+
args: --timeout=10m

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified
141141
$(KUSTOMIZE) build config/crd | kubectl delete -f -
142142

143143
predeploy: manifests kustomize
144-
rm -f config/samples/multinic.fms.io_config.yaml
145-
envsubst < config/samples/multinic.fms.io_config.template > config/samples/multinic.fms.io_config.yaml
146144
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
147145
cd config/samples && $(KUSTOMIZE) edit set image multi-nic-cni-daemon=${DAEMON_IMG}
148146

config/manifests/kustomization.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ patchesJson6902:
3939
value: "--zap-stacktrace-level=error"
4040
- op: add
4141
path: /spec/template/spec/containers/1/args/1
42-
value: "--zap-log-level=4"
42+
value: "--zap-log-level=4"
43+
- op: add
44+
path: /spec/template/spec/containers/1/args/2
45+
value: "--zap-time-encoding=iso8601"

config/samples/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ kind: Kustomization
1515
images:
1616
- name: multi-nic-cni-daemon
1717
newName: ghcr.io/foundation-model-stack/multi-nic-cni-daemon
18-
newTag: v1.0.0
18+
newTag: v1.0.4

config/samples/multinic.fms.io_config.template

Lines changed: 0 additions & 39 deletions
This file was deleted.

config/samples/multinic.fms.io_config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ spec:
3636
resources:
3737
requests:
3838
cpu: "100m"
39-
memory: "50Mi"
39+
memory: "50Mi"
40+
urgentReconcileSeconds: 5
41+
normalReconcileMinutes: 1
42+
longReconcileMinutes: 10
43+
contextTimeoutMinutes: 2
44+
logLevel: 4

controllers/cidr_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type CIDRHandler struct {
5555
func NewCIDRHandler(client client.Client, config *rest.Config, hostInterfaceHandler *HostInterfaceHandler, daemonCache *DaemonCacheHandler, quit chan struct{}) *CIDRHandler {
5656
clientset, _ := kubernetes.NewForConfig(config)
5757
cidrCompute := compute.CIDRCompute{}
58-
updateReq := make(chan struct{}, MAX_QSIZE)
58+
updateReq := make(chan struct{}, vars.MaxQueueSize)
5959
handler := &CIDRHandler{
6060
Client: client,
6161
Clientset: clientset,

controllers/config_controller.go

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/apimachinery/pkg/runtime"
1717
ctrl "sigs.k8s.io/controller-runtime"
1818
"sigs.k8s.io/controller-runtime/pkg/client"
19+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1920

2021
multinicv1 "github.com/foundation-model-stack/multi-nic-cni/api/v1"
2122
"github.com/foundation-model-stack/multi-nic-cni/controllers/vars"
@@ -31,28 +32,20 @@ import (
3132
"os"
3233
)
3334

34-
const (
35-
SERVICE_ACCOUNT_NAME = "multi-nic-cni-operator-controller-manager"
36-
DEFAULT_OPERATOR_NAMESPACE = "multi-nic-cni-operator-system"
37-
38-
// NetworkAttachmentDefinition watching queue size
39-
MAX_QSIZE = 100
40-
)
35+
const configFinalizer = "finalizers.config.multinic.fms.io"
4136

4237
var (
4338
OPERATOR_NAMESPACE string = getOperatorNamespace()
4439
ConfigReady bool = false
4540
// referred by daemon watcher
46-
DAEMON_LABEL_NAME = "app"
47-
DAEMON_LABEL_VALUE = "multi-nicd"
48-
DaemonName string = DAEMON_LABEL_VALUE
41+
DaemonName string = vars.DaemonLabelValue
4942
)
5043

5144
func getOperatorNamespace() string {
5245
key := "OPERATOR_NAMESPACE"
5346
val, found := os.LookupEnv(key)
5447
if !found {
55-
return DEFAULT_OPERATOR_NAMESPACE
48+
return vars.DefaultOperatorNamespace
5649
}
5750
return val
5851
}
@@ -74,13 +67,10 @@ type ConfigReconciler struct {
7467
//+kubebuilder:rbac:groups=multinic.fms.io,resources=configs,verbs=get;list;watch;create;update;patch;delete
7568
//+kubebuilder:rbac:groups=multinic.fms.io,resources=configs/status,verbs=get;update;patch
7669

77-
func (r *ConfigReconciler) CreateDefaultDaemonConfig() error {
78-
objMeta := metav1.ObjectMeta{
79-
Name: DaemonName,
80-
}
70+
func (r *ConfigReconciler) getDefaultConfigSpec() multinicv1.ConfigSpec {
8171
daemonEnv := corev1.EnvVar{
8272
Name: "DAEMON_PORT",
83-
Value: "11000",
73+
Value: fmt.Sprintf("%d", vars.DefaultDaemonPort),
8474
}
8575
routeEnv := corev1.EnvVar{
8676
Name: "RT_TABLE_PATH",
@@ -90,7 +80,7 @@ func (r *ConfigReconciler) CreateDefaultDaemonConfig() error {
9080
binMnt := multinicv1.HostPathMount{
9181
Name: "cnibin",
9282
PodCNIPath: "/host/opt/cni/bin",
93-
HostCNIPath: "/var/lib/cni/bin",
83+
HostCNIPath: r.getCNIHostPath(),
9484
}
9585
devPluginMnt := multinicv1.HostPathMount{
9686
Name: "device-plugin",
@@ -114,25 +104,33 @@ func (r *ConfigReconciler) CreateDefaultDaemonConfig() error {
114104
Privileged: &privileged,
115105
}
116106
daemonSpec := multinicv1.DaemonSpec{
117-
Image: "ghcr.io/foundation-model-stack/multi-nic-cni-daemon:v1.0.4",
107+
Image: vars.DefaultDaemonImage,
118108
Env: env,
119109
HostPathMounts: hostPathMounts,
120110
Resources: resources,
121111
SecurityContext: securityContext,
122-
DaemonPort: 11000,
112+
DaemonPort: vars.DefaultDaemonPort,
123113
}
124114
spec := multinicv1.ConfigSpec{
125-
CNIType: "multi-nic",
126-
IPAMType: "multi-nic-ipam",
115+
CNIType: vars.DefaultCNIType,
116+
IPAMType: vars.DefaultIPAMType,
127117
Daemon: daemonSpec,
128-
JoinPath: "/join",
129-
InterfacePath: "/interface",
130-
AddRoutePath: "/addl3",
131-
DeleteRoutePath: "/deletel3",
118+
JoinPath: vars.DefaultJoinPath,
119+
InterfacePath: vars.DefaultInterfacePath,
120+
AddRoutePath: vars.DefaultAddRoutePath,
121+
DeleteRoutePath: vars.DefaultDeleteRoutePath,
132122
}
123+
return spec
124+
}
125+
126+
func (r *ConfigReconciler) CreateDefaultDaemonConfig() error {
127+
objMeta := metav1.ObjectMeta{
128+
Name: DaemonName,
129+
}
130+
133131
cfg := &multinicv1.Config{
134132
ObjectMeta: objMeta,
135-
Spec: spec,
133+
Spec: r.getDefaultConfigSpec(),
136134
}
137135
err := r.Client.Create(context.TODO(), cfg)
138136
return err
@@ -148,16 +146,39 @@ func (r *ConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
148146
if errors.IsNotFound(err) {
149147
// Request object not found, could have been deleted after reconcile request.
150148
// Return and don't requeue
151-
err = r.callFinalizer(vars.ConfigLog, req.NamespacedName.Name)
152-
if err != nil {
153-
vars.ConfigLog.V(1).Info(fmt.Sprintf("Failed to finalize %s: %v ", req.NamespacedName.Name, err))
154-
}
155149
return ctrl.Result{}, nil
156150
}
157151
vars.ConfigLog.V(7).Info(fmt.Sprintf("Cannot get #%v ", err))
158152
// Error reading the object - requeue the request.
159153
return ctrl.Result{RequeueAfter: vars.LongReconcileTime}, nil
160154
}
155+
156+
// Add finalizer to instance
157+
if !controllerutil.ContainsFinalizer(instance, configFinalizer) {
158+
controllerutil.AddFinalizer(instance, configFinalizer)
159+
err = r.Update(ctx, instance)
160+
if err != nil {
161+
return ctrl.Result{}, err
162+
}
163+
}
164+
165+
// If config is deleted, delete corresponding daemonset
166+
is_deleted := instance.GetDeletionTimestamp() != nil
167+
if is_deleted {
168+
if controllerutil.ContainsFinalizer(instance, configFinalizer) {
169+
if err = r.callFinalizer(vars.ConfigLog, req.NamespacedName.Name); err != nil {
170+
return ctrl.Result{}, err
171+
}
172+
173+
controllerutil.RemoveFinalizer(instance, configFinalizer)
174+
err := r.Client.Update(ctx, instance)
175+
if err != nil {
176+
return ctrl.Result{}, err
177+
}
178+
}
179+
return ctrl.Result{}, nil
180+
}
181+
161182
r.UpdateConfigBySpec(&instance.Spec)
162183

163184
if !ConfigReady {
@@ -172,17 +193,17 @@ func (r *ConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
172193
_, err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Get(context.TODO(), dsName, metav1.GetOptions{})
173194
daemonset := r.newCNIDaemonSet(r.Clientset, dsName, instance.Spec.Daemon)
174195
if err != nil {
175-
// - create CNI daemonset, and NetworkAttachmentDefinition watcher if not exist
196+
// - create CNI daemonset
176197
if errors.IsNotFound(err) {
177-
r.newNetAttachDefWatcher(instance)
198+
SetDaemonConnector(instance.Spec)
178199
_, err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Create(context.TODO(), daemonset, metav1.CreateOptions{})
179200
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))
180201
} else {
181202
vars.ConfigLog.Info(fmt.Sprintf("Cannot get daemonset #%v ", err))
182203
}
183204
} else {
184-
// - otherwise, update the existing daemonset and restart NetworkAttachmentDefinition watcher
185-
r.newNetAttachDefWatcher(instance)
205+
// - otherwise, update the existing daemonset
206+
SetDaemonConnector(instance.Spec)
186207
_, err = r.Clientset.AppsV1().DaemonSets(OPERATOR_NAMESPACE).Update(context.TODO(), daemonset, metav1.UpdateOptions{})
187208
vars.ConfigLog.V(7).Info(fmt.Sprintf("Update multi-nic daemonset #%s, err=%v ", dsName, err))
188209
}
@@ -224,19 +245,13 @@ func (r *ConfigReconciler) UpdateConfigBySpec(spec *multinicv1.ConfigSpec) {
224245
vars.SetLog()
225246
}
226247
}
227-
}
228-
229-
// newNetAttachDefWatcher restarts NetworkAttachmentDefinition watcher
230-
func (r *ConfigReconciler) newNetAttachDefWatcher(instance *multinicv1.Config) {
231-
vars.DaemonPort = instance.Spec.Daemon.DaemonPort
232-
vars.TargetCNI = instance.Spec.CNIType
233-
DaemonName = instance.GetName()
234-
SetDaemon(instance.Spec)
248+
vars.DaemonPort = spec.Daemon.DaemonPort
249+
vars.TargetCNI = spec.CNIType
235250
}
236251

237252
// newCNIDaemonSet creates new CNI daemonset
238253
func (r *ConfigReconciler) newCNIDaemonSet(client *kubernetes.Clientset, name string, daemonSpec multinicv1.DaemonSpec) *appsv1.DaemonSet {
239-
labels := map[string]string{DAEMON_LABEL_NAME: DAEMON_LABEL_VALUE}
254+
labels := map[string]string{vars.DeamonLabelKey: vars.DaemonLabelValue}
240255

241256
// prepare container port
242257
containerPort := corev1.ContainerPort{ContainerPort: int32(daemonSpec.DaemonPort)}
@@ -306,7 +321,7 @@ func (r *ConfigReconciler) newCNIDaemonSet(client *kubernetes.Clientset, name st
306321
ObjectMeta: metav1.ObjectMeta{Labels: labels},
307322
Spec: corev1.PodSpec{
308323
HostNetwork: true,
309-
ServiceAccountName: SERVICE_ACCOUNT_NAME,
324+
ServiceAccountName: vars.ServiceAccountName,
310325
NodeSelector: daemonSpec.NodeSelector,
311326
Tolerations: daemonSpec.Tolerations,
312327
Containers: []corev1.Container{
@@ -320,12 +335,40 @@ func (r *ConfigReconciler) newCNIDaemonSet(client *kubernetes.Clientset, name st
320335
}
321336
}
322337

338+
func (r *ConfigReconciler) getCNIHostPath() string {
339+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
340+
defer cancel()
341+
// find multus pod
342+
labels := fmt.Sprintf("%s=%s", vars.MultusLabelKey, vars.MultusLabelValue)
343+
listOptions := metav1.ListOptions{
344+
LabelSelector: labels,
345+
Limit: 1,
346+
}
347+
multusPods, err := r.Clientset.CoreV1().Pods(metav1.NamespaceAll).List(ctx, listOptions)
348+
if err != nil {
349+
return vars.DefaultCNIHostPath
350+
}
351+
for _, multusPod := range multusPods.Items {
352+
volumes := multusPod.Spec.Volumes
353+
for _, volume := range volumes {
354+
if volume.Name == vars.CNIBinVolumeName {
355+
if volume.HostPath != nil {
356+
cniPath := volume.HostPath.Path
357+
vars.ConfigLog.Info(fmt.Sprintf("Find Multus CNI path: %s", cniPath))
358+
return cniPath
359+
} else {
360+
// hostpath is not defined
361+
return vars.DefaultCNIHostPath
362+
}
363+
}
364+
}
365+
}
366+
return vars.DefaultCNIHostPath
367+
}
368+
323369
// callFinalizer deletes all CIDRs, waits for all ippools deleted, deletes CNI deamonset, and stops NetworkAttachmentDefinition watcher
324370
func (r *ConfigReconciler) callFinalizer(reqLogger logr.Logger, dsName string) error {
325-
// reset default name
326-
DaemonName = DAEMON_LABEL_VALUE
327371
reqLogger.Info(fmt.Sprintf("Finalize %s", dsName))
328-
329372
// delete all CIDRs
330373
cidrMap, err := r.CIDRHandler.ListCIDR()
331374
if err == nil {

controllers/daemon_cache_handler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package controllers
77

88
import (
9-
"fmt"
9+
"errors"
10+
11+
"github.com/foundation-model-stack/multi-nic-cni/controllers/vars"
1012
)
1113

1214
type DaemonPod struct {
@@ -28,7 +30,7 @@ func (h *DaemonCacheHandler) SetCache(key string, value DaemonPod) {
2830
func (h *DaemonCacheHandler) GetCache(key string) (DaemonPod, error) {
2931
value := h.SafeCache.GetCache(key)
3032
if value == nil {
31-
return DaemonPod{}, fmt.Errorf("Not Found")
33+
return DaemonPod{}, errors.New(vars.NotFoundError)
3234
}
3335
return value.(DaemonPod), nil
3436
}

controllers/daemon_connector.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323
var DAEMON_NAMESPACE, DAEMON_PORT, INTERFACE_PATH, ADD_ROUTE_PATH, DELETE_ROUTE_PATH, REGISTER_IPAM_PATH string
2424

2525
// SetDaemon sets daemon environments
26-
func SetDaemon(daemonSpec multinicv1.ConfigSpec) {
27-
DAEMON_PORT = fmt.Sprintf("%d", daemonSpec.Daemon.DaemonPort)
26+
func SetDaemonConnector(daemonSpec multinicv1.ConfigSpec) {
27+
daemonPort := fmt.Sprintf("%d", daemonSpec.Daemon.DaemonPort)
28+
DAEMON_PORT = daemonPort
2829
DAEMON_NAMESPACE = OPERATOR_NAMESPACE
2930
INTERFACE_PATH = daemonSpec.InterfacePath
3031
ADD_ROUTE_PATH = daemonSpec.AddRoutePath

0 commit comments

Comments
 (0)