Skip to content

Commit e316155

Browse files
Cleanup v1beta1 updateStatus functions
1 parent da7dbf6 commit e316155

15 files changed

+485
-293
lines changed

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,21 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
214214

215215
defer func() {
216216
// Always attempt to update status.
217+
if err := r.updateStatus(ctx, controlPlane); err != nil {
218+
log.Error(err, "Failed to update KubeadmControlPlane status")
219+
reterr = kerrors.NewAggregate([]error{reterr, err})
220+
}
221+
217222
if err := r.updateV1Beta1Status(ctx, controlPlane); err != nil {
218223
var connFailure *internal.RemoteClusterConnectionError
219224
if errors.As(err, &connFailure) {
220225
log.Error(err, "Could not connect to workload cluster to fetch status")
221226
} else {
222-
log.Error(err, "Failed to update KubeadmControlPlane status")
227+
log.Error(err, "Failed to update KubeadmControlPlane deprecated v1beta1 status")
223228
reterr = kerrors.NewAggregate([]error{reterr, err})
224229
}
225230
}
226231

227-
r.updateStatus(ctx, controlPlane)
228-
229232
// Always attempt to Patch the KubeadmControlPlane object and status after each reconciliation.
230233
patchOpts := []patch.Option{}
231234
if reterr == nil {

controlplane/kubeadm/internal/controllers/status.go

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ import (
4242
// updateV1Beta1Status is called after every reconciliation loop in a defer statement to always make sure we have the
4343
// KubeadmControlPlane status up-to-date.
4444
func (r *KubeadmControlPlaneReconciler) updateV1Beta1Status(ctx context.Context, controlPlane *internal.ControlPlane) error {
45-
selector := collections.ControlPlaneSelectorForCluster(controlPlane.Cluster.Name)
46-
// Copy label selector to its status counterpart in string format.
47-
// This is necessary for CRDs including scale subresources.
48-
controlPlane.KCP.Status.Selector = selector.String()
49-
5045
upToDateMachines := controlPlane.UpToDateMachines()
5146
if controlPlane.KCP.Status.Deprecated == nil {
5247
controlPlane.KCP.Status.Deprecated = &controlplanev1.KubeadmControlPlaneDeprecatedStatus{}
@@ -60,7 +55,6 @@ func (r *KubeadmControlPlaneReconciler) updateV1Beta1Status(ctx context.Context,
6055
desiredReplicas := *controlPlane.KCP.Spec.Replicas
6156

6257
// set basic data that does not require interacting with the workload cluster
63-
controlPlane.KCP.Status.Replicas = replicas
6458
controlPlane.KCP.Status.Deprecated.V1Beta1.ReadyReplicas = 0
6559
controlPlane.KCP.Status.Deprecated.V1Beta1.UnavailableReplicas = replicas
6660

@@ -70,11 +64,6 @@ func (r *KubeadmControlPlaneReconciler) updateV1Beta1Status(ctx context.Context,
7064
return nil
7165
}
7266

73-
lowestVersion := controlPlane.Machines.LowestVersion()
74-
if lowestVersion != nil {
75-
controlPlane.KCP.Status.Version = lowestVersion
76-
}
77-
7867
switch {
7968
// We are scaling up
8069
case replicas < desiredReplicas:
@@ -109,51 +98,17 @@ func (r *KubeadmControlPlaneReconciler) updateV1Beta1Status(ctx context.Context,
10998
controlPlane.KCP.Status.Deprecated.V1Beta1.ReadyReplicas = status.ReadyNodes
11099
controlPlane.KCP.Status.Deprecated.V1Beta1.UnavailableReplicas = replicas - status.ReadyNodes
111100

112-
// This only gets initialized once and does not change if the kubeadm config map goes away.
113101
if status.HasKubeadmConfig {
114-
if controlPlane.KCP.Status.Initialization == nil {
115-
controlPlane.KCP.Status.Initialization = &controlplanev1.KubeadmControlPlaneInitializationStatus{}
116-
}
117-
controlPlane.KCP.Status.Initialization.ControlPlaneInitialized = true
118102
v1beta1conditions.MarkTrue(controlPlane.KCP, controlplanev1.AvailableV1Beta1Condition)
119103
}
120-
121-
// Surface lastRemediation data in status.
122-
// LastRemediation is the remediation currently in progress, in any, or the
123-
// most recent of the remediation we are keeping track on machines.
124-
var lastRemediation *RemediationData
125-
126-
if v, ok := controlPlane.KCP.Annotations[controlplanev1.RemediationInProgressAnnotation]; ok {
127-
remediationData, err := RemediationDataFromAnnotation(v)
128-
if err != nil {
129-
return err
130-
}
131-
lastRemediation = remediationData
132-
} else {
133-
for _, m := range controlPlane.Machines.UnsortedList() {
134-
if v, ok := m.Annotations[controlplanev1.RemediationForAnnotation]; ok {
135-
remediationData, err := RemediationDataFromAnnotation(v)
136-
if err != nil {
137-
return err
138-
}
139-
if lastRemediation == nil || lastRemediation.Timestamp.Time.Before(remediationData.Timestamp.Time) {
140-
lastRemediation = remediationData
141-
}
142-
}
143-
}
144-
}
145-
146-
if lastRemediation != nil {
147-
controlPlane.KCP.Status.LastRemediation = lastRemediation.ToStatus()
148-
}
149104
return nil
150105
}
151106

152107
// updateStatus reconciles KubeadmControlPlane's status during the entire lifecycle of the object.
153-
func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, controlPlane *internal.ControlPlane) {
108+
func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, controlPlane *internal.ControlPlane) error {
154109
// If the code failed initializing the control plane, do not update the status.
155110
if controlPlane == nil {
156-
return
111+
return nil
157112
}
158113

159114
// Note: some of the status is set on reconcileControlPlaneAndMachinesConditions (EtcdClusterHealthy, ControlPlaneComponentsHealthy conditions),
@@ -163,6 +118,20 @@ func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, contro
163118
// Note: KCP also sets status on machines in reconcileUnhealthyMachines and reconcileControlPlaneAndMachinesConditions; if for
164119
// any reason those functions are not called before, e.g. an error, this func relies on existing Machine's condition.
165120

121+
// Copy label selector to its status counterpart in string format.
122+
// This is necessary for CRDs including scale subresources.
123+
selector := collections.ControlPlaneSelectorForCluster(controlPlane.Cluster.Name)
124+
controlPlane.KCP.Status.Selector = selector.String()
125+
126+
// Set status.version with the lowest K8s version from CP machines.
127+
lowestVersion := controlPlane.Machines.LowestVersion()
128+
if lowestVersion != nil {
129+
controlPlane.KCP.Status.Version = lowestVersion
130+
}
131+
132+
if err := setControlPlaneInitialized(ctx, controlPlane); err != nil {
133+
return err
134+
}
166135
setReplicas(ctx, controlPlane.KCP, controlPlane.Machines)
167136
setInitializedCondition(ctx, controlPlane.KCP)
168137
setRollingOutCondition(ctx, controlPlane.KCP, controlPlane.Machines)
@@ -173,6 +142,32 @@ func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, contro
173142
setRemediatingCondition(ctx, controlPlane.KCP, controlPlane.MachinesToBeRemediatedByKCP(), controlPlane.UnhealthyMachines())
174143
setDeletingCondition(ctx, controlPlane.KCP, controlPlane.DeletingReason, controlPlane.DeletingMessage)
175144
setAvailableCondition(ctx, controlPlane.KCP, controlPlane.IsEtcdManaged(), controlPlane.EtcdMembers, controlPlane.EtcdMembersAndMachinesAreMatching, controlPlane.Machines)
145+
return setLastRemediation(ctx, controlPlane)
146+
}
147+
148+
// setControlPlaneInitialized surface control plane initialized when it is possible to check that the Kubeadm config exists in the workload cluster;
149+
// this is considered a proxy information about the API Server being up and running and kubeadm init successfully completed.
150+
// Note: This only gets initialized once and does not change if the kubeadm config map goes away.
151+
func setControlPlaneInitialized(ctx context.Context, controlPlane *internal.ControlPlane) error {
152+
if controlPlane.KCP.Status.Initialization == nil || !controlPlane.KCP.Status.Initialization.ControlPlaneInitialized {
153+
workloadCluster, err := controlPlane.GetWorkloadCluster(ctx)
154+
if err != nil {
155+
return errors.Wrap(err, "failed to create remote cluster client")
156+
}
157+
status, err := workloadCluster.ClusterStatus(ctx)
158+
if err != nil {
159+
return err
160+
}
161+
162+
if status.HasKubeadmConfig {
163+
if controlPlane.KCP.Status.Initialization == nil {
164+
controlPlane.KCP.Status.Initialization = &controlplanev1.KubeadmControlPlaneInitializationStatus{}
165+
}
166+
controlPlane.KCP.Status.Initialization.ControlPlaneInitialized = true
167+
v1beta1conditions.MarkTrue(controlPlane.KCP, controlplanev1.AvailableV1Beta1Condition)
168+
}
169+
}
170+
return nil
176171
}
177172

178173
func setReplicas(_ context.Context, kcp *controlplanev1.KubeadmControlPlane, machines collections.Machines) {
@@ -189,6 +184,7 @@ func setReplicas(_ context.Context, kcp *controlplanev1.KubeadmControlPlane, mac
189184
}
190185
}
191186

187+
kcp.Status.Replicas = int32(len(machines))
192188
kcp.Status.ReadyReplicas = ptr.To(readyReplicas)
193189
kcp.Status.AvailableReplicas = ptr.To(availableReplicas)
194190
kcp.Status.UpToDateReplicas = ptr.To(upToDateReplicas)
@@ -768,6 +764,38 @@ func setAvailableCondition(_ context.Context, kcp *controlplanev1.KubeadmControl
768764
})
769765
}
770766

767+
// setLastRemediation surface lastRemediation data in status.
768+
// LastRemediation is the remediation currently in progress, in any, or the
769+
// most recent of the remediation we are keeping track on machines.
770+
func setLastRemediation(_ context.Context, controlPlane *internal.ControlPlane) error {
771+
var lastRemediation *RemediationData
772+
773+
if v, ok := controlPlane.KCP.Annotations[controlplanev1.RemediationInProgressAnnotation]; ok {
774+
remediationData, err := RemediationDataFromAnnotation(v)
775+
if err != nil {
776+
return err
777+
}
778+
lastRemediation = remediationData
779+
} else {
780+
for _, m := range controlPlane.Machines.UnsortedList() {
781+
if v, ok := m.Annotations[controlplanev1.RemediationForAnnotation]; ok {
782+
remediationData, err := RemediationDataFromAnnotation(v)
783+
if err != nil {
784+
return err
785+
}
786+
if lastRemediation == nil || lastRemediation.Timestamp.Time.Before(remediationData.Timestamp.Time) {
787+
lastRemediation = remediationData
788+
}
789+
}
790+
}
791+
}
792+
793+
if lastRemediation != nil {
794+
controlPlane.KCP.Status.LastRemediation = lastRemediation.ToStatus()
795+
}
796+
return nil
797+
}
798+
771799
// shouldSurfaceWhenAvailableTrue defines when a control plane components/etcd issue should surface when
772800
// Available condition is true.
773801
// The main goal of this check is to avoid to surface false negatives/flakes, and thus it requires that

0 commit comments

Comments
 (0)