From 9208e44df58e33b1791f0e360428f87f7438055b Mon Sep 17 00:00:00 2001 From: cyclinder Date: Fri, 20 Oct 2023 17:44:03 +0800 Subject: [PATCH] spidermultusconfig: fix panic if spidermultusconfig.spec is empty --- pkg/multuscniconfig/multusconfig_informer.go | 26 ++++++++++++-------- test/doc/spidermultus.md | 1 + test/scripts/install-multus.sh | 2 ++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pkg/multuscniconfig/multusconfig_informer.go b/pkg/multuscniconfig/multusconfig_informer.go index 1434e216cc..9a1aebd5c7 100644 --- a/pkg/multuscniconfig/multusconfig_informer.go +++ b/pkg/multuscniconfig/multusconfig_informer.go @@ -278,12 +278,12 @@ func (mcc *MultusConfigController) syncHandler(ctx context.Context, multusConfig } newNetAttachDef, err := generateNetAttachDef(netAttachName, multusConfig) - if nil != err { + if err != nil { return fmt.Errorf("failed to generate net-attach-def, error: %w", err) } err = controllerutil.SetControllerReference(multusConfig, newNetAttachDef, mcc.client.Scheme()) - if nil != err { + if err != nil { return fmt.Errorf("failed to set net-attach-def %s owner reference with MultusConfig %s/%s, error: %w", newNetAttachDef.Name, multusConfig.Namespace, multusConfig.Name, err) } @@ -339,12 +339,24 @@ func (mcc *MultusConfigController) syncHandler(ctx context.Context, multusConfig } func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.SpiderMultusConfig) (*netv1.NetworkAttachmentDefinition, error) { - multusConfSpec := multusConf.Spec.DeepCopy() + netAttachDef := &netv1.NetworkAttachmentDefinition{ + ObjectMeta: metav1.ObjectMeta{ + Name: netAttachName, + Namespace: multusConf.Namespace, + }, + } + anno := multusConf.Annotations if anno == nil { anno = make(map[string]string) } + emptySpec := spiderpoolv2beta1.MultusCNIConfigSpec{} + if multusConf.Spec == emptySpec { + return netAttachDef, nil + } + + multusConfSpec := multusConf.Spec.DeepCopy() var plugins []interface{} // with Kubernetes OpenAPI validation, multusConfSpec.EnableCoordinator must not be nil @@ -444,13 +456,7 @@ func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.Sp return nil, fmt.Errorf("%w: unrecognized CNI type %s", constant.ErrWrongInput, multusConfSpec.CniType) } - netAttachDef := &netv1.NetworkAttachmentDefinition{ - ObjectMeta: metav1.ObjectMeta{ - Name: netAttachName, - Namespace: multusConf.Namespace, - Annotations: anno, - }, - } + netAttachDef.ObjectMeta.Annotations = anno if len(confStr) > 0 { netAttachDef.Spec = netv1.NetworkAttachmentDefinitionSpec{ Config: confStr, diff --git a/test/doc/spidermultus.md b/test/doc/spidermultus.md index 6ead5488af..f27c711ccb 100644 --- a/test/doc/spidermultus.md +++ b/test/doc/spidermultus.md @@ -20,3 +20,4 @@ | M00023 | vlan is not in the range of 0-4094 and will not be created | p3 | | done | | | M00024 | set disableIPAM to true and see if multus's nad has ipam config | p3 | | done | | | M00025 | set sriov.enableRdma to true and see if multus's nad has rdma config | p3 | | | | +| M00026 | set spidermultusconfig.spec to empty and see if works | p3 | | | | diff --git a/test/scripts/install-multus.sh b/test/scripts/install-multus.sh index abcc2c0d6f..65051cdf69 100755 --- a/test/scripts/install-multus.sh +++ b/test/scripts/install-multus.sh @@ -122,6 +122,7 @@ spec: | sed 's?<>?vlan200-v4?g' \ | sed 's?<>?vlan200-v6?g' \ | kubectl apply --kubeconfig ${E2E_KUBECONFIG} -f - + } @@ -233,6 +234,7 @@ Install::SpiderpoolCR kubectl get spidercoordinator default -o yaml --kubeconfig ${E2E_KUBECONFIG} kubectl get sp -o wide --kubeconfig ${E2E_KUBECONFIG} +kubectl get spidermultusconfig -n kube-system --kubeconfig ${E2E_KUBECONFIG} kubectl get network-attachment-definitions.k8s.cni.cncf.io --kubeconfig ${E2E_KUBECONFIG} -n kube-system -o yaml echo "$CURRENT_FILENAME : done" \ No newline at end of file