-
Notifications
You must be signed in to change notification settings - Fork 8
/
helm.go
272 lines (234 loc) · 8.4 KB
/
helm.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
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
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package helm
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/dynamic"
"helm.sh/helm/v3/pkg/releaseutil"
"helm.sh/helm/v3/pkg/action"
helm "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/flomesh-io/fsm/pkg/configurator"
"github.com/flomesh-io/fsm/pkg/utils"
)
// RenderChart renders a chart and returns the rendered manifest
func RenderChart(
templateClient *helm.Install,
object metav1.Object,
chartSource []byte,
mc configurator.Configurator,
client client.Client,
scheme *runtime.Scheme,
resolveValues func(metav1.Object, configurator.Configurator) (map[string]interface{}, error),
) (ctrl.Result, error) {
chart, err := loader.LoadArchive(bytes.NewReader(chartSource))
if err != nil {
return ctrl.Result{}, fmt.Errorf("error loading chart for installation: %s", err)
}
log.Debug().Msgf("[HELM UTIL] Chart = %v", chart)
values, err := resolveValues(object, mc)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error resolve values for installation: %s", err)
}
log.Debug().Msgf("[HELM UTIL] Values = %s", values)
rel, err := templateClient.Run(chart, values)
if err != nil {
log.Error().Msgf("[HELM UTIL] Error rendering chart: %s", err)
return ctrl.Result{}, fmt.Errorf("error rendering templates: %s", err)
}
manifests := rel.Manifest
log.Debug().Msgf("[HELM UTIL] Manifest = \n%s\n", manifests)
if result, err := applyChartYAMLs(object, manifests, client, scheme); err != nil {
log.Error().Msgf("[HELM UTIL] Error applying chart YAMLs: %s", err)
return result, err
}
return ctrl.Result{}, nil
}
func TemplateClient(cfg *helm.Configuration, releaseName, namespace string, kubeVersion *chartutil.KubeVersion) *helm.Install {
//log.Debug().Msgf("[HELM UTIL] Creating Helm Install Client ...")
installClient := helm.NewInstall(cfg)
installClient.ReleaseName = releaseName
installClient.Namespace = namespace
installClient.CreateNamespace = false
installClient.DryRun = true
installClient.ClientOnly = true
installClient.KubeVersion = kubeVersion
return installClient
}
func ActionConfig(namespace string, debugLog action.DebugLog) *helm.Configuration {
configFlags := &genericclioptions.ConfigFlags{Namespace: &namespace}
actionConfig := new(action.Configuration)
_ = actionConfig.Init(configFlags, namespace, "secret", debugLog)
return actionConfig
}
func applyChartYAMLs(owner metav1.Object, manifests string, client client.Client, scheme *runtime.Scheme) (ctrl.Result, error) {
yamlReader := utilyaml.NewYAMLReader(bufio.NewReader(bytes.NewReader([]byte(manifests))))
for {
buf, err := yamlReader.Read()
if err != nil {
if err == io.EOF {
break
}
log.Error().Msgf("Error reading yaml: %s", err)
return ctrl.Result{RequeueAfter: 1 * time.Second}, err
}
log.Debug().Msgf("[HELM UTIL] Processing YAML : \n\n%s\n\n", string(buf))
obj, err := utils.DecodeYamlToUnstructured(buf)
if err != nil {
log.Error().Msgf("Error decoding YAML to Unstructured object: %s", err)
return ctrl.Result{RequeueAfter: 1 * time.Second}, err
}
log.Debug().Msgf("[HELM UTIL] Unstructured Object = \n\n%v\n\n", obj)
if isValidOwner(owner, obj) {
if err = ctrl.SetControllerReference(owner, obj, scheme); err != nil {
log.Error().Msgf("Error setting controller reference: %s", err)
return ctrl.Result{RequeueAfter: 1 * time.Second}, err
}
log.Debug().Msgf("[HELM UTIL] Resource %s/%s, Owner: %v", obj.GetNamespace(), obj.GetName(), obj.GetOwnerReferences())
}
result, err := utils.CreateOrUpdate(context.TODO(), client, obj)
if err != nil {
log.Error().Msgf("Error creating/updating object: %s", err)
return ctrl.Result{RequeueAfter: 1 * time.Second}, err
}
log.Debug().Msgf("[HELM UTIL] Successfully %s object: %v", result, obj)
}
return ctrl.Result{}, nil
}
func isValidOwner(owner, object metav1.Object) bool {
ownerNs := owner.GetNamespace()
if ownerNs != "" {
objNs := object.GetNamespace()
if objNs == "" {
log.Warn().Msgf("cluster-scoped resource must not have a namespace-scoped owner, owner's namespace %s", ownerNs)
return false
}
if ownerNs != objNs {
log.Warn().Msgf("cross-namespace owner references are disallowed, owner's namespace %s, obj's namespace %s", owner.GetNamespace(), object.GetNamespace())
return false
}
}
return true
}
func ApplyYAMLs(
dynamicClient dynamic.Interface,
mapper meta.RESTMapper,
manifests string,
handler YAMLHandlerFunc,
showFiles ...string,
) error {
splitManifests := releaseutil.SplitManifests(manifests)
manifestsKeys := make([]string, 0, len(splitManifests))
for k := range splitManifests {
manifestsKeys = append(manifestsKeys, k)
}
sort.Sort(releaseutil.BySplitManifestsOrder(manifestsKeys))
if len(showFiles) > 0 {
manifestNameRegex := regexp.MustCompile("# Source: [^/]+/(.+)")
var manifestsToRender []string
for _, f := range showFiles {
missing := true
// Use linux-style filepath separators to unify user's input path
f = filepath.ToSlash(f)
for _, manifestKey := range manifestsKeys {
manifest := splitManifests[manifestKey]
submatch := manifestNameRegex.FindStringSubmatch(manifest)
if len(submatch) == 0 {
continue
}
manifestName := submatch[1]
// manifest.Name is rendered using linux-style filepath separators on Windows as
// well as macOS/linux.
manifestPathSplit := strings.Split(manifestName, "/")
// manifest.Path is connected using linux-style filepath separators on Windows as
// well as macOS/linux
manifestPath := strings.Join(manifestPathSplit, "/")
// if the filepath provided matches a manifest path in the
// chart, render that manifest
if matched, _ := filepath.Match(f, manifestPath); !matched {
continue
}
manifestsToRender = append(manifestsToRender, manifest)
missing = false
}
if missing {
return fmt.Errorf("could not find template %s in chart", f)
}
}
for _, manifest := range manifestsToRender {
if err := handler(dynamicClient, mapper, manifest); err != nil {
return err
}
}
} else {
for _, manifestKey := range manifestsKeys {
manifest := splitManifests[manifestKey]
if err := handler(dynamicClient, mapper, manifest); err != nil {
return err
}
}
}
return nil
}
func ApplyManifest(dynamicClient dynamic.Interface, mapper meta.RESTMapper, manifest string) error {
obj, err := utils.DecodeYamlToUnstructured([]byte(manifest))
if err != nil {
return err
}
if err := utils.CreateOrUpdateUnstructured(context.TODO(), dynamicClient, mapper, obj); err != nil {
return err
}
return nil
}
func DeleteManifest(dynamicClient dynamic.Interface, mapper meta.RESTMapper, manifest string) error {
obj, err := utils.DecodeYamlToUnstructured([]byte(manifest))
if err != nil {
return err
}
if err := utils.DeleteUnstructured(context.TODO(), dynamicClient, mapper, obj); err != nil {
// ignore if not found
if !errors.IsNotFound(err) {
return err
}
}
return nil
}