/
client.go
244 lines (196 loc) · 7.24 KB
/
client.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package helm
import (
"context"
"fmt"
"sort"
"strings"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/release"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/cloudogu/cesapp-lib/core"
"github.com/cloudogu/k8s-component-operator/pkg/config"
"github.com/cloudogu/k8s-component-operator/pkg/helm/client"
)
const (
helmRepositoryCache = "/tmp/.helmcache"
helmRepositoryConfig = "/tmp/.helmrepo"
helmRegistryConfigFile = "/tmp/.helmregistry/config.json"
ociSchemePrefix = string(config.EndpointSchemaOCI + "://")
)
// HelmClient embeds the client.Client interface for usage in this package.
type HelmClient interface {
client.Client
}
// Client wraps the HelmClient with config.HelmRepositoryData
type Client struct {
helmClient HelmClient
helmRepoData *config.HelmRepositoryData
dependencyChecker dependencyChecker
}
// NewClient create a new instance of the helm client.
func NewClient(namespace string, helmRepoData *config.HelmRepositoryData, debug bool, debugLog action.DebugLog) (*Client, error) {
opt := &client.RestConfClientOptions{
Options: &client.Options{
Namespace: namespace,
RepositoryCache: helmRepositoryCache,
RepositoryConfig: helmRepositoryConfig,
RegistryConfig: helmRegistryConfigFile,
Debug: debug,
DebugLog: debugLog,
PlainHttp: helmRepoData.PlainHttp,
InsecureTls: helmRepoData.InsecureTLS,
},
RestConfig: ctrl.GetConfigOrDie(),
}
helmClient, err := client.NewClientFromRestConf(opt)
if err != nil {
return nil, fmt.Errorf("failed to create helm client: %w", err)
}
return &Client{
helmClient: helmClient,
helmRepoData: helmRepoData,
dependencyChecker: &installedDependencyChecker{},
}, nil
}
// InstallOrUpgrade takes a helmChart and applies it.
func (c *Client) InstallOrUpgrade(ctx context.Context, chart *client.ChartSpec) error {
// This helm-client currently only works with OCI-Helm-Repositories.
// Therefore, the chartName has to include the FQDN of the repository (e.g. "oci://my.repo/...")
// If in the future non-oci-repositories need to be used, this should be done here...
c.patchOciEndpoint(chart)
if err := c.patchChartVersion(chart); err != nil {
return fmt.Errorf("error patching chart-version for chart %s: %w", chart.ChartName, err)
}
_, err := c.helmClient.InstallOrUpgradeChart(ctx, chart)
if err != nil {
return fmt.Errorf("error while installOrUpgrade chart %s: %w", chart.ChartName, err)
}
return nil
}
// SatisfiesDependencies checks if all dependencies are satisfied in terms of installation and version.
func (c *Client) SatisfiesDependencies(ctx context.Context, chart *client.ChartSpec) error {
logger := log.FromContext(ctx)
logger.Info("Checking if components dependencies are satisfied", "component", chart.ChartName)
c.patchOciEndpoint(chart)
if err := c.patchChartVersion(chart); err != nil {
return fmt.Errorf("error patching chart-version for chart %s: %w", chart.ChartName, err)
}
componentChart, err := c.getChart(ctx, chart)
if err != nil {
return fmt.Errorf("failed to get chart %s: %w", chart.ChartName, err)
}
dependencies := getComponentDependencies(componentChart)
deployedReleases, err := c.ListDeployedReleases()
if err != nil {
return fmt.Errorf("failed to list deployed releases: %w", err)
}
err = c.dependencyChecker.CheckSatisfied(dependencies, deployedReleases)
if err != nil {
return &dependencyUnsatisfiedError{err: err}
}
return nil
}
func (c *Client) getChart(ctx context.Context, chartSpec *client.ChartSpec) (*chart.Chart, error) {
logger := log.FromContext(ctx)
logger.Info("Trying to get chart with options",
"chart", chartSpec.ChartName,
"version", chartSpec.Version,
"plainHTTP", c.helmRepoData.PlainHttp,
"insecureTLS", c.helmRepoData.InsecureTLS)
componentChart, _, err := c.helmClient.GetChart(chartSpec)
if err != nil {
return nil, fmt.Errorf("error while getting chart for %s:%s: %w", chartSpec.ChartName, chartSpec.Version, err)
}
return componentChart, nil
}
// Uninstall removes the helmRelease for the given name
func (c *Client) Uninstall(releaseName string) error {
if err := c.helmClient.UninstallReleaseByName(releaseName); err != nil {
return fmt.Errorf("error while uninstalling helm-release %s: %w", releaseName, err)
}
return nil
}
// ListDeployedReleases returns all deployed helm releases
func (c *Client) ListDeployedReleases() ([]*release.Release, error) {
return c.helmClient.ListDeployedReleases()
}
// GetReleaseValues returns the (optionally, all computed) values for the specified release.
func (c *Client) GetReleaseValues(name string, allValues bool) (map[string]interface{}, error) {
return c.helmClient.GetReleaseValues(name, allValues)
}
// GetDeployedReleaseVersion returns the version for the specified release (if the release exists).
func (c *Client) GetDeployedReleaseVersion(ctx context.Context, name string) (string, error) {
logger := log.FromContext(ctx)
deployedReleases, err := c.ListDeployedReleases()
if err != nil {
return "", fmt.Errorf("could not list deployed Helm releases: %w", err)
}
for _, k8sRelease := range deployedReleases {
if name == k8sRelease.Name {
version := k8sRelease.Chart.AppVersion()
logger.Info("Found existing release for reconciled component",
"component", name, "version", version)
return version, nil
}
}
logger.Info("could not find a deployed release for component: ", name)
return "", nil
}
// GetChartSpecValues returns the additional values for the specified ChartSpec.
func (c *Client) GetChartSpecValues(spec *client.ChartSpec) (map[string]interface{}, error) {
return c.helmClient.GetChartSpecValues(spec)
}
func (c *Client) patchOciEndpoint(chart *client.ChartSpec) {
if strings.HasPrefix(chart.ChartName, ociSchemePrefix) {
return
}
chart.ChartName = fmt.Sprintf("%s/%s", c.helmRepoData.URL(), chart.ChartName)
}
func (c *Client) patchChartVersion(chart *client.ChartSpec) error {
if chart.Version != "" {
return nil
}
ref := strings.TrimPrefix(chart.ChartName, ociSchemePrefix)
tags, err := c.helmClient.Tags(ref)
if err != nil {
return fmt.Errorf("error resolving tags for chart %s: %w", chart.ChartName, err)
}
//sort tags by version
sortedTags := sortByVersionDescending(tags)
if len(sortedTags) <= 0 {
return fmt.Errorf("could not find any tags for chart %s", chart.ChartName)
}
// set version to the latest tag
chart.Version = sortedTags[0]
return nil
}
func sortByVersionDescending(tags []string) []string {
versions := make([]core.Version, 0)
for _, tag := range tags {
v, err := core.ParseVersion(tag)
if err == nil {
versions = append(versions, v)
}
}
sort.SliceStable(versions, func(i, j int) bool {
return versions[i].IsNewerOrEqualThan(versions[j])
})
result := make([]string, len(versions))
for i, version := range versions {
result[i] = version.Raw
}
return result
}
type dependencyUnsatisfiedError struct {
err error
}
// Error returns the string representation of the wrapped error.
func (due *dependencyUnsatisfiedError) Error() string {
return fmt.Sprintf("one or more dependencies are not satisfied: %s", due.err.Error())
}
// Unwrap returns the root error.
func (due *dependencyUnsatisfiedError) Unwrap() error {
return due.err
}