-
Notifications
You must be signed in to change notification settings - Fork 153
/
profiles.go
177 lines (148 loc) · 5.12 KB
/
profiles.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
package server
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
sourcev1beta1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/go-logr/logr"
grpcruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pb "github.com/weaveworks/weave-gitops/pkg/api/profiles"
"github.com/weaveworks/weave-gitops/pkg/helm/watcher/cache"
"github.com/weaveworks/weave-gitops/pkg/kube"
"google.golang.org/genproto/googleapis/api/httpbody"
"google.golang.org/grpc/metadata"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
OctetStreamType = "application/octet-stream"
JsonType = "application/json"
)
type ProfilesConfig struct {
helmRepoNamespace string
helmRepoName string
helmCache cache.Cache
clusterConfig kube.ClusterConfig
}
func NewProfilesConfig(clusterConfig kube.ClusterConfig, helmCache cache.Cache, helmRepoNamespace, helmRepoName string) ProfilesConfig {
return ProfilesConfig{
helmRepoNamespace: helmRepoNamespace,
helmRepoName: helmRepoName,
helmCache: helmCache,
clusterConfig: clusterConfig,
}
}
type ProfilesServer struct {
pb.UnimplementedProfilesServer
Log logr.Logger
HelmRepoName string
HelmRepoNamespace string
HelmCache cache.Cache
ClientGetter kube.ClientGetter
}
func NewProfilesServer(log logr.Logger, config ProfilesConfig) pb.ProfilesServer {
configGetter := kube.NewImpersonatingConfigGetter(config.clusterConfig.DefaultConfig, false)
clientGetter := kube.NewDefaultClientGetter(configGetter, config.clusterConfig.ClusterName)
return &ProfilesServer{
Log: log.WithName("profiles-server"),
HelmRepoNamespace: config.helmRepoNamespace,
HelmRepoName: config.helmRepoName,
HelmCache: config.helmCache,
ClientGetter: clientGetter,
}
}
func (s *ProfilesServer) GetProfiles(ctx context.Context, msg *pb.GetProfilesRequest) (*pb.GetProfilesResponse, error) {
kubeClient, err := s.ClientGetter.Client(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get a Kubernetes client: %w", err)
}
helmRepo := &sourcev1beta1.HelmRepository{}
err = kubeClient.Get(ctx, client.ObjectKey{
Name: s.HelmRepoName,
Namespace: s.HelmRepoNamespace,
}, helmRepo)
if err != nil {
if apierrors.IsNotFound(err) {
errMsg := fmt.Sprintf("HelmRepository %q/%q does not exist", s.HelmRepoNamespace, s.HelmRepoName)
s.Log.Error(err, errMsg)
return &pb.GetProfilesResponse{
Profiles: []*pb.Profile{},
}, &grpcruntime.HTTPStatusError{
Err: errors.New(errMsg),
HTTPStatus: http.StatusOK,
}
}
return nil, fmt.Errorf("failed to get HelmRepository %q/%q: %w", s.HelmRepoNamespace, s.HelmRepoName, err)
}
log := s.Log.WithValues("repository", types.NamespacedName{
Namespace: helmRepo.Namespace,
Name: helmRepo.Name,
})
ps, err := s.HelmCache.ListProfiles(logr.NewContext(ctx, log), helmRepo.Namespace, helmRepo.Name)
if err != nil {
return nil, fmt.Errorf("failed to scan HelmRepository %q/%q for charts: %w", s.HelmRepoNamespace, s.HelmRepoName, err)
}
return &pb.GetProfilesResponse{
Profiles: ps,
}, nil
}
func (s *ProfilesServer) GetProfileValues(ctx context.Context, msg *pb.GetProfileValuesRequest) (*httpbody.HttpBody, error) {
kubeClient, err := s.ClientGetter.Client(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get a Kubernetes client: %w", err)
}
helmRepo := &sourcev1beta1.HelmRepository{}
err = kubeClient.Get(ctx, client.ObjectKey{
Name: s.HelmRepoName,
Namespace: s.HelmRepoNamespace,
}, helmRepo)
if err != nil {
if apierrors.IsNotFound(err) {
errMsg := fmt.Sprintf("HelmRepository %q/%q does not exist", s.HelmRepoNamespace, s.HelmRepoName)
s.Log.Error(err, errMsg)
return &httpbody.HttpBody{
ContentType: "application/json",
Data: []byte{},
}, &grpcruntime.HTTPStatusError{
Err: errors.New(errMsg),
HTTPStatus: http.StatusOK,
}
}
return nil, fmt.Errorf("failed to get HelmRepository %q/%q", s.HelmRepoNamespace, s.HelmRepoName)
}
log := s.Log.WithValues("repository", types.NamespacedName{
Namespace: helmRepo.Namespace,
Name: helmRepo.Name,
})
data, err := s.HelmCache.GetProfileValues(logr.NewContext(ctx, log), helmRepo.Namespace, helmRepo.Name, msg.ProfileName, msg.ProfileVersion)
if err != nil {
return nil, fmt.Errorf("failed to retrieve values file from Helm chart '%s' (%s): %w", msg.ProfileName, msg.ProfileVersion, err)
}
var acceptHeader string
if md, ok := metadata.FromIncomingContext(ctx); ok {
if accept, ok := md["accept"]; ok {
acceptHeader = strings.Join(accept, ",")
}
}
if strings.Contains(acceptHeader, OctetStreamType) {
return &httpbody.HttpBody{
ContentType: OctetStreamType,
Data: data,
}, nil
}
res, err := json.Marshal(&pb.GetProfileValuesResponse{
Values: base64.StdEncoding.EncodeToString(data),
})
if err != nil {
return nil, fmt.Errorf("failed to marshal response to JSON: %w", err)
}
return &httpbody.HttpBody{
ContentType: JsonType,
Data: res,
}, nil
}