Skip to content

Commit fc98614

Browse files
feat(*): raw kubeadm configuration in user data (#79)
1 parent 7c37272 commit fc98614

File tree

4 files changed

+20
-105
lines changed

4 files changed

+20
-105
lines changed

initramfs/cmd/init/pkg/service/kubeadm.go

Lines changed: 14 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,51 @@
11
package service
22

33
import (
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.
5717
type Kubeadm struct{}
5818

5919
// Pre implements the Service interface.
6020
func (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.
9035
func (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.
12671
func (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
}

initramfs/cmd/init/pkg/service/kubelet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (p *Kubelet) Cmd(data userdata.UserData) (name string, args []string) {
5858
default:
5959
}
6060

61-
if data.Kubernetes.Join {
61+
if !data.Kubernetes.Init {
6262
labels := "--node-labels="
6363
for k, v := range data.Kubernetes.Labels {
6464
labels += k + "=" + v + ","

initramfs/cmd/init/pkg/userdata/userdata.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,11 @@ type CertificateAndKeyPaths struct {
4545

4646
// Kubernetes represents the Kubernetes specific configuration options.
4747
type Kubernetes struct {
48-
CA *CertificateAndKeyPaths `yaml:"ca,omitempty"`
49-
Token string `yaml:"token"`
50-
Join bool `yaml:"join,omitempty"`
51-
APIServer string `yaml:"apiServer,omitempty"`
52-
NodeName string `yaml:"nodeName,omitempty"`
53-
Labels map[string]string `yaml:"labels,omitempty"`
54-
ContainerRuntime string `yaml:"containerRuntime,omitempty"`
55-
DiscoveryTokenCACertHashes []string `yaml:"discoveryTokenCACertHashes,omitempty"`
48+
CA *CertificateAndKeyPaths `yaml:"ca,omitempty"`
49+
Init bool `yaml:"init,omitempty"`
50+
ContainerRuntime string `yaml:"containerRuntime,omitempty"`
51+
Labels map[string]string `yaml:"labels,omitempty"`
52+
Configuration string `yaml:"configuration,omitempty"`
5653
}
5754

5855
// Download initializes a UserData struct from a remote URL.

initramfs/cmd/osctl/cmd/inject.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ var injectKubernetesCmd = &cobra.Command{
110110
}
111111
data.Kubernetes.CA.Key = base64.StdEncoding.EncodeToString(fileBytes)
112112
}
113-
if hash != "" {
114-
fileBytes, err = ioutil.ReadFile(hash + ".sha256")
115-
if err != nil {
116-
fmt.Println(err)
117-
os.Exit(1)
118-
}
119-
data.Kubernetes.DiscoveryTokenCACertHashes = []string{string(fileBytes)}
120-
}
121113

122114
dataBytes, err := yaml.Marshal(data)
123115
if err != nil {

0 commit comments

Comments
 (0)