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

Commit e10c244

Browse files
authored
Merge pull request #161 from detiber/moreCloudInit
Add ntp and user module support for cloud-init
2 parents 492d14d + 571a4a4 commit e10c244

11 files changed

+404
-0
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"`
@@ -117,3 +123,60 @@ type Files struct {
117123
// Content is the actual content of the file.
118124
Content string `json:"content"`
119125
}
126+
127+
// User defines the input for a generated user in cloud-init.
128+
type User struct {
129+
// Name specifies the user name
130+
Name string `json:"name"`
131+
132+
// Gecos specifies the gecos to use for the user
133+
// +optional
134+
Gecos *string `json:"gecos,omitempty"`
135+
136+
// Groups specifies the additional groups for the user
137+
// +optional
138+
Groups *string `json:"groups,omitempty"`
139+
140+
// HomeDir specifies the home directory to use for the user
141+
// +optional
142+
HomeDir *string `json:"homeDir,omitempty"`
143+
144+
// Inactive specifies whether to mark the user as inactive
145+
// +optional
146+
Inactive *bool `json:"inactive,omitempty"`
147+
148+
// Shell specifies the user's shell
149+
// +optional
150+
Shell *string `json:"shell,omitempty"`
151+
152+
// Passwd specifies a hashed password for the user
153+
// +optional
154+
Passwd *string `json:"passwd"`
155+
156+
// PrimaryGroup specifies the primary group for the user
157+
// +optional
158+
PrimaryGroup *string `json:"primaryGroup,omitempty"`
159+
160+
// LockPassword specifies if password login should be disabled
161+
// +optional
162+
LockPassword *bool `json:"lockPassword,omitempty"`
163+
164+
// Sudo specifies a sudo role for the user
165+
// +optional
166+
Sudo *string `json:"sudo,omitempty"`
167+
168+
// SSHAuthorizedKeys specifies a list of ssh authorized keys for the user
169+
// +optional
170+
SSHAuthorizedKeys []string `json:"sshAuthorizedKeys,omitempty"`
171+
}
172+
173+
// NTP defines input for generated ntp in cloud-init
174+
type NTP struct {
175+
// Servers specifies which NTP servers to use
176+
// +optional
177+
Servers []string `json:"servers,omitempty"`
178+
179+
// Enabled specifies whether NTP should be enabled
180+
// +optional
181+
Enabled *bool `json:"enabled,omitempty"`
182+
}

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+
)

0 commit comments

Comments
 (0)