Skip to content

Commit 8f3d016

Browse files
authored
Compress user_data using gzip to avoid to exceed the hard limit (32KB) (#9)
* 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 Co-authored-by: Wonkun Kim <[email protected]>
1 parent 98e0dca commit 8f3d016

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

controllers/cloudstackmachine_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ func (r *CloudStackMachineReconciler) reconcile(
167167
if !ok {
168168
return ctrl.Result{}, errors.New("Bootstrap secret data not ok.")
169169
}
170-
userData := base64.StdEncoding.EncodeToString(value)
170+
171+
compressedValue, err := cloud.CompressData(value)
172+
if err != nil {
173+
log.Error(err, "Failed to compress userData")
174+
return ctrl.Result{}, err
175+
}
176+
177+
userData := base64.StdEncoding.EncodeToString(compressedValue)
171178

172179
// Create VM (or Fetch if present). Will set ready to true.
173180
if err := r.CS.GetOrCreateVMInstance(csMachine, machine, csCluster, userData); err == nil {

pkg/cloud/helpers.go

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

1717
package cloud
1818

19+
import (
20+
"bytes"
21+
"compress/gzip"
22+
)
23+
1924
type set func(string)
2025

2126
func setIfNotEmpty(str string, setFn set) {
2227
if str != "" {
2328
setFn(str)
2429
}
2530
}
31+
32+
func CompressData(data []byte) ([]byte, error) {
33+
buf := &bytes.Buffer{}
34+
gzipWriter := gzip.NewWriter(buf)
35+
defer gzipWriter.Close()
36+
if _, err := gzipWriter.Write(data); err != nil {
37+
return nil, err
38+
}
39+
return buf.Bytes(), nil
40+
}

0 commit comments

Comments
 (0)