@@ -17,8 +17,11 @@ limitations under the License.
1717package driver // import "k8s.io/helm/pkg/storage/driver"
1818
1919import (
20+ "bytes"
21+ "compress/gzip"
2022 "encoding/base64"
2123 "fmt"
24+ "io/ioutil"
2225 "log"
2326 "strconv"
2427 "time"
@@ -40,6 +43,8 @@ const ConfigMapsDriverName = "ConfigMap"
4043
4144var b64 = base64 .StdEncoding
4245
46+ var magicGzip = []byte {0x1f , 0x8b , 0x08 }
47+
4348// ConfigMaps is a wrapper around an implementation of a kubernetes
4449// ConfigMapsInterface.
4550type ConfigMaps struct {
@@ -254,13 +259,23 @@ func newConfigMapsObject(key string, rls *rspb.Release, lbs labels) (*api.Config
254259}
255260
256261// encodeRelease encodes a release returning a base64 encoded
257- // binary protobuf encoding representation, or error.
262+ // gzipped binary protobuf encoding representation, or error.
258263func encodeRelease (rls * rspb.Release ) (string , error ) {
259264 b , err := proto .Marshal (rls )
260265 if err != nil {
261266 return "" , err
262267 }
263- return b64 .EncodeToString (b ), nil
268+ var buf bytes.Buffer
269+ w , err := gzip .NewWriterLevel (& buf , gzip .BestCompression )
270+ if err != nil {
271+ return "" , err
272+ }
273+ if _ , err = w .Write (b ); err != nil {
274+ return "" , err
275+ }
276+ w .Close ()
277+
278+ return b64 .EncodeToString (buf .Bytes ()), nil
264279}
265280
266281// decodeRelease decodes the bytes in data into a release
@@ -274,6 +289,21 @@ func decodeRelease(data string) (*rspb.Release, error) {
274289 return nil , err
275290 }
276291
292+ // For backwards compatibility with releases that were stored before
293+ // compression was introduced we skip decompression if the
294+ // gzip magic header is not found
295+ if bytes .Equal (b [0 :3 ], magicGzip ) {
296+ r , err := gzip .NewReader (bytes .NewReader (b ))
297+ if err != nil {
298+ return nil , err
299+ }
300+ b2 , err := ioutil .ReadAll (r )
301+ if err != nil {
302+ return nil , err
303+ }
304+ b = b2
305+ }
306+
277307 var rls rspb.Release
278308 // unmarshal protobuf bytes
279309 if err := proto .Unmarshal (b , & rls ); err != nil {
0 commit comments