forked from open-cluster-management-io/addon-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelm_agentaddon.go
More file actions
339 lines (293 loc) · 10.3 KB
/
helm_agentaddon.go
File metadata and controls
339 lines (293 loc) · 10.3 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package addonfactory
import (
"bufio"
"fmt"
"io"
"sort"
"strings"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/releaseutil"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/klog/v2"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/addon-framework/pkg/addonmanager/constants"
"open-cluster-management.io/addon-framework/pkg/agent"
)
// helmBuiltinValues includes the built-in values for helm agentAddon.
// the values in helm chart should begin with a lowercase letter, so we need convert it to Values by JsonStructToValues.
// the built-in values can not be overrided by getValuesFuncs
type helmBuiltinValues struct {
ClusterName string `json:"clusterName"`
AddonInstallNamespace string `json:"addonInstallNamespace"`
HubKubeConfigSecret string `json:"hubKubeConfigSecret,omitempty"`
ManagedKubeConfigSecret string `json:"managedKubeConfigSecret,omitempty"`
InstallMode string `json:"installMode"`
}
// helmDefaultValues includes the default values for helm agentAddon.
// the values in helm chart should begin with a lowercase letter, so we need convert it to Values by JsonStructToValues.
// the default values can be overrided by getValuesFuncs
type helmDefaultValues struct {
HubKubeConfigSecret string `json:"hubKubeConfigSecret,omitempty"`
ManagedKubeConfigSecret string `json:"managedKubeConfigSecret,omitempty"`
HostingClusterCapabilities chartutil.Capabilities `json:"hostingClusterCapabilities,omitempty"`
}
type HelmAgentAddon struct {
decoder runtime.Decoder
chart *chart.Chart
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
trimCRDDescription bool
hostingCluster *clusterv1.ManagedCluster
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
createAgentInstallNamespace bool
helmEngineStrict bool
}
func newHelmAgentAddon(factory *AgentAddonFactory, chart *chart.Chart) *HelmAgentAddon {
return &HelmAgentAddon{
decoder: serializer.NewCodecFactory(factory.scheme).UniversalDeserializer(),
chart: chart,
getValuesFuncs: factory.getValuesFuncs,
agentAddonOptions: factory.agentAddonOptions,
trimCRDDescription: factory.trimCRDDescription,
hostingCluster: factory.hostingCluster,
agentInstallNamespace: factory.agentInstallNamespace,
createAgentInstallNamespace: factory.createAgentInstallNamespace,
helmEngineStrict: factory.helmEngineStrict,
}
}
func (a *HelmAgentAddon) Manifests(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
objects, err := a.renderManifests(cluster, addon)
if err != nil {
return nil, err
}
manifests := make([]Manifest, 0, len(objects))
for _, obj := range objects {
a, err := meta.TypeAccessor(obj)
if err != nil {
return nil, err
}
manifests = append(manifests, Manifest{
Object: obj,
Kind: a.GetKind(),
})
}
sortManifestsByKind(manifests, releaseutil.InstallOrder)
for i, manifest := range manifests {
objects[i] = manifest.Object
}
return objects, nil
}
func (a *HelmAgentAddon) renderManifests(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
var objects []runtime.Object
values, err := a.getValues(cluster, addon)
if err != nil {
return objects, err
}
helmEngine := engine.Engine{
Strict: a.helmEngineStrict,
LintMode: false,
}
crds := a.chart.CRDObjects()
for _, crd := range crds {
klog.V(4).Infof("%v/n", crd.File.Data)
object, _, err := a.decoder.Decode(crd.File.Data, nil, nil)
if err != nil {
return nil, err
}
objects = append(objects, object)
}
templates, err := helmEngine.Render(a.chart, values)
if err != nil {
return objects, err
}
agentInstallNamespace := a.getValueAgentInstallNamespace(addon)
for k, data := range templates {
if len(data) == 0 {
continue
}
klog.V(4).Infof("rendered template: %v", data)
yamlReader := yaml.NewYAMLReader(bufio.NewReader(strings.NewReader(data)))
for {
b, err := yamlReader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if len(b) != 0 {
object, _, err := a.decoder.Decode(b, nil, nil)
if err != nil {
// In some conditions, resources will be provide by other hub-side components.
// Example case: https://github.com/open-cluster-management-io/addon-framework/pull/72
if runtime.IsMissingKind(err) {
klog.V(4).Infof("Skipping template %v, reason: %v", k, err)
continue
}
return nil, err
}
objects = append(objects, object)
}
}
if agentInstallNamespace != "" && a.createAgentInstallNamespace {
var ns unstructured.Unstructured
ns.SetAPIVersion("v1")
ns.SetKind("Namespace")
ns.SetName(agentInstallNamespace)
objects = append(objects, &ns)
}
}
if a.trimCRDDescription {
objects = trimCRDDescription(objects)
}
return objects, nil
}
func (a *HelmAgentAddon) GetAgentAddonOptions() agent.AgentAddonOptions {
return a.agentAddonOptions
}
func (a *HelmAgentAddon) getValues(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) (chartutil.Values, error) {
overrideValues := map[string]interface{}{}
defaultValues, err := a.getDefaultValues(cluster, addon)
if err != nil {
klog.Errorf("failed to get defaultValue. err:%v", err)
return nil, err
}
overrideValues = MergeValues(overrideValues, defaultValues)
for i := 0; i < len(a.getValuesFuncs); i++ {
if a.getValuesFuncs[i] != nil {
userValues, err := a.getValuesFuncs[i](cluster, addon)
if err != nil {
return overrideValues, err
}
klog.V(4).Infof("index=%d, user values: %v", i, userValues)
overrideValues = MergeValues(overrideValues, userValues)
klog.V(4).Infof("index=%d, override values: %v", i, overrideValues)
}
}
builtinValues, err := a.getBuiltinValues(cluster, addon)
if err != nil {
klog.Errorf("failed to get builtinValue. err:%v", err)
return nil, err
}
overrideValues = MergeValues(overrideValues, builtinValues)
values, err := chartutil.ToRenderValues(a.chart, overrideValues,
a.releaseOptions(addon), a.capabilities(cluster, addon))
if err != nil {
klog.Errorf("failed to render helm chart with values %v. err:%v", overrideValues, err)
return values, err
}
return values, nil
}
func (a *HelmAgentAddon) getValueAgentInstallNamespace(addon *addonapiv1alpha1.ManagedClusterAddOn) string {
installNamespace := addon.Spec.InstallNamespace
if len(installNamespace) == 0 {
installNamespace = AddonDefaultInstallNamespace
}
if a.agentInstallNamespace != nil {
ns := a.agentInstallNamespace(addon)
if len(ns) > 0 {
installNamespace = ns
}
}
return installNamespace
}
func (a *HelmAgentAddon) getBuiltinValues(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) (Values, error) {
builtinValues := helmBuiltinValues{}
builtinValues.ClusterName = cluster.GetName()
builtinValues.AddonInstallNamespace = a.getValueAgentInstallNamespace(addon)
builtinValues.InstallMode, _ = constants.GetHostedModeInfo(addon.GetAnnotations())
helmBuiltinValues, err := JsonStructToValues(builtinValues)
if err != nil {
klog.Errorf("failed to convert builtinValues to values %v.err:%v", builtinValues, err)
return nil, err
}
return helmBuiltinValues, nil
}
func (a *HelmAgentAddon) getDefaultValues(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) (Values, error) {
defaultValues := helmDefaultValues{}
// TODO: hubKubeConfigSecret depends on the signer configuration in registration, and the registration is an array.
if a.agentAddonOptions.Registration != nil {
defaultValues.HubKubeConfigSecret = fmt.Sprintf("%s-hub-kubeconfig", a.agentAddonOptions.AddonName)
}
defaultValues.ManagedKubeConfigSecret = fmt.Sprintf("%s-managed-kubeconfig", addon.Name)
if a.hostingCluster != nil {
defaultValues.HostingClusterCapabilities = *a.capabilities(a.hostingCluster, addon)
}
helmDefaultValues, err := JsonStructToValues(defaultValues)
if err != nil {
klog.Errorf("failed to convert defaultValues to values %v.err:%v", defaultValues, err)
return nil, err
}
return helmDefaultValues, nil
}
// only support Capabilities.KubeVersion
func (a *HelmAgentAddon) capabilities(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) *chartutil.Capabilities {
return &chartutil.Capabilities{
KubeVersion: chartutil.KubeVersion{Version: cluster.Status.Version.Kubernetes},
}
}
// only support Release.Name, Release.Namespace
func (a *HelmAgentAddon) releaseOptions(
addon *addonapiv1alpha1.ManagedClusterAddOn) chartutil.ReleaseOptions {
return chartutil.ReleaseOptions{
Name: a.agentAddonOptions.AddonName,
Namespace: a.getValueAgentInstallNamespace(addon),
}
}
// Manifest represents a manifest file, which has a name and some content.
type Manifest struct {
Object runtime.Object
Kind string
}
// sort manifests by kind.
//
// Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering releaseutil.KindSortOrder) []Manifest {
sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind(manifests[i], manifests[j], manifests[i].Kind, manifests[j].Kind, ordering)
})
return manifests
}
func lessByKind(a interface{}, b interface{}, kindA string, kindB string, o releaseutil.KindSortOrder) bool {
ordering := make(map[string]int, len(o))
for v, k := range o {
ordering[k] = v
}
first, aok := ordering[kindA]
second, bok := ordering[kindB]
if !aok && !bok {
// if both are unknown then sort alphabetically by kind, keep original order if same kind
if kindA != kindB {
return kindA < kindB
}
return first < second
}
// unknown kind is last
if !aok {
return false
}
if !bok {
return true
}
// sort different kinds, keep original order if same priority
return first < second
}