Skip to content

Commit f016269

Browse files
authored
Feature/compress user data (#14)
* Compress user_data using gzip to avoid to exceed the hard limit (32KB) * Fix a lint error * Extract a function compressing bytes to the cloud package * Pass buffer to CompressData function and add unit tests * Change the function to compress and encode string * Fix typos * Remove code added mistakenly during merge * Use Close instead of Flush because Flush doesn't work in Docker-Linux environment Co-authored-by: Wonkun Kim <[email protected]>
1 parent 73fdf15 commit f016269

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

controllers/cloudstackmachine_controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controllers
1818

1919
import (
2020
"context"
21-
"encoding/base64"
2221
"errors"
2322
"fmt"
2423
"reflect"
@@ -167,10 +166,9 @@ func (r *CloudStackMachineReconciler) reconcile(
167166
if !ok {
168167
return ctrl.Result{}, errors.New("Bootstrap secret data not ok.")
169168
}
170-
userData := base64.StdEncoding.EncodeToString(value)
171169

172170
// Create VM (or Fetch if present). Will set ready to true.
173-
if err := r.CS.GetOrCreateVMInstance(csMachine, machine, csCluster, userData); err == nil {
171+
if err := r.CS.GetOrCreateVMInstance(csMachine, machine, csCluster, string(value)); err == nil {
174172
if !controllerutil.ContainsFinalizer(csMachine, infrav1.MachineFinalizer) { // Fetched or Created?
175173
log.Info("CloudStack instance Created", "instanceStatus", csMachine.Status, "instanceSpec", csMachine.Spec)
176174
controllerutil.AddFinalizer(csMachine, infrav1.MachineFinalizer)

pkg/cloud/helpers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,27 @@ limitations under the License.
1616

1717
package cloud
1818

19+
import (
20+
"bytes"
21+
"compress/gzip"
22+
"encoding/base64"
23+
)
24+
1925
type set func(string)
2026

2127
func setIfNotEmpty(str string, setFn set) {
2228
if str != "" {
2329
setFn(str)
2430
}
2531
}
32+
33+
func CompressAndEncodeString(str string) (string, error) {
34+
buf := &bytes.Buffer{}
35+
gzipWriter := gzip.NewWriter(buf)
36+
if _, err := gzipWriter.Write([]byte(str)); err != nil {
37+
gzipWriter.Close()
38+
return "", err
39+
}
40+
gzipWriter.Close()
41+
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
42+
}

pkg/cloud/helpers_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ limitations under the License.
1717
package cloud_test
1818

1919
import (
20+
"bytes"
21+
"compress/gzip"
22+
"encoding/base64"
23+
"io/ioutil"
2024
"os"
2125
"path"
2226

@@ -42,6 +46,19 @@ var _ = Describe("Helpers", func() {
4246
Ω(err.Error()).Should(ContainSubstring("section Global not found"))
4347
})
4448
})
49+
50+
It("should compress and encode string", func() {
51+
str := "Hello World"
52+
53+
compressedAndEncodedData, err := cloud.CompressAndEncodeString(str)
54+
55+
compressedData, _ := base64.StdEncoding.DecodeString(compressedAndEncodedData)
56+
reader, _ := gzip.NewReader(bytes.NewReader(compressedData))
57+
result, _ := ioutil.ReadAll(reader)
58+
59+
Ω(err).Should(BeNil())
60+
Ω(string(result)).Should(Equal(str))
61+
})
4562
})
4663

4764
func getConfigPath(filename string) string {

pkg/cloud/instance.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,19 @@ func (c *client) GetOrCreateVMInstance(
150150
setIfNotEmpty(csMachine.Name, p.SetName)
151151
setIfNotEmpty(csMachine.Name, p.SetDisplayname)
152152
setIfNotEmpty(csMachine.Spec.SSHKey, p.SetKeypair)
153-
setIfNotEmpty(userData, p.SetUserdata)
154-
p.SetAffinitygroupids(csMachine.Spec.AffinityGroupIds)
153+
154+
if compressedAndEncodedUserData, err := CompressAndEncodeString(userData); err != nil {
155+
return err
156+
} else {
157+
setIfNotEmpty(compressedAndEncodedUserData, p.SetUserdata)
158+
}
159+
160+
if len(csMachine.Spec.AffinityGroupIds) > 0 {
161+
p.SetAffinitygroupids(csMachine.Spec.AffinityGroupIds)
162+
}
155163
setIfNotEmpty(csCluster.Spec.Account, p.SetAccount)
156164
setIfNotEmpty(csCluster.Status.DomainID, p.SetDomainid)
165+
157166
// If this VM instance is a control plane, consider setting it's IP.
158167
_, isControlPlanceMachine := machine.ObjectMeta.Labels["cluster.x-k8s.io/control-plane"]
159168
if isControlPlanceMachine && csCluster.Status.NetworkType == NetworkTypeShared {

0 commit comments

Comments
 (0)