Skip to content

Commit 7632a00

Browse files
committed
Cleanup KCP code: variable/func renames, func order, UpToDate result
struct
1 parent fb8c589 commit 7632a00

File tree

13 files changed

+143
-210
lines changed

13 files changed

+143
-210
lines changed

api/bootstrap/kubeadm/v1beta2/kubeadm_types.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,7 @@ type ClusterConfiguration struct {
174174
CertificatesDir string `json:"certificatesDir,omitempty"`
175175

176176
// imageRepository sets the container registry to pull images from.
177-
// * If not set, the default registry of kubeadm will be used, i.e.
178-
// * registry.k8s.io (new registry): >= v1.22.17, >= v1.23.15, >= v1.24.9, >= v1.25.0
179-
// * k8s.gcr.io (old registry): all older versions
180-
// Please note that when imageRepository is not set we don't allow upgrades to
181-
// versions >= v1.22.0 which use the old registry (k8s.gcr.io). Please use
182-
// a newer patch version with the new registry instead (i.e. >= v1.22.17,
183-
// >= v1.23.15, >= v1.24.9, >= v1.25.0).
184-
// * If the version is a CI build (kubernetes version starts with `ci/` or `ci-cross/`)
185-
// `gcr.io/k8s-staging-ci-images` will be used as a default for control plane components
186-
// and for kube-proxy, while `registry.k8s.io` will be used for all the other images.
177+
// If not set, the default registry of kubeadm will be used (registry.k8s.io).
187178
// +optional
188179
// +kubebuilder:validation:MinLength=1
189180
// +kubebuilder:validation:MaxLength=512

api/controlplane/kubeadm/v1beta2/kubeadm_control_plane_types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,6 @@ type KubeadmControlPlaneSpec struct {
420420
Replicas *int32 `json:"replicas,omitempty"`
421421

422422
// version defines the desired Kubernetes version.
423-
// Please note that if kubeadmConfigSpec.ClusterConfiguration.imageRepository is not set
424-
// we don't allow upgrades to versions >= v1.22.0 for which kubeadm uses the old registry (k8s.gcr.io).
425-
// Please use a newer patch version with the new registry instead. The default registries of kubeadm are:
426-
// * registry.k8s.io (new registry): >= v1.22.17, >= v1.23.15, >= v1.24.9, >= v1.25.0
427-
// * k8s.gcr.io (old registry): all older versions
428423
// +required
429424
// +kubebuilder:validation:MinLength=1
430425
// +kubebuilder:validation:MaxLength=256

bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanes.yaml

Lines changed: 2 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanetemplates.yaml

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/internal/control_plane.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ type ControlPlane struct {
5050
Machines collections.Machines
5151
machinesPatchHelpers map[string]*patch.Helper
5252

53-
machinesNotUptoDate collections.Machines
54-
machinesNotUptoDateLogMessages map[string][]string
55-
machinesNotUptoDateConditionMessages map[string][]string
53+
machinesNotUptoDate collections.Machines
54+
machinesUpToDateResults map[string]UpToDateResult
5655

5756
// reconciliationTime is the time of the current reconciliation, and should be used for all "now" calculations
5857
reconciliationTime metav1.Time
@@ -84,6 +83,11 @@ type ControlPlane struct {
8483
DeletingMessage string
8584
}
8685

86+
type UpToDateResult struct {
87+
LogMessages []string
88+
ConditionMessages []string
89+
}
90+
8791
// PreflightCheckResults contains description about pre flight check results blocking machines creation or deletion.
8892
type PreflightCheckResults struct {
8993
// HasDeletingMachine reports true if preflight check detected a deleting machine.
@@ -98,7 +102,7 @@ type PreflightCheckResults struct {
98102

99103
// NewControlPlane returns an instantiated ControlPlane.
100104
func NewControlPlane(ctx context.Context, managementCluster ManagementCluster, client client.Client, cluster *clusterv1.Cluster, kcp *controlplanev1.KubeadmControlPlane, ownedMachines collections.Machines) (*ControlPlane, error) {
101-
infraObjects, err := getInfraResources(ctx, client, ownedMachines)
105+
infraMachines, err := getInfraMachines(ctx, client, ownedMachines)
102106
if err != nil {
103107
return nil, err
104108
}
@@ -118,32 +122,29 @@ func NewControlPlane(ctx context.Context, managementCluster ManagementCluster, c
118122
// Select machines that should be rolled out because of an outdated configuration or because rolloutAfter/Before expired.
119123
reconciliationTime := metav1.Now()
120124
machinesNotUptoDate := make(collections.Machines, len(ownedMachines))
121-
machinesNotUptoDateLogMessages := map[string][]string{}
122-
machinesNotUptoDateConditionMessages := map[string][]string{}
125+
machinesUpToDateResults := map[string]UpToDateResult{}
123126
for _, m := range ownedMachines {
124-
upToDate, logMessages, conditionMessages, err := UpToDate(m, kcp, &reconciliationTime, infraObjects, kubeadmConfigs)
127+
upToDate, upToDateResult, err := UpToDate(m, kcp, &reconciliationTime, infraMachines, kubeadmConfigs)
125128
if err != nil {
126129
return nil, err
127130
}
128131
if !upToDate {
129132
machinesNotUptoDate.Insert(m)
130-
machinesNotUptoDateLogMessages[m.Name] = logMessages
131-
machinesNotUptoDateConditionMessages[m.Name] = conditionMessages
133+
machinesUpToDateResults[m.Name] = *upToDateResult
132134
}
133135
}
134136

135137
return &ControlPlane{
136-
KCP: kcp,
137-
Cluster: cluster,
138-
Machines: ownedMachines,
139-
machinesPatchHelpers: patchHelpers,
140-
machinesNotUptoDate: machinesNotUptoDate,
141-
machinesNotUptoDateLogMessages: machinesNotUptoDateLogMessages,
142-
machinesNotUptoDateConditionMessages: machinesNotUptoDateConditionMessages,
143-
KubeadmConfigs: kubeadmConfigs,
144-
InfraResources: infraObjects,
145-
reconciliationTime: reconciliationTime,
146-
managementCluster: managementCluster,
138+
KCP: kcp,
139+
Cluster: cluster,
140+
Machines: ownedMachines,
141+
machinesPatchHelpers: patchHelpers,
142+
machinesNotUptoDate: machinesNotUptoDate,
143+
machinesUpToDateResults: machinesUpToDateResults,
144+
KubeadmConfigs: kubeadmConfigs,
145+
InfraResources: infraMachines,
146+
reconciliationTime: reconciliationTime,
147+
managementCluster: managementCluster,
147148
}, nil
148149
}
149150

@@ -256,15 +257,15 @@ func (c *ControlPlane) GetKubeadmConfig(machineName string) (*bootstrapv1.Kubead
256257
}
257258

258259
// MachinesNeedingRollout return a list of machines that need to be rolled out.
259-
func (c *ControlPlane) MachinesNeedingRollout() (collections.Machines, map[string][]string) {
260+
func (c *ControlPlane) MachinesNeedingRollout() (collections.Machines, map[string]UpToDateResult) {
260261
// Note: Machines already deleted are dropped because they will be replaced by new machines after deletion completes.
261-
return c.machinesNotUptoDate.Filter(collections.Not(collections.HasDeletionTimestamp)), c.machinesNotUptoDateLogMessages
262+
return c.machinesNotUptoDate.Filter(collections.Not(collections.HasDeletionTimestamp)), c.machinesUpToDateResults
262263
}
263264

264265
// NotUpToDateMachines return a list of machines that are not up to date with the control
265266
// plane's configuration.
266-
func (c *ControlPlane) NotUpToDateMachines() (collections.Machines, map[string][]string) {
267-
return c.machinesNotUptoDate, c.machinesNotUptoDateConditionMessages
267+
func (c *ControlPlane) NotUpToDateMachines() (collections.Machines, map[string]UpToDateResult) {
268+
return c.machinesNotUptoDate, c.machinesUpToDateResults
268269
}
269270

270271
// UpToDateMachines returns the machines that are up to date with the control
@@ -273,18 +274,18 @@ func (c *ControlPlane) UpToDateMachines() collections.Machines {
273274
return c.Machines.Difference(c.machinesNotUptoDate)
274275
}
275276

276-
// getInfraResources fetches the external infrastructure resource for each machine in the collection and returns a map of machine.Name -> infraResource.
277-
func getInfraResources(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
277+
// getInfraMachines fetches the InfraMachine for each machine in the collection and returns a map of machine.Name -> InfraMachine.
278+
func getInfraMachines(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
278279
result := map[string]*unstructured.Unstructured{}
279280
for _, m := range machines {
280-
infraObj, err := external.GetObjectFromContractVersionedRef(ctx, cl, m.Spec.InfrastructureRef, m.Namespace)
281+
infraMachine, err := external.GetObjectFromContractVersionedRef(ctx, cl, m.Spec.InfrastructureRef, m.Namespace)
281282
if err != nil {
282283
if apierrors.IsNotFound(errors.Cause(err)) {
283284
continue
284285
}
285-
return nil, errors.Wrapf(err, "failed to retrieve infra obj for machine %q", m.Name)
286+
return nil, errors.Wrapf(err, "failed to retrieve InfraMachine for Machine %s", m.Name)
286287
}
287-
result[m.Name] = infraObj
288+
result[m.Name] = infraMachine
288289
}
289290
return result, nil
290291
}
@@ -297,14 +298,14 @@ func getKubeadmConfigs(ctx context.Context, cl client.Client, machines collectio
297298
if !bootstrapRef.IsDefined() {
298299
continue
299300
}
300-
machineConfig := &bootstrapv1.KubeadmConfig{}
301-
if err := cl.Get(ctx, client.ObjectKey{Name: bootstrapRef.Name, Namespace: m.Namespace}, machineConfig); err != nil {
301+
kubeadmConfig := &bootstrapv1.KubeadmConfig{}
302+
if err := cl.Get(ctx, client.ObjectKey{Name: bootstrapRef.Name, Namespace: m.Namespace}, kubeadmConfig); err != nil {
302303
if apierrors.IsNotFound(errors.Cause(err)) {
303304
continue
304305
}
305-
return nil, errors.Wrapf(err, "failed to retrieve bootstrap config for machine %q", m.Name)
306+
return nil, errors.Wrapf(err, "failed to retrieve KubeadmConfig for Machine %s", m.Name)
306307
}
307-
result[m.Name] = machineConfig
308+
result[m.Name] = kubeadmConfig
308309
}
309310
return result, nil
310311
}

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,12 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, controlPl
467467
}
468468

469469
// Control plane machines rollout due to configuration changes (e.g. upgrades) takes precedence over other operations.
470-
machinesNeedingRollout, machinesNeedingRolloutLogMessages := controlPlane.MachinesNeedingRollout()
470+
machinesNeedingRollout, machinesUpToDateResults := controlPlane.MachinesNeedingRollout()
471471
switch {
472472
case len(machinesNeedingRollout) > 0:
473473
var allMessages []string
474-
for machine, messages := range machinesNeedingRolloutLogMessages {
475-
allMessages = append(allMessages, fmt.Sprintf("Machine %s needs rollout: %s", machine, strings.Join(messages, ",")))
474+
for machine, upToDateResult := range machinesUpToDateResults {
475+
allMessages = append(allMessages, fmt.Sprintf("Machine %s needs rollout: %s", machine, strings.Join(upToDateResult.LogMessages, ",")))
476476
}
477477
log.Info(fmt.Sprintf("Rolling out Control Plane machines: %s", strings.Join(allMessages, ",")), "machinesNeedingRollout", machinesNeedingRollout.Names())
478478
v1beta1conditions.MarkFalse(controlPlane.KCP, controlplanev1.MachinesSpecUpToDateV1Beta1Condition, controlplanev1.RollingUpdateInProgressV1Beta1Reason, clusterv1.ConditionSeverityWarning, "Rolling %d replicas with outdated spec (%d replicas up to date)", len(machinesNeedingRollout), len(controlPlane.Machines)-len(machinesNeedingRollout))
@@ -840,7 +840,7 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
840840
return errors.Wrapf(err, "failed to clean up managedFields of InfrastructureMachine %s", klog.KObj(infraMachine))
841841
}
842842
// Update in-place mutating fields on InfrastructureMachine.
843-
if err := r.updateExternalObject(ctx, infraMachine, controlPlane.KCP, controlPlane.Cluster); err != nil {
843+
if err := r.updateExternalObject(ctx, infraMachine, infraMachine.GroupVersionKind(), controlPlane.KCP, controlPlane.Cluster); err != nil {
844844
return errors.Wrapf(err, "failed to update InfrastructureMachine %s", klog.KObj(infraMachine))
845845
}
846846
}
@@ -849,8 +849,6 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
849849
// Only update the KubeadmConfig if it is already found, otherwise just skip it.
850850
// This could happen e.g. if the cache is not up-to-date yet.
851851
if kubeadmConfigFound {
852-
// Note: Set the GroupVersionKind because updateExternalObject depends on it.
853-
kubeadmConfig.SetGroupVersionKind(bootstrapv1.GroupVersion.WithKind("KubeadmConfig"))
854852
// Cleanup managed fields of all KubeadmConfigs to drop ownership of labels and annotations
855853
// from "manager". We do this so that KubeadmConfigs that are created using the Create method
856854
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
@@ -859,7 +857,7 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
859857
return errors.Wrapf(err, "failed to clean up managedFields of KubeadmConfig %s", klog.KObj(kubeadmConfig))
860858
}
861859
// Update in-place mutating fields on BootstrapConfig.
862-
if err := r.updateExternalObject(ctx, kubeadmConfig, controlPlane.KCP, controlPlane.Cluster); err != nil {
860+
if err := r.updateExternalObject(ctx, kubeadmConfig, bootstrapv1.GroupVersion.WithKind("KubeadmConfig"), controlPlane.KCP, controlPlane.Cluster); err != nil {
863861
return errors.Wrapf(err, "failed to update KubeadmConfig %s", klog.KObj(kubeadmConfig))
864862
}
865863
}
@@ -980,16 +978,17 @@ func (r *KubeadmControlPlaneReconciler) reconcileControlPlaneAndMachinesConditio
980978
}
981979

982980
func reconcileMachineUpToDateCondition(_ context.Context, controlPlane *internal.ControlPlane) {
983-
machinesNotUptoDate, machinesNotUptoDateConditionMessages := controlPlane.NotUpToDateMachines()
981+
machinesNotUptoDate, machinesUpToDateResults := controlPlane.NotUpToDateMachines()
984982
machinesNotUptoDateNames := sets.New(machinesNotUptoDate.Names()...)
985983

986984
for _, machine := range controlPlane.Machines {
987985
if machinesNotUptoDateNames.Has(machine.Name) {
988986
// Note: the code computing the message for KCP's RolloutOut condition is making assumptions on the format/content of this message.
989987
message := ""
990-
if reasons, ok := machinesNotUptoDateConditionMessages[machine.Name]; ok {
991-
for i := range reasons {
992-
reasons[i] = fmt.Sprintf("* %s", reasons[i])
988+
if upToDateResults, ok := machinesUpToDateResults[machine.Name]; ok && len(upToDateResults.ConditionMessages) > 0 {
989+
var reasons []string
990+
for _, conditionMessage := range upToDateResults.ConditionMessages {
991+
reasons = append(reasons, fmt.Sprintf("* %s", conditionMessage))
993992
}
994993
message = strings.Join(reasons, "\n")
995994
}

0 commit comments

Comments
 (0)