-
Notifications
You must be signed in to change notification settings - Fork 254
/
controller.go
221 lines (176 loc) · 6.64 KB
/
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
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
/*
Copyright 2018 The CDI 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 controller
import (
"context"
"fmt"
"time"
"kubevirt.io/containerized-data-importer/pkg/operator"
cdicerts "kubevirt.io/containerized-data-importer/pkg/operator/resources/cert"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk/callbacks"
sdkr "kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk/reconciler"
"github.com/kelseyhightower/envconfig"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
cdicluster "kubevirt.io/containerized-data-importer/pkg/operator/resources/cluster"
cdinamespaced "kubevirt.io/containerized-data-importer/pkg/operator/resources/namespaced"
"kubevirt.io/containerized-data-importer/pkg/util"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const (
finalizerName = "operator.cdi.kubevirt.io"
createVersionLabel = "operator.cdi.kubevirt.io/createVersion"
updateVersionLabel = "operator.cdi.kubevirt.io/updateVersion"
lastAppliedConfigAnnotation = "operator.cdi.kubevirt.io/lastAppliedConfiguration"
certPollInterval = 1 * time.Minute
createResourceFailed = "CreateResourceFailed"
createResourceSuccess = "CreateResourceSuccess"
deleteResourceFailed = "DeleteResourceFailed"
deleteResourceSuccess = "DeleteResourceSuccess"
)
var log = logf.Log.WithName("cdi-operator")
// Add creates a new CDI Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
r, err := newReconciler(mgr)
if err != nil {
return err
}
return r.add(mgr)
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) (*ReconcileCDI, error) {
var namespacedArgs cdinamespaced.FactoryArgs
namespace := util.GetNamespace()
restClient := mgr.GetClient()
clusterArgs := &cdicluster.FactoryArgs{
Namespace: namespace,
Client: restClient,
Logger: log,
}
err := envconfig.Process("", &namespacedArgs)
if err != nil {
return nil, err
}
namespacedArgs.Namespace = namespace
log.Info("", "VARS", fmt.Sprintf("%+v", namespacedArgs))
scheme := mgr.GetScheme()
uncachedClient, err := client.New(mgr.GetConfig(), client.Options{
Scheme: scheme,
Mapper: mgr.GetRESTMapper(),
})
if err != nil {
return nil, err
}
recorder := mgr.GetEventRecorderFor("operator-controller")
r := &ReconcileCDI{
client: restClient,
uncachedClient: uncachedClient,
scheme: scheme,
recorder: recorder,
namespace: namespace,
clusterArgs: clusterArgs,
namespacedArgs: &namespacedArgs,
}
callbackDispatcher := callbacks.NewCallbackDispatcher(log, restClient, uncachedClient, scheme, namespace)
r.reconciler = sdkr.NewReconciler(r, log, restClient, callbackDispatcher, scheme, createVersionLabel, updateVersionLabel, lastAppliedConfigAnnotation, certPollInterval, finalizerName, recorder)
r.registerHooks()
addReconcileCallbacks(r)
return r, nil
}
var _ reconcile.Reconciler = &ReconcileCDI{}
// ReconcileCDI reconciles a CDI object
type ReconcileCDI struct {
// This Client, initialized using mgr.client() above, is a split Client
// that reads objects from the cache and writes to the apiserver
client client.Client
// use this for getting any resources not in the install namespace or cluster scope
uncachedClient client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
controller controller.Controller
namespace string
clusterArgs *cdicluster.FactoryArgs
namespacedArgs *cdinamespaced.FactoryArgs
certManager CertManager
reconciler *sdkr.Reconciler
}
// SetController sets the controller dependency
func (r *ReconcileCDI) SetController(controller controller.Controller) {
r.controller = controller
r.reconciler.WithController(controller)
}
// Reconcile reads that state of the cluster for a CDI object and makes changes based on the state read
// and what is in the CDI.Spec
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileCDI) Reconcile(request reconcile.Request) (reconcile.Result, error) {
operatorVersion := r.namespacedArgs.OperatorVersion
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling CDI")
return r.reconciler.Reconcile(request, operatorVersion, reqLogger)
}
func (r *ReconcileCDI) add(mgr manager.Manager) error {
// Create a new controller
c, err := controller.New("cdi-operator-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
r.SetController(c)
if err = r.reconciler.WatchCR(); err != nil {
return err
}
cm, err := NewCertManager(mgr, r.namespace)
if err != nil {
return err
}
r.certManager = cm
return nil
}
func (r *ReconcileCDI) getCertificateDefinitions() []cdicerts.CertificateDefinition {
return cdicerts.CreateCertificateDefinitions(&cdicerts.FactoryArgs{Namespace: r.namespace})
}
func (r *ReconcileCDI) getConfigMap() (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
key := client.ObjectKey{Name: operator.ConfigMapName, Namespace: r.namespace}
if err := r.client.Get(context.TODO(), key, cm); err != nil {
if errors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return cm, nil
}
// createOperatorConfig creates operator config map
func (r *ReconcileCDI) createOperatorConfig(cr controllerutil.Object) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: operator.ConfigMapName,
Namespace: r.namespace,
Labels: map[string]string{"operator.cdi.kubevirt.io": ""},
},
}
if err := controllerutil.SetControllerReference(cr, cm, r.scheme); err != nil {
return err
}
return r.client.Create(context.TODO(), cm)
}