Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: oldthreefeng <[email protected]>
  • Loading branch information
oldthreefeng committed Sep 3, 2021
1 parent b6144f3 commit ce582a6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 103 deletions.
137 changes: 47 additions & 90 deletions install/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,28 @@ const (
DefaultContainerdCRISocket = "/run/containerd/containerd.sock"
DefaultCgroupDriver = "cgroupfs"
DefaultSystemdCgroupDriver = "systemd"

KubeadmV1beta1 = "kubeadm.k8s.io/v1beta1"
KubeadmV1beta2 = "kubeadm.k8s.io/v1beta2"
KubeadmV1beta3 = "kubeadm.k8s.io/v1beta3"
Bootstraptokenv1 = "bootstraptoken/v1"
)

const InitTemplateTextV1beta1 = string(`apiVersion: kubeadm.k8s.io/v1beta1
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: {{.Master0}}
bindPort: 6443
---
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: {{.Version}}
controlPlaneEndpoint: "{{.ApiServer}}:6443"
imageRepository: {{.Repo}}
networking:
# dnsDomain: cluster.local
podSubnet: {{.PodCIDR}}
serviceSubnet: {{.SvcCIDR}}
apiServer:
certSANs:
- 127.0.0.1
- {{.ApiServer}}
{{range .Masters -}}
- {{.}}
{{end -}}
{{range .CertSANS -}}
- {{.}}
{{end -}}
- {{.VIP}}
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- name: localtime
hostPath: /etc/localtime
mountPath: /etc/localtime
readOnly: true
pathType: File
controllerManager:
extraArgs:
feature-gates: TTLAfterFinished=true
experimental-cluster-signing-duration: 876000h
{{- if eq .Network "cilium" }}
allocate-node-cidrs: \"true\"
{{- end }}
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
scheduler:
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
excludeCIDRs:
- "{{.VIP}}/32"
---
` + kubeletConfigDefault)

const JoinCPTemplateTextV1beta2 = string(`apiVersion: kubeadm.k8s.io/v1beta2
const (
InitTemplateText = string(InitConfigurationDefault +
ClusterConfigurationDefault +
kubeproxyConfigDefault +
kubeletConfigDefault)
JoinCPTemplateText = string(bootstrapTokenDefault +
JoinConfigurationDefault +
kubeletConfigDefault)

bootstrapTokenDefault = `{{- if .BootstrapApi -eq "bootstraptoken/v1" }}
apiVersion: {{.BootstrapApi}}
{{- else}}
apiVersion: {{.KubeadmApi}}
{{- end}}
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
bootstrapToken:
Expand All @@ -110,6 +63,17 @@ discovery:
caCertHashes:
- {{.TokenDiscoveryCAHash}}
timeout: 5m0s
`
InitConfigurationDefault = `apiVersion: {{.KubeadmApi}}
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: {{.Master0}}
bindPort: 6443
nodeRegistration:
criSocket: {{.CriSocket}}
`

JoinConfigurationDefault = `
kind: JoinConfiguration
{{- if .Master }}
controlPlane:
Expand All @@ -119,18 +83,10 @@ controlPlane:
{{- end}}
nodeRegistration:
criSocket: {{.CriSocket}}
---
` + kubeletConfigDefault)
`

const InitTemplateTextV1bate2 = string(`apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: {{.Master0}}
bindPort: 6443
nodeRegistration:
criSocket: /run/containerd/containerd.sock
---
apiVersion: kubeadm.k8s.io/v1beta2
ClusterConfigurationDefault = `---
apiVersion: {{.KubeadmApi}}
kind: ClusterConfiguration
kubernetesVersion: {{.Version}}
controlPlaneEndpoint: "{{.ApiServer}}:6443"
Expand Down Expand Up @@ -180,27 +136,19 @@ scheduler:
name: localtime
readOnly: true
pathType: File
`
kubeproxyConfigDefault = `
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
excludeCIDRs:
- "{{.VIP}}/32"
`
kubeletConfigDefault = `
---
` + kubeletConfigDefault)

const (
ContainerdShell = `if grep "SystemdCgroup = true" /etc/containerd/config.toml &> /dev/null; then
driver=systemd
else
driver=cgroupfs
fi
echo ${driver}`
DockerShell = `driver=$(docker info -f "{{.CgroupDriver}}")
echo "${driver}"`

kubeletConfigDefault = `apiVersion: kubelet.config.k8s.io/v1beta1
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
authentication:
anonymous:
Expand Down Expand Up @@ -269,4 +217,13 @@ staticPodPath: /etc/kubernetes/manifests
streamingConnectionIdleTimeout: 4h0m0s
syncFrequency: 1m0s
volumeStatsAggPeriod: 1m0s`

ContainerdShell = `if grep "SystemdCgroup = true" /etc/containerd/config.toml &> /dev/null; then
driver=systemd
else
driver=cgroupfs
fi
echo ${driver}`
DockerShell = `driver=$(docker info -f "{{.CgroupDriver}}")
echo "${driver}"`
)
39 changes: 26 additions & 13 deletions install/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ import (

var ConfigType string

func setKubeadmApi(version string) {
major ,_ := GetMajorMinorInt(version)
switch {
//
case major < 120:
KubeadmApi = KubeadmV1beta1
CriSocket = DefaultDockerCRISocket
case major < 123 && major >= 120 :
KubeadmApi = KubeadmV1beta2
CriSocket = DefaultContainerdCRISocket
case major >= 123 :
KubeadmApi = KubeadmV1beta3
CriSocket = DefaultContainerdCRISocket
default:
KubeadmApi = KubeadmV1beta3
CriSocket = DefaultContainerdCRISocket
BootstrapApi = Bootstraptokenv1
}
logger.Info("KubeadmApi: %s", KubeadmApi)
logger.Info("CriSocket: %s", CriSocket)
}

func Config() {
switch ConfigType {
case "kubeadm":
Expand All @@ -24,7 +46,7 @@ func Config() {

func joinKubeadmConfig() string {
var sb strings.Builder
sb.Write([]byte(JoinCPTemplateTextV1beta2))
sb.Write([]byte(JoinCPTemplateText))
return sb.String()
}

Expand All @@ -34,13 +56,7 @@ func printlnJoinKubeadmConfig() {

func kubeadmConfig() string {
var sb strings.Builder
// kubernetes gt 1.20, use Containerd instead of docker
if For120(Version) {
sb.Write([]byte(InitTemplateTextV1bate2))
} else {
sb.Write([]byte(InitTemplateTextV1beta1))
}

sb.Write([]byte(InitTemplateText))
return sb.String()
}

Expand All @@ -59,6 +75,7 @@ func JoinTemplate(ip string, cgroup string) []byte {
}

func JoinTemplateFromTemplateContent(templateContent, ip, cgroup string) []byte {
setKubeadmApi(Version)
tmpl, err := template.New("text").Parse(templateContent)
defer func() {
if r := recover(); r != nil {
Expand All @@ -74,11 +91,7 @@ func JoinTemplateFromTemplateContent(templateContent, ip, cgroup string) []byte
envMap["TokenDiscovery"] = JoinToken
envMap["TokenDiscoveryCAHash"] = TokenCaCertHash
envMap["VIP"] = VIP
if For120(Version) {
CriSocket = DefaultContainerdCRISocket
} else {
CriSocket = DefaultDockerCRISocket
}
envMap["KubeadmApi"] = KubeadmApi
envMap["CriSocket"] = CriSocket
envMap["CgroupDriver"] = cgroup
var buffer bytes.Buffer
Expand Down
2 changes: 2 additions & 0 deletions install/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
//criSocket
CriSocket string
CgroupDriver string
KubeadmApi string
BootstrapApi string

VIP string
PkgUrl string
Expand Down

0 comments on commit ce582a6

Please sign in to comment.