/
operator.go
343 lines (298 loc) · 10.2 KB
/
operator.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
/*
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 olm
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "sigs.k8s.io/controller-runtime/pkg/client"
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/util/kubernetes"
)
// The following properties can be overridden at build time via ldflags
// DefaultOperatorName is the Camel K operator name in OLM
var DefaultOperatorName = "camel-k-operator"
// DefaultPackage is the Camel K package in OLM
var DefaultPackage = "camel-k"
// DefaultChannel is the distribution channel in Operator Hub
var DefaultChannel = "stable"
// DefaultSource is the name of the operator source where the operator is published
var DefaultSource = "community-operators"
// DefaultSourceNamespace is the namespace of the operator source
var DefaultSourceNamespace = "openshift-marketplace"
// DefaultStartingCSV contains the specific version to install
var DefaultStartingCSV = ""
// DefaultGlobalNamespace indicates a namespace containing an OperatorGroup that enables the operator to watch all namespaces.
// It will be used in global installation mode.
var DefaultGlobalNamespace = "openshift-operators"
// Options contains information about an operator in OLM
type Options struct {
OperatorName string
Package string
Channel string
Source string
SourceNamespace string
StartingCSV string
GlobalNamespace string
}
// IsOperatorInstalled tells if a OLM CSV or a Subscription is already installed in the namespace
func IsOperatorInstalled(ctx context.Context, client client.Client, namespace string, global bool, options Options) (bool, error) {
options = fillDefaults(options)
// CSV is present in current namespace for both local and global installation modes
if csv, err := findCSV(ctx, client, namespace, options); err != nil {
return false, err
} else if csv != nil {
return true, nil
}
// A subscription may indicate an in-progress installation
if sub, err := findSubscription(ctx, client, namespace, global, options); err != nil {
return false, err
} else if sub != nil {
return true, nil
}
return false, nil
}
// HasPermissionToInstall checks if the current user/serviceaccount has the right permissions to install camel k via OLM
func HasPermissionToInstall(ctx context.Context, client client.Client, namespace string, global bool, options Options) (bool, error) {
if ok, err := kubernetes.CheckPermission(ctx, client, operatorsv1alpha1.GroupName, "clusterserviceversions", namespace, options.Package, "list"); err != nil {
return false, err
} else if !ok {
return false, nil
}
targetNamespace := namespace
if global {
targetNamespace = options.GlobalNamespace
}
if ok, err := kubernetes.CheckPermission(ctx, client, operatorsv1alpha1.GroupName, "subscriptions", targetNamespace, options.Package, "create"); err != nil {
return false, err
} else if !ok {
return false, nil
}
if installed, err := IsOperatorInstalled(ctx, client, namespace, global, options); err != nil {
return false, err
} else if installed {
return true, nil
}
if !global {
if ok, err := kubernetes.CheckPermission(ctx, client, operatorsv1.GroupName, "operatorgroups", namespace, options.Package, "list"); err != nil {
return false, err
} else if !ok {
return false, nil
}
group, err := findOperatorGroup(ctx, client, namespace)
if err != nil {
return false, err
}
if group == nil {
if ok, err := kubernetes.CheckPermission(ctx, client, operatorsv1.GroupName, "operatorgroups", namespace, options.Package, "create"); err != nil {
return false, err
} else if !ok {
return false, nil
}
}
}
return true, nil
}
// Install creates a subscription for the OLM package
func Install(ctx context.Context, client client.Client, namespace string, global bool, options Options, collection *kubernetes.Collection,
tolerations []string, nodeSelectors []string, resourcesRequirements []string) (bool, error) {
options = fillDefaults(options)
if installed, err := IsOperatorInstalled(ctx, client, namespace, global, options); err != nil {
return false, err
} else if installed {
// Already installed
return false, nil
}
targetNamespace := namespace
if global {
targetNamespace = options.GlobalNamespace
}
sub := operatorsv1alpha1.Subscription{
ObjectMeta: v1.ObjectMeta{
Name: options.Package,
Namespace: targetNamespace,
},
Spec: &operatorsv1alpha1.SubscriptionSpec{
CatalogSource: options.Source,
CatalogSourceNamespace: options.SourceNamespace,
Package: options.Package,
Channel: options.Channel,
StartingCSV: options.StartingCSV,
InstallPlanApproval: operatorsv1alpha1.ApprovalAutomatic,
},
}
// Additional configuration
err := maybeSetTolerations(&sub, tolerations)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("could not set tolerations"))
}
err = maybeSetNodeSelectors(&sub, nodeSelectors)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("could not set node selectors"))
}
err = maybeSetResourcesRequirements(&sub, resourcesRequirements)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("could not set resources requirements"))
}
if collection != nil {
collection.Add(&sub)
} else if err := client.Create(ctx, &sub); err != nil {
return false, err
}
if !global {
group, err := findOperatorGroup(ctx, client, namespace)
if err != nil {
return false, err
}
if group == nil {
group = &operatorsv1.OperatorGroup{
ObjectMeta: v1.ObjectMeta{
Namespace: namespace,
GenerateName: fmt.Sprintf("%s-", namespace),
},
Spec: operatorsv1.OperatorGroupSpec{
TargetNamespaces: []string{namespace},
},
}
if collection != nil {
collection.Add(group)
} else if err := client.Create(ctx, group); err != nil {
return false, errors.Wrap(err, fmt.Sprintf("namespace %s has no operator group defined and "+
"current user is not able to create it. "+
"Make sure you have the right roles to install operators from OLM", namespace))
}
}
}
return true, nil
}
func maybeSetTolerations(sub *operatorsv1alpha1.Subscription, tolArray []string) error {
if tolArray != nil {
tolerations, err := kubernetes.NewTolerations(tolArray)
if err != nil {
return err
}
sub.Spec.Config.Tolerations = tolerations
}
return nil
}
func maybeSetNodeSelectors(sub *operatorsv1alpha1.Subscription, nsArray []string) error {
if nsArray != nil {
nodeSelectors, err := kubernetes.NewNodeSelectors(nsArray)
if err != nil {
return err
}
sub.Spec.Config.NodeSelector = nodeSelectors
}
return nil
}
func maybeSetResourcesRequirements(sub *operatorsv1alpha1.Subscription, reqArray []string) error {
if reqArray != nil {
resourcesReq, err := kubernetes.NewResourceRequirements(reqArray)
if err != nil {
return err
}
sub.Spec.Config.Resources = resourcesReq
}
return nil
}
// Uninstall removes CSV and subscription from the namespace
func Uninstall(ctx context.Context, client client.Client, namespace string, global bool, options Options) error {
sub, err := findSubscription(ctx, client, namespace, global, options)
if err != nil {
return err
}
if sub != nil {
if err := client.Delete(ctx, sub); err != nil {
return err
}
}
csv, err := findCSV(ctx, client, namespace, options)
if err != nil {
return err
}
if csv != nil {
if err := client.Delete(ctx, csv); err != nil {
return err
}
}
return nil
}
func findSubscription(ctx context.Context, client client.Client, namespace string, global bool, options Options) (*operatorsv1alpha1.Subscription, error) {
subNamespace := namespace
if global {
// In case of global installation, global subscription must be removed
subNamespace = options.GlobalNamespace
}
subscriptionList := operatorsv1alpha1.SubscriptionList{}
if err := client.List(ctx, &subscriptionList, runtime.InNamespace(subNamespace)); err != nil {
return nil, err
}
for _, item := range subscriptionList.Items {
if item.Spec.Package == options.Package {
return &item, nil
}
}
return nil, nil
}
func findCSV(ctx context.Context, client client.Client, namespace string, options Options) (*operatorsv1alpha1.ClusterServiceVersion, error) {
csvList := operatorsv1alpha1.ClusterServiceVersionList{}
if err := client.List(ctx, &csvList, runtime.InNamespace(namespace)); err != nil {
return nil, err
}
for _, item := range csvList.Items {
if strings.HasPrefix(item.Name, options.OperatorName) {
return &item, nil
}
}
return nil, nil
}
func findOperatorGroup(ctx context.Context, client client.Client, namespace string) (*operatorsv1.OperatorGroup, error) {
opGroupList := operatorsv1.OperatorGroupList{}
if err := client.List(ctx, &opGroupList, runtime.InNamespace(namespace)); err != nil {
return nil, err
}
if len(opGroupList.Items) > 0 {
return &opGroupList.Items[0], nil
}
return nil, nil
}
func fillDefaults(o Options) Options {
if o.OperatorName == "" {
o.OperatorName = DefaultOperatorName
}
if o.Package == "" {
o.Package = DefaultPackage
}
if o.Channel == "" {
o.Channel = DefaultChannel
}
if o.Source == "" {
o.Source = DefaultSource
}
if o.SourceNamespace == "" {
o.SourceNamespace = DefaultSourceNamespace
}
if o.StartingCSV == "" {
o.StartingCSV = DefaultStartingCSV
}
if o.GlobalNamespace == "" {
o.GlobalNamespace = DefaultGlobalNamespace
}
return o
}