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

Commit be5a881

Browse files
authored
Merge pull request #204 from chuckha/encoding
⚠️ Adds encoding field for cloud-init files
2 parents e3b2bcf + d03edcd commit be5a881

File tree

6 files changed

+125
-9
lines changed

6 files changed

+125
-9
lines changed

api/v1alpha2/kubeadmbootstrapconfig_types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ func init() {
107107
SchemeBuilder.Register(&KubeadmConfig{}, &KubeadmConfigList{})
108108
}
109109

110+
// Encoding specifies the cloud-init file encoding.
111+
// +kubebuilder:validation:Enum=base64;gzip;gzip+base64
112+
type Encoding string
113+
114+
const (
115+
// Base64 implies the contents of the file are encoded as base64.
116+
Base64 Encoding = "base64"
117+
// Gzip implies the contents of the file are encoded with gzip.
118+
Gzip Encoding = "gzip"
119+
// GzipBase64 implies the contents of the file are first base64 encoded and then gzip encoded.
120+
GzipBase64 Encoding = "gzip+base64"
121+
)
122+
110123
// File defines the input for generating write_files in cloud-init.
111124
type File struct {
112125
// Path specifies the full path on disk where to store the file.
@@ -120,6 +133,10 @@ type File struct {
120133
// +optional
121134
Permissions string `json:"permissions,omitempty"`
122135

136+
// Encoding specifies the encoding of the file contents.
137+
// +optional
138+
Encoding Encoding `json:"encoding,omitempty"`
139+
123140
// Content is the actual content of the file.
124141
Content string `json:"content"`
125142
}

cloudinit/cloudinit_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
import (
20+
"bytes"
21+
"testing"
22+
23+
infrav1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
24+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/certs"
25+
)
26+
27+
func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
28+
cpinput := &ControlPlaneInput{
29+
BaseUserData: BaseUserData{
30+
Header: "test",
31+
PreKubeadmCommands: nil,
32+
PostKubeadmCommands: nil,
33+
AdditionalFiles: []infrav1.File{
34+
{
35+
Path: "/tmp/my-path",
36+
Encoding: infrav1.Base64,
37+
Content: "aGk=",
38+
},
39+
{
40+
Path: "/tmp/my-other-path",
41+
Content: "hi",
42+
},
43+
},
44+
WriteFiles: nil,
45+
Users: nil,
46+
NTP: nil,
47+
},
48+
Certificates: certs.Certificates{
49+
ClusterCA: &certs.KeyPair{
50+
Cert: []byte("ca cert"),
51+
Key: []byte("ca key"),
52+
},
53+
EtcdCA: &certs.KeyPair{
54+
Cert: []byte("etcd ca cert"),
55+
Key: []byte("etcd ca key"),
56+
},
57+
FrontProxyCA: &certs.KeyPair{
58+
Cert: []byte("front proxy ca cert"),
59+
Key: []byte("front proxy ca key"),
60+
},
61+
ServiceAccount: &certs.KeyPair{
62+
Cert: []byte("service account ca cert"),
63+
Key: []byte("service account ca key"),
64+
},
65+
},
66+
ClusterConfiguration: "my-cluster-config",
67+
InitConfiguration: "my-init-config",
68+
}
69+
70+
out, err := NewInitControlPlane(cpinput)
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
expectedFiles := []string{
75+
`- path: /tmp/my-path
76+
encoding: "base64"
77+
content: |
78+
aGk=`,
79+
`- path: /tmp/my-other-path
80+
content: |
81+
hi`,
82+
}
83+
for _, f := range expectedFiles {
84+
if !bytes.Contains(out, []byte(f)) {
85+
t.Errorf("%s\ndid not contain\n%s", out, f)
86+
}
87+
}
88+
}

cloudinit/files.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ const (
2020
filesTemplate = `{{ define "files" -}}
2121
write_files:{{ range . }}
2222
- path: {{.Path}}
23-
encoding: "base64"
23+
{{ if ne .Encoding "" -}}
24+
encoding: "{{.Encoding}}"
25+
{{ end -}}
2426
{{ if ne .Owner "" -}}
2527
owner: {{.Owner}}
2628
{{ end -}}
2729
{{ if ne .Permissions "" -}}
2830
permissions: '{{.Permissions}}'
2931
{{ end -}}
3032
content: |
31-
{{.Content | Base64Encode | Indent 6}}
33+
{{.Content | Indent 6}}
3234
{{- end -}}
3335
{{- end -}}
3436
`

cloudinit/utils.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,16 @@ limitations under the License.
1717
package cloudinit
1818

1919
import (
20-
"encoding/base64"
2120
"strings"
2221
"text/template"
2322
)
2423

2524
var (
2625
defaultTemplateFuncMap = template.FuncMap{
27-
"Base64Encode": templateBase64Encode,
28-
"Indent": templateYAMLIndent,
26+
"Indent": templateYAMLIndent,
2927
}
3028
)
3129

32-
func templateBase64Encode(s string) string {
33-
return base64.StdEncoding.EncodeToString([]byte(s))
34-
}
35-
3630
func templateYAMLIndent(i int, input string) string {
3731
split := strings.Split(input, "\n")
3832
ident := "\n" + strings.Repeat(" ", i)

config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,13 @@ spec:
359359
content:
360360
description: Content is the actual content of the file.
361361
type: string
362+
encoding:
363+
description: Encoding specifies the encoding of the file contents.
364+
enum:
365+
- base64
366+
- gzip
367+
- gzip+base64
368+
type: string
362369
owner:
363370
description: Owner specifies the ownership of the file, e.g. "root:root".
364371
type: string

config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ spec:
380380
content:
381381
description: Content is the actual content of the file.
382382
type: string
383+
encoding:
384+
description: Encoding specifies the encoding of the file
385+
contents.
386+
enum:
387+
- base64
388+
- gzip
389+
- gzip+base64
390+
type: string
383391
owner:
384392
description: Owner specifies the ownership of the file,
385393
e.g. "root:root".

0 commit comments

Comments
 (0)