/
awsmanagedcluster_controller.go
200 lines (164 loc) · 7.09 KB
/
awsmanagedcluster_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
/*
Copyright 2022 The Kubernetes 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 controllers
import (
"context"
"fmt"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
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"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
)
// AWSManagedClusterReconciler reconciles AWSManagedCluster.
type AWSManagedClusterReconciler struct {
client.Client
Recorder record.EventRecorder
WatchFilterValue string
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmanagedclusters,verbs=get;list;watch;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmanagedclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=awsmanagedcontrolplanes;awsmanagedcontrolplanes/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
func (r *AWSManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
log := ctrl.LoggerFrom(ctx)
// Fetch the AWSManagedCluster instance
awsManagedCluster := &infrav1.AWSManagedCluster{}
err := r.Get(ctx, req.NamespacedName, awsManagedCluster)
if err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, awsManagedCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
log.Info("Cluster Controller has not yet set OwnerRef")
return reconcile.Result{}, nil
}
if annotations.IsPaused(cluster, awsManagedCluster) {
log.Info("AWSManagedCluster or linked Cluster is marked as paused. Won't reconcile")
return reconcile.Result{}, nil
}
log = log.WithValues("cluster", cluster.Name)
controlPlane := &ekscontrolplanev1.AWSManagedControlPlane{}
controlPlaneRef := types.NamespacedName{
Name: cluster.Spec.ControlPlaneRef.Name,
Namespace: cluster.Spec.ControlPlaneRef.Namespace,
}
if err := r.Get(ctx, controlPlaneRef, controlPlane); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get control plane ref: %w", err)
}
log = log.WithValues("controlPlane", controlPlaneRef.Name)
patchHelper, err := patch.NewHelper(awsManagedCluster, r.Client)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to init patch helper: %w", err)
}
// Set the values from the managed control plane
awsManagedCluster.Status.Ready = true
awsManagedCluster.Spec.ControlPlaneEndpoint = controlPlane.Spec.ControlPlaneEndpoint
awsManagedCluster.Status.FailureDomains = controlPlane.Status.FailureDomains
if err := patchHelper.Patch(ctx, awsManagedCluster); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to patch AWSManagedCluster: %w", err)
}
log.Info("Successfully reconciled AWSManagedCluster")
return reconcile.Result{}, nil
}
func (r *AWSManagedClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
log := logger.FromContext(ctx)
awsManagedCluster := &infrav1.AWSManagedCluster{}
controller, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(awsManagedCluster).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(log.GetLogger())).
Build(r)
if err != nil {
return fmt.Errorf("error creating controller: %w", err)
}
// Add a watch for clusterv1.Cluster unpaise
if err = controller.Watch(
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("AWSManagedCluster"), mgr.GetClient(), &infrav1.AWSManagedCluster{})),
predicates.ClusterUnpaused(log.GetLogger()),
); err != nil {
return fmt.Errorf("failed adding a watch for ready clusters: %w", err)
}
// Add a watch for AWSManagedControlPlane
if err = controller.Watch(
source.Kind(mgr.GetCache(), &ekscontrolplanev1.AWSManagedControlPlane{}),
handler.EnqueueRequestsFromMapFunc(r.managedControlPlaneToManagedCluster(ctx, log)),
); err != nil {
return fmt.Errorf("failed adding watch on AWSManagedControlPlane: %w", err)
}
return nil
}
func (r *AWSManagedClusterReconciler) managedControlPlaneToManagedCluster(_ context.Context, log *logger.Logger) handler.MapFunc {
return func(ctx context.Context, o client.Object) []ctrl.Request {
awsManagedControlPlane, ok := o.(*ekscontrolplanev1.AWSManagedControlPlane)
if !ok {
log.Error(errors.Errorf("expected an AWSManagedControlPlane, got %T instead", o), "failed to map AWSManagedControlPlane")
return nil
}
log := log.WithValues("objectMapper", "awsmcpTomc", "awsmanagedcontrolplane", klog.KRef(awsManagedControlPlane.Namespace, awsManagedControlPlane.Name))
if !awsManagedControlPlane.ObjectMeta.DeletionTimestamp.IsZero() {
log.Info("AWSManagedControlPlane has a deletion timestamp, skipping mapping")
return nil
}
if awsManagedControlPlane.Spec.ControlPlaneEndpoint.IsZero() {
log.Debug("AWSManagedControlPlane has no control plane endpoint, skipping mapping")
return nil
}
cluster, err := util.GetOwnerCluster(ctx, r.Client, awsManagedControlPlane.ObjectMeta)
if err != nil {
log.Error(err, "failed to get owning cluster")
return nil
}
if cluster == nil {
log.Info("no owning cluster, skipping mapping")
return nil
}
managedClusterRef := cluster.Spec.InfrastructureRef
if managedClusterRef == nil || managedClusterRef.Kind != "AWSManagedCluster" {
log.Info("InfrastructureRef is nil or not AWSManagedCluster, skipping mapping")
return nil
}
return []ctrl.Request{
{
NamespacedName: types.NamespacedName{
Name: managedClusterRef.Name,
Namespace: managedClusterRef.Namespace,
},
},
}
}
}