Skip to content

Commit a300451

Browse files
committed
fix comments
1 parent b203ca8 commit a300451

File tree

10 files changed

+58
-113
lines changed

10 files changed

+58
-113
lines changed

cloud/scope/firewall.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
"k8s.io/client-go/util/retry"
22+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2223
"sigs.k8s.io/cluster-api/util/patch"
2324
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2425

@@ -34,12 +35,14 @@ type FirewallScope struct {
3435
PatchHelper *patch.Helper
3536
LinodeClient LinodeClient
3637
LinodeFirewall *infrav1alpha2.LinodeFirewall
38+
Cluster *clusterv1.Cluster
3739
}
3840

3941
// FirewallScopeParams defines the input parameters used to create a new Scope.
4042
type FirewallScopeParams struct {
4143
Client K8sClient
4244
LinodeFirewall *infrav1alpha2.LinodeFirewall
45+
Cluster *clusterv1.Cluster
4346
}
4447

4548
func validateFirewallScopeParams(params FirewallScopeParams) error {
@@ -75,6 +78,7 @@ func NewFirewallScope(ctx context.Context, linodeClientConfig ClientConfig, para
7578
LinodeClient: linodeClient,
7679
LinodeFirewall: params.LinodeFirewall,
7780
PatchHelper: helper,
81+
Cluster: params.Cluster,
7882
}, nil
7983
}
8084

cloud/scope/placement_group.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323

24+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2425
"sigs.k8s.io/cluster-api/util/patch"
2526
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2627

@@ -36,12 +37,14 @@ type PlacementGroupScope struct {
3637
PatchHelper *patch.Helper
3738
LinodeClient LinodeClient
3839
LinodePlacementGroup *infrav1alpha2.LinodePlacementGroup
40+
Cluster *clusterv1.Cluster
3941
}
4042

4143
// PlacementGroupScopeParams defines the input parameters used to create a new Scope.
4244
type PlacementGroupScopeParams struct {
4345
Client K8sClient
4446
LinodePlacementGroup *infrav1alpha2.LinodePlacementGroup
47+
Cluster *clusterv1.Cluster
4548
}
4649

4750
func validatePlacementGroupScope(params PlacementGroupScopeParams) error {
@@ -118,6 +121,7 @@ func NewPlacementGroupScope(ctx context.Context, linodeClientConfig ClientConfig
118121
LinodeClient: linodeClient,
119122
LinodePlacementGroup: params.LinodePlacementGroup,
120123
PatchHelper: helper,
124+
Cluster: params.Cluster,
121125
}, nil
122126
}
123127

cloud/scope/vpc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323

24+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2425
"sigs.k8s.io/cluster-api/util/patch"
2526
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2627

@@ -36,12 +37,14 @@ type VPCScope struct {
3637
PatchHelper *patch.Helper
3738
LinodeClient LinodeClient
3839
LinodeVPC *infrav1alpha2.LinodeVPC
40+
Cluster *clusterv1.Cluster
3941
}
4042

4143
// VPCScopeParams defines the input parameters used to create a new Scope.
4244
type VPCScopeParams struct {
4345
Client K8sClient
4446
LinodeVPC *infrav1alpha2.LinodeVPC
47+
Cluster *clusterv1.Cluster
4548
}
4649

4750
func validateVPCScopeParams(params VPCScopeParams) error {
@@ -77,6 +80,7 @@ func NewVPCScope(ctx context.Context, linodeClientConfig ClientConfig, params VP
7780
LinodeClient: linodeClient,
7881
LinodeVPC: params.LinodeVPC,
7982
PatchHelper: helper,
83+
Cluster: params.Cluster,
8084
}, nil
8185
}
8286

internal/controller/linodecluster_controller.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ func (r *LinodeClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
115115
return ctrl.Result{}, fmt.Errorf("failed to create cluster scope: %w", err)
116116
}
117117

118-
if isPaused, _, err := paused.EnsurePausedCondition(ctx, clusterScope.Client, clusterScope.Cluster, clusterScope.LinodeCluster); err != nil {
119-
if isPaused {
120-
logger.Info("linodeCluster or linked cluster is marked as paused, won't reconcile.")
121-
return ctrl.Result{}, nil
122-
}
118+
isPaused, _, err := paused.EnsurePausedCondition(ctx, clusterScope.Client, clusterScope.Cluster, clusterScope.LinodeCluster)
119+
if err != nil {
123120
return ctrl.Result{}, err
124121
}
125-
126-
if reconciler.IsPaused(cluster, linodeCluster) {
122+
if isPaused {
127123
logger.Info("linodeCluster or linked cluster is marked as paused, won't reconcile.")
128124
return ctrl.Result{}, nil
129125
}
@@ -132,8 +128,8 @@ func (r *LinodeClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
132128
}
133129

134130
func (r *LinodeClusterReconciler) reconcileVPCLabel(ctx context.Context, clusterScope *scope.ClusterScope, logger logr.Logger) error {
135-
if clusterScope.LinodeCluster.Spec.VPCRef == nil {
136-
logger.Info("label reconciliation is skipped due to missing VPC ref")
131+
if clusterScope.Cluster == nil || clusterScope.LinodeCluster.Spec.VPCRef == nil {
132+
logger.Info("label reconciliation is skipped due to missing VPC ref/Cluster ref")
137133
return nil
138134
}
139135
linodeVPC := infrav1alpha2.LinodeVPC{
@@ -152,7 +148,7 @@ func (r *LinodeClusterReconciler) reconcileVPCLabel(ctx context.Context, cluster
152148
if _, ok := labels[clusterv1.ClusterNameLabel]; ok {
153149
return nil
154150
}
155-
labels[clusterv1.ClusterNameLabel] = clusterScope.LinodeCluster.Name
151+
labels[clusterv1.ClusterNameLabel] = clusterScope.Cluster.Name
156152
linodeVPC.SetLabels(labels)
157153
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
158154
return clusterScope.Client.Update(ctx, &linodeVPC)
@@ -180,9 +176,11 @@ func (r *LinodeClusterReconciler) reconcile(
180176
}()
181177

182178
labels := map[string]string{
183-
clusterv1.ClusterNameLabel: clusterScope.LinodeCluster.Name,
184179
clusterv1.MachineControlPlaneLabel: "",
185180
}
181+
if clusterScope.Cluster != nil {
182+
labels[clusterv1.ClusterNameLabel] = clusterScope.Cluster.Name
183+
}
186184
if err := r.TracedClient().List(ctx, &clusterScope.LinodeMachines, client.InNamespace(clusterScope.LinodeCluster.Namespace), client.MatchingLabels(labels)); err != nil {
187185
return res, err
188186
}

internal/controller/linodefirewall_controller.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333
kutil "sigs.k8s.io/cluster-api/util"
3434
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
35+
"sigs.k8s.io/cluster-api/util/paused"
3536
"sigs.k8s.io/cluster-api/util/predicates"
3637
ctrl "sigs.k8s.io/controller-runtime"
3738
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -79,34 +80,37 @@ func (r *LinodeFirewallReconciler) Reconcile(ctx context.Context, req ctrl.Reque
7980
return ctrl.Result{}, client.IgnoreNotFound(err)
8081
}
8182
var err error
83+
var cluster *clusterv1.Cluster
84+
if _, ok := linodeFirewall.ObjectMeta.Labels[clusterv1.ClusterNameLabel]; ok {
85+
cluster, err = kutil.GetClusterFromMetadata(ctx, r.TracedClient(), linodeFirewall.ObjectMeta)
86+
if err != nil {
87+
log.Error(err, "failed to fetch cluster from metadata")
88+
return ctrl.Result{}, err
89+
}
90+
}
8291
// Create the firewall scope.
8392
fwScope, err := scope.NewFirewallScope(
8493
ctx,
8594
r.LinodeClientConfig,
8695
scope.FirewallScopeParams{
8796
Client: r.TracedClient(),
8897
LinodeFirewall: linodeFirewall,
98+
Cluster: cluster,
8999
})
90100
if err != nil {
91101
log.Error(err, "failed to create firewall scope")
92102

93103
return ctrl.Result{}, fmt.Errorf("failed to create cluster scope: %w", err)
94104
}
95105

96-
var cluster *clusterv1.Cluster
97-
if _, ok := linodeFirewall.ObjectMeta.Labels[clusterv1.ClusterNameLabel]; ok {
98-
cluster, err = kutil.GetClusterFromMetadata(ctx, r.TracedClient(), linodeFirewall.ObjectMeta)
99-
if err != nil {
100-
log.Error(err, "failed to fetch cluster from metadata")
101-
return ctrl.Result{}, err
102-
}
106+
isPaused, _, err := paused.EnsurePausedCondition(ctx, fwScope.Client, fwScope.Cluster, fwScope.LinodeFirewall)
107+
if err != nil {
108+
return ctrl.Result{}, err
103109
}
104-
105-
if reconciler.IsPaused(cluster, linodeFirewall) {
110+
if isPaused {
106111
log.Info("linodefirewall or linked cluster is paused, skipping reconciliation")
107112
return ctrl.Result{}, nil
108113
}
109-
110114
return r.reconcile(ctx, log, fwScope)
111115
}
112116

internal/controller/linodemachine_controller.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,11 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques
168168
return ctrl.Result{}, fmt.Errorf("failed to create machine scope: %w", err)
169169
}
170170

171-
if isPaused, _, err := paused.EnsurePausedCondition(ctx, machineScope.Client, machineScope.Cluster, machineScope.LinodeMachine); err != nil {
172-
if isPaused {
173-
log.Info("LinodeMachine or linked cluster is marked as paused, won't reconcile.")
174-
return ctrl.Result{}, nil
175-
}
171+
isPaused, _, err := paused.EnsurePausedCondition(ctx, machineScope.Client, machineScope.Cluster, machineScope.LinodeMachine)
172+
if err != nil {
176173
return ctrl.Result{}, err
177174
}
178-
179-
// Stop if paused
180-
if reconciler.IsPaused(machineScope.Cluster, machineScope.Machine) {
175+
if isPaused {
181176
log.Info("LinodeMachine or linked cluster is marked as paused, won't reconcile.")
182177
return ctrl.Result{}, nil
183178
}
@@ -208,7 +203,7 @@ func (r *LinodeMachineReconciler) reconcileFirewallLabel(ctx context.Context, lo
208203
if _, ok := labels[clusterv1.ClusterNameLabel]; ok {
209204
return nil
210205
}
211-
labels[clusterv1.ClusterNameLabel] = machineScope.Machine.Labels[clusterv1.ClusterNameLabel]
206+
labels[clusterv1.ClusterNameLabel] = machineScope.Cluster.Name
212207
linodeFW.SetLabels(labels)
213208
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
214209
return machineScope.Client.Update(ctx, &linodeFW)
@@ -237,7 +232,7 @@ func (r *LinodeMachineReconciler) reconcilePGLabel(ctx context.Context, logger l
237232
if _, ok := labels[clusterv1.ClusterNameLabel]; ok {
238233
return nil
239234
}
240-
labels[clusterv1.ClusterNameLabel] = machineScope.Machine.Labels[clusterv1.ClusterNameLabel]
235+
labels[clusterv1.ClusterNameLabel] = machineScope.Cluster.Name
241236
linodePG.ObjectMeta.SetLabels(labels)
242237
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
243238
return machineScope.Client.Update(ctx, &linodePG)
@@ -247,6 +242,10 @@ func (r *LinodeMachineReconciler) reconcilePGLabel(ctx context.Context, logger l
247242
// reconcileLabel adds the cluster-name label to placement groups and firewalls that are difference in the machine spec
248243
// to ensure that they are represented as part of the cluster
249244
func (r *LinodeMachineReconciler) reconcileLabels(ctx context.Context, logger logr.Logger, machineScope *scope.MachineScope) error {
245+
if machineScope.Cluster == nil {
246+
logger.Info("Skipping firewall and pg labels due to missing capi cluster ref")
247+
return nil
248+
}
250249
if err := r.reconcilePGLabel(ctx, logger, machineScope); err != nil {
251250
return fmt.Errorf("failed to label referenced placement group: %w", err)
252251
}

internal/controller/linodeplacementgroup_controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3535
kutil "sigs.k8s.io/cluster-api/util"
3636
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
37+
"sigs.k8s.io/cluster-api/util/paused"
3738
"sigs.k8s.io/cluster-api/util/predicates"
3839
ctrl "sigs.k8s.io/controller-runtime"
3940
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -102,15 +103,19 @@ func (r *LinodePlacementGroupReconciler) Reconcile(ctx context.Context, req ctrl
102103
scope.PlacementGroupScopeParams{
103104
Client: r.TracedClient(),
104105
LinodePlacementGroup: linodeplacementgroup,
106+
Cluster: cluster,
105107
},
106108
)
107109
if err != nil {
108110
log.Error(err, "Failed to create Placement Group scope")
109111

110112
return ctrl.Result{}, fmt.Errorf("failed to create Placement Group scope: %w", err)
111113
}
112-
113-
if reconciler.IsPaused(cluster, linodeplacementgroup) {
114+
isPaused, _, err := paused.EnsurePausedCondition(ctx, pgScope.Client, pgScope.Cluster, pgScope.LinodePlacementGroup)
115+
if err != nil {
116+
return ctrl.Result{}, err
117+
}
118+
if isPaused {
114119
log.Info("linodeplacementgroup or linked cluster is paused, skipping reconciliation")
115120
return ctrl.Result{}, nil
116121
}

internal/controller/linodevpc_controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3434
kutil "sigs.k8s.io/cluster-api/util"
3535
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
36+
"sigs.k8s.io/cluster-api/util/paused"
3637
"sigs.k8s.io/cluster-api/util/predicates"
3738
ctrl "sigs.k8s.io/controller-runtime"
3839
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -106,15 +107,19 @@ func (r *LinodeVPCReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
106107
scope.VPCScopeParams{
107108
Client: r.TracedClient(),
108109
LinodeVPC: linodeVPC,
110+
Cluster: cluster,
109111
},
110112
)
111113
if err != nil {
112114
log.Error(err, "Failed to create VPC scope")
113115

114116
return ctrl.Result{}, fmt.Errorf("failed to create VPC scope: %w", err)
115117
}
116-
117-
if reconciler.IsPaused(cluster, linodeVPC) {
118+
isPaused, _, err := paused.EnsurePausedCondition(ctx, vpcScope.Client, vpcScope.Cluster, vpcScope.LinodeVPC)
119+
if err != nil {
120+
return ctrl.Result{}, err
121+
}
122+
if isPaused {
118123
log.Info("linodeVPC or linked cluster is paused, skipping reconciliation")
119124
return ctrl.Result{}, nil
120125
}

util/reconciler/annotation_test.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

util/reconciler/annotations.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)