From faa624ae4dd8f877bc0c3cf0959300681e0aef7c Mon Sep 17 00:00:00 2001 From: Tim Treptow Date: Mon, 2 Dec 2024 13:38:58 -0800 Subject: [PATCH] gzip cloud-init data --- bootstrap/internal/cloudinit/cloudinit.go | 14 ++++++++++---- .../internal/cloudinit/cloudinit_suite_test.go | 11 +++++++++++ bootstrap/internal/cloudinit/cloudinit_test.go | 14 +++++++------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/bootstrap/internal/cloudinit/cloudinit.go b/bootstrap/internal/cloudinit/cloudinit.go index 95863255..b07fdf76 100644 --- a/bootstrap/internal/cloudinit/cloudinit.go +++ b/bootstrap/internal/cloudinit/cloudinit.go @@ -19,6 +19,7 @@ package cloudinit import ( "bytes" + "compress/gzip" "fmt" "strings" "text/template" @@ -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 { return nil, errors.Wrapf(err, "failed to generate %s template", kind) } - - return out.Bytes(), nil + err = out.Close() + if err != nil { + return nil, errors.Wrapf(err, "failed to generate %s compressed userdata", kind) + } + return buf.Bytes(), nil } func cleanupAdditionalCloudInit(cloudInitData string) (string, error) { diff --git a/bootstrap/internal/cloudinit/cloudinit_suite_test.go b/bootstrap/internal/cloudinit/cloudinit_suite_test.go index b7cf1822..04a387a0 100644 --- a/bootstrap/internal/cloudinit/cloudinit_suite_test.go +++ b/bootstrap/internal/cloudinit/cloudinit_suite_test.go @@ -17,6 +17,9 @@ limitations under the License. package cloudinit import ( + "bytes" + "compress/gzip" + "io" "testing" . "github.com/onsi/ginkgo/v2" @@ -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) +} diff --git a/bootstrap/internal/cloudinit/cloudinit_test.go b/bootstrap/internal/cloudinit/cloudinit_test.go index acdc08aa..93c42f77 100644 --- a/bootstrap/internal/cloudinit/cloudinit_test.go +++ b/bootstrap/internal/cloudinit/cloudinit_test.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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()) @@ -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())