-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathoperator_controller.go
119 lines (100 loc) · 3.62 KB
/
operator_controller.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
/*
Copyright 2022 The KubeSphere 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 addon
import (
"context"
"fmt"
"strings"
"github.com/go-logr/logr"
"github.com/kubesphere/ks-devops/pkg/api/devops/v1alpha3"
v1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
//+kubebuilder:rbac:groups=devops.kubesphere.io,resources=addonStrategies,verbs=get;delete;create;update
//+kubebuilder:rbac:groups=devops.kubesphere.io,resources=releasercontrollers,verbs=get;delete;create;update
//+kubebuilder:rbac:groups=argoproj.io,resources=argocds,verbs=get;delete;create;update
// OperatorCRDReconciler watches those CRDs which belong to Operator
type OperatorCRDReconciler struct {
client.Client
log logr.Logger
}
// TODO make this to be configurable
var supportedOperators = []string{"ReleaserController", "ArgoCD"}
// Reconcile manages addonStrategy according to the CRDs of operators
func (r *OperatorCRDReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
crd := &apiextensions.CustomResourceDefinition{}
if err = r.Client.Get(ctx, req.NamespacedName, crd); err != nil {
err = client.IgnoreNotFound(err)
return
}
spec := crd.Spec
err = r.operatorsHandle(spec.Names.Kind, spec.Versions[0].Name)
return
}
func (r *OperatorCRDReconciler) operatorsHandle(name string, version string) (err error) {
ctx := context.Background()
r.log.Info(fmt.Sprintf("start to reconcile: %s-%s", name, version))
if !operatorSupport(name) {
r.log.V(8).Info(fmt.Sprintf("not support %s as an operator", name))
return
}
strategyName := getStrategyName(name, string(v1alpha3.AddonInstallStrategySimpleOperator))
strategy := &v1alpha3.AddonStrategy{}
if err = r.Client.Get(ctx, types.NamespacedName{
Name: strategyName,
}, strategy); err != nil {
if apierrors.IsNotFound(err) {
// create addonStrategy
strategy.Name = strategyName
strategy.Spec = v1alpha3.AddStrategySpec{
Type: v1alpha3.AddonInstallStrategySimpleOperator,
SimpleOperator: v1.ObjectReference{
APIVersion: version,
Kind: name,
},
}
err = r.Client.Create(ctx, strategy)
return
}
} else {
strategy.Spec.Available = true
strategy.Spec.SimpleOperator.APIVersion = version
err = r.Client.Update(ctx, strategy)
}
return
}
func operatorSupport(name string) (support bool) {
for _, operatorKind := range supportedOperators {
if name == operatorKind {
support = true
break
}
}
return
}
func getStrategyName(operatorName, kind string) string {
return strings.ToLower(fmt.Sprintf("%s-%s", kind, operatorName))
}
// SetupWithManager set the reconcilers
func (r *OperatorCRDReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.log = ctrl.Log.WithName("OperatorCRDReconciler")
return ctrl.NewControllerManagedBy(mgr).
Named("addon_operator").
For(&apiextensions.CustomResourceDefinition{}).
Complete(r)
}