aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin/internal/helm/helm.go
blob: d39e8404df61386c283d8e4709e6a8cd86527c78 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
/*
 * Copyright 2018 Intel Corporation, Inc
 * Copyright © 2021 Samsung Electronics
 *
 * 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 helm

import (
	"fmt"
	"io/ioutil"
	"k8s.io/helm/pkg/strvals"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strconv"
	"strings"

	utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"

	"github.com/ghodss/yaml"
	pkgerrors "github.com/pkg/errors"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/runtime/serializer/json"
	"k8s.io/apimachinery/pkg/util/validation"
	k8syaml "k8s.io/apimachinery/pkg/util/yaml"
	"k8s.io/helm/pkg/chartutil"
	"k8s.io/helm/pkg/hooks"
	"k8s.io/helm/pkg/manifest"
	"k8s.io/helm/pkg/proto/hapi/chart"
	protorelease "k8s.io/helm/pkg/proto/hapi/release"
	"k8s.io/helm/pkg/releaseutil"
	"k8s.io/helm/pkg/renderutil"
	"k8s.io/helm/pkg/tiller"
	"k8s.io/helm/pkg/timeconv"
)

// Template is the interface for all helm templating commands
// Any backend implementation will implement this interface and will
// access the functionality via this.
// FIXME Template is not referenced anywhere
type Template interface {
	GenerateKubernetesArtifacts(
		chartPath string,
		valueFiles []string,
		values []string) (map[string][]string, error)
}

// TemplateClient implements the Template interface
// It will also be used to maintain any localized state
type TemplateClient struct {
	emptyRegex    *regexp.Regexp
	kubeVersion   string
	kubeNameSpace string
	releaseName   string
}

// NewTemplateClient returns a new instance of TemplateClient
func NewTemplateClient(k8sversion, namespace, releasename string) *TemplateClient {
	return &TemplateClient{
		// emptyRegex defines template content that could be considered empty yaml-wise
		emptyRegex: regexp.MustCompile(`(?m)\A(^(\s*#.*|\s*)$\n?)*\z`),
		// defaultKubeVersion is the default value of --kube-version flag
		kubeVersion:   k8sversion,
		kubeNameSpace: namespace,
		releaseName:   releasename,
	}
}

// Define hooks that are honored by k8splugin
var honoredEvents = map[string]protorelease.Hook_Event{
	hooks.ReleaseTestSuccess: protorelease.Hook_RELEASE_TEST_SUCCESS,
	hooks.ReleaseTestFailure: protorelease.Hook_RELEASE_TEST_FAILURE,
}

// Combines valueFiles and values into a single values stream.
// values takes precedence over valueFiles
func (h *TemplateClient) processValues(valueFiles []string, values []string) ([]byte, error) {
	base := map[string]interface{}{}

	//Values files that are used for overriding the chart
	for _, filePath := range valueFiles {
		currentMap := map[string]interface{}{}

		var bytes []byte
		var err error
		if strings.TrimSpace(filePath) == "-" {
			bytes, err = ioutil.ReadAll(os.Stdin)
		} else {
			bytes, err = ioutil.ReadFile(filePath)
		}

		if err != nil {
			return []byte{}, err
		}

		if err := yaml.Unmarshal(bytes, &currentMap); err != nil {
			return []byte{}, fmt.Errorf("failed to parse %s: %s", filePath, err)
		}
		// Merge with the previous map
		base = h.mergeValues(base, currentMap)
	}

	//User specified value. Similar to ones provided by -x
	for _, value := range values {
		if err := strvals.ParseInto(value, base); err != nil {
			return []byte{}, fmt.Errorf("failed parsing --set data: %s", err)
		}
	}

	return yaml.Marshal(base)
}

func (h *TemplateClient) mergeValues(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
	for k, v := range src {
		// If the key doesn't exist already, then just set the key to that value
		if _, exists := dest[k]; !exists {
			dest[k] = v
			continue
		}
		nextMap, ok := v.(map[string]interface{})
		// If it isn't another map, overwrite the value
		if !ok {
			dest[k] = v
			continue
		}
		// Edge case: If the key exists in the destination, but isn't a map
		destMap, isMap := dest[k].(map[string]interface{})
		// If the source map has a map for this key, prefer it
		if !isMap {
			dest[k] = v
			continue
		}
		// If we got to this point, it is a map in both, so merge them
		dest[k] = h.mergeValues(destMap, nextMap)
	}
	return dest
}

// Checks whether resource is a hook and if it is, returns hook struct
//Logic is based on private method
//file *manifestFile) sort(result *result) error
//of helm/pkg/tiller package
func isHook(path, resource string) (*protorelease.Hook, error) {

	var entry releaseutil.SimpleHead
	err := yaml.Unmarshal([]byte(resource), &entry)
	if err != nil {
		return nil, pkgerrors.Wrap(err, "Loading resource to YAML")
	}
	//If resource has no metadata it can't be a hook
	if entry.Metadata == nil ||
		entry.Metadata.Annotations == nil ||
		len(entry.Metadata.Annotations) == 0 {
		return nil, nil
	}
	//Determine hook weight
	hookWeight, err := strconv.Atoi(entry.Metadata.Annotations[hooks.HookWeightAnno])
	if err != nil {
		hookWeight = 0
	}
	//Prepare hook obj
	resultHook := &protorelease.Hook{
		Name:           entry.Metadata.Name,
		Kind:           entry.Kind,
		Path:           path,
		Manifest:       resource,
		Events:         []protorelease.Hook_Event{},
		Weight:         int32(hookWeight),
		DeletePolicies: []protorelease.Hook_DeletePolicy{},
	}
	//Determine hook's events
	hookTypes, ok := entry.Metadata.Annotations[hooks.HookAnno]
	if !ok {
		return resultHook, nil
	}
	for _, hookType := range strings.Split(hookTypes, ",") {
		hookType = strings.ToLower(strings.TrimSpace(hookType))
		e, ok := honoredEvents[hookType]
		if ok {
			resultHook.Events = append(resultHook.Events, e)
		}
	}
	return resultHook, nil
}

// GenerateKubernetesArtifacts a mapping of type to fully evaluated helm template
func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFiles []string,
	values []string) ([]KubernetesResourceTemplate, []*protorelease.Hook, error) {

	var outputDir, chartPath, namespace, releaseName string
	var retData []KubernetesResourceTemplate
	var hookList []*protorelease.Hook

	releaseName = h.releaseName
	namespace = h.kubeNameSpace

	// verify chart path exists
	if _, err := os.Stat(inputPath); err == nil {
		if chartPath, err = filepath.Abs(inputPath); err != nil {
			return retData, hookList, err
		}
	} else {
		return retData, hookList, err
	}

	//Create a temp directory in the system temp folder
	outputDir, err := ioutil.TempDir("", "helm-tmpl-")
	if err != nil {
		return retData, hookList, pkgerrors.Wrap(err, "Got error creating temp dir")
	}

	if namespace == "" {
		namespace = "default"
	}

	// get combined values and create config
	rawVals, err := h.processValues(valueFiles, values)
	if err != nil {
		return retData, hookList, err
	}
	config := &chart.Config{Raw: string(rawVals), Values: map[string]*chart.Value{}}

	if msgs := validation.IsDNS1123Label(releaseName); releaseName != "" && len(msgs) > 0 {
		return retData, hookList, fmt.Errorf("release name %s is not a valid DNS label: %s", releaseName, strings.Join(msgs, ";"))
	}

	// Check chart requirements to make sure all dependencies are present in /charts
	c, err := chartutil.Load(chartPath)
	if err != nil {
		return retData, hookList, pkgerrors.Errorf("Got error: %s", err.Error())
	}

	renderOpts := renderutil.Options{
		ReleaseOptions: chartutil.ReleaseOptions{
			Name:      releaseName,
			IsInstall: true,
			IsUpgrade: false,
			Time:      timeconv.Now(),
			Namespace: namespace,
		},
		KubeVersion: h.kubeVersion,
	}

	renderedTemplates, err := renderutil.Render(c, config, renderOpts)
	if err != nil {
		return retData, hookList, err
	}

	newRenderedTemplates := make(map[string]string)

	//Some manifests can contain multiple yaml documents
	//This step is splitting them up into multiple files
	//Each file contains only a single k8s kind
	for k, v := range renderedTemplates {
		//Splits into manifest-0, manifest-1 etc
		if filepath.Base(k) == "NOTES.txt" {
			continue
		}
		rmap := releaseutil.SplitManifests(v)

		// Iterating over map can yield different order at times
		// so first we'll sort keys
		sortedKeys := make([]string, len(rmap))
		for k1, _ := range rmap {
			sortedKeys = append(sortedKeys, k1)
		}
		// This makes empty files have the lowest indices
		sort.Strings(sortedKeys)

		for k1, v1 := range sortedKeys {
			key := fmt.Sprintf("%s-%d", k, k1)
			newRenderedTemplates[key] = rmap[v1]
		}
	}

	listManifests := manifest.SplitManifests(newRenderedTemplates)
	var manifestsToRender []manifest.Manifest
	//render all manifests in the chart
	manifestsToRender = listManifests
	for _, m := range tiller.SortByKind(manifestsToRender) {
		data := m.Content
		b := filepath.Base(m.Name)
		if b == "NOTES.txt" {
			continue
		}
		if strings.HasPrefix(b, "_") {
			continue
		}

		// blank template after execution
		if h.emptyRegex.MatchString(data) {
			continue
		}

		hook, _ := isHook(b, data)
		// if hook is not nil, then append it to hooks list and continue
		// if it's not, disregard error
		if hook != nil {
			hookList = append(hookList, hook)
			continue
		}

		mfilePath := filepath.Join(outputDir, m.Name)
		utils.EnsureDirectory(mfilePath)
		err = ioutil.WriteFile(mfilePath, []byte(data), 0666)
		if err != nil {
			return retData, hookList, err
		}

		gvk, err := getGroupVersionKind(data)
		if err != nil {
			return retData, hookList, err
		}

		kres := KubernetesResourceTemplate{
			GVK:      gvk,
			FilePath: mfilePath,
		}
		retData = append(retData, kres)
	}
	return retData, hookList, nil
}

func getGroupVersionKind(data string) (schema.GroupVersionKind, error) {
	out, err := k8syaml.ToJSON([]byte(data))
	if err != nil {
		return schema.GroupVersionKind{}, pkgerrors.Wrap(err, "Converting yaml to json:\n"+data)
	}

	simpleMeta := json.SimpleMetaFactory{}
	gvk, err := simpleMeta.Interpret(out)
	if err != nil {
		return schema.GroupVersionKind{}, pkgerrors.Wrap(err, "Parsing apiversion and kind")
	}

	return *gvk, nil
}