/
chart.go
192 lines (167 loc) · 6.21 KB
/
chart.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
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chart
import (
"context"
"github.com/gardener/gardener/pkg/chartrenderer"
gardenerkubernetes "github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener/pkg/utils/imagevector"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Chart represents a Helm chart (and its sub-charts) that can be applied and deleted.
type Chart struct {
Name string
Path string
Images []string
Objects []*Object
SubCharts []*Chart
}
// Object represents an object deployed by a Chart.
type Object struct {
Type runtime.Object
Name string
}
// Apply applies this chart in the given namespace using the given ChartApplier. Before applying the chart,
// it collects its values, injecting images and merging the given values as needed.
func (c *Chart) Apply(
ctx context.Context,
chartApplier gardenerkubernetes.ChartApplier,
namespace string,
imageVector imagevector.ImageVector,
runtimeVersion, targetVersion string,
additionalValues map[string]interface{},
) error {
// Get values with injected images
values, err := c.injectImages(imageVector, runtimeVersion, targetVersion)
if err != nil {
return err
}
// Apply chart
err = chartApplier.Apply(ctx, c.Path, namespace, c.Name, gardenerkubernetes.Values(utils.MergeMaps(values, additionalValues)))
if err != nil {
return errors.Wrapf(err, "could not apply chart '%s' in namespace '%s'", c.Name, namespace)
}
return nil
}
// Render renders this chart in the given namespace using the given chartRenderer. Before rendering the chart,
// it collects its values, injecting images and merging the given values as needed.
func (c *Chart) Render(
chartRenderer chartrenderer.Interface,
namespace string,
imageVector imagevector.ImageVector,
runtimeVersion, targetVersion string,
additionalValues map[string]interface{},
) (string, []byte, error) {
// Get values with injected images
values, err := c.injectImages(imageVector, runtimeVersion, targetVersion)
if err != nil {
return "", nil, err
}
// Apply chart
rc, err := chartRenderer.Render(c.Path, c.Name, namespace, utils.MergeMaps(values, additionalValues))
if err != nil {
return "", nil, errors.Wrapf(err, "could not render chart '%s' in namespace '%s'", c.Name, namespace)
}
return rc.ChartName, rc.Manifest(), nil
}
// injectImages collects returns a values map with injected images, including sub-charts.
func (c *Chart) injectImages(
imageVector imagevector.ImageVector,
runtimeVersion, targetVersion string,
) (map[string]interface{}, error) {
// Inject images
values := make(map[string]interface{})
var err error
if len(c.Images) > 0 {
values, err = InjectImages(values, imageVector, c.Images, imagevector.RuntimeVersion(runtimeVersion), imagevector.TargetVersion(targetVersion))
if err != nil {
return nil, errors.Wrapf(err, "could not inject chart '%s' images", c.Name)
}
}
// Add subchart values
for _, sc := range c.SubCharts {
scValues, err := sc.injectImages(imageVector, runtimeVersion, targetVersion)
if err != nil {
return nil, err
}
values[sc.Name] = scValues
}
return values, nil
}
// Delete deletes this chart's objects from the given namespace using the given client.
func (c *Chart) Delete(ctx context.Context, client client.Client, namespace string) error {
// Delete objects
for _, o := range c.Objects {
if err := o.Delete(ctx, client, namespace); err != nil {
return errors.Wrap(err, "could not delete chart '%s' object")
}
}
// Delete subchart objects
for _, sc := range c.SubCharts {
if err := sc.Delete(ctx, client, namespace); err != nil {
return err
}
}
return nil
}
// Delete deletes this object from the given namespace using the given client.
func (o *Object) Delete(ctx context.Context, client client.Client, namespace string) error {
obj := o.Type.DeepCopyObject()
kind := obj.GetObjectKind().GroupVersionKind().Kind
key := objectKey(namespace, o.Name)
if err := client.Get(ctx, key, obj); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, "could not get %s '%s'", kind, key.String())
}
if err := client.Delete(ctx, obj); err != nil {
return errors.Wrapf(err, "could not delete %s '%s'", kind, key.String())
}
return nil
}
func objectKey(namespace, name string) client.ObjectKey {
return client.ObjectKey{Namespace: namespace, Name: name}
}
// CopyValues creates a shallow copy of the given Values.
func CopyValues(values map[string]interface{}) map[string]interface{} {
copiedValues := make(map[string]interface{}, len(values))
for k, v := range values {
copiedValues[k] = v
}
return copiedValues
}
// ImageMapToValues transforms the given image name to image mapping into chart Values.
func ImageMapToValues(m map[string]*imagevector.Image) map[string]interface{} {
out := make(map[string]interface{}, len(m))
for k, v := range m {
out[k] = v.String()
}
return out
}
// InjectImages finds the images with the given names and opts, makes a shallow copy of the given
// Values and injects a name to image string mapping at the `images` key of that map and returns it.
func InjectImages(values map[string]interface{}, v imagevector.ImageVector, names []string, opts ...imagevector.FindOptionFunc) (map[string]interface{}, error) {
images, err := imagevector.FindImages(v, names, opts...)
if err != nil {
return nil, err
}
values = CopyValues(values)
values["images"] = ImageMapToValues(images)
return values, nil
}