Skip to content

Commit

Permalink
Merge pull request #2603 from cyclinder/spidermultusconfig/file_empty…
Browse files Browse the repository at this point in the history
…_value

spidermultusconfig: set default value if it not set
  • Loading branch information
weizhoublue authored Nov 17, 2023
2 parents 9a799c0 + ac1c042 commit d97cc55
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 44 deletions.
28 changes: 28 additions & 0 deletions charts/spiderpool/templates/tls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ webhooks:
resources:
- spidersubnets
sideEffects: None
{{- if .Values.multus.enableMultusConfig }}
- admissionReviewVersions:
- v1
clientConfig:
service:
name: {{ .Values.spiderpoolController.name | trunc 63 | trimSuffix "-" }}
namespace: {{ .Release.Namespace }}
path: /mutate-spiderpool-spidernet-io-v2beta1-spidermultusconfig
port: {{ .Values.spiderpoolController.webhookPort }}
{{- if (eq .Values.spiderpoolController.tls.method "provided") }}
caBundle: {{ .Values.spiderpoolController.tls.provided.tlsCa | required "missing spiderpoolController.tls.provided.tlsCa" }}
{{- else if (eq .Values.spiderpoolController.tls.method "auto") }}
caBundle: {{ .ca.Cert | b64enc }}
{{- end }}
failurePolicy: Fail
name: spidermultusconfig.spiderpool.spidernet.io
rules:
- apiGroups:
- spiderpool.spidernet.io
apiVersions:
- v2beta1
operations:
- CREATE
- UPDATE
resources:
- spidermultusconfigs
sideEffects: None
{{- end }}
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
8 changes: 8 additions & 0 deletions pkg/constant/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,11 @@ const (
ResourceNameAnnot = "k8s.v1.cni.cncf.io/resourceName"
ResourceNameOvsCniValue = "ovs-cni.network.kubevirt.io"
)

const (
MacvlanCNI = "macvlan"
IPVlanCNI = "ipvlan"
SriovCNI = "sriov"
OvsCNI = "ovs"
CustomCNI = "custom"
)
6 changes: 4 additions & 2 deletions pkg/coordinatormanager/coordinator_mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package coordinatormanager
import (
"context"
"fmt"
"k8s.io/utils/pointer"
"net/netip"
"strings"

"k8s.io/utils/pointer"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

coordinator_cmd "github.com/spidernet-io/spiderpool/cmd/coordinator/cmd"
"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/logutils"
Expand All @@ -22,7 +24,7 @@ func mutateCoordinator(ctx context.Context, coord *spiderpoolv2beta1.SpiderCoord
logger.Info("Start to mutate Coordinator")

if coord.Spec.Mode == nil {
coord.Spec.Mode = pointer.String("underlay")
coord.Spec.Mode = pointer.String(string(coordinator_cmd.ModeAuto))
}
if coord.Spec.TunePodRoutes == nil {
coord.Spec.TunePodRoutes = pointer.Bool(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type CoordinatorSpec struct {
// +kubebuilder:validation:Enum=auto;underlay;overlay;disabled
// +kubebuilder:validation:Optional
Mode *string `json:"mode,omitempty"`
Mode *string `json:"mode"`

// CoordinatorSpec is used by SpiderCoordinator and SpiderMultusConfig
// in spidermultusconfig CRD , podCIDRType should not be required, which could be merged from SpiderCoordinator CR
Expand All @@ -21,16 +21,16 @@ type CoordinatorSpec struct {
PodCIDRType *string `json:"podCIDRType,omitempty"`

// +kubebuilder:validation:Optional
HijackCIDR []string `json:"hijackCIDR,omitempty"`
HijackCIDR []string `json:"hijackCIDR"`

// +kubebuilder:validation:Optional
PodMACPrefix *string `json:"podMACPrefix,omitempty"`
PodMACPrefix *string `json:"podMACPrefix"`

// +kubebuilder:validation:Optional
TunePodRoutes *bool `json:"tunePodRoutes,omitempty"`
TunePodRoutes *bool `json:"tunePodRoutes"`

// +kubebuilder:validation:Optional
PodDefaultRouteNIC *string `json:"podDefaultRouteNIC,omitempty"`
PodDefaultRouteNIC *string `json:"podDefaultRouteNIC"`

// +kubebuilder:validation:Optional
HostRuleTable *int `json:"hostRuleTable,omitempty"`
Expand All @@ -39,10 +39,10 @@ type CoordinatorSpec struct {
HostRPFilter *int `json:"hostRPFilter,omitempty"`

// +kubebuilder:validation:Optional
DetectIPConflict *bool `json:"detectIPConflict,omitempty"`
DetectIPConflict *bool `json:"detectIPConflict"`

// +kubebuilder:validation:Optional
DetectGateway *bool `json:"detectGateway,omitempty"`
DetectGateway *bool `json:"detectGateway"`
}

// CoordinationStatus defines the observed state of SpiderCoordinator.
Expand Down
26 changes: 13 additions & 13 deletions pkg/k8s/apis/spiderpool.spidernet.io/v2beta1/spidermultus_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type MultusCNIConfigSpec struct {
DisableIPAM *bool `json:"disableIPAM"`

// +kubebuilder:validation:Optional
CoordinatorConfig *CoordinatorSpec `json:"coordinator,omitempty"`
CoordinatorConfig *CoordinatorSpec `json:"coordinator"`

// OtherCniTypeConfig only used for CniType custom, valid json format, can be empty
// +kubebuilder:validation:Optional
Expand All @@ -66,13 +66,13 @@ type SpiderMacvlanCniConfig struct {
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=4094
VlanID *int32 `json:"vlanID,omitempty"`
VlanID *int32 `json:"vlanID"`

// +kubebuilder:validation:Optional
Bond *BondConfig `json:"bond,omitempty"`
Bond *BondConfig `json:"bond"`

// +kubebuilder:validation:Optional
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools,omitempty"`
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools"`
}

type SpiderIPvlanCniConfig struct {
Expand All @@ -82,13 +82,13 @@ type SpiderIPvlanCniConfig struct {
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=4094
VlanID *int32 `json:"vlanID,omitempty"`
VlanID *int32 `json:"vlanID"`

// +kubebuilder:validation:Optional
Bond *BondConfig `json:"bond,omitempty"`
Bond *BondConfig `json:"bond"`

// +kubebuilder:validation:Optional
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools,omitempty"`
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools"`
}

type SpiderSRIOVCniConfig struct {
Expand All @@ -98,14 +98,14 @@ type SpiderSRIOVCniConfig struct {
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=4094
VlanID *int32 `json:"vlanID,omitempty"`
VlanID *int32 `json:"vlanID"`

// +kubebuilder:default=false
// +kubebuilder:validation:Optional
EnableRdma bool `json:"enableRdma"`

// +kubebuilder:validation:Optional
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools,omitempty"`
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools"`
}

type SpiderOvsCniConfig struct {
Expand All @@ -119,7 +119,7 @@ type SpiderOvsCniConfig struct {
// PCI address of a VF in valid sysfs format
DeviceID string `json:"deviceID"`
// +kubebuilder:validation:Optional
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools,omitempty"`
SpiderpoolConfigPools *SpiderpoolPools `json:"ippools"`
}

type Trunk struct {
Expand Down Expand Up @@ -147,16 +147,16 @@ type BondConfig struct {
Mode int32 `json:"mode"`

// +kubebuilder:validation:Optional
Options *string `json:"options,omitempty"`
Options *string `json:"options"`
}

// SpiderpoolPools could specify the IPAM spiderpool CNI configuration default IPv4&IPv6 pools.
type SpiderpoolPools struct {
// +kubebuilder:validation:Optional
IPv4IPPool []string `json:"ipv4,omitempty"`
IPv4IPPool []string `json:"ipv4"`

// +kubebuilder:validation:Optional
IPv6IPPool []string `json:"ipv6,omitempty"`
IPv6IPPool []string `json:"ipv6"`
}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/multuscniconfig/multusconfig_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.Sp
macvlanCNIConf := generateMacvlanCNIConf(disableIPAM, *multusConfSpec)
// head insertion
plugins = append([]interface{}{macvlanCNIConf}, plugins...)
if multusConfSpec.MacvlanConfig.VlanID != nil && *multusConfSpec.MacvlanConfig.VlanID != 0 {
if len(multusConfSpec.MacvlanConfig.Master) > 0 || (multusConfSpec.MacvlanConfig.VlanID != nil && *multusConfSpec.MacvlanConfig.VlanID != 0) {
// we need to set Subvlan as first at the CNI plugin chain
subVlanCNIConf := generateIfacer(multusConfSpec.MacvlanConfig.Master,
*multusConfSpec.MacvlanConfig.VlanID,
Expand All @@ -402,7 +402,7 @@ func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.Sp
ipvlanCNIConf := generateIPvlanCNIConf(disableIPAM, *multusConfSpec)
// head insertion
plugins = append([]interface{}{ipvlanCNIConf}, plugins...)
if multusConfSpec.IPVlanConfig.VlanID != nil && *multusConfSpec.IPVlanConfig.VlanID != 0 {
if len(multusConfSpec.IPVlanConfig.Master) > 0 || (multusConfSpec.IPVlanConfig.VlanID != nil && *multusConfSpec.IPVlanConfig.VlanID != 0) {
// we need to set Subvlan as first at the CNI plugin chain
subVlanCNIConf := generateIfacer(multusConfSpec.IPVlanConfig.Master,
*multusConfSpec.IPVlanConfig.VlanID,
Expand Down
168 changes: 168 additions & 0 deletions pkg/multuscniconfig/multusconfig_mutate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0
package multuscniconfig

import (
"context"

"k8s.io/utils/pointer"

coordinator_cmd "github.com/spidernet-io/spiderpool/cmd/coordinator/cmd"
"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/logutils"
)

func mutateSpiderMultusConfig(ctx context.Context, smc *spiderpoolv2beta1.SpiderMultusConfig) error {
logger := logutils.FromContext(ctx)
logger.Info("Start to mutate Spidermulutconfig")

switch smc.Spec.CniType {
case constant.MacvlanCNI:
setMacvlanDefaultConfig(smc.Spec.MacvlanConfig)
case constant.IPVlanCNI:
setIPVlanDefaultConfig(smc.Spec.IPVlanConfig)
case constant.SriovCNI:
setSriovDefaultConfig(smc.Spec.SriovConfig)
case constant.OvsCNI:
setOvsDefaultConfig(smc.Spec.OvsConfig)
case constant.CustomCNI:
if smc.Spec.CustomCNIConfig == nil {
smc.Spec.CustomCNIConfig = pointer.String("")
}
}

smc.Spec.CoordinatorConfig = setCoordinatorDefaultConfig(smc.Spec.CoordinatorConfig)
return nil
}

func setMacvlanDefaultConfig(macvlanConfig *spiderpoolv2beta1.SpiderMacvlanCniConfig) {
if macvlanConfig == nil {
return
}

if macvlanConfig.VlanID == nil {
macvlanConfig.VlanID = pointer.Int32(0)
}

macvlanConfig.Bond = setBondDefaultConfig(macvlanConfig.Bond)

if macvlanConfig.SpiderpoolConfigPools == nil {
macvlanConfig.SpiderpoolConfigPools = &spiderpoolv2beta1.SpiderpoolPools{
IPv4IPPool: []string{},
IPv6IPPool: []string{},
}
}
}

func setBondDefaultConfig(bond *spiderpoolv2beta1.BondConfig) *spiderpoolv2beta1.BondConfig {
if bond == nil {
return &spiderpoolv2beta1.BondConfig{
Name: "",
Mode: 0,
Options: pointer.String(""),
}
}

if bond.Options == nil {
bond.Options = pointer.String("")
}
return bond
}

func setIPVlanDefaultConfig(ipvlanConfig *spiderpoolv2beta1.SpiderIPvlanCniConfig) {
if ipvlanConfig == nil {
return
}

if ipvlanConfig.VlanID == nil {
ipvlanConfig.VlanID = pointer.Int32(0)
}

ipvlanConfig.Bond = setBondDefaultConfig(ipvlanConfig.Bond)

if ipvlanConfig.SpiderpoolConfigPools == nil {
ipvlanConfig.SpiderpoolConfigPools = &spiderpoolv2beta1.SpiderpoolPools{
IPv4IPPool: []string{},
IPv6IPPool: []string{},
}
}
}

func setSriovDefaultConfig(sriovConfig *spiderpoolv2beta1.SpiderSRIOVCniConfig) {
if sriovConfig == nil {
return
}

if sriovConfig.VlanID == nil {
sriovConfig.VlanID = pointer.Int32(0)
}

if sriovConfig.SpiderpoolConfigPools == nil {
sriovConfig.SpiderpoolConfigPools = &spiderpoolv2beta1.SpiderpoolPools{
IPv4IPPool: []string{},
IPv6IPPool: []string{},
}
}
}

func setOvsDefaultConfig(ovsConfig *spiderpoolv2beta1.SpiderOvsCniConfig) {
if ovsConfig == nil {
return
}

if ovsConfig.VlanTag == nil {
ovsConfig.VlanTag = pointer.Int32(0)
}

if ovsConfig.SpiderpoolConfigPools == nil {
ovsConfig.SpiderpoolConfigPools = &spiderpoolv2beta1.SpiderpoolPools{
IPv4IPPool: []string{},
IPv6IPPool: []string{},
}
}
}

func setCoordinatorDefaultConfig(coordinator *spiderpoolv2beta1.CoordinatorSpec) *spiderpoolv2beta1.CoordinatorSpec {
if coordinator == nil {
return &spiderpoolv2beta1.CoordinatorSpec{
Mode: pointer.String(string(coordinator_cmd.ModeAuto)),
HijackCIDR: []string{},
DetectGateway: pointer.Bool(false),
DetectIPConflict: pointer.Bool(false),
PodMACPrefix: pointer.String(""),
PodDefaultRouteNIC: pointer.String(""),
TunePodRoutes: pointer.Bool(true),
}
}

if coordinator.Mode == nil {
coordinator.Mode = pointer.String(string(coordinator_cmd.ModeAuto))
}

if len(coordinator.HijackCIDR) == 0 {
coordinator.HijackCIDR = []string{}
}

if coordinator.DetectGateway == nil {
coordinator.DetectGateway = pointer.Bool(false)
}

if coordinator.DetectIPConflict == nil {
coordinator.DetectIPConflict = pointer.Bool(false)
}

if coordinator.PodMACPrefix == nil {
coordinator.PodMACPrefix = pointer.String("")
}

if coordinator.PodDefaultRouteNIC == nil {
coordinator.PodDefaultRouteNIC = pointer.String("")
}

if coordinator.TunePodRoutes == nil {
coordinator.TunePodRoutes = pointer.Bool(false)
}

return coordinator
}
Loading

0 comments on commit d97cc55

Please sign in to comment.