From 2833db7cb87dee35972a577eb8dc9198a8daf701 Mon Sep 17 00:00:00 2001 From: Icarus9913 Date: Wed, 13 Dec 2023 17:53:31 +0800 Subject: [PATCH] support empty config with custom type Signed-off-by: Icarus9913 --- ...pool.spidernet.io_spidermultusconfigs.yaml | 5 +- cmd/spiderpool-init/cmd/config.go | 14 ++-- cmd/spiderpool-init/cmd/multus.go | 10 ++- .../v2beta1/spidermultus_types.go | 6 +- .../v2beta1/zz_generated.deepcopy.go | 75 +++++++++++++++++++ pkg/multuscniconfig/multusconfig_informer.go | 32 ++++---- pkg/multuscniconfig/multusconfig_mutate.go | 18 ++++- pkg/multuscniconfig/multusconfig_validate.go | 25 ++++--- pkg/multuscniconfig/multusconfig_webhook.go | 21 +++--- test/doc/spidermultus.md | 38 +++++----- .../macvlan_overlay_one_test.go | 21 +++--- test/e2e/ifacer/ifacer_test.go | 8 +- test/e2e/ippoolcr/ippoolcr_test.go | 13 ++-- .../spidermultus/spidermultus_suite_test.go | 3 +- test/e2e/spidermultus/spidermultus_test.go | 50 ++++++++++--- 15 files changed, 228 insertions(+), 111 deletions(-) diff --git a/charts/spiderpool/crds/spiderpool.spidernet.io_spidermultusconfigs.yaml b/charts/spiderpool/crds/spiderpool.spidernet.io_spidermultusconfigs.yaml index 755c9780f2..493738a62b 100644 --- a/charts/spiderpool/crds/spiderpool.spidernet.io_spidermultusconfigs.yaml +++ b/charts/spiderpool/crds/spiderpool.spidernet.io_spidermultusconfigs.yaml @@ -38,6 +38,7 @@ spec: description: Spec is the specification of the MultusCNIConfig properties: cniType: + default: custom enum: - macvlan - ipvlan @@ -97,6 +98,8 @@ spec: type: boolean enableCoordinator: default: true + description: if CniType was set to custom, we'll mutate this field + to be false type: boolean ibsriov: properties: @@ -312,8 +315,6 @@ spec: required: - resourceName type: object - required: - - cniType type: object type: object served: true diff --git a/cmd/spiderpool-init/cmd/config.go b/cmd/spiderpool-init/cmd/config.go index 058c31b6e3..b64958feac 100644 --- a/cmd/spiderpool-init/cmd/config.go +++ b/cmd/spiderpool-init/cmd/config.go @@ -5,16 +5,18 @@ package cmd import ( "fmt" + "os" + "strconv" + "strings" + "github.com/containernetworking/cni/libcni" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" + coordinatorcmd "github.com/spidernet-io/spiderpool/cmd/coordinator/cmd" "github.com/spidernet-io/spiderpool/pkg/constant" spiderpoolip "github.com/spidernet-io/spiderpool/pkg/ip" spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" - "os" - "strconv" - "strings" ) const ( @@ -361,7 +363,7 @@ func getMultusCniConfig(cniName, cniType string, ns string) *spiderpoolv2beta1.S Annotations: annotations, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: cniType, + CniType: pointer.String(cniType), EnableCoordinator: pointer.Bool(false), }, } diff --git a/cmd/spiderpool-init/cmd/multus.go b/cmd/spiderpool-init/cmd/multus.go index b0e937b230..e59712bc6e 100644 --- a/cmd/spiderpool-init/cmd/multus.go +++ b/cmd/spiderpool-init/cmd/multus.go @@ -6,14 +6,16 @@ import ( "context" "encoding/json" "fmt" - "github.com/spidernet-io/spiderpool/pkg/constant" - "github.com/spidernet-io/spiderpool/pkg/utils" + "os" + "path" + v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apitypes "k8s.io/apimachinery/pkg/types" - "os" - "path" controller_client "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/spidernet-io/spiderpool/pkg/constant" + "github.com/spidernet-io/spiderpool/pkg/utils" ) // MultusNetConf for cni config file written in json diff --git a/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/spidermultus_types.go b/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/spidermultus_types.go index fcca592c66..c0b8909aa6 100644 --- a/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/spidermultus_types.go +++ b/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/spidermultus_types.go @@ -27,9 +27,10 @@ type SpiderMultusConfigList struct { // MultusCNIConfigSpec defines the desired state of SpiderMultusConfig. type MultusCNIConfigSpec struct { - // +kubebuilder:validation:Required + // +kubebuilder:validation:Optional // +kubebuilder:validation:Enum=macvlan;ipvlan;sriov;ovs;ib-sriov;ipoib;custom - CniType string `json:"cniType"` + // +kubebuilder:default=custom + CniType *string `json:"cniType,omitempty"` // +kubebuilder:validation:Optional MacvlanConfig *SpiderMacvlanCniConfig `json:"macvlan,omitempty"` @@ -49,6 +50,7 @@ type MultusCNIConfigSpec struct { // +kubebuilder:validation:Optional IpoibConfig *SpiderIpoibCniConfig `json:"ipoib,omitempty"` + // if CniType was set to custom, we'll mutate this field to be false // +kubebuilder:default=true // +kubebuilder:validation:Optional EnableCoordinator *bool `json:"enableCoordinator,omitempty"` diff --git a/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/zz_generated.deepcopy.go b/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/zz_generated.deepcopy.go index cdef117ec5..c85be8fbbe 100644 --- a/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/zz_generated.deepcopy.go +++ b/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/zz_generated.deepcopy.go @@ -301,6 +301,11 @@ func (in *IPPoolStatus) DeepCopy() *IPPoolStatus { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *MultusCNIConfigSpec) DeepCopyInto(out *MultusCNIConfigSpec) { *out = *in + if in.CniType != nil { + in, out := &in.CniType, &out.CniType + *out = new(string) + **out = **in + } if in.MacvlanConfig != nil { in, out := &in.MacvlanConfig, &out.MacvlanConfig *out = new(SpiderMacvlanCniConfig) @@ -321,6 +326,16 @@ func (in *MultusCNIConfigSpec) DeepCopyInto(out *MultusCNIConfigSpec) { *out = new(SpiderOvsCniConfig) (*in).DeepCopyInto(*out) } + if in.IbSriovConfig != nil { + in, out := &in.IbSriovConfig, &out.IbSriovConfig + *out = new(SpiderIBSriovCniConfig) + (*in).DeepCopyInto(*out) + } + if in.IpoibConfig != nil { + in, out := &in.IpoibConfig, &out.IpoibConfig + *out = new(SpiderIpoibCniConfig) + (*in).DeepCopyInto(*out) + } if in.EnableCoordinator != nil { in, out := &in.EnableCoordinator, &out.EnableCoordinator *out = new(bool) @@ -614,6 +629,46 @@ func (in *SpiderEndpointList) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SpiderIBSriovCniConfig) DeepCopyInto(out *SpiderIBSriovCniConfig) { + *out = *in + if in.Pkey != nil { + in, out := &in.Pkey, &out.Pkey + *out = new(string) + **out = **in + } + if in.LinkState != nil { + in, out := &in.LinkState, &out.LinkState + *out = new(string) + **out = **in + } + if in.RdmaIsolation != nil { + in, out := &in.RdmaIsolation, &out.RdmaIsolation + *out = new(bool) + **out = **in + } + if in.IbKubernetesEnabled != nil { + in, out := &in.IbKubernetesEnabled, &out.IbKubernetesEnabled + *out = new(bool) + **out = **in + } + if in.SpiderpoolConfigPools != nil { + in, out := &in.SpiderpoolConfigPools, &out.SpiderpoolConfigPools + *out = new(SpiderpoolPools) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpiderIBSriovCniConfig. +func (in *SpiderIBSriovCniConfig) DeepCopy() *SpiderIBSriovCniConfig { + if in == nil { + return nil + } + out := new(SpiderIBSriovCniConfig) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SpiderIPPool) DeepCopyInto(out *SpiderIPPool) { *out = *in @@ -708,6 +763,26 @@ func (in *SpiderIPvlanCniConfig) DeepCopy() *SpiderIPvlanCniConfig { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SpiderIpoibCniConfig) DeepCopyInto(out *SpiderIpoibCniConfig) { + *out = *in + if in.SpiderpoolConfigPools != nil { + in, out := &in.SpiderpoolConfigPools, &out.SpiderpoolConfigPools + *out = new(SpiderpoolPools) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpiderIpoibCniConfig. +func (in *SpiderIpoibCniConfig) DeepCopy() *SpiderIpoibCniConfig { + if in == nil { + return nil + } + out := new(SpiderIpoibCniConfig) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SpiderMacvlanCniConfig) DeepCopyInto(out *SpiderMacvlanCniConfig) { *out = *in diff --git a/pkg/multuscniconfig/multusconfig_informer.go b/pkg/multuscniconfig/multusconfig_informer.go index e360c2c506..05c8d334c1 100644 --- a/pkg/multuscniconfig/multusconfig_informer.go +++ b/pkg/multuscniconfig/multusconfig_informer.go @@ -8,6 +8,9 @@ import ( "encoding/json" "errors" "fmt" + "reflect" + "time" + netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" "go.uber.org/zap" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -17,10 +20,8 @@ import ( "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" - "reflect" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "time" coordinatorcmd "github.com/spidernet-io/spiderpool/cmd/coordinator/cmd" "github.com/spidernet-io/spiderpool/cmd/spiderpool/cmd" @@ -338,24 +339,13 @@ func (mcc *MultusConfigController) syncHandler(ctx context.Context, multusConfig } func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.SpiderMultusConfig) (*netv1.NetworkAttachmentDefinition, error) { - netAttachDef := &netv1.NetworkAttachmentDefinition{ - ObjectMeta: metav1.ObjectMeta{ - Name: netAttachName, - Namespace: multusConf.Namespace, - }, - } + multusConfSpec := multusConf.Spec.DeepCopy() 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 @@ -380,7 +370,8 @@ func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.Sp var confStr string var err error - switch multusConfSpec.CniType { + // with Kubernetes OpenAPI validation, multusConfSpec.CniType must not be nil and default to "custom" + switch *multusConfSpec.CniType { case constant.MacvlanCNI: macvlanCNIConf := generateMacvlanCNIConf(disableIPAM, *multusConfSpec) // head insertion @@ -477,13 +468,18 @@ func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.Sp } confStr = *multusConfSpec.CustomCNIConfig } - default: // It's impossible get into the default branch - return nil, fmt.Errorf("%w: unrecognized CNI type %s", constant.ErrWrongInput, multusConfSpec.CniType) + return nil, fmt.Errorf("%w: unrecognized CNI type %s", constant.ErrWrongInput, *multusConfSpec.CniType) } - netAttachDef.ObjectMeta.Annotations = anno + netAttachDef := &netv1.NetworkAttachmentDefinition{ + ObjectMeta: metav1.ObjectMeta{ + Name: netAttachName, + Namespace: multusConf.Namespace, + Annotations: anno, + }, + } if len(confStr) > 0 { netAttachDef.Spec = netv1.NetworkAttachmentDefinitionSpec{ Config: confStr, diff --git a/pkg/multuscniconfig/multusconfig_mutate.go b/pkg/multuscniconfig/multusconfig_mutate.go index 247a913eb5..8749512164 100644 --- a/pkg/multuscniconfig/multusconfig_mutate.go +++ b/pkg/multuscniconfig/multusconfig_mutate.go @@ -13,11 +13,16 @@ import ( "github.com/spidernet-io/spiderpool/pkg/logutils" ) -func mutateSpiderMultusConfig(ctx context.Context, smc *spiderpoolv2beta1.SpiderMultusConfig) error { +func mutateSpiderMultusConfig(ctx context.Context, smc *spiderpoolv2beta1.SpiderMultusConfig) { logger := logutils.FromContext(ctx) logger.Info("Start to mutate SpiderMultusConfig") - switch smc.Spec.CniType { + // In the SpiderMultusConfig resource first creation, if we don't set `Spec.CniType` field, we need to set it to `custom`. + // The kubernetes webhook is called before OpenAPI JSONSchema validation + if (*smc).Spec.CniType == nil { + (*smc).Spec.CniType = pointer.String(constant.CustomCNI) + } + switch *smc.Spec.CniType { case constant.MacvlanCNI: setMacvlanDefaultConfig(smc.Spec.MacvlanConfig) case constant.IPVlanCNI: @@ -36,8 +41,13 @@ func mutateSpiderMultusConfig(ctx context.Context, smc *spiderpoolv2beta1.Spider } } - smc.Spec.CoordinatorConfig = setCoordinatorDefaultConfig(smc.Spec.CoordinatorConfig) - return nil + // with custom CNI configuration, we don't need to add Coordinator configuration + if *smc.Spec.CniType == constant.CustomCNI { + smc.Spec.CoordinatorConfig = nil + smc.Spec.EnableCoordinator = pointer.Bool(false) + } else { + smc.Spec.CoordinatorConfig = setCoordinatorDefaultConfig(smc.Spec.CoordinatorConfig) + } } func setMacvlanDefaultConfig(macvlanConfig *spiderpoolv2beta1.SpiderMacvlanCniConfig) { diff --git a/pkg/multuscniconfig/multusconfig_validate.go b/pkg/multuscniconfig/multusconfig_validate.go index edd3c1e303..38eaf41de1 100644 --- a/pkg/multuscniconfig/multusconfig_validate.go +++ b/pkg/multuscniconfig/multusconfig_validate.go @@ -6,13 +6,15 @@ package multuscniconfig import ( "encoding/json" "fmt" + + k8svalidation "k8s.io/apimachinery/pkg/util/validation" + "k8s.io/apimachinery/pkg/util/validation/field" + "k8s.io/utils/strings/slices" + "github.com/spidernet-io/spiderpool/cmd/spiderpool/cmd" "github.com/spidernet-io/spiderpool/pkg/constant" "github.com/spidernet-io/spiderpool/pkg/coordinatormanager" spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" - k8svalidation "k8s.io/apimachinery/pkg/util/validation" - "k8s.io/apimachinery/pkg/util/validation/field" - "k8s.io/utils/strings/slices" ) var ( @@ -75,7 +77,8 @@ func checkExistedConfig(spec *spiderpoolv2beta1.MultusCNIConfigSpec, exclude str } func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *field.Error { - switch multusConfig.Spec.CniType { + // with Kubernetes OpenAPI validation and Mutating Webhook, multusConfSpec.CniType must not be nil and default to "custom" + switch *multusConfig.Spec.CniType { case constant.MacvlanCNI: if multusConfig.Spec.MacvlanConfig == nil { return field.Required(macvlanConfigField, fmt.Sprintf("no %s specified", macvlanConfigField.String())) @@ -92,7 +95,7 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel } if checkExistedConfig(&(multusConfig.Spec), constant.MacvlanCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, macvlanConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, macvlanConfigField.String())) } case constant.IPVlanCNI: @@ -111,7 +114,7 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel } if checkExistedConfig(&(multusConfig.Spec), constant.IPVlanCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, ipvlanConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, ipvlanConfigField.String())) } case constant.SriovCNI: @@ -136,7 +139,7 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel } if checkExistedConfig(&(multusConfig.Spec), constant.SriovCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, sriovConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, sriovConfigField.String())) } case constant.IBSriovCNI: @@ -149,7 +152,7 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel } if checkExistedConfig(&(multusConfig.Spec), constant.IBSriovCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, sriovConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, sriovConfigField.String())) } case constant.IPoIBCNI: @@ -162,7 +165,7 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel } if checkExistedConfig(&(multusConfig.Spec), constant.IPoIBCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, sriovConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, sriovConfigField.String())) } case constant.OvsCNI: @@ -199,13 +202,13 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel } if checkExistedConfig(&(multusConfig.Spec), constant.OvsCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, sriovConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, sriovConfigField.String())) } case constant.CustomCNI: // multusConfig.Spec.CustomCNIConfig can be empty if checkExistedConfig(&(multusConfig.Spec), constant.CustomCNI) { - return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", multusConfig.Spec.CniType, customCniConfigField.String())) + return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", *multusConfig.Spec.CniType, customCniConfigField.String())) } if multusConfig.Spec.CustomCNIConfig != nil && *multusConfig.Spec.CustomCNIConfig != "" { diff --git a/pkg/multuscniconfig/multusconfig_webhook.go b/pkg/multuscniconfig/multusconfig_webhook.go index 2c20b26461..d89d09cc36 100644 --- a/pkg/multuscniconfig/multusconfig_webhook.go +++ b/pkg/multuscniconfig/multusconfig_webhook.go @@ -43,14 +43,11 @@ func (*MultusConfigWebhook) Default(ctx context.Context, obj runtime.Object) err smc := obj.(*spiderpoolv2beta1.SpiderMultusConfig) mutateLogger := logger.Named("Mutating").With( - zap.String("Spidermultusconfig", smc.Name)) - mutateLogger.Sugar().Debugf("Request Spidermultusconfig: %+v", *smc) + zap.String("SpiderMultusConfig", smc.Name)) + mutateLogger.Sugar().Debugf("Request SpiderMultusConfig: %+v", *smc) - if err := mutateSpiderMultusConfig(logutils.IntoContext(ctx, mutateLogger), smc); err != nil { - mutateLogger.Sugar().Errorf("Failed to mutate Spidermultusconfig: %v", err) - } - - mutateLogger.Sugar().Debugf("Finish Spidermultusconfig: %+v", smc) + mutateSpiderMultusConfig(logutils.IntoContext(ctx, mutateLogger), smc) + mutateLogger.Sugar().Debugf("Finish SpiderMultusConfig: %+v", smc) return nil } @@ -58,10 +55,10 @@ func (mcw *MultusConfigWebhook) ValidateCreate(ctx context.Context, obj runtime. multusConfig := obj.(*spiderpoolv2beta1.SpiderMultusConfig) log := logger.Named("Validating").With( - zap.String("MultusConfig", fmt.Sprintf("%s/%s", multusConfig.Namespace, multusConfig.Name)), + zap.String("SpiderMultusConfig", fmt.Sprintf("%s/%s", multusConfig.Namespace, multusConfig.Name)), zap.String("Operation", "CREATE"), ) - log.Sugar().Debugf("Request MultusConfig: %+v", *multusConfig) + log.Sugar().Debugf("Request SpiderMultusConfig: %+v", *multusConfig) err := validate(nil, multusConfig) if nil != err { @@ -80,11 +77,11 @@ func (mcw *MultusConfigWebhook) ValidateUpdate(ctx context.Context, oldObj, newO newMultusConfig := newObj.(*spiderpoolv2beta1.SpiderMultusConfig) log := logger.Named("Validating").With( - zap.String("MultusConfig", fmt.Sprintf("%s/%s", newMultusConfig.Namespace, newMultusConfig.Name)), + zap.String("SpiderMultusConfig", fmt.Sprintf("%s/%s", newMultusConfig.Namespace, newMultusConfig.Name)), zap.String("Operation", "UPDATE"), ) - log.Sugar().Debugf("Request old MultusConfig: %+v", *oldMultusConfig) - log.Sugar().Debugf("Request new MultusConfig: %+v", *newMultusConfig) + log.Sugar().Debugf("Request old SpiderMultusConfig: %+v", *oldMultusConfig) + log.Sugar().Debugf("Request new SpiderMultusConfig: %+v", *newMultusConfig) err := validate(oldMultusConfig, newMultusConfig) if nil != err { diff --git a/test/doc/spidermultus.md b/test/doc/spidermultus.md index c8e37dde5c..eb01f60364 100644 --- a/test/doc/spidermultus.md +++ b/test/doc/spidermultus.md @@ -1,23 +1,23 @@ # E2E Cases for spidermultus | Case ID | Title | Priority | Smoke | Status | Other | -|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------| -------- |-------| ------ | ----- | +|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------|----------|-------|--------| ----- | | M00001 | testing creating spiderMultusConfig with cniType: macvlan and checking the net-attach-conf config if works | p1 | smoke | done | | -| M00002 | testing creating spiderMultusConfig with cniType: ipvlan and checking the net-attach-conf config if works | p1 | smoke | done | | -| M00003 | testing creating spiderMultusConfig with cniType: sriov and checking the net-attach-conf config if works | p1 | smoke | done | | -| M00004 | testing creating spiderMultusConfig with cniType: custom and checking the net-attach-conf config if works | p1 | smoke | done | | -| M00005 | testing creating spiderMultusConfig with cniType: custom and invalid json config, expect error happened | p2 | | done | | -| M00007 | testing creating spiderMultusConfig with cniType: macvlan with vlanId with two master with bond config and checking the net-attach-conf config if works | p1 | smoke | | | -| M00011 | After deleting spiderMultusConfig, the corresponding net-attach-conf will also be deleted | p2 | | done | | -| M00013 | Update spidermultusConfig: add new bond config | p1 | smoke | | | -| M00014 | Manually delete the net-attach-conf of multus, it will be created automatically | p1 | | done | | -| M00015 | Customize net-attach-conf name via annotation multus.spidernet.io/cr-name | p2 | | done | | -| M00016 | webhook validation for multus.spidernet.io/cr-name | p3 | | done | | -| M00017 | Change net-attach-conf version via annotation multus.spidernet.io/cni-version | p2 | | done | | -| M00018 | webhook validation for multus.spidernet.io/cni-version | p3 | | done | | -| M00020 | Already have multus cr, spidermultus should take care of it | p3 | | done | | -| M00022 | The value of webhook verification cniType is inconsistent with cniConf | p3 | | done | | -| 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 | | done | | -| M00026 | set spidermultusconfig.spec to empty and see if works | p3 | | | | +| M00002 | testing creating spiderMultusConfig with cniType: ipvlan and checking the net-attach-conf config if works | p1 | smoke | done | | +| M00003 | testing creating spiderMultusConfig with cniType: sriov and checking the net-attach-conf config if works | p1 | smoke | done | | +| M00004 | testing creating spiderMultusConfig with cniType: custom and checking the net-attach-conf config if works | p1 | smoke | done | | +| M00005 | testing creating spiderMultusConfig with cniType: custom and invalid json config, expect error happened | p2 | | done | | +| M00007 | testing creating spiderMultusConfig with cniType: macvlan with vlanId with two master with bond config and checking the net-attach-conf config if works | p1 | smoke | | | +| M00011 | After deleting spiderMultusConfig, the corresponding net-attach-conf will also be deleted | p2 | | done | | +| M00013 | Update spidermultusConfig: add new bond config | p1 | smoke | | | +| M00014 | Manually delete the net-attach-conf of multus, it will be created automatically | p1 | | done | | +| M00015 | Customize net-attach-conf name via annotation multus.spidernet.io/cr-name | p2 | | done | | +| M00016 | webhook validation for multus.spidernet.io/cr-name | p3 | | done | | +| M00017 | Change net-attach-conf version via annotation multus.spidernet.io/cni-version | p2 | | done | | +| M00018 | webhook validation for multus.spidernet.io/cni-version | p3 | | done | | +| M00020 | Already have multus cr, spidermultus should take care of it | p3 | | done | | +| M00022 | The value of webhook verification cniType is inconsistent with cniConf | p3 | | done | | +| 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 | | done | | +| M00026 | set spidermultusconfig.spec to empty and see if works | p3 | | | | diff --git a/test/e2e/coordinator/macvlan-overlay-one/macvlan_overlay_one_test.go b/test/e2e/coordinator/macvlan-overlay-one/macvlan_overlay_one_test.go index 35c900b02a..1bf398153c 100644 --- a/test/e2e/coordinator/macvlan-overlay-one/macvlan_overlay_one_test.go +++ b/test/e2e/coordinator/macvlan-overlay-one/macvlan_overlay_one_test.go @@ -13,20 +13,19 @@ import ( "strings" "time" + kdoctorV1beta1 "github.com/kdoctor-io/kdoctor/pkg/k8s/apis/kdoctor.io/v1beta1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/spidernet-io/e2eframework/tools" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/utils/pointer" - kdoctorV1beta1 "github.com/kdoctor-io/kdoctor/pkg/k8s/apis/kdoctor.io/v1beta1" "github.com/spidernet-io/spiderpool/pkg/constant" "github.com/spidernet-io/spiderpool/pkg/ip" spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" "github.com/spidernet-io/spiderpool/pkg/types" "github.com/spidernet-io/spiderpool/test/e2e/common" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/spidernet-io/e2eframework/tools" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - apitypes "k8s.io/apimachinery/pkg/types" ) var r = rand.New(rand.NewSource(time.Now().UnixNano())) @@ -208,7 +207,7 @@ var _ = Describe("MacvlanOverlayOne", Label("overlay", "one-nic", "coordinator") Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, @@ -384,7 +383,7 @@ var _ = Describe("MacvlanOverlayOne", Label("overlay", "one-nic", "coordinator") Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, @@ -484,7 +483,7 @@ var _ = Describe("MacvlanOverlayOne", Label("overlay", "one-nic", "coordinator") Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, VlanID: pointer.Int32(200), @@ -694,7 +693,7 @@ var _ = Describe("MacvlanOverlayOne", Label("overlay", "one-nic", "coordinator") Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, diff --git a/test/e2e/ifacer/ifacer_test.go b/test/e2e/ifacer/ifacer_test.go index 9ac45810e5..67e629c42c 100644 --- a/test/e2e/ifacer/ifacer_test.go +++ b/test/e2e/ifacer/ifacer_test.go @@ -9,11 +9,13 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" + "github.com/spidernet-io/e2eframework/tools" + "github.com/spidernet-io/spiderpool/pkg/constant" spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" "github.com/spidernet-io/spiderpool/test/e2e/common" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" ) var _ = Describe("test ifacer", Label("ifacer"), func() { @@ -70,7 +72,7 @@ var _ = Describe("test ifacer", Label("ifacer"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, VlanID: pointer.Int32(int32(vlanInterface)), diff --git a/test/e2e/ippoolcr/ippoolcr_test.go b/test/e2e/ippoolcr/ippoolcr_test.go index 74358ac4fb..05fa1beb04 100644 --- a/test/e2e/ippoolcr/ippoolcr_test.go +++ b/test/e2e/ippoolcr/ippoolcr_test.go @@ -9,16 +9,17 @@ import ( "strings" "time" - "github.com/spidernet-io/spiderpool/pkg/constant" - spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" + "github.com/spidernet-io/e2eframework/tools" + "github.com/spidernet-io/spiderpool/pkg/constant" + spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" "github.com/spidernet-io/spiderpool/pkg/types" "github.com/spidernet-io/spiderpool/test/e2e/common" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) var _ = Describe("test ippool CR", Label("ippoolCR"), func() { @@ -479,7 +480,7 @@ var _ = Describe("test ippool CR", Label("ippoolCR"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, SpiderpoolConfigPools: &spiderpoolv2beta1.SpiderpoolPools{ diff --git a/test/e2e/spidermultus/spidermultus_suite_test.go b/test/e2e/spidermultus/spidermultus_suite_test.go index c06686727b..21e9eada8a 100644 --- a/test/e2e/spidermultus/spidermultus_suite_test.go +++ b/test/e2e/spidermultus/spidermultus_suite_test.go @@ -8,8 +8,9 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" e2e "github.com/spidernet-io/e2eframework/framework" - spiderpool "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" "k8s.io/apimachinery/pkg/runtime" + + spiderpool "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" ) func TestSpiderMultus(t *testing.T) { diff --git a/test/e2e/spidermultus/spidermultus_test.go b/test/e2e/spidermultus/spidermultus_test.go index b5c0d116f1..7709c06522 100644 --- a/test/e2e/spidermultus/spidermultus_test.go +++ b/test/e2e/spidermultus/spidermultus_test.go @@ -4,7 +4,10 @@ package spidermultus_test import ( "encoding/json" + "fmt" + "time" + v1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" api_errors "k8s.io/apimachinery/pkg/api/errors" @@ -12,14 +15,13 @@ import ( k8svalidation "k8s.io/apimachinery/pkg/util/validation" "k8s.io/utils/pointer" - v1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" "github.com/spidernet-io/spiderpool/pkg/constant" "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" "github.com/spidernet-io/spiderpool/test/e2e/common" ) -var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { +var _ = Describe("test spidermultus", Label("SpiderMultusConfig", "overlay"), func() { var namespace string BeforeEach(func() { @@ -54,7 +56,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, @@ -133,7 +135,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, @@ -237,7 +239,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, @@ -274,7 +276,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "ipvlan", + CniType: pointer.String(constant.IPVlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, }, @@ -296,7 +298,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, VlanID: pointer.Int32(-1), @@ -315,7 +317,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "macvlan", + CniType: pointer.String(constant.MacvlanCNI), MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{ Master: []string{common.NIC1}, VlanID: pointer.Int32(4095), @@ -338,7 +340,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "ipvlan", + CniType: pointer.String(constant.IPVlanCNI), IPVlanConfig: &spiderpoolv2beta1.SpiderIPvlanCniConfig{ Master: []string{common.NIC3}, }, @@ -371,7 +373,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "sriov", + CniType: pointer.String(constant.SriovCNI), SriovConfig: &spiderpoolv2beta1.SpiderSRIOVCniConfig{ ResourceName: "sriov-test", }, @@ -405,7 +407,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: v2beta1.MultusCNIConfigSpec{ - CniType: "custom", + CniType: pointer.String(constant.CustomCNI), CustomCNIConfig: &invalidJson, }, } @@ -446,7 +448,7 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { Namespace: namespace, }, Spec: spiderpoolv2beta1.MultusCNIConfigSpec{ - CniType: "sriov", + CniType: pointer.String(constant.SriovCNI), SriovConfig: &spiderpoolv2beta1.SpiderSRIOVCniConfig{ ResourceName: "spidernet.io/mellanoxrdma", EnableRdma: true, @@ -479,4 +481,28 @@ var _ = Describe("test spidermultus", Label("spiderMultus", "overlay"), func() { // rdma should be enabled Expect(rdmaMultusConfig.Spec.Config).Should(ContainSubstring(rdmaString)) }) + + It("set spidermultusconfig.spec to empty and see if works", Label("M00026"), func() { + smcName := "test-multus-" + common.GenerateString(10, true) + smc := &spiderpoolv2beta1.SpiderMultusConfig{ + ObjectMeta: metav1.ObjectMeta{ + Name: smcName, + Namespace: namespace, + }, + } + err := frame.CreateSpiderMultusInstance(smc) + Expect(err).NotTo(HaveOccurred()) + + Eventually(func() error { + netAttachDef, err := frame.GetMultusInstance(smcName, namespace) + if nil != err { + return err + } + if netAttachDef.Spec.Config != "" { + return fmt.Errorf("empty SpiderMultusConfig %s/%s corresponding net-attach-def resource has CNI configuration: %s", smcName, namespace, netAttachDef.Spec.Config) + } + + return nil + }).WithTimeout(time.Minute * 3).WithPolling(time.Second * 5).Should(BeNil()) + }) })