-
Notifications
You must be signed in to change notification settings - Fork 313
/
configmap_observer.go
120 lines (102 loc) · 3.77 KB
/
configmap_observer.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
package cmca
import (
"context"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
corev1listers "k8s.io/client-go/listers/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/manifests"
resourcemanifests "github.com/openshift/hypershift/control-plane-operator/hostedclusterconfigoperator/controllers/resources/manifests"
"github.com/openshift/hypershift/support/config"
"github.com/openshift/hypershift/support/upsert"
)
const (
ServiceCAConfigMap = "service-ca"
DefaultIngressCAConfigMap = "default-ingress-cert"
)
type syncDesc struct {
destination *corev1.ConfigMap
sourceKey string
destKey string
}
func configMapsToSync(ns string) map[string]syncDesc {
return map[string]syncDesc{
"service-ca": {
destination: manifests.ServiceServingCA(ns),
sourceKey: "ca-bundle.crt",
destKey: "service-ca.crt",
},
"default-ingress-cert": {
destination: manifests.IngressObservedDefaultIngressCertCA(ns),
sourceKey: "ca-bundle.crt",
destKey: "ca.crt",
},
}
}
// ManagedCAObserver watches 2 CA configmaps in the target cluster:
// - openshift-managed-config/router-ca
// - openshift-managed-config/service-ca
// It populates a configmap on the management cluster with their content.
// A separate controller uses that content to adjust the configmap for
// the Kube controller manager CA.
type ManagedCAObserver struct {
createOrUpdate upsert.CreateOrUpdateFN
// cpClient is a client that allows access to the management cluster
cpClient client.Client
// cmLister is a lister of configmaps on the guest cluster
cmLister corev1listers.ConfigMapLister
// namespace is the namespace where the control plane of the cluster
// lives on the management server
namespace string
// hcpName is the name of the hostedcontrolplane resource in the
// control plane namespace
hcpName string
// log is the logger for this controller
log logr.Logger
}
// Reconcile periodically watches configmaps in the guest cluster and syncs them to the control plane side
func (r *ManagedCAObserver) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
if req.Namespace != ManagedConfigNamespace {
return ctrl.Result{}, nil
}
configMaps := configMapsToSync(r.namespace)
if _, found := configMaps[req.Name]; !found {
return ctrl.Result{}, nil
}
log := r.log.WithValues("configmap", req.NamespacedName)
log.Info("syncing configmap")
hcp := resourcemanifests.HostedControlPlane(r.namespace, r.hcpName)
if err := r.cpClient.Get(ctx, client.ObjectKeyFromObject(hcp), hcp); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get hosted control plane %s/%s: %w", r.namespace, r.hcpName, err)
}
ownerRef := config.OwnerRefFrom(hcp)
sourceCM, err := r.cmLister.ConfigMaps(req.Namespace).Get(req.Name)
if err != nil {
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to get source ConfigMap %s: %w", req.Name, err)
}
sourceCM = nil
}
desc := configMaps[req.Name]
destinationCM := desc.destination
if sourceCM != nil {
if _, err := r.createOrUpdate(ctx, r.cpClient, destinationCM, func() error {
ownerRef.ApplyTo(destinationCM)
if destinationCM.Data == nil {
destinationCM.Data = map[string]string{}
}
destinationCM.Data[desc.destKey] = sourceCM.Data[desc.sourceKey]
return nil
}); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to update destination ConfigMap %s: %w", destinationCM.Name, err)
}
} else {
if err := r.cpClient.Delete(ctx, destinationCM); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to delete destination ConfigMap %s: %w", destinationCM.Name, err)
}
}
return ctrl.Result{}, nil
}