-
Notifications
You must be signed in to change notification settings - Fork 345
/
kamelets.go
219 lines (193 loc) · 6.41 KB
/
kamelets.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 install
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"golang.org/x/sync/errgroup"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/client"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/patch"
)
const (
kameletDirEnv = "KAMELET_CATALOG_DIR"
defaultKameletDir = "/kamelets/"
)
var (
log = logf.Log
hasServerSideApply atomic.Value
tryServerSideApply sync.Once
)
// KameletCatalog installs the bundled Kamelets into the specified namespace.
func KameletCatalog(ctx context.Context, c client.Client, namespace string) error {
kameletDir := os.Getenv(kameletDirEnv)
if kameletDir == "" {
kameletDir = defaultKameletDir
}
d, err := os.Stat(kameletDir)
switch {
case err != nil && os.IsNotExist(err):
return nil
case err != nil:
return err
case !d.IsDir():
return fmt.Errorf("kamelet directory %q is a file", kameletDir)
}
g, gCtx := errgroup.WithContext(ctx)
err = filepath.WalkDir(kameletDir, func(p string, f fs.DirEntry, err error) error {
if err != nil {
return err
}
if f.IsDir() && f.Name() != d.Name() {
return fs.SkipDir
}
if !(strings.HasSuffix(f.Name(), ".yaml") || strings.HasSuffix(f.Name(), ".yml")) {
return nil
}
// We may want to throttle the creation of Go routines if the number of bundled Kamelets increases.
g.Go(func() error {
kamelet, err := loadKamelet(filepath.Join(kameletDir, f.Name()), namespace)
if err != nil {
return err
}
once := false
tryServerSideApply.Do(func() {
once = true
if err = serverSideApply(gCtx, c, kamelet); err != nil {
if isIncompatibleServerError(err) {
log.Info("Fallback to client-side apply for installing bundled Kamelets")
hasServerSideApply.Store(false)
err = nil
} else {
// Unexpected error occurred
err = fmt.Errorf("unexpected error occurred whilst validating kamelet: %w", err)
log.Error(err, "Error occurred whilst loading kamelets")
}
} else {
hasServerSideApply.Store(true)
}
})
if err != nil {
return err
}
v := hasServerSideApply.Load()
var bundleKamError error
if vb, ok := v.(bool); ok && vb {
if !once {
bundleKamError = serverSideApply(gCtx, c, kamelet)
}
} else {
bundleKamError = clientSideApply(gCtx, c, kamelet)
}
// We only log the error. If we returned the error, the creation of the ITP would have stopped
if bundleKamError != nil {
log.Error(bundleKamError, "Error occurred whilst applying bundled kamelet")
}
return nil
})
return nil
})
if err != nil {
return err
}
return g.Wait()
}
func serverSideApply(ctx context.Context, c client.Client, resource runtime.Object) error {
target, err := patch.ApplyPatch(resource)
if err != nil {
return err
}
return c.Patch(ctx, target, ctrl.Apply, ctrl.ForceOwnership, ctrl.FieldOwner("camel-k-operator"))
}
func clientSideApply(ctx context.Context, c client.Client, resource ctrl.Object) error {
if err := c.Create(ctx, resource); err == nil {
return nil
} else if !k8serrors.IsAlreadyExists(err) {
return fmt.Errorf("error during create resource: %s/%s: %w", resource.GetNamespace(), resource.GetName(), err)
}
// Directly use the serialized resource as JSON merge patch since it's prescriptive
p, err := json.Marshal(resource)
if err != nil {
return err
}
return c.Patch(ctx, resource, ctrl.RawPatch(types.MergePatchType, p))
}
func isIncompatibleServerError(err error) bool {
// First simpler check for older servers (i.e. OpenShift 3.11)
if strings.Contains(err.Error(), "415: Unsupported Media Type") {
return true
}
// 415: Unsupported media type means we're talking to a server which doesn't
// support server-side apply.
var serr *k8serrors.StatusError
if errors.As(err, &serr) {
return serr.Status().Code == http.StatusUnsupportedMediaType
}
// Non-StatusError means the error isn't because the server is incompatible.
return false
}
func loadKamelet(path string, namespace string) (ctrl.Object, error) {
content, err := util.ReadFile(path)
if err != nil {
return nil, err
}
kamelet, err := kubernetes.LoadUnstructuredFromYaml(string(content))
if err != nil {
return nil, err
}
gvk := kamelet.GetObjectKind().GroupVersionKind()
if gvk.Group != v1.SchemeGroupVersion.Group || gvk.Kind != "Kamelet" {
return nil, fmt.Errorf("file %q does not define a Kamelet", path)
}
kamelet.SetNamespace(namespace)
annotations := kamelet.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[kamelVersionAnnotation] = defaults.Version
kamelet.SetAnnotations(annotations)
labels := kamelet.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[v1.KameletBundledLabel] = "true"
labels[v1.KameletReadOnlyLabel] = "true"
kamelet.SetLabels(labels)
return kamelet, nil
}
// KameletViewerRole installs the role that allows any user ro access kamelets in the global namespace.
func KameletViewerRole(ctx context.Context, c client.Client, namespace string) error {
if err := Resource(ctx, c, namespace, true, IdentityResourceCustomizer, "/resources/viewer/user-global-kamelet-viewer-role.yaml"); err != nil {
return err
}
return Resource(ctx, c, namespace, true, IdentityResourceCustomizer, "/resources/viewer/user-global-kamelet-viewer-role-binding.yaml")
}