Skip to content

Commit

Permalink
Refine calico configuration
Browse files Browse the repository at this point in the history
Signed-off-by: pixiake <[email protected]>
  • Loading branch information
pixiake committed May 23, 2024
1 parent 5594785 commit 5ca5c05
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
34 changes: 22 additions & 12 deletions cmd/kk/apis/kubekey/v1alpha2/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ 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"`
Replicas int `yaml:"replicas" json:"replicas,omitempty"`
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector,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"`
Typha Typha `yaml:"typha" json:"typha,omitempty"`
Controller Controller `yaml:"controller" json:"controller,omitempty"`
}

type Typha struct {
Replicas int `yaml:"replicas" json:"replicas,omitempty"`
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector,omitempty"`
Enabled *bool `yaml:"enabled" json:"enabled,omitempty"`
}

type Controller struct {
Replicas int `yaml:"replicas" json:"replicas,omitempty"`
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector,omitempty"`
}

type FlannelCfg struct {
Expand Down Expand Up @@ -186,12 +196,12 @@ func (c *CalicoCfg) EnableDefaultIPPOOL() bool {
return *c.DefaultIPPOOL
}

// Typha is used to determine whether to enable calico Typha
func (c *CalicoCfg) Typha() bool {
if c.EnableTypha == nil {
// EnableTypha is used to determine whether to enable calico Typha
func (c *CalicoCfg) EnableTypha() bool {
if c.Typha.Enabled == nil {
return false
}
return *c.EnableTypha
return *c.Typha.Enabled
}

// EnableInit is used to determine whether to create default network
Expand Down
8 changes: 5 additions & 3 deletions cmd/kk/pkg/plugins/network/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (g *GenerateCalicoManifests) Execute(runtime connector.Runtime) error {
"CalicoFlexvolImage": images.GetImage(runtime, g.KubeConf, "calico-flexvol").ImageName(),
"CalicoControllersImage": images.GetImage(runtime, g.KubeConf, "calico-kube-controllers").ImageName(),
"CalicoTyphaImage": images.GetImage(runtime, g.KubeConf, "calico-typha").ImageName(),
"TyphaEnabled": len(runtime.GetHostsByRole(common.K8s)) > 50 || g.KubeConf.Cluster.Network.Calico.Typha(),
"TyphaEnabled": len(runtime.GetHostsByRole(common.K8s)) > 50 || g.KubeConf.Cluster.Network.Calico.EnableTypha(),
"VethMTU": g.KubeConf.Cluster.Network.Calico.VethMTU,
"NodeCidrMaskSize": g.KubeConf.Cluster.Kubernetes.NodeCidrMaskSize,
"IPIPMode": g.KubeConf.Cluster.Network.Calico.IPIPMode,
Expand All @@ -469,8 +469,10 @@ 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,
"TyphaReplicas": g.KubeConf.Cluster.Network.Calico.Typha.Replicas,
"TyphaNodeSelector": g.KubeConf.Cluster.Network.Calico.Typha.NodeSelector,
"ControllerReplicas": g.KubeConf.Cluster.Network.Calico.Controller.Replicas,
"ControllerNodeSelector": g.KubeConf.Cluster.Network.Calico.Controller.NodeSelector,
},
}
templateAction.Init(nil, nil)
Expand Down
16 changes: 8 additions & 8 deletions cmd/kk/pkg/plugins/network/templates/calico.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5171,7 +5171,7 @@ metadata:
k8s-app: calico-kube-controllers
spec:
# The controllers can only have a single active instance.
replicas: {{ if .Replicas }}{{ .Replicas }}{{ else }}1{{ end }}
replicas: {{ if .ControllerReplicas }}{{ .ControllerReplicas }}{{ else }}1{{ end }}
selector:
matchLabels:
k8s-app: calico-kube-controllers
Expand All @@ -5186,8 +5186,8 @@ spec:
spec:
nodeSelector:
kubernetes.io/os: linux
{{ if .NodeSelector }}
{{- toYaml .NodeSelector | indent 8 }}
{{ if .ControllerNodeSelector }}
{{- toYaml .ControllerNodeSelector | indent 8 }}
{{- end }}
tolerations:
# Mark the pod as a critical add-on for rescheduling.
Expand All @@ -5197,7 +5197,7 @@ spec:
effect: NoSchedule
- key: node-role.kubernetes.io/control-plane
effect: NoSchedule
{{ if .NodeSelector }}
{{ if .ControllerNodeSelector }}
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
Expand Down Expand Up @@ -5258,7 +5258,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: {{ if .Replicas }}{{ .Replicas }}{{ else }}1{{ end }}
replicas: {{ if .TyphaReplicas }}{{ .TyphaReplicas }}{{ else }}1{{ end }}
revisionHistoryLimit: 2
selector:
matchLabels:
Expand All @@ -5285,14 +5285,14 @@ spec:
spec:
nodeSelector:
kubernetes.io/os: linux
{{ if .NodeSelector }}
{{- toYaml .NodeSelector | indent 8 }}
{{ if .TyphaNodeSelector }}
{{- toYaml .TyphaNodeSelector | 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 }}
{{ if .TyphaNodeSelector }}
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
Expand Down

0 comments on commit 5ca5c05

Please sign in to comment.