Skip to content

Commit cd8a2c8

Browse files
authored
Merge pull request #12793 from sbueringer/pr-kcp-refactoring
🌱 Cleanup KCP code: variable/func renames, func order
2 parents 7bd466a + e25a587 commit cd8a2c8

14 files changed

+158
-227
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: 29 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+
machinesNotUpToDateResults map[string]NotUpToDateResult
5655

5756
// reconciliationTime is the time of the current reconciliation, and should be used for all "now" calculations
5857
reconciliationTime metav1.Time
@@ -98,7 +97,7 @@ type PreflightCheckResults struct {
9897

9998
// NewControlPlane returns an instantiated ControlPlane.
10099
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)
100+
infraMachines, err := getInfraMachines(ctx, client, ownedMachines)
102101
if err != nil {
103102
return nil, err
104103
}
@@ -118,32 +117,29 @@ func NewControlPlane(ctx context.Context, managementCluster ManagementCluster, c
118117
// Select machines that should be rolled out because of an outdated configuration or because rolloutAfter/Before expired.
119118
reconciliationTime := metav1.Now()
120119
machinesNotUptoDate := make(collections.Machines, len(ownedMachines))
121-
machinesNotUptoDateLogMessages := map[string][]string{}
122-
machinesNotUptoDateConditionMessages := map[string][]string{}
120+
machinesNotUpToDateResults := map[string]NotUpToDateResult{}
123121
for _, m := range ownedMachines {
124-
upToDate, logMessages, conditionMessages, err := UpToDate(m, kcp, &reconciliationTime, infraObjects, kubeadmConfigs)
122+
upToDate, notUpToDateResult, err := UpToDate(m, kcp, &reconciliationTime, infraMachines, kubeadmConfigs)
125123
if err != nil {
126124
return nil, err
127125
}
128126
if !upToDate {
129127
machinesNotUptoDate.Insert(m)
130-
machinesNotUptoDateLogMessages[m.Name] = logMessages
131-
machinesNotUptoDateConditionMessages[m.Name] = conditionMessages
128+
machinesNotUpToDateResults[m.Name] = *notUpToDateResult
132129
}
133130
}
134131

135132
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,
133+
KCP: kcp,
134+
Cluster: cluster,
135+
Machines: ownedMachines,
136+
machinesPatchHelpers: patchHelpers,
137+
machinesNotUptoDate: machinesNotUptoDate,
138+
machinesNotUpToDateResults: machinesNotUpToDateResults,
139+
KubeadmConfigs: kubeadmConfigs,
140+
InfraResources: infraMachines,
141+
reconciliationTime: reconciliationTime,
142+
managementCluster: managementCluster,
147143
}, nil
148144
}
149145

@@ -256,15 +252,15 @@ func (c *ControlPlane) GetKubeadmConfig(machineName string) (*bootstrapv1.Kubead
256252
}
257253

258254
// MachinesNeedingRollout return a list of machines that need to be rolled out.
259-
func (c *ControlPlane) MachinesNeedingRollout() (collections.Machines, map[string][]string) {
255+
func (c *ControlPlane) MachinesNeedingRollout() (collections.Machines, map[string]NotUpToDateResult) {
260256
// 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
257+
return c.machinesNotUptoDate.Filter(collections.Not(collections.HasDeletionTimestamp)), c.machinesNotUpToDateResults
262258
}
263259

264260
// NotUpToDateMachines return a list of machines that are not up to date with the control
265261
// plane's configuration.
266-
func (c *ControlPlane) NotUpToDateMachines() (collections.Machines, map[string][]string) {
267-
return c.machinesNotUptoDate, c.machinesNotUptoDateConditionMessages
262+
func (c *ControlPlane) NotUpToDateMachines() (collections.Machines, map[string]NotUpToDateResult) {
263+
return c.machinesNotUptoDate, c.machinesNotUpToDateResults
268264
}
269265

270266
// UpToDateMachines returns the machines that are up to date with the control
@@ -273,18 +269,18 @@ func (c *ControlPlane) UpToDateMachines() collections.Machines {
273269
return c.Machines.Difference(c.machinesNotUptoDate)
274270
}
275271

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) {
272+
// getInfraMachines fetches the InfraMachine for each machine in the collection and returns a map of machine.Name -> InfraMachine.
273+
func getInfraMachines(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
278274
result := map[string]*unstructured.Unstructured{}
279275
for _, m := range machines {
280-
infraObj, err := external.GetObjectFromContractVersionedRef(ctx, cl, m.Spec.InfrastructureRef, m.Namespace)
276+
infraMachine, err := external.GetObjectFromContractVersionedRef(ctx, cl, m.Spec.InfrastructureRef, m.Namespace)
281277
if err != nil {
282278
if apierrors.IsNotFound(errors.Cause(err)) {
283279
continue
284280
}
285-
return nil, errors.Wrapf(err, "failed to retrieve infra obj for machine %q", m.Name)
281+
return nil, errors.Wrapf(err, "failed to retrieve InfraMachine for Machine %s", m.Name)
286282
}
287-
result[m.Name] = infraObj
283+
result[m.Name] = infraMachine
288284
}
289285
return result, nil
290286
}
@@ -297,14 +293,14 @@ func getKubeadmConfigs(ctx context.Context, cl client.Client, machines collectio
297293
if !bootstrapRef.IsDefined() {
298294
continue
299295
}
300-
machineConfig := &bootstrapv1.KubeadmConfig{}
301-
if err := cl.Get(ctx, client.ObjectKey{Name: bootstrapRef.Name, Namespace: m.Namespace}, machineConfig); err != nil {
296+
kubeadmConfig := &bootstrapv1.KubeadmConfig{}
297+
if err := cl.Get(ctx, client.ObjectKey{Name: bootstrapRef.Name, Namespace: m.Namespace}, kubeadmConfig); err != nil {
302298
if apierrors.IsNotFound(errors.Cause(err)) {
303299
continue
304300
}
305-
return nil, errors.Wrapf(err, "failed to retrieve bootstrap config for machine %q", m.Name)
301+
return nil, errors.Wrapf(err, "failed to retrieve KubeadmConfig for Machine %s", m.Name)
306302
}
307-
result[m.Name] = machineConfig
303+
result[m.Name] = kubeadmConfig
308304
}
309305
return result, nil
310306
}

controlplane/kubeadm/internal/control_plane_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,17 @@ func TestControlPlane(t *testing.T) {
122122

123123
g.Expect(controlPlane.Machines).To(HaveLen(5))
124124

125-
machinesNotUptoDate, machinesNotUptoDateConditionMessages := controlPlane.NotUpToDateMachines()
125+
machinesNotUptoDate, machinesNotUpToDateResults := controlPlane.NotUpToDateMachines()
126126
g.Expect(machinesNotUptoDate.Names()).To(ConsistOf("m2", "m3"))
127-
g.Expect(machinesNotUptoDateConditionMessages).To(HaveLen(2))
128-
g.Expect(machinesNotUptoDateConditionMessages).To(HaveKeyWithValue("m2", []string{"Version v1.29.0, v1.31.0 required"}))
129-
g.Expect(machinesNotUptoDateConditionMessages).To(HaveKeyWithValue("m3", []string{"Version v1.29.3, v1.31.0 required"}))
127+
g.Expect(machinesNotUpToDateResults).To(HaveLen(2))
128+
g.Expect(machinesNotUpToDateResults["m2"].ConditionMessages).To(Equal([]string{"Version v1.29.0, v1.31.0 required"}))
129+
g.Expect(machinesNotUpToDateResults["m3"].ConditionMessages).To(Equal([]string{"Version v1.29.3, v1.31.0 required"}))
130130

131-
machinesNeedingRollout, machinesNotUptoDateLogMessages := controlPlane.MachinesNeedingRollout()
131+
machinesNeedingRollout, machinesNotUpToDateResults := controlPlane.MachinesNeedingRollout()
132132
g.Expect(machinesNeedingRollout.Names()).To(ConsistOf("m2"))
133-
g.Expect(machinesNotUptoDateLogMessages).To(HaveLen(2))
134-
g.Expect(machinesNotUptoDateLogMessages).To(HaveKeyWithValue("m2", []string{"Machine version \"v1.29.0\" is not equal to KCP version \"v1.31.0\""}))
135-
g.Expect(machinesNotUptoDateLogMessages).To(HaveKeyWithValue("m3", []string{"Machine version \"v1.29.3\" is not equal to KCP version \"v1.31.0\""}))
133+
g.Expect(machinesNotUpToDateResults).To(HaveLen(2))
134+
g.Expect(machinesNotUpToDateResults["m2"].LogMessages).To(Equal([]string{"Machine version \"v1.29.0\" is not equal to KCP version \"v1.31.0\""}))
135+
g.Expect(machinesNotUpToDateResults["m3"].LogMessages).To(Equal([]string{"Machine version \"v1.29.3\" is not equal to KCP version \"v1.31.0\""}))
136136

137137
upToDateMachines := controlPlane.UpToDateMachines()
138138
g.Expect(upToDateMachines).To(HaveLen(3))

0 commit comments

Comments
 (0)