From 49239112fedcb0594951f7543384a23d46c541c3 Mon Sep 17 00:00:00 2001 From: pixiake Date: Sun, 7 Apr 2024 09:12:18 +0800 Subject: [PATCH] add replicas and nodeselector fields for calico Signed-off-by: pixiake (cherry picked from commit 0652e816b7cda151d7866ff1b6eef453402d7258) --- cmd/kk/apis/kubekey/v1alpha2/network_types.go | 14 ++++--- cmd/kk/pkg/plugins/network/tasks.go | 6 ++- .../pkg/plugins/network/templates/calico.tmpl | 38 ++++++++++++++++++- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/cmd/kk/apis/kubekey/v1alpha2/network_types.go b/cmd/kk/apis/kubekey/v1alpha2/network_types.go index af0b1b6f4..727b4db81 100644 --- a/cmd/kk/apis/kubekey/v1alpha2/network_types.go +++ b/cmd/kk/apis/kubekey/v1alpha2/network_types.go @@ -28,12 +28,14 @@ type NetworkConfig struct { } type CalicoCfg struct { - IPIPMode string `yaml:"ipipMode" json:"ipipMode,omitempty"` - VXLANMode string `yaml:"vxlanMode" json:"vxlanMode,omitempty"` - VethMTU int `yaml:"vethMTU" json:"vethMTU,omitempty"` - Ipv4NatOutgoing *bool `yaml:"ipv4NatOutgoing" json:"ipv4NatOutgoing,omitempty"` - DefaultIPPOOL *bool `yaml:"defaultIPPOOL" json:"defaultIPPOOL,omitempty"` - EnableTypha *bool `yaml:"enableTypha" json:"enableTypha,omitempty"` + IPIPMode string `yaml:"ipipMode" json:"ipipMode,omitempty"` + VXLANMode string `yaml:"vxlanMode" json:"vxlanMode,omitempty"` + VethMTU int `yaml:"vethMTU" json:"vethMTU,omitempty"` + Ipv4NatOutgoing *bool `yaml:"ipv4NatOutgoing" json:"ipv4NatOutgoing,omitempty"` + DefaultIPPOOL *bool `yaml:"defaultIPPOOL" json:"defaultIPPOOL,omitempty"` + EnableTypha *bool `yaml:"enableTypha" json:"enableTypha,omitempty"` + Replicas int `yaml:"replicas" json:"replicas,omitempty"` + NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector,omitempty"` } type FlannelCfg struct { diff --git a/cmd/kk/pkg/plugins/network/tasks.go b/cmd/kk/pkg/plugins/network/tasks.go index 86efe28e6..08f5d066b 100644 --- a/cmd/kk/pkg/plugins/network/tasks.go +++ b/cmd/kk/pkg/plugins/network/tasks.go @@ -26,6 +26,8 @@ import ( "text/template" "time" + "github.com/kubesphere/kubekey/v3/cmd/kk/pkg/utils" + "github.com/pkg/errors" "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2" @@ -437,7 +439,7 @@ func (g *GenerateCalicoManifests) Execute(runtime connector.Runtime) error { if err != nil { return err } - calico := template.Must(template.New("network-plugin.yaml").Parse(string(calicoContent))) + calico := template.Must(template.New("network-plugin.yaml").Funcs(utils.FuncMap).Parse(string(calicoContent))) IPv6Support := false kubePodsV6CIDR := "" @@ -467,6 +469,8 @@ func (g *GenerateCalicoManifests) Execute(runtime connector.Runtime) error { "IPV4POOLNATOUTGOING": g.KubeConf.Cluster.Network.Calico.EnableIPV4POOL_NAT_OUTGOING(), "DefaultIPPOOL": g.KubeConf.Cluster.Network.Calico.EnableDefaultIPPOOL(), "IPv6Support": IPv6Support, + "Replicas": g.KubeConf.Cluster.Network.Calico.Replicas, + "NodeSelector": g.KubeConf.Cluster.Network.Calico.NodeSelector, }, } templateAction.Init(nil, nil) diff --git a/cmd/kk/pkg/plugins/network/templates/calico.tmpl b/cmd/kk/pkg/plugins/network/templates/calico.tmpl index 73c391022..1d0a4e025 100644 --- a/cmd/kk/pkg/plugins/network/templates/calico.tmpl +++ b/cmd/kk/pkg/plugins/network/templates/calico.tmpl @@ -5161,7 +5161,7 @@ metadata: k8s-app: calico-kube-controllers spec: # The controllers can only have a single active instance. - replicas: 1 + replicas: {{ if .Replicas }}{{ .Replicas }}{{ else }}1{{ end }} selector: matchLabels: k8s-app: calico-kube-controllers @@ -5176,6 +5176,9 @@ spec: spec: nodeSelector: kubernetes.io/os: linux +{{ if .NodeSelector }} + {{- toYaml .NodeSelector | indent 8 }} +{{- end }} tolerations: # Mark the pod as a critical add-on for rescheduling. - key: CriticalAddonsOnly @@ -5184,6 +5187,20 @@ spec: effect: NoSchedule - key: node-role.kubernetes.io/control-plane effect: NoSchedule +{{ if .NodeSelector }} + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: k8s-app + operator: In + values: + - calico-kube-controllers + topologyKey: kubernetes.io/hostname +{{- end }} serviceAccountName: calico-kube-controllers priorityClassName: system-cluster-critical containers: @@ -5231,7 +5248,7 @@ spec: # We recommend using Typha if you have more than 50 nodes. Above 100 nodes it is essential # (when using the Kubernetes datastore). Use one replica for every 100-200 nodes. In # production, we recommend running at least 3 replicas to reduce the impact of rolling upgrade. - replicas: 1 + replicas: {{ if .Replicas }}{{ .Replicas }}{{ else }}1{{ end }} revisionHistoryLimit: 2 selector: matchLabels: @@ -5258,10 +5275,27 @@ spec: spec: nodeSelector: kubernetes.io/os: linux +{{ if .NodeSelector }} + {{- toYaml .NodeSelector | indent 8 }} +{{- end }} hostNetwork: true # Typha supports graceful shut down, disconnecting clients slowly during the grace period. # The TYPHA_SHUTDOWNTIMEOUTSECS env var should be kept in sync with this value. terminationGracePeriodSeconds: 300 +{{ if .NodeSelector }} + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: k8s-app + operator: In + values: + - calico-typha + topologyKey: kubernetes.io/hostname +{{- end }} tolerations: # Mark the pod as a critical add-on for rescheduling. - key: CriticalAddonsOnly