-
Notifications
You must be signed in to change notification settings - Fork 153
/
releases.go
103 lines (85 loc) · 2.81 KB
/
releases.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package helm
import (
"bytes"
"fmt"
"io"
"strings"
"time"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
)
const DefaultBufferSize = 2048
// MakeHelmRelease returns a HelmRelease object given a name, version, cluster, namespace, and HelmRepository's name and namespace.
func MakeHelmRelease(name, version, cluster, namespace string, helmRepository types.NamespacedName) *helmv2.HelmRelease {
return &helmv2.HelmRelease{
ObjectMeta: metav1.ObjectMeta{
Name: cluster + "-" + name,
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
APIVersion: helmv2.GroupVersion.Identifier(),
Kind: helmv2.HelmReleaseKind,
},
Spec: helmv2.HelmReleaseSpec{
Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
Chart: name,
Version: version,
SourceRef: helmv2.CrossNamespaceObjectReference{
APIVersion: sourcev1.GroupVersion.Identifier(),
Kind: sourcev1.HelmRepositoryKind,
Name: helmRepository.Name,
Namespace: helmRepository.Namespace,
},
},
},
Interval: metav1.Duration{Duration: time.Minute},
},
}
}
// AppendHelmReleaseToString appends "---" and a HelmRelease to string that may or may not be empty.
// This creates the content of a manifest that contains HelmReleases separated by "---".
func AppendHelmReleaseToString(content string, newRelease *helmv2.HelmRelease) (string, error) {
var sb strings.Builder
if content != "" {
sb.WriteString(content + "\n")
}
helmReleaseManifest, err := yaml.Marshal(newRelease)
if err != nil {
return "", fmt.Errorf("failed to marshal HelmRelease: %w", err)
}
sb.WriteString("---\n" + string(helmReleaseManifest))
return sb.String(), nil
}
// SplitHelmReleaseYAML splits a manifest file that contains one or more Helm Releases that may be separated by '---'.
func SplitHelmReleaseYAML(resources []byte) ([]*helmv2.HelmRelease, error) {
var helmReleaseList []*helmv2.HelmRelease
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(resources), DefaultBufferSize)
for {
var value helmv2.HelmRelease
if err := decoder.Decode(&value); err != nil {
if err == io.EOF {
break
}
return nil, err
}
helmReleaseList = append(helmReleaseList, &value)
}
return helmReleaseList, nil
}
// MarshalHelmReleases marshals a list of HelmReleases.
func MarshalHelmReleases(existingReleases []*helmv2.HelmRelease) (string, error) {
var sb strings.Builder
for _, r := range existingReleases {
b, err := yaml.Marshal(r)
if err != nil {
return "", fmt.Errorf("failed to marshal: %w", err)
}
sb.WriteString("---\n" + string(b))
}
return sb.String(), nil
}