Skip to content

Commit 306e8f0

Browse files
add more conditions
1 parent 910f46d commit 306e8f0

File tree

6 files changed

+84
-19
lines changed

6 files changed

+84
-19
lines changed

controlplane/kubeadm/api/v1alpha3/condition_consts.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const (
2828
const (
2929
// CertificatesAvailableCondition documents that cluster certificates were generated as part of the
3030
// processing of a a KubeadmControlPlane object.
31-
// IMPORTANT: This condition won't be re-created after clusterctl move.
3231
CertificatesAvailableCondition clusterv1.ConditionType = "CertificatesAvailable"
3332

3433
// CertificatesGenerationFailedReason (Severity=Warning) documents a KubeadmControlPlane controller detecting
@@ -42,7 +41,28 @@ const (
4241
// and so the control plane is available and an API server instance is ready for processing requests.
4342
AvailableCondition clusterv1.ConditionType = "Available"
4443

45-
// WaitingForKubeadmInitReason (Severity=Info) a KubeadmControlPlane object waiting for the first
44+
// WaitingForKubeadmInitReason (Severity=Info) documents a KubeadmControlPlane object waiting for the first
4645
// control plane instance to complete the kubeadm init operation.
4746
WaitingForKubeadmInitReason = "WaitingForKubeadmInit"
4847
)
48+
49+
const (
50+
// MachinesSpecUpToDateCondition documents that the spec of the machines controlled by the KubeadmControlPlane
51+
// is up to date. Whe this condition is false, the KubeadmControlPlane is executing a rolling upgrade.
52+
MachinesSpecUpToDateCondition clusterv1.ConditionType = "MachinesSpecUpToDate"
53+
54+
// RollingUpdateInProgressReason (Severity=Warning) documents a KubeadmControlPlane object executing a
55+
// rolling upgrade for aligning the machines spec to the desired state.
56+
RollingUpdateInProgressReason = "RollingUpdateInProgress"
57+
)
58+
59+
const (
60+
// ResizedCondition documents a KubeadmControlPlane that is resizing the set of controlled machines.
61+
ResizedCondition clusterv1.ConditionType = "Resized"
62+
63+
// ScalingUpReason (Severity=Info) documents a KubeadmControlPlane that is increasing the number of replicas.
64+
ScalingUpReason = "ScalingUp"
65+
66+
// ScalingDownReason (Severity=Info) documents a KubeadmControlPlane that is decreasing the number of replicas.
67+
ScalingDownReason = "ScalingDown"
68+
)

controlplane/kubeadm/controllers/controller.go

+27-14
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,6 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(req ctrl.Request) (res ctrl.Re
162162
}
163163
}
164164

165-
// Always update the readyCondition.
166-
conditions.SetSummary(kcp,
167-
conditions.WithConditions(
168-
controlplanev1.MachinesReadyCondition,
169-
controlplanev1.AvailableCondition,
170-
controlplanev1.CertificatesAvailableCondition,
171-
),
172-
)
173-
174165
// Always attempt to update status.
175166
if err := r.updateStatus(ctx, kcp, cluster); err != nil {
176167
var connFailure *internal.RemoteClusterConnectionError
@@ -182,6 +173,17 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(req ctrl.Request) (res ctrl.Re
182173
}
183174
}
184175

176+
// Always update the readyCondition.
177+
conditions.SetSummary(kcp,
178+
conditions.WithConditions(
179+
controlplanev1.MachinesSpecUpToDateCondition,
180+
controlplanev1.ResizedCondition,
181+
controlplanev1.MachinesReadyCondition,
182+
controlplanev1.AvailableCondition,
183+
controlplanev1.CertificatesAvailableCondition,
184+
),
185+
)
186+
185187
// Always attempt to Patch the KubeadmControlPlane object and status after each reconciliation.
186188
if err := patchHelper.Patch(ctx, kcp); err != nil {
187189
logger.Error(err, "Failed to patch KubeadmControlPlane")
@@ -269,14 +271,25 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, cluster *
269271
controlPlane := internal.NewControlPlane(cluster, kcp, ownedMachines)
270272

271273
// Aggregate the operational state of all the machines; while aggregating we are adding the
272-
// source ref to the aggregate reason (reason@machine/name) so the problem can be easily tracked down to its source.
274+
// source ref (reason@machine/name) so the problem can be easily tracked down to its source machine.
273275
conditions.SetAggregate(controlPlane.KCP, controlplanev1.MachinesReadyCondition, ownedMachines.ConditionGetters(), conditions.AddSourceRef())
274276

275-
requireUpgrade := controlPlane.MachinesNeedingUpgrade()
276-
// Upgrade takes precedence over other operations
277-
if len(requireUpgrade) > 0 {
278-
logger.Info("Upgrading Control Plane")
277+
// Control plane machines rollout due to configuration changes (e.g. upgrades) takes precedence over other operations.
278+
needRollout := controlPlane.MachinesNeedingRollout()
279+
switch {
280+
case len(needRollout) > 0:
281+
logger.Info("Rolling out Control Plane machines")
282+
// NOTE: we are using Status.UpdatedReplicas from the previous reconciliation only to provide a meaningful message
283+
// and this does not influence any reconciliation logic.
284+
conditions.MarkFalse(controlPlane.KCP, controlplanev1.MachinesSpecUpToDateCondition, controlplanev1.RollingUpdateInProgressReason, clusterv1.ConditionSeverityWarning, "Rolling %d replicas with outdated spec (%d replicas up to date)", len(needRollout), kcp.Status.UpdatedReplicas)
279285
return r.upgradeControlPlane(ctx, cluster, kcp, controlPlane)
286+
default:
287+
// make sure last upgrade operation is marked as completed.
288+
// NOTE: we are checking the condition already exists in order to avoid to set this condition at the first
289+
// reconciliation/before a rolling upgrade actually starts.
290+
if conditions.Has(controlPlane.KCP, controlplanev1.MachinesSpecUpToDateCondition) {
291+
conditions.MarkTrue(controlPlane.KCP, controlplanev1.MachinesSpecUpToDateCondition)
292+
}
280293
}
281294

282295
// If we've made it this far, we can assume that all ownedMachines are up to date

controlplane/kubeadm/controllers/scale.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (r *KubeadmControlPlaneReconciler) scaleDownControlPlane(
146146

147147
func selectMachineForScaleDown(controlPlane *internal.ControlPlane) (*clusterv1.Machine, error) {
148148
machines := controlPlane.Machines
149-
if needingUpgrade := controlPlane.MachinesNeedingUpgrade(); needingUpgrade.Len() > 0 {
149+
if needingUpgrade := controlPlane.MachinesNeedingRollout(); needingUpgrade.Len() > 0 {
150150
machines = needingUpgrade
151151
}
152152
return controlPlane.MachineInFailureDomainWithMostMachines(machines)

controlplane/kubeadm/controllers/status.go

+18
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,30 @@ func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, kcp *c
4545
kcp.Status.UpdatedReplicas = int32(len(currentMachines))
4646

4747
replicas := int32(len(ownedMachines))
48+
desiredReplicas := *kcp.Spec.Replicas
4849

4950
// set basic data that does not require interacting with the workload cluster
5051
kcp.Status.Replicas = replicas
5152
kcp.Status.ReadyReplicas = 0
5253
kcp.Status.UnavailableReplicas = replicas
5354

55+
switch {
56+
// We are scaling up
57+
case replicas < desiredReplicas:
58+
conditions.MarkFalse(kcp, controlplanev1.ResizedCondition, controlplanev1.ScalingUpReason, clusterv1.ConditionSeverityWarning, "Scaling up to %d replicas (actual %d)", desiredReplicas, replicas)
59+
// We are scaling down
60+
case replicas > desiredReplicas:
61+
conditions.MarkFalse(kcp, controlplanev1.ResizedCondition, controlplanev1.ScalingDownReason, clusterv1.ConditionSeverityWarning, "Scaling down to %d replicas (actual %d)", desiredReplicas, replicas)
62+
default:
63+
// make sure last resize operation is marked as completed.
64+
// NOTE: we are checking the number of machines ready so we report resize completed only when the machines
65+
// are actually provisioned (vs reporting completed immediately after the last machine object is created).
66+
readyMachines := ownedMachines.Filter(machinefilters.IsReady())
67+
if int32(len(readyMachines)) == replicas {
68+
conditions.MarkTrue(kcp, controlplanev1.ResizedCondition)
69+
}
70+
}
71+
5472
// Return early if the deletion timestamp is set, we don't want to try to connect to the workload cluster.
5573
if !kcp.DeletionTimestamp.IsZero() {
5674
return nil

controlplane/kubeadm/internal/control_plane.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ func (c *ControlPlane) EtcdImageData() (string, string) {
9393
return "", ""
9494
}
9595

96-
// MachinesNeedingUpgrade return a list of machines that need to be upgraded.
97-
func (c *ControlPlane) MachinesNeedingUpgrade() FilterableMachineCollection {
96+
// MachinesNeedingRollout return a list of machines that need to be rolled out due to configuration changes.
97+
//
98+
// NOTE: Expiration of the spec.UpgradeAfter value forces inclusion of all the machines in this set even if
99+
// no changes have been made to the KubeadmControlPlane.
100+
func (c *ControlPlane) MachinesNeedingRollout() FilterableMachineCollection {
98101
now := metav1.Now()
99102
if c.KCP.Spec.UpgradeAfter != nil && c.KCP.Spec.UpgradeAfter.Before(&now) {
100103
return c.Machines.AnyFilter(

controlplane/kubeadm/internal/machinefilters/machine_filters.go

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
"k8s.io/apimachinery/pkg/labels"
2222
"k8s.io/apimachinery/pkg/selection"
23+
"sigs.k8s.io/cluster-api/util/conditions"
2324

2425
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2526
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3"
@@ -148,6 +149,16 @@ func MatchesConfigurationHash(configHash string) Func {
148149
}
149150
}
150151

152+
// IsReady returns a filter to find all machines with the ReadyCondition equals to True.
153+
func IsReady() Func {
154+
return func(machine *clusterv1.Machine) bool {
155+
if machine == nil {
156+
return false
157+
}
158+
return conditions.IsTrue(machine, clusterv1.ReadyCondition)
159+
}
160+
}
161+
151162
// OlderThan returns a filter to find all machines
152163
// that have a CreationTimestamp earlier than the given time.
153164
func OlderThan(t *metav1.Time) Func {

0 commit comments

Comments
 (0)