-
Notifications
You must be signed in to change notification settings - Fork 153
/
helm_release.go
181 lines (144 loc) · 4.58 KB
/
helm_release.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package server
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/fluxcd/pkg/ssa"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
"github.com/weaveworks/weave-gitops/core/server/types"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (cs *coreServer) ListHelmReleases(ctx context.Context, msg *pb.ListHelmReleasesRequest) (*pb.ListHelmReleasesResponse, error) {
res, err := cs.listObjects(ctx, msg.Namespace, listHelmReleasesInNamespace)
if err != nil {
return nil, err
}
var results []*pb.HelmRelease
for _, object := range res {
obj, ok := object.(*pb.HelmRelease)
if !ok {
return nil, nil
}
results = append(results, obj)
}
return &pb.ListHelmReleasesResponse{
HelmReleases: results,
}, nil
}
func (cs *coreServer) GetHelmRelease(ctx context.Context, msg *pb.GetHelmReleaseRequest) (*pb.GetHelmReleaseResponse, error) {
clustersClient := clustersmngr.ClientFromCtx(ctx)
helmRelease := helmv2.HelmRelease{}
key := client.ObjectKey{
Name: msg.Name,
Namespace: msg.Namespace,
}
if err := clustersClient.Get(ctx, msg.ClusterName, key, &helmRelease); err != nil {
return nil, err
}
inventory, err := getHelmReleaseInventory(ctx, helmRelease, clustersClient, msg.ClusterName)
if err != nil {
return nil, err
}
return &pb.GetHelmReleaseResponse{
HelmRelease: types.HelmReleaseToProto(&helmRelease, msg.ClusterName, inventory),
}, err
}
func getHelmReleaseInventory(ctx context.Context, helmRelease v2beta1.HelmRelease, c clustersmngr.Client, cluster string) ([]*pb.GroupVersionKind, error) {
storageNamespace := helmRelease.GetNamespace()
if helmRelease.Spec.StorageNamespace != "" {
storageNamespace = helmRelease.Spec.StorageNamespace
}
storageName := helmRelease.GetName()
if helmRelease.Spec.ReleaseName != "" {
storageName = helmRelease.Spec.ReleaseName
}
storageVersion := helmRelease.Status.LastReleaseRevision
if storageVersion < 1 {
// skip release if it failed to install
return nil, nil
}
storageSecret := &v1.Secret{}
secretName := fmt.Sprintf("sh.helm.release.v1.%s.v%v", storageName, storageVersion)
key := client.ObjectKey{
Name: secretName,
Namespace: storageNamespace,
}
if err := c.Get(ctx, cluster, key, storageSecret); err != nil {
return nil, err
}
releaseData, releaseFound := storageSecret.Data["release"]
if !releaseFound {
return nil, fmt.Errorf("failed to decode the Helm storage object for HelmRelease '%s'", helmRelease.Name)
}
byteData, err := base64.StdEncoding.DecodeString(string(releaseData))
if err != nil {
return nil, err
}
var magicGzip = []byte{0x1f, 0x8b, 0x08}
if bytes.Equal(byteData[0:3], magicGzip) {
r, err := gzip.NewReader(bytes.NewReader(byteData))
if err != nil {
return nil, err
}
defer r.Close()
uncompressedByteData, err := io.ReadAll(r)
if err != nil {
return nil, err
}
byteData = uncompressedByteData
}
storage := types.HelmReleaseStorage{}
if err := json.Unmarshal(byteData, &storage); err != nil {
return nil, fmt.Errorf("failed to decode the Helm storage object for HelmRelease '%s': %w", helmRelease.Name, err)
}
objects, err := ssa.ReadObjects(strings.NewReader(storage.Manifest))
if err != nil {
return nil, fmt.Errorf("failed to read the Helm storage object for HelmRelease '%s': %w", helmRelease.Name, err)
}
var gvk []*pb.GroupVersionKind
found := map[string]bool{}
for _, entry := range objects {
idstr := strings.Join([]string{entry.GetAPIVersion(), entry.GetKind()}, "_")
if !found[idstr] {
found[idstr] = true
gvk = append(gvk, &pb.GroupVersionKind{
Group: entry.GroupVersionKind().Group,
Version: entry.GroupVersionKind().Version,
Kind: entry.GroupVersionKind().Kind,
})
}
}
return gvk, nil
}
func listHelmReleasesInNamespace(
ctx context.Context,
clustersClient clustersmngr.Client,
namespace string,
) ([]interface{}, error) {
clist := clustersmngr.NewClusteredList(func() client.ObjectList {
return &helmv2.HelmReleaseList{}
})
if err := clustersClient.ClusteredList(ctx, clist, client.InNamespace(namespace)); err != nil {
return nil, err
}
var results []interface{}
for n, l := range clist.Lists() {
list, ok := l.(*helmv2.HelmReleaseList)
if !ok {
continue
}
for _, helmrelease := range list.Items {
results = append(results, types.HelmReleaseToProto(&helmrelease, n, []*pb.GroupVersionKind{}))
}
}
return results, nil
}