Skip to content

Commit ed320bc

Browse files
committed
Zip last-applied annotation
1 parent 1c8d494 commit ed320bc

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

patch/annotation.go

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
package patch
1616

1717
import (
18+
"archive/zip"
19+
"bytes"
1820
"encoding/base64"
21+
"io/ioutil"
22+
"net/http"
1923

2024
json "github.com/json-iterator/go"
2125

@@ -58,7 +62,9 @@ func (a *Annotator) GetOriginalConfiguration(obj runtime.Object) ([]byte, error)
5862

5963
// Try to base64 decode, and fallback to non-base64 encoded content for backwards compatibility.
6064
if decoded, err := base64.StdEncoding.DecodeString(original); err == nil {
61-
return decoded, nil
65+
if http.DetectContentType(decoded) == "application/zip" {
66+
return UnZipAnnotation(decoded)
67+
}
6268
}
6369

6470
return []byte(original), nil
@@ -80,7 +86,10 @@ func (a *Annotator) SetOriginalConfiguration(obj runtime.Object, original []byte
8086
annots = map[string]string{}
8187
}
8288

83-
annots[a.key] = base64.StdEncoding.EncodeToString(original)
89+
annots[a.key], err = ZipAndBase64EncodeAnnotation(original)
90+
if err != nil {
91+
return err
92+
}
8493
return a.metadataAccessor.SetAnnotations(obj, annots)
8594
}
8695

@@ -121,7 +130,10 @@ func (a *Annotator) GetModifiedConfiguration(obj runtime.Object, annotate bool)
121130
}
122131

123132
if annotate {
124-
annots[a.key] = base64.StdEncoding.EncodeToString(modified)
133+
annots[a.key], err = ZipAndBase64EncodeAnnotation(modified)
134+
if err != nil {
135+
return nil, err
136+
}
125137
if err := a.metadataAccessor.SetAnnotations(obj, annots); err != nil {
126138
return nil, err
127139
}
@@ -155,3 +167,58 @@ func (a *Annotator) SetLastAppliedAnnotation(obj runtime.Object) error {
155167
}
156168
return a.SetOriginalConfiguration(obj, modifiedWithoutNulls)
157169
}
170+
171+
func ZipAndBase64EncodeAnnotation(original []byte) (string, error) {
172+
// Create a buffer to write our archive to.
173+
buf := new(bytes.Buffer)
174+
175+
// Create a new zip archive.
176+
w := zip.NewWriter(buf)
177+
178+
f, err := w.Create("original")
179+
if err != nil {
180+
return "", err
181+
}
182+
_, err = f.Write(original)
183+
if err != nil {
184+
return "", err
185+
}
186+
187+
// Make sure to check the error on Close.
188+
err = w.Close()
189+
if err != nil {
190+
return "", err
191+
}
192+
193+
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
194+
}
195+
196+
func UnZipAnnotation(original []byte) ([]byte, error) {
197+
annotation, err := ioutil.ReadAll(bytes.NewReader(original))
198+
if err != nil {
199+
return nil, err
200+
}
201+
202+
zipReader, err := zip.NewReader(bytes.NewReader(annotation), int64(len(annotation)))
203+
if err != nil {
204+
return nil, err
205+
}
206+
207+
// Read the file from zip archive
208+
zipFile := zipReader.File[0]
209+
unzippedFileBytes, err := readZipFile(zipFile)
210+
if err != nil {
211+
return nil, err
212+
}
213+
214+
return unzippedFileBytes, nil
215+
}
216+
217+
func readZipFile(zf *zip.File) ([]byte, error) {
218+
f, err := zf.Open()
219+
if err != nil {
220+
return nil, err
221+
}
222+
defer f.Close()
223+
return ioutil.ReadAll(f)
224+
}

0 commit comments

Comments
 (0)