Skip to content

Commit 050fd9a

Browse files
committed
chore: inline v1beta1 helper utils to enable upgrade
1 parent a6ff3d6 commit 050fd9a

File tree

8 files changed

+250
-23
lines changed

8 files changed

+250
-23
lines changed

cloud/scope/machine.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"sigs.k8s.io/cluster-api-provider-gcp/cloud/providerid"
3838
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/shared"
3939
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
40-
"sigs.k8s.io/cluster-api/util"
4140
"sigs.k8s.io/cluster-api/util/patch"
4241
"sigs.k8s.io/controller-runtime/pkg/client"
4342
)
@@ -138,18 +137,24 @@ func (m *MachineScope) ControlPlaneGroupName() string {
138137

139138
// IsControlPlane returns true if the machine is a control plane.
140139
func (m *MachineScope) IsControlPlane() bool {
141-
return util.IsControlPlaneMachine(m.Machine)
140+
return IsControlPlaneMachine(m.Machine)
142141
}
143142

144143
// Role returns the machine role from the labels.
145144
func (m *MachineScope) Role() string {
146-
if util.IsControlPlaneMachine(m.Machine) {
145+
if IsControlPlaneMachine(m.Machine) {
147146
return "control-plane"
148147
}
149148

150149
return "node"
151150
}
152151

152+
// IsControlPlaneMachine checks machine is a control plane node.
153+
func IsControlPlaneMachine(machine *clusterv1.Machine) bool {
154+
_, ok := machine.Labels[clusterv1.MachineControlPlaneLabel]
155+
return ok
156+
}
157+
153158
// GetInstanceID returns the GCPMachine instance id by parsing Spec.ProviderID.
154159
func (m *MachineScope) GetInstanceID() *string {
155160
parsed, err := NewProviderID(m.GetProviderID())

controllers/gcpcluster_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/loadbalancers"
3232
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/networks"
3333
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/subnets"
34+
"sigs.k8s.io/cluster-api-provider-gcp/pkg/capiutils"
3435
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
3536
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
3637
"sigs.k8s.io/cluster-api/util"
@@ -119,7 +120,7 @@ func (r *GCPClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
119120
}
120121

121122
// Fetch the Cluster.
122-
cluster, err := util.GetOwnerCluster(ctx, r.Client, gcpCluster.ObjectMeta)
123+
cluster, err := capiutils.GetOwnerCluster(ctx, r.Client, gcpCluster.ObjectMeta)
123124
if err != nil {
124125
log.Error(err, "Failed to get owner cluster")
125126
return ctrl.Result{}, err
@@ -129,7 +130,7 @@ func (r *GCPClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
129130
return ctrl.Result{}, nil
130131
}
131132

132-
if annotations.IsPaused(cluster, gcpCluster) {
133+
if capiutils.IsPaused(cluster, gcpCluster) {
133134
log.Info("GCPCluster of linked Cluster is marked as paused. Won't reconcile")
134135
return ctrl.Result{}, nil
135136
}

controllers/gcpmachine_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
2727
"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"
2828
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/instances"
29+
"sigs.k8s.io/cluster-api-provider-gcp/pkg/capiutils"
2930
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
3031
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
3132
"sigs.k8s.io/cluster-api/util"
32-
"sigs.k8s.io/cluster-api/util/annotations"
3333
"sigs.k8s.io/cluster-api/util/predicates"
3434
"sigs.k8s.io/cluster-api/util/record"
3535
ctrl "sigs.k8s.io/controller-runtime"
@@ -82,7 +82,7 @@ func (r *GCPMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
8282
if err := c.Watch(
8383
source.Kind[client.Object](mgr.GetCache(), &clusterv1.Cluster{},
8484
handler.EnqueueRequestsFromMapFunc(clusterToObjectFunc),
85-
predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), log),
85+
capiutils.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), log),
8686
)); err != nil {
8787
return errors.Wrap(err, "failed adding a watch for ready clusters")
8888
}
@@ -145,7 +145,7 @@ func (r *GCPMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
145145
return ctrl.Result{}, err
146146
}
147147

148-
machine, err := util.GetOwnerMachine(ctx, r.Client, gcpMachine.ObjectMeta)
148+
machine, err := capiutils.GetOwnerMachine(ctx, r.Client, gcpMachine.ObjectMeta)
149149
if err != nil {
150150
return ctrl.Result{}, err
151151
}
@@ -155,14 +155,14 @@ func (r *GCPMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
155155
}
156156

157157
log = log.WithValues("machine", machine.Name)
158-
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
158+
cluster, err := capiutils.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
159159
if err != nil {
160160
log.Info("Machine is missing cluster label or cluster does not exist")
161161

162162
return ctrl.Result{}, nil
163163
}
164164

165-
if annotations.IsPaused(cluster, gcpMachine) {
165+
if capiutils.IsPaused(cluster, gcpMachine) {
166166
log.Info("GCPMachine or linked Cluster is marked as paused. Won't reconcile")
167167
return ctrl.Result{}, nil
168168
}

exp/controllers/gcpmanagedcluster_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ import (
3232
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/networks"
3333
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/subnets"
3434
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
35+
"sigs.k8s.io/cluster-api-provider-gcp/pkg/capiutils"
3536
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
3637
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
3738
"sigs.k8s.io/cluster-api/util"
38-
"sigs.k8s.io/cluster-api/util/annotations"
3939
"sigs.k8s.io/cluster-api/util/predicates"
4040
"sigs.k8s.io/cluster-api/util/record"
4141
ctrl "sigs.k8s.io/controller-runtime"
@@ -81,7 +81,7 @@ func (r *GCPManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re
8181
}
8282

8383
// Fetch the Cluster.
84-
cluster, err := util.GetOwnerCluster(ctx, r.Client, gcpCluster.ObjectMeta)
84+
cluster, err := capiutils.GetOwnerCluster(ctx, r.Client, gcpCluster.ObjectMeta)
8585
if err != nil {
8686
log.Error(err, "Failed to get owner cluster")
8787
return ctrl.Result{}, err
@@ -91,7 +91,7 @@ func (r *GCPManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re
9191
return ctrl.Result{}, nil
9292
}
9393

94-
if annotations.IsPaused(cluster, gcpCluster) {
94+
if capiutils.IsPaused(cluster, gcpCluster) {
9595
log.Info("GCPManagedCluster or linked Cluster is marked as paused. Won't reconcile")
9696
return ctrl.Result{}, nil
9797
}
@@ -280,7 +280,7 @@ func (r *GCPManagedClusterReconciler) managedControlPlaneMapper() handler.MapFun
280280
return nil
281281
}
282282

283-
cluster, err := util.GetOwnerCluster(ctx, r.Client, gcpManagedControlPlane.ObjectMeta)
283+
cluster, err := capiutils.GetOwnerCluster(ctx, r.Client, gcpManagedControlPlane.ObjectMeta)
284284
if err != nil {
285285
log.Error(err, "failed to get owning cluster")
286286
return nil

exp/controllers/gcpmanagedcontrolplane_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ import (
2121
"fmt"
2222
"time"
2323

24-
"sigs.k8s.io/cluster-api/util/annotations"
25-
2624
"github.com/pkg/errors"
2725
apierrors "k8s.io/apimachinery/pkg/api/errors"
2826
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
2927
"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"
3028
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/container/clusters"
3129
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
30+
"sigs.k8s.io/cluster-api-provider-gcp/pkg/capiutils"
3231
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
3332
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
3433
"sigs.k8s.io/cluster-api/util"
@@ -76,7 +75,7 @@ func (r *GCPManagedControlPlaneReconciler) SetupWithManager(ctx context.Context,
7675
if err = c.Watch(
7776
source.Kind[client.Object](mgr.GetCache(), &clusterv1.Cluster{},
7877
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, gcpManagedControlPlane.GroupVersionKind(), mgr.GetClient(), &infrav1exp.GCPManagedControlPlane{})),
79-
predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), log),
78+
capiutils.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), log),
8079
)); err != nil {
8180
return fmt.Errorf("failed adding a watch for ready clusters: %w", err)
8281
}
@@ -100,7 +99,7 @@ func (r *GCPManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ct
10099
}
101100

102101
// Get the cluster
103-
cluster, err := util.GetOwnerCluster(ctx, r.Client, gcpManagedControlPlane.ObjectMeta)
102+
cluster, err := capiutils.GetOwnerCluster(ctx, r.Client, gcpManagedControlPlane.ObjectMeta)
104103
if err != nil {
105104
log.Error(err, "Failed to retrieve owner Cluster from the API Server")
106105
return ctrl.Result{}, err
@@ -110,7 +109,7 @@ func (r *GCPManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ct
110109
return ctrl.Result{}, nil
111110
}
112111

113-
if annotations.IsPaused(cluster, gcpManagedControlPlane) {
112+
if capiutils.IsPaused(cluster, gcpManagedControlPlane) {
114113
log.Info("Reconciliation is paused for this object")
115114
return ctrl.Result{}, nil
116115
}

exp/controllers/gcpmanagedmachinepool_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"k8s.io/apimachinery/pkg/runtime/schema"
3131
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
3232
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/container/nodepools"
33+
"sigs.k8s.io/cluster-api-provider-gcp/pkg/capiutils"
3334
expclusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
34-
"sigs.k8s.io/cluster-api/util/annotations"
3535
"sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions"
3636
"sigs.k8s.io/cluster-api/util/record"
3737
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
@@ -181,7 +181,7 @@ func (r *GCPManagedMachinePoolReconciler) SetupWithManager(ctx context.Context,
181181
if err := c.Watch(
182182
source.Kind[client.Object](mgr.GetCache(), &clusterv1.Cluster{},
183183
handler.EnqueueRequestsFromMapFunc(clusterToObjectFunc),
184-
predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), log),
184+
capiutils.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), log),
185185
)); err != nil {
186186
return errors.Wrap(err, "failed adding a watch for ready clusters")
187187
}
@@ -252,12 +252,12 @@ func (r *GCPManagedMachinePoolReconciler) Reconcile(ctx context.Context, req ctr
252252
}
253253

254254
// Get the cluster
255-
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machinePool.ObjectMeta)
255+
cluster, err := capiutils.GetClusterFromMetadata(ctx, r.Client, machinePool.ObjectMeta)
256256
if err != nil {
257257
log.Info("Failed to retrieve Cluster from MachinePool")
258258
return ctrl.Result{}, err
259259
}
260-
if annotations.IsPaused(cluster, gcpManagedMachinePool) {
260+
if capiutils.IsPaused(cluster, gcpManagedMachinePool) {
261261
log.Info("Reconciliation is paused for this object")
262262
return ctrl.Result{}, nil
263263
}

pkg/capiutils/predicates.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package capiutils
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/go-logr/logr"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/klog/v2"
25+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
26+
"sigs.k8s.io/controller-runtime/pkg/event"
27+
"sigs.k8s.io/controller-runtime/pkg/predicate"
28+
29+
"sigs.k8s.io/cluster-api/util/predicates"
30+
31+
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
32+
)
33+
34+
// ClusterUpdateInfraReady returns a predicate that returns true for an update event when a cluster has Status.InfrastructureReady changed from false to true
35+
// it also returns true if the resource provided is not a Cluster to allow for use with controller-runtime NewControllerManagedBy.
36+
// Deprecated: replace with predicates.ClusterUpdateInfraReady when we move to v1beta2
37+
func ClusterUpdateInfraReady(scheme *runtime.Scheme, logger logr.Logger) predicate.Funcs {
38+
return predicate.Funcs{
39+
UpdateFunc: func(e event.UpdateEvent) bool {
40+
log := logger.WithValues("predicate", "ClusterUpdateInfraReady", "eventType", "update")
41+
if gvk, err := apiutil.GVKForObject(e.ObjectOld, scheme); err == nil {
42+
log = log.WithValues(gvk.Kind, klog.KObj(e.ObjectOld))
43+
}
44+
45+
oldCluster, ok := e.ObjectOld.(*clusterv1.Cluster)
46+
if !ok {
47+
log.V(4).Info("Expected Cluster", "type", fmt.Sprintf("%T", e.ObjectOld))
48+
return false
49+
}
50+
51+
newCluster := e.ObjectNew.(*clusterv1.Cluster)
52+
53+
if !oldCluster.Status.InfrastructureReady && newCluster.Status.InfrastructureReady {
54+
log.V(6).Info("Cluster infrastructure became ready, allowing further processing")
55+
return true
56+
}
57+
58+
log.V(4).Info("Cluster infrastructure did not become ready, blocking further processing")
59+
return false
60+
},
61+
CreateFunc: func(event.CreateEvent) bool { return false },
62+
DeleteFunc: func(event.DeleteEvent) bool { return false },
63+
GenericFunc: func(event.GenericEvent) bool { return false },
64+
}
65+
}
66+
67+
// ClusterPausedTransitions returns a predicate that returns true for an update event when a cluster has Spec.Paused changed.
68+
// Deprecated: replace with predicates.ClusterPausedTransitions when we move to v1beta2
69+
func ClusterPausedTransitions(scheme *runtime.Scheme, logger logr.Logger) predicate.Funcs {
70+
return predicate.Funcs{
71+
UpdateFunc: func(e event.UpdateEvent) bool {
72+
log := logger.WithValues("predicate", "ClusterPausedTransitions", "eventType", "update")
73+
if gvk, err := apiutil.GVKForObject(e.ObjectOld, scheme); err == nil {
74+
log = log.WithValues(gvk.Kind, klog.KObj(e.ObjectOld))
75+
}
76+
77+
oldCluster, ok := e.ObjectOld.(*clusterv1.Cluster)
78+
if !ok {
79+
log.V(4).Info("Expected Cluster", "type", fmt.Sprintf("%T", e.ObjectOld))
80+
return false
81+
}
82+
83+
newCluster := e.ObjectNew.(*clusterv1.Cluster)
84+
85+
if oldCluster.Spec.Paused && !newCluster.Spec.Paused {
86+
log.V(6).Info("Cluster unpausing, allowing further processing")
87+
return true
88+
}
89+
90+
if !oldCluster.Spec.Paused && newCluster.Spec.Paused {
91+
log.V(6).Info("Cluster pausing, allowing further processing")
92+
return true
93+
}
94+
95+
// This predicate always work in "or" with Paused predicates
96+
// so the logs are adjusted to not provide false negatives/verbosity at V<=5.
97+
log.V(6).Info("Cluster paused state was not changed, blocking further processing")
98+
return false
99+
},
100+
CreateFunc: func(event.CreateEvent) bool { return false },
101+
DeleteFunc: func(event.DeleteEvent) bool { return false },
102+
GenericFunc: func(event.GenericEvent) bool { return false },
103+
}
104+
}
105+
106+
// ClusterPausedTransitionsOrInfrastructureReady returns a Predicate that returns true on Cluster Update events where
107+
// either Cluster.Spec.Paused transitions or Cluster.Status.InfrastructureReady transitions to true.
108+
// This implements a common requirement for some cluster-api and provider controllers (such as Machine Infrastructure
109+
// controllers) to resume reconciliation when the Cluster gets paused or unpaused and when the infrastructure becomes ready.
110+
// Example use:
111+
//
112+
// err := controller.Watch(
113+
// source.Kind(cache, &clusterv1.Cluster{}),
114+
// handler.EnqueueRequestsFromMapFunc(clusterToMachines)
115+
// predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), r.Log),
116+
// )
117+
//
118+
// Deprecated: replace with predicates.ClusterPausedTransitionsOrInfrastructureReady when we move to v1beta2
119+
func ClusterPausedTransitionsOrInfrastructureReady(scheme *runtime.Scheme, logger logr.Logger) predicate.Funcs {
120+
log := logger.WithValues("predicate", "ClusterPausedTransitionsOrInfrastructureReady")
121+
122+
return predicates.Any(scheme, log, ClusterPausedTransitions(scheme, log), ClusterUpdateInfraReady(scheme, log))
123+
}

0 commit comments

Comments
 (0)