-
Notifications
You must be signed in to change notification settings - Fork 299
/
resources.go
788 lines (698 loc) · 32.5 KB
/
resources.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package resources
import (
"context"
"crypto/md5"
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
configv1 "github.com/openshift/api/config/v1"
imageregistryv1 "github.com/openshift/api/imageregistry/v1"
hyperv1 "github.com/openshift/hypershift/api/v1alpha1"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/crd"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/ingress"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/konnectivity"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/kubeadminpassword"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/manifests"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/monitoring"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/namespaces"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/oapi"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/oauth"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/olm"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/rbac"
"github.com/openshift/hypershift/hosted-cluster-config-operator/controllers/resources/registry"
"github.com/openshift/hypershift/hosted-cluster-config-operator/operator"
"github.com/openshift/hypershift/support/globalconfig"
"github.com/openshift/hypershift/support/releaseinfo"
"github.com/openshift/hypershift/support/upsert"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
)
const (
ControllerName = "resources"
SecretHashAnnotation = "hypershift.openshift.io/kubeadmin-secret-hash"
)
type reconciler struct {
client client.Client
upsert.CreateOrUpdateProvider
platformType hyperv1.PlatformType
clusterSignerCA string
cpClient client.Client
hcpName string
hcpNamespace string
releaseProvider releaseinfo.Provider
konnectivityServerAddress string
konnectivityServerPort int32
oauthAddress string
oauthPort int32
versions map[string]string
}
// eventHandler is the handler used throughout. As this controller reconciles all kind of different resources
// it uses an empty request but always reconciles everything.
func eventHandler() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(
func(client.Object) []reconcile.Request {
return []reconcile.Request{{}}
})
}
func Setup(opts *operator.HostedClusterConfigOperatorConfig) error {
if err := imageregistryv1.AddToScheme(opts.Manager.GetScheme()); err != nil {
return fmt.Errorf("failed to add to scheme: %w", err)
}
c, err := controller.New(ControllerName, opts.Manager, controller.Options{Reconciler: &reconciler{
client: opts.Manager.GetClient(),
CreateOrUpdateProvider: opts.TargetCreateOrUpdateProvider,
platformType: opts.PlatformType,
clusterSignerCA: opts.ClusterSignerCA,
cpClient: opts.CPCluster.GetClient(),
hcpName: opts.HCPName,
hcpNamespace: opts.Namespace,
releaseProvider: opts.ReleaseProvider,
konnectivityServerAddress: opts.KonnectivityAddress,
konnectivityServerPort: opts.KonnectivityPort,
oauthAddress: opts.OAuthAddress,
oauthPort: opts.OAuthPort,
versions: opts.Versions,
}})
if err != nil {
return fmt.Errorf("failed to construct controller: %w", err)
}
resourcesToWatch := []client.Object{
&imageregistryv1.Config{},
&corev1.ConfigMap{},
&corev1.Namespace{},
&corev1.Secret{},
&corev1.Service{},
&corev1.Endpoints{},
&rbacv1.ClusterRole{},
&rbacv1.ClusterRoleBinding{},
&configv1.Infrastructure{},
&configv1.DNS{},
&configv1.Ingress{},
&configv1.Network{},
&configv1.Proxy{},
&appsv1.DaemonSet{},
&configv1.ClusterOperator{},
&configv1.ClusterVersion{},
&apiregistrationv1.APIService{},
}
for _, r := range resourcesToWatch {
if err := c.Watch(&source.Kind{Type: r}, eventHandler()); err != nil {
return fmt.Errorf("failed to watch %T: %w", r, err)
}
}
if err := c.Watch(source.NewKindWithCache(&hyperv1.HostedControlPlane{}, opts.CPCluster.GetCache()), eventHandler()); err != nil {
return fmt.Errorf("failed to watch HostedControlPlane: %w", err)
}
return nil
}
func (r *reconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{}, r.reconcile(ctx)
}
func (r *reconciler) reconcile(ctx context.Context) error {
log := ctrl.LoggerFrom(ctx)
log.Info("Reconciling")
hcp := manifests.HostedControlPlane(r.hcpNamespace, r.hcpName)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(hcp), hcp); err != nil {
return fmt.Errorf("failed to get hosted control plane %s/%s: %w", r.hcpNamespace, r.hcpName, err)
}
globalConfig, err := globalconfig.ParseGlobalConfig(ctx, hcp.Spec.Configuration)
if err != nil {
return fmt.Errorf("failed to parse global config for control plane %s/%s: %w", r.hcpNamespace, r.hcpName, err)
}
pullSecret := manifests.PullSecret(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(pullSecret), pullSecret); err != nil {
return fmt.Errorf("failed to get pull secret: %w", err)
}
releaseImage, err := r.releaseProvider.Lookup(ctx, hcp.Spec.ReleaseImage, pullSecret.Data[corev1.DockerConfigJsonKey])
if err != nil {
return fmt.Errorf("failed to get lookup release image %s: %w", hcp.Spec.ReleaseImage, err)
}
var errs []error
log.Info("reconciling guest cluster crds")
if err := r.reconcileCRDs(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile crds: %w", err))
}
log.Info("reconciling clusterversion")
if err := r.reconcileClusterVersion(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile clusterversion: %w", err))
}
log.Info("reconciling clusterOperators")
if err := r.reconcileClusterOperators(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile clusterOperators: %w", err))
}
log.Info("reconciling guest cluster global configuration")
if err := r.reconcileConfig(ctx, hcp, globalConfig); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile global configuration: %w", err))
}
log.Info("reconciling guest cluster namespaces")
if err := r.reconcileNamespaces(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile namespaces: %w", err))
}
log.Info("reconciling guest cluster rbac")
if err := r.reconcileRBAC(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile rbac: %w", err))
}
// IBMCloud platform should not be constantly reconciled as we allow customers to change initial deployment.
// Initial deployment handled by managed service wrapping hypershift
if r.platformType != hyperv1.IBMCloudPlatform {
log.Info("reconciling registry config")
registryConfig := manifests.Registry()
if _, err := r.CreateOrUpdate(ctx, r.client, registryConfig, func() error {
registry.ReconcileRegistryConfig(registryConfig, r.platformType == hyperv1.NonePlatform)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile imageregistry config: %w", err))
}
}
log.Info("reconciling ingress controller")
if err := r.reconcileIngressController(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile ingress controller: %w", err))
}
log.Info("reconciling kube control plane signer secret")
kubeControlPlaneSignerSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-kube-apiserver-operator",
Name: "kube-control-plane-signer",
},
}
if _, err := r.CreateOrUpdate(ctx, r.client, kubeControlPlaneSignerSecret, func() error {
kubeControlPlaneSignerSecret.Data = map[string][]byte{corev1.TLSCertKey: []byte(r.clusterSignerCA)}
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile the %s Secret: %w", client.ObjectKeyFromObject(kubeControlPlaneSignerSecret), err))
}
log.Info("reconciling kubelet serving CA configmap")
kubeletServingCAConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-config-managed",
Name: "kubelet-serving-ca",
},
}
if _, err := r.CreateOrUpdate(ctx, r.client, kubeletServingCAConfigMap, func() error {
kubeletServingCAConfigMap.Data = map[string]string{"ca-bundle.crt": r.clusterSignerCA}
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile the %s ConfigMap: %w", client.ObjectKeyFromObject(kubeletServingCAConfigMap), err))
}
log.Info("reconciling konnectivity agent")
if err := r.reconcileKonnectivityAgent(ctx, hcp, releaseImage); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile konnectivity agent: %w", err))
}
log.Info("reconciling openshift apiserver apiservices")
if err := r.reconcileOpenshiftAPIServerAPIServices(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift apiserver service: %w", err))
}
log.Info("reconciling openshift apiserver service")
openshiftAPIServerService := manifests.OpenShiftAPIServerClusterService()
if _, err := r.CreateOrUpdate(ctx, r.client, openshiftAPIServerService, func() error {
oapi.ReconcileClusterService(openshiftAPIServerService)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift apiserver service: %w", err))
}
log.Info("reconciling openshift apiserver endpoints")
if err := r.reconcileOpenshiftAPIServerEndpoints(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift apiserver endpoints: %w", err))
}
log.Info("reconciling openshift oauth apiserver apiservices")
if err := r.reconcileOpenshiftOAuthAPIServerAPIServices(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift apiserver service: %w", err))
}
log.Info("reconciling openshift oauth apiserver service")
openshiftOAuthAPIServerService := manifests.OpenShiftOAuthAPIServerClusterService()
if _, err := r.CreateOrUpdate(ctx, r.client, openshiftOAuthAPIServerService, func() error {
oapi.ReconcileClusterService(openshiftOAuthAPIServerService)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift oauth apiserver service: %w", err))
}
log.Info("reconciling openshift oauth apiserver endpoints")
if err := r.reconcileOpenshiftOAuthAPIServerEndpoints(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift apiserver endpoints: %w", err))
}
log.Info("reconciling kube apiserver service monitor")
kasServiceMonitor := manifests.KubeAPIServerServiceMonitor()
if _, err := r.CreateOrUpdate(ctx, r.client, kasServiceMonitor, func() error {
return monitoring.ReconcileKubeAPIServerServiceMonitor(kasServiceMonitor)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile the kube apiserver service monitor: %w", err))
}
log.Info("reconciling kubeadmin password hash secret")
if err := r.reconcileKubeadminPasswordHashSecret(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile kubeadmin password hash secret: %w", err))
}
log.Info("reconciling monitoring configuration")
monitoringConfig := manifests.MonitoringConfig()
if _, err := r.CreateOrUpdate(ctx, r.client, monitoringConfig, func() error {
return monitoring.ReconcileMonitoringConfig(monitoringConfig)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile monitoring config: %w", err))
}
log.Info("reconciling pull secret")
for _, ns := range manifests.PullSecretTargetNamespaces() {
secret := manifests.PullSecret(ns)
if _, err := r.CreateOrUpdate(ctx, r.client, secret, func() error {
secret.Data = pullSecret.Data
secret.Type = pullSecret.Type
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile pull secret at namespace %s: %w", ns, err))
}
}
log.Info("reconciling oauth serving cert ca bundle")
if err := r.reconcileOAuthServingCertCABundle(ctx, hcp); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile oauth serving cert CA bundle: %w", err))
}
log.Info("reconciling oauth browser client")
oauthBrowserClient := manifests.OAuthServerBrowserClient()
if _, err := r.CreateOrUpdate(ctx, r.client, oauthBrowserClient, func() error {
return oauth.ReconcileBrowserClient(oauthBrowserClient, r.oauthAddress, r.oauthPort)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile oauth browser client: %w", err))
}
log.Info("reconciling oauth challenging client")
oauthChallengingClient := manifests.OAuthServerChallengingClient()
if _, err := r.CreateOrUpdate(ctx, r.client, oauthChallengingClient, func() error {
return oauth.ReconcileChallengingClient(oauthChallengingClient, r.oauthAddress, r.oauthPort)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile oauth challenging client: %w", err))
}
log.Info("reconciling cloud credential secrets")
errs = append(errs, r.reconcileCloudCredentialSecrets(ctx, hcp)...)
log.Info("reconciling openshift controller manager service ca bundle")
ocmServiceCA := manifests.OpenShiftControllerManagerServiceCA()
if _, err := r.CreateOrUpdate(ctx, r.client, ocmServiceCA, func() error {
if ocmServiceCA.Annotations == nil {
ocmServiceCA.Annotations = map[string]string{}
}
ocmServiceCA.Annotations["service.beta.openshift.io/inject-cabundle"] = "true"
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift controller manager service ca bundle: %w", err))
}
log.Info("reconciling olm resources")
errs = append(errs, r.reconcileOLM(ctx, hcp)...)
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileCRDs(ctx context.Context) error {
var errs []error
requestCount := manifests.RequestCountCRD()
if _, err := r.CreateOrUpdate(ctx, r.client, requestCount, func() error {
return crd.ReconcileRequestCountCRD(requestCount)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile request count crd: %w", err))
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileConfig(ctx context.Context, hcp *hyperv1.HostedControlPlane, globalConfig globalconfig.GlobalConfig) error {
var errs []error
apiServerAddress := hcp.Status.ControlPlaneEndpoint.Host
if len(apiServerAddress) == 0 {
return fmt.Errorf("hosted control plane does not have an APIServer endpoint address")
}
// Infrastructure is first reconciled for its spec
infra := globalconfig.InfrastructureConfig()
var currentInfra *configv1.Infrastructure
if _, err := r.CreateOrUpdate(ctx, r.client, infra, func() error {
currentInfra = infra.DeepCopy()
globalconfig.ReconcileInfrastructure(infra, hcp)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile infrastructure config spec: %w", err))
} else {
// It is reconciled a second time to update its status
globalconfig.ReconcileInfrastructure(infra, hcp)
if !equality.Semantic.DeepEqual(infra.Status, currentInfra.Status) {
if err := r.client.Status().Update(ctx, infra); err != nil {
errs = append(errs, fmt.Errorf("failed to update infrastructure status: %w", err))
}
}
}
dns := globalconfig.DNSConfig()
if _, err := r.CreateOrUpdate(ctx, r.client, dns, func() error {
globalconfig.ReconcileDNSConfig(dns, hcp)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile dns config: %w", err))
}
ingress := globalconfig.IngressConfig()
if _, err := r.CreateOrUpdate(ctx, r.client, ingress, func() error {
globalconfig.ReconcileIngressConfig(ingress, hcp, globalConfig)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile ingress config: %w", err))
}
network := globalconfig.NetworkConfig()
if _, err := r.CreateOrUpdate(ctx, r.client, network, func() error {
globalconfig.ReconcileNetworkConfig(network, hcp, globalConfig)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile network config: %w", err))
}
proxy := globalconfig.ProxyConfig()
if _, err := r.CreateOrUpdate(ctx, r.client, proxy, func() error {
globalconfig.ReconcileProxyConfig(proxy, hcp, globalConfig)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile proxy config: %w", err))
}
icsp := globalconfig.ImageContentSourcePolicy()
if _, err := r.CreateOrUpdate(ctx, r.client, icsp, func() error {
return globalconfig.ReconcileImageContentSourcePolicy(icsp, hcp)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile image content source policy: %w", err))
}
installConfigCM := manifests.InstallConfigConfigMap()
if _, err := r.CreateOrUpdate(ctx, r.client, installConfigCM, func() error {
installConfigCM.Data = map[string]string{
"install-config": globalconfig.NewInstallConfig(hcp).String(),
}
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile dns config: %w", err))
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileNamespaces(ctx context.Context) error {
namespaceManifests := []struct {
manifest func() *corev1.Namespace
reconcile func(*corev1.Namespace) error
}{
{manifest: manifests.NamespaceOpenShiftAPIServer},
{manifest: manifests.NamespaceOpenShiftControllerManager},
{manifest: manifests.NamespaceKubeAPIServer, reconcile: namespaces.ReconcileKubeAPIServerNamespace},
{manifest: manifests.NamespaceKubeControllerManager},
{manifest: manifests.NamespaceKubeScheduler},
{manifest: manifests.NamespaceEtcd},
{manifest: manifests.NamespaceIngress, reconcile: namespaces.ReconcileOpenShiftIngressNamespace},
{manifest: manifests.NamespaceAuthentication},
}
var errs []error
for _, m := range namespaceManifests {
ns := m.manifest()
if _, err := r.CreateOrUpdate(ctx, r.client, ns, func() error {
if m.reconcile != nil {
return m.reconcile(ns)
}
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile namespace %s: %w", ns.Name, err))
}
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileRBAC(ctx context.Context) error {
roles := []struct {
manifest func() *rbacv1.ClusterRole
reconcile func(*rbacv1.ClusterRole) error
}{
{manifest: manifests.CSRApproverClusterRole, reconcile: rbac.ReconcileCSRApproverClusterRole},
{manifest: manifests.IngressToRouteControllerClusterRole, reconcile: rbac.ReconcileIngressToRouteControllerClusterRole},
{manifest: manifests.NamespaceSecurityAllocationControllerClusterRole, reconcile: rbac.ReconcileNamespaceSecurityAllocationControllerClusterRole},
}
roleBindings := []struct {
manifest func() *rbacv1.ClusterRoleBinding
reconcile func(*rbacv1.ClusterRoleBinding) error
}{
{manifest: manifests.CSRApproverClusterRoleBinding, reconcile: rbac.ReconcileCSRApproverClusterRoleBinding},
{manifest: manifests.IngressToRouteControllerClusterRoleBinding, reconcile: rbac.ReconcileIngressToRouteControllerClusterRoleBinding},
{manifest: manifests.NamespaceSecurityAllocationControllerClusterRoleBinding, reconcile: rbac.ReconcileNamespaceSecurityAllocationControllerClusterRoleBinding},
{manifest: manifests.NodeBootstrapperClusterRoleBinding, reconcile: rbac.ReconcileNodeBootstrapperClusterRoleBinding},
{manifest: manifests.CSRRenewalClusterRoleBinding, reconcile: rbac.ReconcileCSRRenewalClusterRoleBinding},
}
var errs []error
for _, m := range roles {
role := m.manifest()
if _, err := r.CreateOrUpdate(ctx, r.client, role, func() error {
return m.reconcile(role)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile role %s: %w", role.Name, err))
}
}
for _, m := range roleBindings {
rb := m.manifest()
if _, err := r.CreateOrUpdate(ctx, r.client, rb, func() error {
return m.reconcile(rb)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile role binding %s: %w", rb.Name, err))
}
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileIngressController(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
var errs []error
p := ingress.NewIngressParams(hcp)
ingressController := manifests.IngressDefaultIngressController()
if _, err := r.CreateOrUpdate(ctx, r.client, ingressController, func() error {
return ingress.ReconcileDefaultIngressController(ingressController, p.IngressSubdomain, p.PlatformType, p.Replicas)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile default ingress controller: %w", err))
}
sourceCert := manifests.IngressCert(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(sourceCert), sourceCert); err != nil {
errs = append(errs, fmt.Errorf("failed to get ingress cert (%s/%s) from control plane: %w", sourceCert.Namespace, sourceCert.Name, err))
} else {
ingressControllerCert := manifests.IngressDefaultIngressControllerCert()
if _, err := r.CreateOrUpdate(ctx, r.client, ingressControllerCert, func() error {
return ingress.ReconcileDefaultIngressControllerCertSecret(ingressControllerCert, sourceCert)
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile default ingress controller cert: %w", err))
}
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileKonnectivityAgent(ctx context.Context, hcp *hyperv1.HostedControlPlane, releaseImage *releaseinfo.ReleaseImage) error {
var errs []error
p := konnectivity.NewKonnectivityParams(hcp, releaseImage.ComponentImages(), r.konnectivityServerAddress, r.konnectivityServerPort)
controlPlaneAgentSecret := manifests.KonnectivityControlPlaneAgentSecret(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(controlPlaneAgentSecret), controlPlaneAgentSecret); err != nil {
errs = append(errs, fmt.Errorf("failed to get control plane konnectivity agent secret: %w", err))
} else {
agentSecret := manifests.KonnectivityAgentSecret()
if _, err := r.CreateOrUpdate(ctx, r.client, agentSecret, func() error {
konnectivity.ReconcileKonnectivityAgentSecret(agentSecret, controlPlaneAgentSecret)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile konnectivity agent secret: %w", err))
}
}
agentDaemonset := manifests.KonnectivityAgentDaemonSet()
if _, err := r.CreateOrUpdate(ctx, r.client, agentDaemonset, func() error {
konnectivity.ReconcileAgentDaemonSet(agentDaemonset, p.DeploymentConfig, p.Image, p.ExternalAddress, p.ExternalPort)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile konnectivity agent daemonset: %w", err))
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileClusterVersion(ctx context.Context) error {
clusterVersion := &configv1.ClusterVersion{ObjectMeta: metav1.ObjectMeta{Name: "version"}}
if _, err := r.CreateOrUpdate(ctx, r.client, clusterVersion, func() error {
clusterVersion.Spec.Upstream = ""
clusterVersion.Spec.Channel = ""
clusterVersion.Spec.DesiredUpdate = nil
return nil
}); err != nil {
return fmt.Errorf("failed to reconcile clusterVersion: %w", err)
}
return nil
}
func (r *reconciler) reconcileOpenshiftAPIServerAPIServices(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
rootCA := manifests.RootCASecret(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(rootCA), rootCA); err != nil {
return fmt.Errorf("failed to get root ca from control plane: %w", err)
}
var errs []error
for _, apiSvcGroup := range manifests.OpenShiftAPIServerAPIServiceGroups() {
apiSvc := manifests.OpenShiftAPIServerAPIService(apiSvcGroup)
if _, err := r.CreateOrUpdate(ctx, r.client, apiSvc, func() error {
oapi.ReconcileAPIService(apiSvc, manifests.OpenShiftAPIServerClusterService(), rootCA, apiSvcGroup)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift apiserver apiservice (%s): %w", apiSvcGroup, err))
}
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileOpenshiftOAuthAPIServerAPIServices(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
rootCA := manifests.RootCASecret(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(rootCA), rootCA); err != nil {
return fmt.Errorf("failed to get root ca from control plane: %w", err)
}
var errs []error
for _, apiSvcGroup := range manifests.OpenShiftOAuthAPIServerAPIServiceGroups() {
apiSvc := manifests.OpenShiftOAuthAPIServerAPIService(apiSvcGroup)
if _, err := r.CreateOrUpdate(ctx, r.client, apiSvc, func() error {
oapi.ReconcileAPIService(apiSvc, manifests.OpenShiftOAuthAPIServerClusterService(), rootCA, apiSvcGroup)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile openshift oauth apiserver apiservice (%s): %w", apiSvcGroup, err))
}
}
return errors.NewAggregate(errs)
}
func (r *reconciler) reconcileOpenshiftAPIServerEndpoints(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
cpService := manifests.OpenShiftAPIServerService(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(cpService), cpService); err != nil {
return fmt.Errorf("failed to get openshift apiserver service from control plane: %w", err)
}
if len(cpService.Spec.ClusterIP) == 0 {
return fmt.Errorf("openshift apiserver service in control plane does not yet have a cluster IP")
}
openshiftAPIServerEndpoints := manifests.OpenShiftAPIServerClusterEndpoints()
_, err := r.CreateOrUpdate(ctx, r.client, openshiftAPIServerEndpoints, func() error {
oapi.ReconcileEndpoints(openshiftAPIServerEndpoints, cpService.Spec.ClusterIP)
return nil
})
return err
}
func (r *reconciler) reconcileOpenshiftOAuthAPIServerEndpoints(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
cpService := manifests.OpenShiftOAuthAPIServerService(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(cpService), cpService); err != nil {
return fmt.Errorf("failed to get openshift oauth apiserver service from control plane: %w", err)
}
if len(cpService.Spec.ClusterIP) == 0 {
return fmt.Errorf("openshift oauth apiserver service in control plane does not yet have a cluster IP")
}
openshiftOAuthAPIServerEndpoints := manifests.OpenShiftOAuthAPIServerClusterEndpoints()
_, err := r.CreateOrUpdate(ctx, r.client, openshiftOAuthAPIServerEndpoints, func() error {
oapi.ReconcileEndpoints(openshiftOAuthAPIServerEndpoints, cpService.Spec.ClusterIP)
return nil
})
return err
}
func (r *reconciler) reconcileKubeadminPasswordHashSecret(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
kubeadminPasswordSecret := manifests.KubeadminPasswordSecret(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(kubeadminPasswordSecret), kubeadminPasswordSecret); err != nil {
return fmt.Errorf("failed to get kubeadmin password secret: %w", err)
}
kubeadminPasswordHashSecret := manifests.KubeadminPasswordHashSecret()
if _, err := r.CreateOrUpdate(ctx, r.client, kubeadminPasswordHashSecret, func() error {
return kubeadminpassword.ReconcileKubeadminPasswordHashSecret(kubeadminPasswordHashSecret, kubeadminPasswordSecret)
}); err != nil {
return err
}
oauthDeployment := manifests.OAuthDeployment(hcp.Namespace)
if _, err := r.CreateOrUpdate(ctx, r.cpClient, oauthDeployment, func() error {
if oauthDeployment.Spec.Template.ObjectMeta.Annotations == nil {
oauthDeployment.Spec.Template.ObjectMeta.Annotations = map[string]string{}
}
oauthDeployment.Spec.Template.ObjectMeta.Annotations[SecretHashAnnotation] = secretHash(kubeadminPasswordHashSecret.Data["kubeadmin"])
return nil
}); err != nil {
return err
}
return nil
}
func secretHash(data []byte) string {
return fmt.Sprintf("%x", md5.Sum(data))
}
func (r *reconciler) reconcileOAuthServingCertCABundle(ctx context.Context, hcp *hyperv1.HostedControlPlane) error {
oauthServingCert := manifests.OpenShiftOAuthServerCert(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(oauthServingCert), oauthServingCert); err != nil {
return fmt.Errorf("cannot get oauth serving cert: %w", err)
}
caBundle := manifests.OAuthCABundle()
if _, err := r.CreateOrUpdate(ctx, r.client, caBundle, func() error {
return oauth.ReconcileOAuthServerCertCABundle(caBundle, oauthServingCert)
}); err != nil {
return fmt.Errorf("failed to reconcile oauth server cert ca bundle: %w", err)
}
return nil
}
const awsCredentialsTemplate = `[default]
role_arn = %s
web_identity_token_file = /var/run/secrets/openshift/serviceaccount/token
`
func (r *reconciler) reconcileCloudCredentialSecrets(ctx context.Context, hcp *hyperv1.HostedControlPlane) []error {
var errs []error
switch hcp.Spec.Platform.Type {
case hyperv1.AWSPlatform:
for _, role := range hcp.Spec.Platform.AWS.Roles {
secret := manifests.AWSCloudCredsSecret(role)
if _, err := r.CreateOrUpdate(ctx, r.client, secret, func() error {
credentials := fmt.Sprintf(awsCredentialsTemplate, role.ARN)
secret.Data = map[string][]byte{"credentials": []byte(credentials)}
secret.Type = corev1.SecretTypeOpaque
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile aws cloud credential secret %s/%s: %w", secret.Namespace, secret.Name, err))
}
}
}
return errs
}
func (r *reconciler) reconcileOLM(ctx context.Context, hcp *hyperv1.HostedControlPlane) []error {
var errs []error
catalogs := []struct {
manifest func() *operatorsv1alpha1.CatalogSource
reconcile func(*operatorsv1alpha1.CatalogSource)
}{
{manifest: manifests.CertifiedOperatorsCatalogSource, reconcile: olm.ReconcileCertifiedOperatorsCatalogSource},
{manifest: manifests.CommunityOperatorsCatalogSource, reconcile: olm.ReconcileCommunityOperatorsCatalogSource},
{manifest: manifests.RedHatMarketplaceCatalogSource, reconcile: olm.ReconcileRedHatMarketplaceCatalogSource},
{manifest: manifests.RedHatOperatorsCatalogSource, reconcile: olm.ReconcileRedHatOperatorsCatalogSource},
}
for _, catalog := range catalogs {
cs := catalog.manifest()
if _, err := r.CreateOrUpdate(ctx, r.client, cs, func() error {
catalog.reconcile(cs)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile catalog source %s/%s: %w", cs.Namespace, cs.Name, err))
}
}
olmAlertRules := manifests.OLMAlertRules()
if _, err := r.CreateOrUpdate(ctx, r.client, olmAlertRules, func() error {
olm.ReconcileOLMAlertRules(olmAlertRules)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile olm alert rules: %w", err))
}
rootCA := manifests.RootCASecret(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(rootCA), rootCA); err != nil {
errs = append(errs, fmt.Errorf("failed to get root ca cert from control plane namespace: %w", err))
} else {
packageServerAPIService := manifests.OLMPackageServerAPIService()
if _, err := r.CreateOrUpdate(ctx, r.client, packageServerAPIService, func() error {
olm.ReconcilePackageServerAPIService(packageServerAPIService, rootCA)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile OLM packageserver API service: %w", err))
}
}
packageServerService := manifests.OLMPackageServerService()
if _, err := r.CreateOrUpdate(ctx, r.client, packageServerService, func() error {
olm.ReconcilePackageServerService(packageServerService)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile OLM packageserver service: %w", err))
}
cpService := manifests.OLMPackageServerControlPlaneService(hcp.Namespace)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(cpService), cpService); err != nil {
errs = append(errs, fmt.Errorf("failed to get packageserver service from control plane namespace: %w", err))
} else {
if len(cpService.Spec.ClusterIP) == 0 {
errs = append(errs, fmt.Errorf("packageserver service does not yet have a cluster IP"))
} else {
packageServerEndpoints := manifests.OLMPackageServerEndpoints()
if _, err := r.CreateOrUpdate(ctx, r.client, packageServerEndpoints, func() error {
olm.ReconcilePackageServerEndpoints(packageServerEndpoints, cpService.Spec.ClusterIP)
return nil
}); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile OLM packageserver service: %w", err))
}
}
}
return errs
}