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

Commit

Permalink
Adds encoding field for cloud-init files
Browse files Browse the repository at this point in the history
Signed-off-by: Chuck Ha <chuckh@vmware.com>
  • Loading branch information
chuckha committed Sep 3, 2019
1 parent e3b2bcf commit f8eb0a9
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 9 deletions.
17 changes: 17 additions & 0 deletions api/v1alpha2/kubeadmbootstrapconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ func init() {
SchemeBuilder.Register(&KubeadmConfig{}, &KubeadmConfigList{})
}

// Encoding specifies the cloud-init file encoding.
// +kubebuilder:validation:Enum=b64;gzip;gz+b64
type Encoding string

const (
// Base64 implies the contents of the file are encoded as base64.
Base64 Encoding = "b64"
// Gzip implies the contents of the file are encoded with gzip.
Gzip Encoding = "gzip"
// GzipBase64 implies the contents of the file are first base64 encoded and then gzip encoded.
GzipBase64 Encoding = "gz+b64"
)

// File defines the input for generating write_files in cloud-init.
type File struct {
// Path specifies the full path on disk where to store the file.
Expand All @@ -120,6 +133,10 @@ type File struct {
// +optional
Permissions string `json:"permissions,omitempty"`

// Encoding specifies the encoding of the file contents.
// +optional
Encoding Encoding `json:"encoding,omitempty"`

// Content is the actual content of the file.
Content string `json:"content"`
}
Expand Down
88 changes: 88 additions & 0 deletions cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cloudinit

import (
"bytes"
"testing"

infrav1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/certs"
)

func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
cpinput := &ControlPlaneInput{
BaseUserData: BaseUserData{
Header: "test",
PreKubeadmCommands: nil,
PostKubeadmCommands: nil,
AdditionalFiles: []infrav1.File{
{
Path: "/tmp/my-path",
Encoding: infrav1.Base64,
Content: "aGk=",
},
{
Path: "/tmp/my-other-path",
Content: "hi",
},
},
WriteFiles: nil,
Users: nil,
NTP: nil,
},
Certificates: certs.Certificates{
ClusterCA: &certs.KeyPair{
Cert: []byte("ca cert"),
Key: []byte("ca key"),
},
EtcdCA: &certs.KeyPair{
Cert: []byte("etcd ca cert"),
Key: []byte("etcd ca key"),
},
FrontProxyCA: &certs.KeyPair{
Cert: []byte("front proxy ca cert"),
Key: []byte("front proxy ca key"),
},
ServiceAccount: &certs.KeyPair{
Cert: []byte("service account ca cert"),
Key: []byte("service account ca key"),
},
},
ClusterConfiguration: "my-cluster-config",
InitConfiguration: "my-init-config",
}

out, err := NewInitControlPlane(cpinput)
if err != nil {
t.Fatal(err)
}
expectedFiles := []string{
`- path: /tmp/my-path
encoding: "b64"
content: |
aGk=`,
`- path: /tmp/my-other-path
content: |
hi`,
}
for _, f := range expectedFiles {
if !bytes.Contains(out, []byte(f)) {
t.Errorf("%s\ndid not contain\n%s", out, f)
}
}
}
6 changes: 4 additions & 2 deletions cloudinit/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ const (
filesTemplate = `{{ define "files" -}}
write_files:{{ range . }}
- path: {{.Path}}
encoding: "base64"
{{ if ne .Encoding "" -}}
encoding: "{{.Encoding}}"
{{ end -}}
{{ if ne .Owner "" -}}
owner: {{.Owner}}
{{ end -}}
{{ if ne .Permissions "" -}}
permissions: '{{.Permissions}}'
{{ end -}}
content: |
{{.Content | Base64Encode | Indent 6}}
{{.Content | Indent 6}}
{{- end -}}
{{- end -}}
`
Expand Down
8 changes: 1 addition & 7 deletions cloudinit/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ limitations under the License.
package cloudinit

import (
"encoding/base64"
"strings"
"text/template"
)

var (
defaultTemplateFuncMap = template.FuncMap{
"Base64Encode": templateBase64Encode,
"Indent": templateYAMLIndent,
"Indent": templateYAMLIndent,
}
)

func templateBase64Encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}

func templateYAMLIndent(i int, input string) string {
split := strings.Split(input, "\n")
ident := "\n" + strings.Repeat(" ", i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ spec:
content:
description: Content is the actual content of the file.
type: string
encoding:
description: Encoding specifies the encoding of the file contents.
enum:
- b64
- gzip
- gz+b64
type: string
owner:
description: Owner specifies the ownership of the file, e.g. "root:root".
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ spec:
content:
description: Content is the actual content of the file.
type: string
encoding:
description: Encoding specifies the encoding of the file
contents.
enum:
- b64
- gzip
- gz+b64
type: string
owner:
description: Owner specifies the ownership of the file,
e.g. "root:root".
Expand Down

0 comments on commit f8eb0a9

Please sign in to comment.