Skip to content

Commit e1c1614

Browse files
committed
Fix conditions
1 parent 8324fb2 commit e1c1614

File tree

5 files changed

+79
-39
lines changed

5 files changed

+79
-39
lines changed

cloud/scope/machinepool.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/shared"
3535
expinfrav1 "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
3636
"sigs.k8s.io/cluster-api-provider-gcp/pkg/gcp"
37-
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
38-
patch "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/patch"
37+
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
38+
"sigs.k8s.io/cluster-api/util/patch"
3939
"sigs.k8s.io/controller-runtime/pkg/log"
4040
)
4141

@@ -119,10 +119,7 @@ func (m *MachinePoolScope) getBootstrapData(ctx context.Context) (string, error)
119119
func (m *MachinePoolScope) Zones() []string {
120120
zones := m.MachinePool.Spec.FailureDomains
121121
if len(zones) == 0 {
122-
failureDomains := m.ClusterGetter.FailureDomains()
123-
for zone := range failureDomains {
124-
zones = append(zones, zone)
125-
}
122+
zones = append(zones, m.ClusterGetter.FailureDomains()...)
126123
}
127124
return zones
128125
}
@@ -137,9 +134,9 @@ func (m *MachinePoolScope) PatchObject(ctx context.Context) error {
137134
return m.patchHelper.Patch(
138135
ctx,
139136
m.GCPMachinePool,
140-
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
141-
expinfrav1.MIGReadyCondition,
142-
expinfrav1.InstanceTemplateReadyCondition,
137+
patch.WithOwnedConditions{Conditions: []string{
138+
string(expinfrav1.MIGReadyCondition),
139+
string(expinfrav1.InstanceTemplateReadyCondition),
143140
}})
144141
}
145142

@@ -373,10 +370,7 @@ func (m *MachinePoolScope) InstanceImageSpec(ctx context.Context) *compute.Attac
373370
// FUTURE: Can we dedup with MachinePool InstanceImageSpec - until then we have to keep them in sync manually
374371
spec := m.GCPMachinePool.Spec
375372

376-
version := ""
377-
if m.MachinePool.Spec.Template.Spec.Version != nil {
378-
version = *m.MachinePool.Spec.Template.Spec.Version
379-
}
373+
version := m.MachinePool.Spec.Template.Spec.Version
380374

381375
image := "capi-ubuntu-1804-k8s-" + strings.ReplaceAll(semver.MajorMinor(version), ".", "-")
382376
sourceImage := path.Join("projects", m.ClusterGetter.Project(), "global", "images", "family", image)

exp/api/v1beta1/conditions_consts.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ limitations under the License.
1616

1717
package v1beta1
1818

19-
import clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
19+
import (
20+
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
21+
clusterv1beta2 "sigs.k8s.io/cluster-api/api/core/v1beta2"
22+
)
2023

2124
const (
25+
// ReadyCondition is our overall Ready condition.
26+
ReadyCondition string = "Ready"
27+
2228
// GKEControlPlaneReadyCondition condition reports on the successful reconciliation of GKE control plane.
2329
GKEControlPlaneReadyCondition clusterv1beta1.ConditionType = "GKEControlPlaneReady"
2430
// GKEControlPlaneCreatingCondition condition reports on whether the GKE control plane is creating.
@@ -72,7 +78,7 @@ const (
7278
GKEMachinePoolReconciliationFailedReason = "GKEMachinePoolReconciliationFailed"
7379

7480
// MIGReadyCondition reports on current status of the managed instance group. Ready indicates the group is provisioned.
75-
MIGReadyCondition clusterv1.ConditionType = "ManagedInstanceGroupReady"
81+
MIGReadyCondition clusterv1beta2.ConditionType = "ManagedInstanceGroupReady"
7682
// MIGNotFoundReason used when the managed instance group couldn't be retrieved.
7783
MIGNotFoundReason = "ManagedInstanceGroupNotFound"
7884
// MIGProvisionFailedReason used for failures during managed instance group provisioning.
@@ -81,7 +87,7 @@ const (
8187
MIGDeletionInProgress = "ManagedInstanceGroupDeletionInProgress"
8288

8389
// InstanceTemplateReadyCondition represents the status of an AWSMachinePool's associated Launch Template.
84-
InstanceTemplateReadyCondition clusterv1.ConditionType = "InstanceTemplateReady"
90+
InstanceTemplateReadyCondition clusterv1beta2.ConditionType = "InstanceTemplateReady"
8591
// InstanceTemplateNotFoundReason is used when an associated Launch Template can't be found.
8692
InstanceTemplateNotFoundReason = "InstanceTemplateNotFound"
8793
// InstanceTemplateCreateFailedReason used for failures during Launch Template creation.

exp/api/v1beta1/gcpmachinepool_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222

2323
capg "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
24+
"sigs.k8s.io/cluster-api/util/conditions"
2425
)
2526

2627
// Constants block.
@@ -220,3 +221,16 @@ func (r *GCPMachinePool) GetObjectKind() schema.ObjectKind {
220221
func (r *GCPMachinePoolList) GetObjectKind() schema.ObjectKind {
221222
return &r.TypeMeta
222223
}
224+
225+
// GCPMachinePool implements the conditions.Setter interface.
226+
var _ conditions.Setter = &GCPMachinePool{}
227+
228+
// SetConditions sets conditions for a MachinePool.
229+
func (r *GCPMachinePool) SetConditions(conditions []metav1.Condition) {
230+
r.Status.Conditions = conditions
231+
}
232+
233+
// GetConditions gets conditions for a MachinePool.
234+
func (r *GCPMachinePool) GetConditions() []metav1.Condition {
235+
return r.Status.Conditions
236+
}

exp/controllers/gcpmachinepool_controller.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/google/go-cmp/cmp"
2727
corev1 "k8s.io/api/core/v1"
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
29+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/client-go/tools/record"
3031
"k8s.io/klog/v2"
3132
ctrl "sigs.k8s.io/controller-runtime"
@@ -37,17 +38,17 @@ import (
3738
"sigs.k8s.io/controller-runtime/pkg/predicate"
3839
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3940

40-
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
41+
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4142
"sigs.k8s.io/cluster-api/util/annotations"
42-
"sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions"
43-
v1beta1conditions "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions"
43+
"sigs.k8s.io/cluster-api/util/conditions"
4444
"sigs.k8s.io/cluster-api/util/predicates"
4545

4646
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
4747
"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"
4848
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/instancegroupmanagers"
4949
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/instancetemplates"
5050
expinfrav1 "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
51+
"sigs.k8s.io/cluster-api-provider-gcp/pkg/capiutils"
5152
exputil "sigs.k8s.io/cluster-api/exp/util"
5253
"sigs.k8s.io/controller-runtime/pkg/log"
5354
)
@@ -124,18 +125,12 @@ func (r *GCPMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Reque
124125
return ctrl.Result{}, err
125126
}
126127

127-
// Always close the scope when exiting this function so we can persist any GCPMachine changes.
128+
// Always close the scope when exiting this function so we can persist any GCPMachinePool changes.
128129
defer func() {
129-
// set Ready condition before GCPMachinePool is patched
130-
v1beta1conditions.SetSummary(machinePoolScope.GCPMachinePool,
131-
v1beta1conditions.WithConditions(
132-
expinfrav1.MIGReadyCondition,
133-
expinfrav1.InstanceTemplateReadyCondition,
134-
),
135-
v1beta1conditions.WithStepCounterIfOnly(
136-
expinfrav1.MIGReadyCondition,
137-
expinfrav1.InstanceTemplateReadyCondition,
138-
),
130+
// Compute the Ready condition from the other conditions
131+
conditions.SetSummaryCondition(machinePoolScope.GCPMachinePool, machinePoolScope.GCPMachinePool,
132+
expinfrav1.ReadyCondition,
133+
conditions.ForConditionTypes([]string{string(expinfrav1.MIGReadyCondition), string(expinfrav1.InstanceTemplateReadyCondition)}),
139134
)
140135

141136
if err := machinePoolScope.Close(); err != nil && reterr == nil {
@@ -215,31 +210,52 @@ func (r *GCPMachinePoolReconciler) reconcile(ctx context.Context, machinePoolSco
215210
// Make sure bootstrap data is available and populated
216211
if machinePoolScope.MachinePool.Spec.Template.Spec.Bootstrap.DataSecretName == nil {
217212
log.Info("Bootstrap data secret reference is not yet available")
218-
conditions.MarkFalse(machinePoolScope.GCPMachinePool, expinfrav1.MIGReadyCondition, infrav1.WaitingForBootstrapDataReason, clusterv1.ConditionSeverityInfo, "")
213+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
214+
Type: string(expinfrav1.MIGReadyCondition),
215+
Status: metav1.ConditionFalse,
216+
Reason: infrav1.WaitingForBootstrapDataReason,
217+
Message: "Bootstrap data secret reference is not yet available",
218+
})
219219
return ctrl.Result{}, nil
220220
}
221221

222222
instanceTemplateKey, err := instancetemplates.New(machinePoolScope).Reconcile(ctx)
223223
if err != nil {
224224
log.Error(err, "Error reconciling instanceTemplate")
225+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
226+
Type: string(expinfrav1.InstanceTemplateReadyCondition),
227+
Status: metav1.ConditionFalse,
228+
Reason: expinfrav1.InstanceTemplateReconcileFailedReason,
229+
Message: fmt.Sprintf("Error creating instance template: %v", err),
230+
})
225231
// record.Warnf(machineScope.GCPMachine, "GCPMachineReconcile", "Reconcile error - %v", err)
226-
conditions.MarkUnknown(machinePoolScope.GCPMachinePool, expinfrav1.InstanceTemplateReadyCondition, expinfrav1.InstanceTemplateNotFoundReason, "%s", err.Error())
227232
return ctrl.Result{}, err
228233
}
229234

230235
// set the InstanceTemplateReadyCondition condition
231-
conditions.MarkTrue(machinePoolScope.GCPMachinePool, expinfrav1.InstanceTemplateReadyCondition)
236+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
237+
Type: string(expinfrav1.InstanceTemplateReadyCondition),
238+
Status: metav1.ConditionTrue,
239+
})
232240

233241
igm, err := instancegroupmanagers.New(machinePoolScope).Reconcile(ctx, instanceTemplateKey)
234242
if err != nil {
235243
log.Error(err, "Error reconciling instanceGroupManager")
236244
// record.Warnf(machineScope.GCPMachine, "GCPMachineReconcile", "Reconcile error - %v", err)
237-
conditions.MarkUnknown(machinePoolScope.GCPMachinePool, expinfrav1.MIGReadyCondition, expinfrav1.MIGNotFoundReason, "%s", err.Error())
245+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
246+
Type: string(expinfrav1.MIGReadyCondition),
247+
Status: metav1.ConditionFalse,
248+
Reason: expinfrav1.MIGProvisionFailedReason,
249+
Message: fmt.Sprintf("Error creating instanceGroupManager: %v", err),
250+
})
238251
return ctrl.Result{}, err
239252
}
240253

241254
// set the MIGReadyCondition condition
242-
conditions.MarkTrue(machinePoolScope.GCPMachinePool, expinfrav1.MIGReadyCondition)
255+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
256+
Type: string(expinfrav1.MIGReadyCondition),
257+
Status: metav1.ConditionTrue,
258+
})
243259

244260
igmInstances, err := instancegroupmanagers.New(machinePoolScope).ListInstances(ctx, igm)
245261
if err != nil {
@@ -286,7 +302,12 @@ func (r *GCPMachinePoolReconciler) reconcileDelete(ctx context.Context, machineP
286302
r.Recorder.Eventf(machinePoolScope.GCPMachinePool, corev1.EventTypeWarning, "FailedDelete", "Failed to delete instancegroupmanager: %v", err)
287303

288304
// record.Warnf(machineScope.GCPMachine, "GCPMachineReconcile", "Reconcile error - %v", err)
289-
conditions.MarkUnknown(machinePoolScope.GCPMachinePool, expinfrav1.MIGReadyCondition, expinfrav1.MIGNotFoundReason, "%s", err.Error())
305+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
306+
Type: string(expinfrav1.MIGReadyCondition),
307+
Status: metav1.ConditionUnknown,
308+
Reason: expinfrav1.MIGDeletionInProgress,
309+
Message: fmt.Sprintf("Error deleting instanceGroupManager: %v", err),
310+
})
290311
return err
291312
}
292313

@@ -295,7 +316,12 @@ func (r *GCPMachinePoolReconciler) reconcileDelete(ctx context.Context, machineP
295316
r.Recorder.Eventf(machinePoolScope.GCPMachinePool, corev1.EventTypeWarning, "FailedDelete", "Failed to delete instance template: %v", err)
296317

297318
// record.Warnf(machineScope.GCPMachine, "GCPMachineReconcile", "Reconcile error - %v", err)
298-
conditions.MarkUnknown(machinePoolScope.GCPMachinePool, expinfrav1.InstanceTemplateReadyCondition, expinfrav1.InstanceTemplateReconcileFailedReason, "%s", err.Error())
319+
conditions.Set(machinePoolScope.GCPMachinePool, metav1.Condition{
320+
Type: string(expinfrav1.InstanceTemplateReadyCondition),
321+
Status: metav1.ConditionUnknown,
322+
Reason: expinfrav1.InstanceTemplateReconcileFailedReason,
323+
Message: fmt.Sprintf("Error deleting instanceTemplate: %v", err),
324+
})
299325
return err
300326
}
301327

exp/controllers/gcpmanagedmachinepool_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
4343
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
4444
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
45+
exputil "sigs.k8s.io/cluster-api/exp/util"
4546
"sigs.k8s.io/cluster-api/util"
4647
"sigs.k8s.io/cluster-api/util/predicates"
4748
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -199,7 +200,6 @@ func getMachinePoolByName(ctx context.Context, c client.Client, namespace, name
199200
return m, nil
200201
}
201202

202-
203203
//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=gcpmanagedmachinepools,verbs=get;list;watch;create;update;patch;delete
204204
//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=gcpmanagedmachinepools/status,verbs=get;update;patch
205205
//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=gcpmanagedmachinepools/finalizers,verbs=update
@@ -225,7 +225,7 @@ func (r *GCPManagedMachinePoolReconciler) Reconcile(ctx context.Context, req ctr
225225
}
226226

227227
// Get the machine pool
228-
machinePool, err := util.GetOwnerMachinePool(ctx, r.Client, gcpManagedMachinePool.ObjectMeta)
228+
machinePool, err := exputil.GetOwnerMachinePool(ctx, r.Client, gcpManagedMachinePool.ObjectMeta)
229229
if err != nil {
230230
log.Error(err, "Failed to retrieve owner MachinePool from the API Server")
231231
return ctrl.Result{}, err

0 commit comments

Comments
 (0)