-
Notifications
You must be signed in to change notification settings - Fork 88
/
checluster_controller.go
301 lines (267 loc) · 11 KB
/
checluster_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
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
//
// Copyright (c) 2019-2023 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package che
import (
"context"
imagepuller "github.com/eclipse-che/che-operator/pkg/deploy/image-puller"
editorsdefinitions "github.com/eclipse-che/che-operator/pkg/deploy/editors-definitions"
"github.com/eclipse-che/che-operator/pkg/common/test"
containerbuild "github.com/eclipse-che/che-operator/pkg/deploy/container-build"
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/eclipse-che/che-operator/pkg/common/utils"
"github.com/eclipse-che/che-operator/pkg/deploy"
"github.com/eclipse-che/che-operator/pkg/deploy/consolelink"
"github.com/eclipse-che/che-operator/pkg/deploy/dashboard"
devworkspaceconfig "github.com/eclipse-che/che-operator/pkg/deploy/dev-workspace-config"
"github.com/eclipse-che/che-operator/pkg/deploy/devfileregistry"
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
identityprovider "github.com/eclipse-che/che-operator/pkg/deploy/identity-provider"
"github.com/eclipse-che/che-operator/pkg/deploy/migration"
"github.com/eclipse-che/che-operator/pkg/deploy/pluginregistry"
"github.com/eclipse-che/che-operator/pkg/deploy/postgres"
"github.com/eclipse-che/che-operator/pkg/deploy/rbac"
"github.com/eclipse-che/che-operator/pkg/deploy/server"
"github.com/eclipse-che/che-operator/pkg/deploy/tls"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
chev2 "github.com/eclipse-che/che-operator/api/v2"
networking "k8s.io/api/networking/v1"
)
// CheClusterReconciler reconciles a CheCluster object
type CheClusterReconciler struct {
Log logr.Logger
Scheme *k8sruntime.Scheme
// 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
// This client, is a simple client
// that reads objects without using the cache,
// to simply read objects thta we don't intend
// to further watch
nonCachedClient client.Client
// A discovery client to check for the existence of certain APIs registered
// in the API Server
discoveryClient discovery.DiscoveryInterface
reconcileManager *deploy.ReconcileManager
// the namespace to which to limit the reconciliation. If empty, all namespaces are considered
namespace string
}
// NewReconciler returns a new CheClusterReconciler
func NewReconciler(
k8sclient client.Client,
noncachedClient client.Client,
discoveryClient discovery.DiscoveryInterface,
scheme *k8sruntime.Scheme,
namespace string) *CheClusterReconciler {
reconcileManager := deploy.NewReconcileManager()
// order does matter
if !test.IsTestMode() {
reconcileManager.RegisterReconciler(migration.NewMigrator())
reconcileManager.RegisterReconciler(migration.NewCheClusterDefaultsCleaner())
reconcileManager.RegisterReconciler(NewCheClusterValidator())
}
reconcileManager.RegisterReconciler(tls.NewCertificatesReconciler())
reconcileManager.RegisterReconciler(tls.NewTlsSecretReconciler())
reconcileManager.RegisterReconciler(devworkspaceconfig.NewDevWorkspaceConfigReconciler())
reconcileManager.RegisterReconciler(rbac.NewGatewayPermissionsReconciler())
// we have to expose che endpoint independently of syncing other server
// resources since che host is used for dashboard deployment and che config map
reconcileManager.RegisterReconciler(server.NewCheHostReconciler())
reconcileManager.RegisterReconciler(postgres.NewPostgresReconciler())
if infrastructure.IsOpenShift() {
reconcileManager.RegisterReconciler(identityprovider.NewIdentityProviderReconciler())
}
reconcileManager.RegisterReconciler(devfileregistry.NewDevfileRegistryReconciler())
reconcileManager.RegisterReconciler(pluginregistry.NewPluginRegistryReconciler())
reconcileManager.RegisterReconciler(editorsdefinitions.NewEditorsDefinitionsReconciler())
reconcileManager.RegisterReconciler(dashboard.NewDashboardReconciler())
reconcileManager.RegisterReconciler(gateway.NewGatewayReconciler())
reconcileManager.RegisterReconciler(server.NewCheServerReconciler())
reconcileManager.RegisterReconciler(imagepuller.NewImagePuller())
if infrastructure.IsOpenShift() {
reconcileManager.RegisterReconciler(containerbuild.NewContainerBuildReconciler())
reconcileManager.RegisterReconciler(consolelink.NewConsoleLinkReconciler())
}
return &CheClusterReconciler{
Scheme: scheme,
Log: ctrl.Log.WithName("controllers").WithName("CheCluster"),
client: k8sclient,
nonCachedClient: noncachedClient,
discoveryClient: discoveryClient,
namespace: namespace,
reconcileManager: reconcileManager,
}
}
// SetupWithManager sets up the controller with the Manager.
func (r *CheClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
onAllExceptGenericEventsPredicate := predicate.Funcs{
UpdateFunc: func(evt event.UpdateEvent) bool {
return true
},
CreateFunc: func(evt event.CreateEvent) bool {
return true
},
DeleteFunc: func(evt event.DeleteEvent) bool {
return true
},
GenericFunc: func(evt event.GenericEvent) bool {
return false
},
}
var toTrustedBundleConfigMapRequestMapper handler.MapFunc = func(obj client.Object) []ctrl.Request {
isTrusted, reconcileRequest := IsTrustedBundleConfigMap(r.client, r.namespace, obj)
if isTrusted {
return []ctrl.Request{reconcileRequest}
}
return []ctrl.Request{}
}
var toEclipseCheRelatedObjRequestMapper handler.MapFunc = func(obj client.Object) []ctrl.Request {
isEclipseCheRelatedObj, reconcileRequest := IsEclipseCheRelatedObj(r.client, r.namespace, obj)
if isEclipseCheRelatedObj {
return []ctrl.Request{reconcileRequest}
}
return []ctrl.Request{}
}
controllerBuilder := ctrl.NewControllerManagedBy(mgr).
// Watch for changes to primary resource CheCluster
Watches(&source.Kind{Type: &chev2.CheCluster{}}, &handler.EnqueueRequestForObject{}).
// Watch for changes to secondary resources and requeue the owner CheCluster
Watches(&source.Kind{Type: &corev1.Service{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &corev1.ConfigMap{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &rbacv1.Role{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &rbacv1.RoleBinding{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &corev1.ServiceAccount{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
}).
Watches(&source.Kind{Type: &corev1.ConfigMap{}},
handler.EnqueueRequestsFromMapFunc(toTrustedBundleConfigMapRequestMapper),
builder.WithPredicates(onAllExceptGenericEventsPredicate),
).
Watches(&source.Kind{Type: &corev1.Secret{}},
handler.EnqueueRequestsFromMapFunc(toEclipseCheRelatedObjRequestMapper),
builder.WithPredicates(onAllExceptGenericEventsPredicate),
).
Watches(&source.Kind{Type: &corev1.ConfigMap{}},
handler.EnqueueRequestsFromMapFunc(toEclipseCheRelatedObjRequestMapper),
builder.WithPredicates(onAllExceptGenericEventsPredicate),
)
if infrastructure.IsOpenShift() {
controllerBuilder = controllerBuilder.Watches(&source.Kind{Type: &routev1.Route{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
})
} else {
controllerBuilder = controllerBuilder.Watches(&source.Kind{Type: &networking.Ingress{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &chev2.CheCluster{},
})
}
if r.namespace != "" {
controllerBuilder = controllerBuilder.WithEventFilter(utils.InNamespaceEventFilter(r.namespace))
}
return controllerBuilder.
For(&chev2.CheCluster{}).
Complete(r)
}
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.9.5/pkg/reconcile
func (r *CheClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("checluster", req.NamespacedName)
clusterAPI := chetypes.ClusterAPI{
Client: r.client,
NonCachingClient: r.nonCachedClient,
DiscoveryClient: r.discoveryClient,
Scheme: r.Scheme,
}
// Fetch the CheCluster instance
checluster, err := deploy.FindCheClusterCRInNamespace(r.client, req.NamespacedName.Namespace)
if checluster == nil {
r.Log.Info("CheCluster Custom Resource not found.")
return ctrl.Result{}, nil
} else if err != nil {
// Error reading the object - requeue the request.
return ctrl.Result{}, err
}
deployContext := &chetypes.DeployContext{
ClusterAPI: clusterAPI,
CheCluster: checluster,
}
// Read proxy configuration
proxy, err := GetProxyConfiguration(deployContext)
if err != nil {
r.Log.Error(err, "Error on reading proxy configuration")
return ctrl.Result{}, err
}
deployContext.Proxy = proxy
// Detect whether self-signed certificate is used
isSelfSignedCertificate, err := tls.IsSelfSignedCertificateUsed(deployContext)
if err != nil {
r.Log.Error(err, "Failed to detect if self-signed certificate used.")
return ctrl.Result{}, err
}
deployContext.IsSelfSignedCertificate = isSelfSignedCertificate
if deployContext.CheCluster.ObjectMeta.DeletionTimestamp.IsZero() {
result, done, err := r.reconcileManager.ReconcileAll(deployContext)
if !done {
return result, err
} else {
if err := deploy.SetStatusDetails(deployContext, "", ""); err != nil {
return ctrl.Result{}, err
}
logrus.Info("Successfully reconciled.")
return ctrl.Result{}, nil
}
} else {
deployContext.CheCluster.Status.ChePhase = chev2.ClusterPhasePendingDeletion
_ = deploy.UpdateCheCRStatus(deployContext, "ChePhase", chev2.ClusterPhasePendingDeletion)
done := r.reconcileManager.FinalizeAll(deployContext)
return ctrl.Result{Requeue: !done}, nil
}
}