-
Notifications
You must be signed in to change notification settings - Fork 107
/
deploy.go
415 lines (358 loc) · 12.2 KB
/
deploy.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
Copyright 2023.
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 deploy
package deploy
import (
"archive/tar"
"bufio"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/exp/maps"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/plugins"
ofapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
ofapiv2 "github.com/operator-framework/api/pkg/operators/v2"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
const (
DefaultManifestPath = "/opt/manifests"
)
// DownloadManifests function performs following tasks:
// 1. It takes component URI and only downloads folder specified by component.ContextDir field
// 2. It saves the manifests in the odh-manifests/component-name/ folder
func DownloadManifests(componentName string, manifestConfig components.ManifestsConfig) error {
// Get the component repo from the given url
// e.g https://github.com/example/tarball/master
resp, err := http.Get(manifestConfig.URI)
if err != nil {
return fmt.Errorf("error downloading manifests: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error downloading manifests: %v HTTP status", resp.StatusCode)
}
// Create a new gzip reader
gzipReader, err := gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("error creating gzip reader: %v", err)
}
defer gzipReader.Close()
// Create a new TAR reader
tarReader := tar.NewReader(gzipReader)
// Create manifest directory
mode := os.ModePerm
err = os.MkdirAll(DefaultManifestPath, mode)
if err != nil {
return fmt.Errorf("error creating manifests directory : %v", err)
}
// Extract the contents of the TAR archive to the current directory
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
componentFiles := strings.Split(header.Name, "/")
componentFileName := header.Name
componentManifestPath := componentFiles[0] + "/" + manifestConfig.ContextDir
if strings.Contains(componentFileName, componentManifestPath) {
// Get manifest path relative to repo
// e.g. of repo/a/b/manifests/base --> base/
componentFoldersList := strings.Split(componentFileName, "/")
componentFileRelativePathFound := strings.Join(componentFoldersList[len(strings.Split(componentManifestPath, "/")):], "/")
if header.Typeflag == tar.TypeDir {
err = os.MkdirAll(DefaultManifestPath+"/"+componentName+"/"+componentFileRelativePathFound, mode)
if err != nil {
return fmt.Errorf("error creating directory:%v", err)
}
continue
}
if header.Typeflag == tar.TypeReg {
file, err := os.Create(DefaultManifestPath + "/" + componentName + "/" + componentFileRelativePathFound)
if err != nil {
fmt.Println("Error creating file:", err)
}
_, err = io.Copy(file, tarReader)
if err != nil {
fmt.Println("Error downloading file contents:", err)
}
file.Close()
continue
}
}
}
return err
}
func DeployManifestsFromPath(cli client.Client, owner metav1.Object, manifestPath string, namespace string, componentName string, componentEnabled bool) error {
// Render the Kustomize manifests
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
fs := filesys.MakeFsOnDisk()
fmt.Printf("Updating manifests : %v \n", manifestPath)
// Create resmap
// Use kustomization file under manifestPath or use `default` overlay
var resMap resmap.ResMap
_, err := os.Stat(filepath.Join(manifestPath, "kustomization.yaml"))
if err != nil {
if os.IsNotExist(err) {
resMap, err = k.Run(fs, filepath.Join(manifestPath, "default"))
}
} else {
resMap, err = k.Run(fs, manifestPath)
}
if err != nil {
return fmt.Errorf("error during resmap resources: %w", err)
}
// Apply NamespaceTransformer Plugin
if err := plugins.ApplyNamespacePlugin(namespace, resMap); err != nil {
return err
}
// Apply LabelTransformer Plugin
if err := plugins.ApplyAddLabelsPlugin(componentName, resMap); err != nil {
return err
}
objs, err := getResources(resMap)
if err != nil {
return err
}
// Create / apply / delete resources in the cluster
for _, obj := range objs {
err = manageResource(context.TODO(), cli, obj, owner, namespace, componentName, componentEnabled)
if err != nil {
return err
}
}
return nil
}
func getResources(resMap resmap.ResMap) ([]*unstructured.Unstructured, error) {
var resources []*unstructured.Unstructured
for _, res := range resMap.Resources() {
u := &unstructured.Unstructured{}
err := yaml.Unmarshal([]byte(res.MustYaml()), u)
if err != nil {
return nil, err
}
resources = append(resources, u)
}
return resources, nil
}
func manageResource(ctx context.Context, cli client.Client, obj *unstructured.Unstructured, owner metav1.Object, applicationNamespace, componentName string, enabled bool) error {
resourceName := obj.GetName()
namespace := obj.GetNamespace()
found := obj.DeepCopy()
err := cli.Get(ctx, types.NamespacedName{Name: resourceName, Namespace: namespace}, found)
// Return if resource is of Kind: Namespace and Name: odhApplicationsNamespace
if obj.GetKind() == "Namespace" && obj.GetName() == applicationNamespace {
return nil
}
// Resource exists but component is disabled
if !enabled {
// Return nil for any errors getting the resource, since the component itself is disabled
if err != nil {
return nil
}
// Check for shared resources before deletion
resourceLabels := found.GetLabels()
var componentCounter []string
if resourceLabels != nil {
for i := range resourceLabels {
if strings.Contains(i, "app.opendatahub.io") {
compFound := strings.Split(i, "/")[1]
componentCounter = append(componentCounter, compFound)
}
}
// Shared resource , do not delete. Remove label from disabled component
if len(componentCounter) > 1 || (len(componentCounter) == 1 && componentCounter[0] != componentName) {
found.SetLabels(resourceLabels)
// return, do not delete the shared resource
return nil
}
// Do not delete CRDs, as those can be used by non-odh components
if found.GetKind() == "CustomResourceDefinition" {
return nil
}
}
if obj.GetOwnerReferences() == nil {
return cli.Delete(ctx, found)
}
found.SetOwnerReferences([]metav1.OwnerReference{})
data, err := json.Marshal(found)
if err != nil {
return err
}
err = cli.Patch(ctx, found, client.RawPatch(types.ApplyPatchType, data), client.ForceOwnership, client.FieldOwner(owner.GetName()))
if err != nil {
return err
}
return cli.Delete(ctx, found)
}
// Create the resource if it doesn't exist and component is enabled
if apierrs.IsNotFound(err) {
// Set the owner reference for garbage collection
if err = ctrl.SetControllerReference(owner, metav1.Object(obj), cli.Scheme()); err != nil {
return err
}
return cli.Create(ctx, obj)
}
// Exception: ODHDashboardConfig should not be updated even with upgrades
// TODO: Move this out when we have dashboard-controller
if found.GetKind() == "OdhDashboardConfig" {
// Do nothing, return
return nil
}
// Preserve app.opendatahub.io/<component> labels of previous versions of existing objects
foundLabels := make(map[string]string)
for k, v := range found.GetLabels() {
if strings.Contains(k, "app.opendatahub.io") {
foundLabels[k] = v
}
}
newLabels := obj.GetLabels()
maps.Copy(foundLabels, newLabels)
obj.SetLabels(foundLabels)
// Perform server-side apply
data, err := json.Marshal(obj)
if err != nil {
return err
}
return cli.Patch(ctx, found, client.RawPatch(types.ApplyPatchType, data), client.ForceOwnership, client.FieldOwner(owner.GetName()))
}
/*
User env variable passed from CSV (if it is set) to overwrite values from manifests' params.env file
This is useful for air gapped cluster
priority of image values (from high to low):
- image values set in manifests params.env if manifestsURI is set
- RELATED_IMAGE_* values from CSV
- image values set in manifests params.env if manifestsURI is not set.
parameter isUpdateNamespace is used to set if should update namespace with dsci applicationnamespace.
*/
func ApplyParams(componentPath string, imageParamsMap map[string]string, isUpdateNamespace bool) error {
envFilePath := filepath.Join(componentPath, "params.env")
// Require params.env at the root folder
file, err := os.Open(envFilePath)
if err != nil {
if os.IsNotExist(err) {
// params.env doesn't exist, do not apply any changes
return nil
}
return err
}
backupPath := envFilePath + ".bak"
defer file.Close()
envMap := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
envMap[parts[0]] = parts[1]
}
}
if err := scanner.Err(); err != nil {
return err
}
// Update images with env variables
// e.g "odh-kuberay-operator-controller-image": "RELATED_IMAGE_ODH_KUBERAY_OPERATOR_CONTROLLER_IMAGE",
for i := range envMap {
relatedImageValue := os.Getenv(imageParamsMap[i])
if relatedImageValue != "" {
envMap[i] = relatedImageValue
}
}
// Update namespace variable with applicationNamepsace
if isUpdateNamespace {
envMap["namespace"] = imageParamsMap["namespace"]
}
// Move the existing file to a backup file and create empty file
if err := os.Rename(envFilePath, backupPath); err != nil {
return err
}
file, err = os.Create(envFilePath)
if err != nil {
// If create fails, try to restore the backup file
_ = os.Rename(backupPath, envFilePath)
return err
}
defer file.Close()
// Now, write the map back to the file
writer := bufio.NewWriter(file)
for key, value := range envMap {
if _, fErr := fmt.Fprintf(writer, "%s=%s\n", key, value); fErr != nil {
return fErr
}
}
if err := writer.Flush(); err != nil {
if removeErr := os.Remove(envFilePath); removeErr != nil {
fmt.Printf("Failed to remove file: %v", removeErr)
}
if renameErr := os.Rename(backupPath, envFilePath); renameErr != nil {
fmt.Printf("Failed to restore file from backup: %v", renameErr)
}
fmt.Printf("Failed to write to file: %v", err)
return err
}
// cleanup backup file
if err := os.Remove(backupPath); err != nil {
fmt.Printf("Failed to remove backup file: %v", err)
return err
}
return nil
}
// SubscriptionExists checks if a Subscription for the operator exists in the given namespace.
func SubscriptionExists(cli client.Client, namespace string, name string) (bool, error) {
sub := &ofapiv1alpha1.Subscription{}
if err := cli.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, sub); err != nil {
if apierrs.IsNotFound(err) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
// OperatorExists checks if an Operator with 'operatorPrefix' is installed.
// Return true if found it, false if not.
// TODO: if we need to check exact version of the operator installed, can append vX.Y.Z later.
func OperatorExists(cli client.Client, operatorPrefix string) (bool, error) {
opConditionList := &ofapiv2.OperatorConditionList{}
if err := cli.List(context.TODO(), opConditionList); err != nil {
if !apierrs.IsNotFound(err) { // real error to run List()
return false, err
}
} else {
for _, opCondition := range opConditionList.Items {
if strings.HasPrefix(opCondition.Name, operatorPrefix) {
return true, nil
}
}
}
return false, nil
}
// TODO : Add function to cleanup code created as part of pre install and post install task of a component