This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.go
282 lines (242 loc) · 6.74 KB
/
utils.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
273
274
275
276
277
278
279
280
281
282
// Copyright 2016 The Kubernetes Authors All rights reserved.
// Use of this source code is governed by a Apache License Version 2.0 license
// that can be found at https://github.com/kubernetes/helm/blob/master/LICENSE
package helm
import (
"fmt"
"os"
"path"
"sort"
"strconv"
"strings"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"k8s.io/client-go/discovery"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/downloader"
"k8s.io/helm/pkg/getter"
"k8s.io/helm/pkg/hooks"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/proto/hapi/release"
util "k8s.io/helm/pkg/releaseutil"
"k8s.io/helm/pkg/repo"
"k8s.io/helm/pkg/tiller"
"k8s.io/helm/pkg/version"
)
type result struct {
hooks []*release.Hook
generic []tiller.Manifest
}
type manifestFile struct {
entries map[string]string
path string
apis chartutil.VersionSet
}
type kindSorter struct {
ordering map[string]int
manifests []tiller.Manifest
}
func getChart(
repoURL string,
chartName string,
chartVersion string) (*chart.Chart, error) {
dl := downloader.ChartDownloader{
HelmHome: settings.Home,
Out: os.Stdout,
Getters: getter.All(settings),
Verify: downloader.VerifyNever,
}
certFile := ""
keyFile := ""
caFile := ""
chartURL, err := repo.FindChartInRepoURL(
repoURL,
chartName,
chartVersion,
certFile, keyFile, caFile, getter.All(settings))
if err != nil {
return nil, fmt.Errorf("find chart: %v", err)
}
glog.V(1).Infof("Downloading %s ...", chartURL)
fname, _, err := dl.DownloadTo(chartURL, chartVersion, settings.Home.Archive())
if err != nil {
return nil, fmt.Errorf("download chart: %v", err)
}
glog.V(1).Infof("Downloaded %s to %s", chartURL, fname)
return chartutil.LoadFile(fname)
}
func capabilities(disc discovery.DiscoveryInterface) (*chartutil.Capabilities, error) {
sv, err := disc.ServerVersion()
if err != nil {
return nil, err
}
vs, err := tiller.GetVersionSet(disc)
if err != nil {
return nil, fmt.Errorf("Could not get apiVersions from Kubernetes: %s", err)
}
return &chartutil.Capabilities{
APIVersions: vs,
KubeVersion: sv,
TillerVersion: version.GetVersionProto(),
}, nil
}
// sortManifests takes a map of filename/YAML contents, splits the file
// by manifest entries, and sorts the entries into hook types.
//
// The resulting hooks struct will be populated with all of the generated hooks.
// Any file that does not declare one of the hook types will be placed in the
// 'generic' bucket.
//
// Files that do not parse into the expected format are simply placed into a map and
// returned.
func sortManifests(files map[string]string, apis chartutil.VersionSet, sort tiller.SortOrder) ([]*release.Hook, []tiller.Manifest, error) {
result := &result{}
for filePath, c := range files {
// Skip partials. We could return these as a separate map, but there doesn't
// seem to be any need for that at this time.
if strings.HasPrefix(path.Base(filePath), "_") {
continue
}
// Skip empty files and log this.
if len(strings.TrimSpace(c)) == 0 {
glog.Infof("info: manifest %q is empty. Skipping.", filePath)
continue
}
manifestFile := &manifestFile{
entries: util.SplitManifests(c),
path: filePath,
apis: apis,
}
if err := manifestFile.sort(result); err != nil {
return result.hooks, result.generic, err
}
}
return result.hooks, sortByKind(result.generic, sort), nil
}
func sortByKind(manifests []tiller.Manifest, ordering tiller.SortOrder) []tiller.Manifest {
ks := newKindSorter(manifests, ordering)
sort.Sort(ks)
return ks.manifests
}
func (k *kindSorter) Len() int { return len(k.manifests) }
func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifests[j], k.manifests[i] }
func (k *kindSorter) Less(i, j int) bool {
a := k.manifests[i]
b := k.manifests[j]
first, aok := k.ordering[a.Head.Kind]
second, bok := k.ordering[b.Head.Kind]
// if same kind (including unknown) sub sort alphanumeric
if first == second {
// if both are unknown and of different kind sort by kind alphabetically
if !aok && !bok && a.Head.Kind != b.Head.Kind {
return a.Head.Kind < b.Head.Kind
}
return a.Name < b.Name
}
// unknown kind is last
if !aok {
return false
}
if !bok {
return true
}
// sort different kinds
return first < second
}
func newKindSorter(m []tiller.Manifest, s tiller.SortOrder) *kindSorter {
o := make(map[string]int, len(s))
for v, k := range s {
o[k] = v
}
return &kindSorter{
manifests: m,
ordering: o,
}
}
func (file *manifestFile) sort(result *result) error {
for _, m := range file.entries {
var entry util.SimpleHead
err := yaml.Unmarshal([]byte(m), &entry)
if err != nil {
e := fmt.Errorf("YAML parse error on %s: %s", file.path, err)
return e
}
if entry.Version != "" && !file.apis.Has(entry.Version) {
return fmt.Errorf("apiVersion %q in %s is not available", entry.Version, file.path)
}
if !hasAnyAnnotation(entry) {
result.generic = append(result.generic, tiller.Manifest{
Name: file.path,
Content: m,
Head: &entry,
})
continue
}
hookTypes, ok := entry.Metadata.Annotations[hooks.HookAnno]
if !ok {
result.generic = append(result.generic, tiller.Manifest{
Name: file.path,
Content: m,
Head: &entry,
})
continue
}
hw := calculateHookWeight(entry)
h := &release.Hook{
Name: entry.Metadata.Name,
Kind: entry.Kind,
Path: file.path,
Manifest: m,
Events: []release.Hook_Event{},
Weight: hw,
DeletePolicies: []release.Hook_DeletePolicy{},
}
isUnknownHook := false
for _, hookType := range strings.Split(hookTypes, ",") {
hookType = strings.ToLower(strings.TrimSpace(hookType))
e, ok := events[hookType]
if !ok {
isUnknownHook = true
break
}
h.Events = append(h.Events, e)
}
if isUnknownHook {
glog.Infof("info: skipping unknown hook: %q", hookTypes)
continue
}
result.hooks = append(result.hooks, h)
isKnownDeletePolices := false
dps, ok := entry.Metadata.Annotations[hooks.HookDeleteAnno]
if ok {
for _, dp := range strings.Split(dps, ",") {
dp = strings.ToLower(strings.TrimSpace(dp))
p, exist := deletePolices[dp]
if exist {
isKnownDeletePolices = true
h.DeletePolicies = append(h.DeletePolicies, p)
}
}
if !isKnownDeletePolices {
glog.Infof("info: skipping unknown hook delete policy: %q", dps)
}
}
}
return nil
}
func hasAnyAnnotation(entry util.SimpleHead) bool {
if entry.Metadata == nil ||
entry.Metadata.Annotations == nil ||
len(entry.Metadata.Annotations) == 0 {
return false
}
return true
}
func calculateHookWeight(entry util.SimpleHead) int32 {
hws := entry.Metadata.Annotations[hooks.HookWeightAnno]
hw, err := strconv.Atoi(hws)
if err != nil {
hw = 0
}
return int32(hw)
}