Skip to content

Commit

Permalink
spidermultusconfig: fix panic if spidermultusconfig.spec is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclinder committed Oct 20, 2023
1 parent 186fc82 commit 9208e44
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/multuscniconfig/multusconfig_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/doc/spidermultus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | |
2 changes: 2 additions & 0 deletions test/scripts/install-multus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ spec:
| sed 's?<<DEFAULT_IPV4_IPPOOLS>>?vlan200-v4?g' \
| sed 's?<<DEFAULT_IPV6_IPPOOLS>>?vlan200-v6?g' \
| kubectl apply --kubeconfig ${E2E_KUBECONFIG} -f -

}


Expand Down Expand Up @@ -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"

0 comments on commit 9208e44

Please sign in to comment.