11package service
22
33import (
4- "bytes"
54 "encoding/base64"
65 "fmt"
76 "io/ioutil"
87 "os"
98 "path"
10- "text/template"
119
1210 "github.com/autonomy/dianemo/initramfs/cmd/init/pkg/constants"
1311 "github.com/autonomy/dianemo/initramfs/cmd/init/pkg/service/conditions"
1412 "github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
1513)
1614
17- // MasterConfiguration is the kubeadm manifest for master nodes.
18- const MasterConfiguration = `
19- kind: MasterConfiguration
20- apiVersion: kubeadm.k8s.io/v1alpha1
21- kubernetesVersion: v1.10.2
22- token: {{ .Token }}
23- tokenTTL: 0s
24- criSocket: {{ .CRISocket }}
25- networking:
26- dnsDomain: cluster.local
27- serviceSubnet: 10.96.0.0/12
28- podSubnet: 10.244.0.0/16
29- kubeProxy:
30- config:
31- mode: ipvs
32- featureGates:
33- HighAvailability: true
34- SelfHosting: false
35- StoreCertsInSecrets: false
36- DynamicKubeletConfig: true
37- CoreDNS: true
38- `
39-
40- // NodeConfiguration is the kubeadm manifest for worker nodes.
41- const NodeConfiguration = `
42- kind: NodeConfiguration
43- apiVersion: kubeadm.k8s.io/v1alpha1
44- token: {{ .Token }}
45- discoveryTokenAPIServers:
46- - {{ .APIServer }}
47- discoveryTokenCACertHashes:
48- {{ range $_, $hash := .DiscoveryTokenCACertHashes }}
49- - {{ $hash }}
50- {{ end }}
51- criSocket: {{ .CRISocket }}
52- nodeName: {{ .NodeName }}
53- `
54-
5515// Kubeadm implements the Service interface. It serves as the concrete type with
5616// the required methods.
5717type Kubeadm struct {}
5818
5919// Pre implements the Service interface.
6020func (p * Kubeadm ) Pre (data userdata.UserData ) (err error ) {
61- var configuration string
62- if data .Kubernetes .Join {
63- configuration = NodeConfiguration
64- } else {
65- configuration = MasterConfiguration
66- }
67-
68- var socket string
69- switch data .Kubernetes .ContainerRuntime {
70- case constants .ContainerRuntimeDocker :
71- socket = constants .ContainerRuntimeDockerSocket
72- case constants .ContainerRuntimeCRIO :
73- socket = constants .ContainerRuntimeCRIOSocket
21+ if data .Kubernetes .Init {
22+ if err = writeKubeadmPKIFiles (data .Kubernetes .CA ); err != nil {
23+ return
24+ }
7425 }
7526
76- if err = writeKubeadmManifest (data .Kubernetes , configuration , socket ); err != nil {
27+ if err = writeKubeadmManifest (data .Kubernetes . Configuration ); err != nil {
7728 return
7829 }
7930
80- if ! data .Kubernetes .Join {
81- if err = writeKubeadmPKIFiles (data .Kubernetes ); err != nil {
82- return
83- }
84- }
85-
8631 return nil
8732}
8833
8934// Cmd implements the Service interface.
9035func (p * Kubeadm ) Cmd (data userdata.UserData ) (name string , args []string ) {
9136 var cmd string
92- if data .Kubernetes .Join {
93- cmd = "join"
94- } else {
37+ if data .Kubernetes .Init {
9538 cmd = "init"
39+ } else {
40+ cmd = "join"
9641 }
9742 name = "/bin/kubeadm"
9843 args = []string {
9944 cmd ,
10045 "--config=/etc/kubernetes/kubeadm.yaml" ,
10146 "--ignore-preflight-errors=cri" ,
10247 }
103- if ! data .Kubernetes .Join {
48+ if data .Kubernetes .Init {
10449 args = append (args , "--skip-token-print" )
10550 }
10651
@@ -125,35 +70,16 @@ func (p *Kubeadm) Env() []string { return []string{} }
12570// Type implements the Service interface.
12671func (p * Kubeadm ) Type () Type { return Once }
12772
128- func writeKubeadmManifest (data * userdata.Kubernetes , configuration , socket string ) (err error ) {
129- aux := struct {
130- * userdata.Kubernetes
131- CRISocket string
132- }{
133- data ,
134- socket ,
135- }
136-
137- tmpl , err := template .New ("" ).Parse (configuration )
138- if err != nil {
139- return err
140- }
141- var buf []byte
142- writer := bytes .NewBuffer (buf )
143- err = tmpl .Execute (writer , aux )
144- if err != nil {
145- return err
146- }
147-
148- if err = ioutil .WriteFile (constants .KubeadmConfig , writer .Bytes (), 0400 ); err != nil {
73+ func writeKubeadmManifest (data string ) (err error ) {
74+ if err = ioutil .WriteFile (constants .KubeadmConfig , []byte (data ), 0400 ); err != nil {
14975 return fmt .Errorf ("write %s: %s" , constants .KubeadmConfig , err .Error ())
15076 }
15177
15278 return nil
15379}
15480
155- func writeKubeadmPKIFiles (data * userdata.Kubernetes ) (err error ) {
156- caCrtBytes , err := base64 .StdEncoding .DecodeString (data .CA . Crt )
81+ func writeKubeadmPKIFiles (data * userdata.CertificateAndKeyPaths ) (err error ) {
82+ caCrtBytes , err := base64 .StdEncoding .DecodeString (data .Crt )
15783 if err != nil {
15884 return err
15985 }
@@ -164,7 +90,7 @@ func writeKubeadmPKIFiles(data *userdata.Kubernetes) (err error) {
16490 return fmt .Errorf ("write %s: %s" , constants .KubeadmCACert , err .Error ())
16591 }
16692
167- caKeyBytes , err := base64 .StdEncoding .DecodeString (data .CA . Key )
93+ caKeyBytes , err := base64 .StdEncoding .DecodeString (data .Key )
16894 if err != nil {
16995 return err
17096 }
0 commit comments