Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 571a4a4

Browse files
committed
Add ntp and user module support for cloud-init
1 parent 76519df commit 571a4a4

12 files changed

+404
-8
lines changed

api/v1alpha2/kubeadmbootstrapconfig_types.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type KubeadmConfigSpec struct {
5151
// PostKubeadmCommands specifies extra commands to run after kubeadm runs
5252
// +optional
5353
PostKubeadmCommands []string `json:"postKubeadmCommands,omitempty"`
54+
// Users specifies extra users to add
55+
// +optional
56+
Users []User `json:"users,omitempty"`
57+
// NTP specifies NTP configuration
58+
// +optional
59+
NTP *NTP `json:"ntp,omitempty"`
5460
// Format specifies the output format of the bootstrap data
5561
// +optional
5662
Format Format `json:"format,omitempty"`
@@ -109,3 +115,60 @@ type Files struct {
109115
// Content is the actual content of the file.
110116
Content string `json:"content"`
111117
}
118+
119+
// User defines the input for a generated user in cloud-init.
120+
type User struct {
121+
// Name specifies the user name
122+
Name string `json:"name"`
123+
124+
// Gecos specifies the gecos to use for the user
125+
// +optional
126+
Gecos *string `json:"gecos,omitempty"`
127+
128+
// Groups specifies the additional groups for the user
129+
// +optional
130+
Groups *string `json:"groups,omitempty"`
131+
132+
// HomeDir specifies the home directory to use for the user
133+
// +optional
134+
HomeDir *string `json:"homeDir,omitempty"`
135+
136+
// Inactive specifies whether to mark the user as inactive
137+
// +optional
138+
Inactive *bool `json:"inactive,omitempty"`
139+
140+
// Shell specifies the user's shell
141+
// +optional
142+
Shell *string `json:"shell,omitempty"`
143+
144+
// Passwd specifies a hashed password for the user
145+
// +optional
146+
Passwd *string `json:"passwd"`
147+
148+
// PrimaryGroup specifies the primary group for the user
149+
// +optional
150+
PrimaryGroup *string `json:"primaryGroup,omitempty"`
151+
152+
// LockPassword specifies if password login should be disabled
153+
// +optional
154+
LockPassword *bool `json:"lockPassword,omitempty"`
155+
156+
// Sudo specifies a sudo role for the user
157+
// +optional
158+
Sudo *string `json:"sudo,omitempty"`
159+
160+
// SSHAuthorizedKeys specifies a list of ssh authorized keys for the user
161+
// +optional
162+
SSHAuthorizedKeys []string `json:"sshAuthorizedKeys,omitempty"`
163+
}
164+
165+
// NTP defines input for generated ntp in cloud-init
166+
type NTP struct {
167+
// Servers specifies which NTP servers to use
168+
// +optional
169+
Servers []string `json:"servers,omitempty"`
170+
171+
// Enabled specifies whether NTP should be enabled
172+
// +optional
173+
Enabled *bool `json:"enabled,omitempty"`
174+
}

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloudinit/cloudinit.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type BaseUserData struct {
3737
PostKubeadmCommands []string
3838
AdditionalFiles []v1alpha2.Files
3939
WriteFiles []v1alpha2.Files
40+
Users []v1alpha2.User
41+
NTP *v1alpha2.NTP
4042
}
4143

4244
func generate(kind string, tpl string, data interface{}) ([]byte, error) {
@@ -49,6 +51,14 @@ func generate(kind string, tpl string, data interface{}) ([]byte, error) {
4951
return nil, errors.Wrap(err, "failed to parse commands template")
5052
}
5153

54+
if _, err := tm.Parse(ntpTemplate); err != nil {
55+
return nil, errors.Wrap(err, "failed to parse ntp template")
56+
}
57+
58+
if _, err := tm.Parse(usersTemplate); err != nil {
59+
return nil, errors.Wrap(err, "failed to parse users template")
60+
}
61+
5262
t, err := tm.Parse(tpl)
5363
if err != nil {
5464
return nil, errors.Wrapf(err, "failed to parse %s template", kind)

cloudinit/controlplane_init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ runcmd:
3333
{{- template "commands" .PreKubeadmCommands }}
3434
- 'kubeadm init --config /tmp/kubeadm.yaml'
3535
{{- template "commands" .PostKubeadmCommands }}
36+
{{- template "ntp" .NTP }}
37+
{{- template "users" .Users }}
3638
`
3739
)
3840

cloudinit/controlplane_join.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ runcmd:
3333
{{- template "commands" .PreKubeadmCommands }}
3434
- 'kubeadm join --config /tmp/kubeadm-controlplane-join-config.yaml'
3535
{{- template "commands" .PostKubeadmCommands }}
36+
{{- template "ntp" .NTP }}
37+
{{- template "users" .Users }}
3638
`
3739
)
3840

cloudinit/node.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ runcmd:
2929
{{- template "commands" .PreKubeadmCommands }}
3030
- 'kubeadm join --config /tmp/kubeadm-node.yaml'
3131
{{- template "commands" .PostKubeadmCommands }}
32+
{{- template "ntp" .NTP }}
33+
{{- template "users" .Users }}
3234
`
3335
)
3436

cloudinit/ntp.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cloudinit
18+
19+
const (
20+
ntpTemplate = `{{ define "ntp" -}}
21+
{{- if . }}
22+
ntp:
23+
{{ if .Enabled -}}
24+
enabled: true
25+
{{ end -}}
26+
servers:{{ range .Servers }}
27+
- {{ . }}
28+
{{- end -}}
29+
{{- end -}}
30+
{{- end -}}
31+
`
32+
)

cloudinit/users.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cloudinit
18+
19+
const (
20+
usersTemplate = `{{ define "users" -}}
21+
{{- if . }}
22+
users:{{ range . }}
23+
- name: {{ .Name }}
24+
{{- if .Passwd }}
25+
passwd: {{ .Passwd }}
26+
{{- end -}}
27+
{{- if .Gecos }}
28+
gecos: {{ .Gecos }}
29+
{{- end -}}
30+
{{- if .Groups }}
31+
groups: {{ .Groups }}
32+
{{- end -}}
33+
{{- if .HomeDir }}
34+
homedir: {{ .HomeDir }}
35+
{{- end -}}
36+
{{- if .Inactive }}
37+
inactive: true
38+
{{- end -}}
39+
{{- if .LockPassword }}
40+
lock_passwd: {{ .LockPassword }}
41+
{{- end -}}
42+
{{- if .Shell }}
43+
shell: {{ .Shell }}
44+
{{- end -}}
45+
{{- if .PrimaryGroup }}
46+
primary_group: {{ .PrimaryGroup }}
47+
{{- end -}}
48+
{{- if .Sudo }}
49+
sudo: {{ .Sudo }}
50+
{{- end -}}
51+
{{- if .SSHAuthorizedKeys }}
52+
ssh_authorized_keys:{{ range .SSHAuthorizedKeys }}
53+
- {{ . }}
54+
{{- end -}}
55+
{{- end -}}
56+
{{- end -}}
57+
{{- end -}}
58+
{{- end -}}
59+
`
60+
)

cloudinit/utils.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import (
2222
"text/template"
2323
)
2424

25-
const (
26-
rootOwnerValue = "root:root"
27-
)
28-
2925
var (
3026
defaultTemplateFuncMap = template.FuncMap{
3127
"Base64Encode": templateBase64Encode,
@@ -42,7 +38,3 @@ func templateYAMLIndent(i int, input string) string {
4238
ident := "\n" + strings.Repeat(" ", i)
4339
return strings.Repeat(" ", i) + strings.Join(split, ident)
4440
}
45-
46-
func isKeyPairValid(cert, key string) bool {
47-
return cert != "" && key != ""
48-
}

0 commit comments

Comments
 (0)