-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm.go
49 lines (41 loc) · 1.16 KB
/
helm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
v1 "k8s.io/api/core/v1"
)
const SecretTypeHelm = "helm.sh/release.v1"
func FormatHelmSecret(_ context.Context, s *v1.Secret) (string, error) {
if s.Type != SecretTypeHelm {
return "", fmt.Errorf("invalid secret type: %v", s.Type)
}
d, ok := s.Data["release"]
if !ok {
return "", errors.New("secret missing key: release")
}
gzipData := make([]byte, base64.StdEncoding.DecodedLen(len(d)))
if n, err := base64.StdEncoding.Decode(gzipData, d); err != nil {
return "", fmt.Errorf("base64 decoding release data: %w", err)
} else {
gzipData = gzipData[:n]
}
gr, err := gzip.NewReader(bytes.NewReader(gzipData))
if err != nil {
return "", fmt.Errorf("gzip decompressing release data: %w", err)
}
dec := json.NewDecoder(gr)
release := make(map[string]interface{})
if err := dec.Decode(&release); err != nil {
return "", fmt.Errorf("json unmarshaling release data: %w", err)
}
msgBytes, err := json.MarshalIndent(release, "", " ")
if err != nil {
return "", fmt.Errorf("preparing release output: %w", err)
}
return string(msgBytes), nil
}