forked from opendatahub-io/opendatahub-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.go
189 lines (160 loc) · 4.73 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
package deploy
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"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/runtime"
"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/filesys"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap"
"github.com/opendatahub-io/opendatahub-operator/pkg/plugins"
)
const (
DefaultManifestPath = "/opt/odh-manifests"
)
// DownloadManifests function performs following tasks:
// 1. Given remote URI, download manifests, else extract local bundle
// 2. It saves the manifests in the odh-manifests/component-name/ folder
func DownloadManifests(uri string) error {
// Get the component repo from the given url
// e.g https://github.com/example/tarball/master\
var reader io.Reader
if uri != "" {
resp, err := http.Get(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)
}
reader = resp.Body
// Create a new gzip reader
gzipReader, err := gzip.NewReader(reader)
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)
}
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
manifestsPath := strings.Split(header.Name, "/")
// Determine the file or directory path to extract to
target := filepath.Join(DefaultManifestPath, strings.Join(manifestsPath[1:], "/"))
if header.Typeflag == tar.TypeDir {
// Create directories
err = os.MkdirAll(target, os.ModePerm)
if err != nil {
return err
}
} else if header.Typeflag == tar.TypeReg {
// Extract regular files
outputFile, err := os.Create(target)
if err != nil {
return err
}
defer outputFile.Close()
_, err = io.Copy(outputFile, tarReader)
if err != nil {
return err
}
}
}
}
return nil
}
func DeployManifestsFromPath(owner metav1.Object, cli client.Client, manifestPath, namespace string, s *runtime.Scheme) error {
// Render the Kustomize manifests
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
fs := filesys.MakeFsOnDisk()
// Create resmap
// Use kustomization file under manifestPath or use `default` overlay
var resMap resmap.ResMap
_, err := os.Stat(manifestPath + "/kustomization.yaml")
if err != nil {
if os.IsNotExist(err) {
resMap, err = k.Run(fs, manifestPath+"/default")
}
} else {
resMap, err = k.Run(fs, manifestPath)
}
if err != nil {
return fmt.Errorf("error during resmap resources: %v", err)
}
// Apply NamespaceTransformer Plugin
if err := plugins.ApplyNamespacePlugin(namespace, resMap); err != nil {
return err
}
objs, err := getResources(resMap)
if err != nil {
return err
}
// Create or update resources in the cluster
for _, obj := range objs {
err = createOrUpdate(owner, context.TODO(), cli, obj, s)
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 createOrUpdate(owner metav1.Object, ctx context.Context, cli client.Client, obj *unstructured.Unstructured, s *runtime.Scheme) error {
found := obj.DeepCopy()
err := cli.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, found)
if err != nil && errors.IsNotFound(err) {
// Set the owner reference for garbage collection
if err = ctrl.SetControllerReference(owner, metav1.Object(obj), s); err != nil {
return err
}
// Create the resource if it doesn't exist
return cli.Create(ctx, obj)
} else if err != nil {
return err
}
// Update the resource if they already exists
data, err := json.Marshal(obj)
if err != nil {
return err
}
// Perform server-side apply
return cli.Patch(ctx, obj, client.RawPatch(types.ApplyPatchType, data), client.ForceOwnership, client.FieldOwner(owner.GetName()))
}