Skip to content

Commit

Permalink
gzip cloud-init data
Browse files Browse the repository at this point in the history
  • Loading branch information
ttreptow committed Jan 6, 2025
1 parent b1d4cfa commit faa624a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
14 changes: 10 additions & 4 deletions bootstrap/internal/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cloudinit

import (
"bytes"
"compress/gzip"
"fmt"
"strings"
"text/template"
Expand Down Expand Up @@ -135,12 +136,17 @@ func generate(kind string, tpl string, data interface{}) ([]byte, error) {
return nil, errors.Wrapf(err, "failed to parse %s template", kind)
}

var out bytes.Buffer
if err := t.Execute(&out, data); err != nil {
var buf bytes.Buffer
out := gzip.NewWriter(&buf)
err = t.Execute(out, data)
if err != nil {

Check failure on line 142 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)

Check failure on line 142 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
return nil, errors.Wrapf(err, "failed to generate %s template", kind)
}

return out.Bytes(), nil
err = out.Close()

Check failure on line 145 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)

Check failure on line 145 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)
if err != nil {

Check failure on line 146 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)

Check failure on line 146 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
return nil, errors.Wrapf(err, "failed to generate %s compressed userdata", kind)
}
return buf.Bytes(), nil

Check failure on line 149 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

return statements should not be cuddled if block has more than two lines (wsl)

Check failure on line 149 in bootstrap/internal/cloudinit/cloudinit.go

View workflow job for this annotation

GitHub Actions / lint

return statements should not be cuddled if block has more than two lines (wsl)
}

func cleanupAdditionalCloudInit(cloudInitData string) (string, error) {
Expand Down
11 changes: 11 additions & 0 deletions bootstrap/internal/cloudinit/cloudinit_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package cloudinit

import (
"bytes"
"compress/gzip"
"io"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -28,3 +31,11 @@ func TestCloudInit(t *testing.T) {

RunSpecs(t, "CloudInit Suite")
}

func unzipUserdata(userdata []byte) string {
r, err := gzip.NewReader(bytes.NewBuffer(userdata))
Expect(err).ToNot(HaveOccurred())
content, err := io.ReadAll(r)
Expect(err).ToNot(HaveOccurred())
return string(content)
}
14 changes: 7 additions & 7 deletions bootstrap/internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = Describe("WorkerAirGappedCloudInitTest", func() {
It("Should use the image embedded install.sh method", func() {
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())
Expect(workerCloudInitString).To(Equal(`## template: jinja
Expand Down Expand Up @@ -66,7 +66,7 @@ var _ = Describe("WorkerAirGappedWithChecksumCloudInitTest", func() {
It("Should use the image embedded install.sh method and check the checksum first", func() {
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())
Expect(workerCloudInitString).To(Equal(`## template: jinja
Expand Down Expand Up @@ -101,7 +101,7 @@ var _ = Describe("WorkerOnlineCloudInitTest", func() {
It("Should use the RKE2 Online installation method", func() {
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())
Expect(workerCloudInitString).To(Equal(`## template: jinja
Expand Down Expand Up @@ -134,7 +134,7 @@ var _ = Describe("NTPWorkerTest", func() {
It("Should use the RKE2 Online installation method", func() {
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())
Expect(workerCloudInitString).To(Equal(`## template: jinja
Expand Down Expand Up @@ -172,7 +172,7 @@ var _ = Describe("WorkerCISTest", func() {
It("Should run the CIS script", func() {
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())
Expect(workerCloudInitString).To(Equal(`## template: jinja
Expand Down Expand Up @@ -274,7 +274,7 @@ runcmd:
}
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -312,7 +312,7 @@ runcmd:
}
workerCloudInitData, err := NewJoinWorker(input)
Expect(err).ToNot(HaveOccurred())
workerCloudInitString := string(workerCloudInitData)
workerCloudInitString := unzipUserdata(workerCloudInitData)
_, err = GinkgoWriter.Write(workerCloudInitData)
Expect(err).NotTo(HaveOccurred())

Expand Down

0 comments on commit faa624a

Please sign in to comment.