Skip to content

Commit

Permalink
support empty config with custom type
Browse files Browse the repository at this point in the history
Signed-off-by: Icarus9913 <[email protected]>
  • Loading branch information
Icarus9913 committed Dec 15, 2023
1 parent fe936b5 commit 2833db7
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spec:
description: Spec is the specification of the MultusCNIConfig
properties:
cniType:
default: custom
enum:
- macvlan
- ipvlan
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -312,8 +315,6 @@ spec:
required:
- resourceName
type: object
required:
- cniType
type: object
type: object
served: true
Expand Down
14 changes: 8 additions & 6 deletions cmd/spiderpool-init/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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),
},
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/spiderpool-init/cmd/multus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 14 additions & 18 deletions pkg/multuscniconfig/multusconfig_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions pkg/multuscniconfig/multusconfig_mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 2833db7

Please sign in to comment.