/
action.go
324 lines (263 loc) · 8.57 KB
/
action.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
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
// Copyright © 2020 The Tekton Authors.
//
// 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 installer
import (
"bytes"
"errors"
"fmt"
tknVer "github.com/tektoncd/hub/api/pkg/cli/version"
kErr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
decoder "k8s.io/apimachinery/pkg/util/yaml"
)
const (
catalogLabel = "hub.tekton.dev/catalog"
versionLabel = "app.kubernetes.io/version"
)
// Errors
var (
ErrAlreadyExist = errors.New("resource already exists")
ErrNotFound = errors.New("resource not found")
ErrVersionAndCatalogMissing = errors.New("version and catalog label missing")
ErrVersionMissing = errors.New("version label missing")
ErrCatalogMissing = errors.New("catalog label missing")
ErrSameVersion = errors.New("resource already exists with the requested verion")
ErrLowerVersion = errors.New("cannot upgrade resource as requested version is lower than existing")
ErrHigherVersion = errors.New("cannot downgrade resource as requested version is higher than existing")
ErrVersionIncompatible = errors.New("requires compatible version")
ErrWarnVersionNotFound = errors.New("pipeline version unknown")
)
type action string
const (
update action = "update"
upgrade action = "upgrade"
downgrade action = " downgrade"
ResourceMinVersion string = "tekton.dev/pipelines.minVersion"
)
func (i *Installer) TektonPipelinesVersion() {
var err error
i.pipelineVersion, err = tknVer.GetPipelineVersion(i.cs.Dynamic())
if err != nil {
i.pipelineVersion = ""
}
}
func (i *Installer) GetPipelineVersion() string {
return i.pipelineVersion
}
func (i *Installer) checkVersion(resPipMinVersion string) error {
i.TektonPipelinesVersion()
if i.GetPipelineVersion() == "" {
return ErrWarnVersionNotFound
}
if i.GetPipelineVersion() < resPipMinVersion && i.GetPipelineVersion() != "devel" {
return ErrVersionIncompatible
}
return nil
}
// Install a resource
func (i *Installer) Install(data []byte, catalog, namespace string) (*unstructured.Unstructured, []error) {
errors := make([]error, 0)
newRes, err := toUnstructured(data)
if err != nil {
errors = append(errors, err)
return nil, errors
}
newResPipMinVersion := newRes.GetAnnotations()[ResourceMinVersion]
err = i.checkVersion("v" + newResPipMinVersion)
if err != nil {
if err == ErrWarnVersionNotFound {
errors = append(errors, err)
}
if err == ErrVersionIncompatible {
errors = append(errors, err)
return nil, errors
}
}
// Check if resource already exists
existingRes, err := i.get(newRes.GetName(), newRes.GetKind(), namespace, metav1.GetOptions{})
if err != nil {
// If error is notFoundError then create the resource
if kErr.IsNotFound(err) {
resp, err := i.createRes(newRes, catalog, namespace)
if err != nil {
errors = append(errors, err)
}
return resp, errors
}
errors = append(errors, err)
// otherwise return the error
return nil, errors
}
errors = append(errors, ErrAlreadyExist)
return existingRes, errors
}
// LookupInstalled checks if a resource is installed
func (i *Installer) LookupInstalled(name, kind, namespace string) (*unstructured.Unstructured, error) {
var err error
i.existingRes, err = i.get(name, kind, namespace, metav1.GetOptions{})
if err != nil {
if kErr.IsNotFound(err) {
return nil, ErrNotFound
}
return nil, err
}
if err := checkLabels(i.existingRes); err != nil {
return i.existingRes, err
}
return i.existingRes, nil
}
func (i *Installer) ListInstalled(kind, namespace string) ([]unstructured.Unstructured, error) {
i.TektonPipelinesVersion()
listResources, err := i.list(kind, namespace, metav1.ListOptions{})
if err != nil {
return nil, err
}
return listResources.Items, nil
}
// Update will updates an existing resource with the passed resource if exist
func (i *Installer) Update(data []byte, catalog, namespace string) (*unstructured.Unstructured, []error) {
return i.updateByAction(data, catalog, namespace, update)
}
// Upgrade an existing resource to a version upper version by passing it
func (i *Installer) Upgrade(data []byte, catalog, namespace string) (*unstructured.Unstructured, []error) {
return i.updateByAction(data, catalog, namespace, upgrade)
}
// Downgrade an existing resource to a version lower version by passing it
func (i *Installer) Downgrade(data []byte, catalog, namespace string) (*unstructured.Unstructured, []error) {
return i.updateByAction(data, catalog, namespace, downgrade)
}
func (i *Installer) updateByAction(data []byte, catalog, namespace string, action action) (*unstructured.Unstructured, []error) {
var errors []error
newRes, err := toUnstructured(data)
if err != nil {
errors = append(errors, err)
return nil, errors
}
newResPipMinVersion := newRes.GetAnnotations()[ResourceMinVersion]
err = i.checkVersion("v" + newResPipMinVersion)
if err != nil {
if err == ErrWarnVersionNotFound {
errors = append(errors, err)
}
if err == ErrVersionIncompatible {
errors = append(errors, err)
return newRes, errors
}
}
if i.existingRes == nil {
i.existingRes, err = i.get(newRes.GetName(), newRes.GetKind(), namespace, metav1.GetOptions{})
if err != nil {
if kErr.IsNotFound(err) {
errors = append(errors, ErrNotFound)
return nil, errors
}
errors = append(errors, err)
return nil, errors
}
}
existingVersion := i.existingRes.GetLabels()[versionLabel]
newVersion := newRes.GetLabels()[versionLabel]
switch action {
case upgrade:
if err = isUpgradable(existingVersion, newVersion); err != nil {
errors = append(errors, err)
return i.existingRes, errors
}
case downgrade:
if err = isDowngradable(existingVersion, newVersion); err != nil {
errors = append(errors, err)
return i.existingRes, errors
}
}
res, err := i.updateRes(i.existingRes, newRes, catalog, namespace)
if err != nil {
errors = append(errors, err)
}
return res, errors
}
func isUpgradable(existingVersion, newVersion string) error {
if newVersion == existingVersion {
return ErrSameVersion
}
if newVersion < existingVersion {
return ErrLowerVersion
}
return nil
}
func isDowngradable(existingVersion, newVersion string) error {
if newVersion == existingVersion {
return ErrSameVersion
}
if newVersion > existingVersion {
return ErrHigherVersion
}
return nil
}
func checkLabels(res *unstructured.Unstructured) error {
labels := res.GetLabels()
if len(labels) == 0 {
return ErrVersionAndCatalogMissing
}
_, versionOk := labels[versionLabel]
_, catalogOk := labels[catalogLabel]
// If both label exist then return nil
if versionOk == catalogOk && versionOk {
return nil
}
if !catalogOk {
return ErrCatalogMissing
}
if !versionOk {
return ErrVersionMissing
}
return nil
}
func (i *Installer) createRes(obj *unstructured.Unstructured, catalog, namespace string) (*unstructured.Unstructured, error) {
addCatalogLabel(obj, catalog)
res, err := i.create(obj, namespace, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return res, nil
}
func (i *Installer) updateRes(existing, new *unstructured.Unstructured, catalog, namespace string) (*unstructured.Unstructured, error) {
addCatalogLabel(new, catalog)
// replace label, annotation and spec of old resource with new
existing.SetLabels(new.GetLabels())
existing.SetAnnotations(new.GetAnnotations())
existing.Object["spec"] = new.Object["spec"]
res, err := i.update(existing, namespace, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return res, nil
}
func toUnstructured(data []byte) (*unstructured.Unstructured, error) {
r := bytes.NewReader(data)
decoder := decoder.NewYAMLToJSONDecoder(r)
res := &unstructured.Unstructured{}
if err := decoder.Decode(res); err != nil {
return nil, fmt.Errorf("failed to decode resource: %w", err)
}
return res, nil
}
func addCatalogLabel(obj *unstructured.Unstructured, catalog string) {
labels := obj.GetLabels()
if len(labels) == 0 {
labels = make(map[string]string)
}
labels[catalogLabel] = catalog
obj.SetLabels(labels)
}