Skip to content

Commit c2b53ae

Browse files
Fix lint errors
Signed-off-by: Furkat Gofurov <[email protected]>
1 parent 5c056e0 commit c2b53ae

17 files changed

+457
-129
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ GOLANGCI_LINT_BIN := golangci-lint
106106
GOLANGCI_LINT := $(abspath $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER))
107107
GOLANGCI_LINT_PKG := github.com/golangci/golangci-lint/v2/cmd/golangci-lint
108108

109-
GINKGO_VER := v2.22.2
109+
GINKGO_VER := v2.23.4
110110
GINKGO_BIN := ginkgo
111111
GINKGO := $(abspath $(TOOLS_BIN_DIR)/$(GINKGO_BIN)-$(GINKGO_VER))
112112
GINKGO_PKG := github.com/onsi/ginkgo/v2/ginkgo

bootstrap/internal/ignition/ignition_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"compress/gzip"
2222
"encoding/base64"
2323
"fmt"
24-
"io/ioutil"
24+
"io"
2525
"strings"
2626
"testing"
2727

@@ -135,7 +135,7 @@ var _ = Describe("NewJoinWorker", func() {
135135
reader := bytes.NewReader(scriptContentsGzip)
136136
gzreader, err := gzip.NewReader(reader)
137137
Expect(err).ToNot(HaveOccurred())
138-
scriptContents, err := ioutil.ReadAll(gzreader)
138+
scriptContents, err := io.ReadAll(gzreader)
139139
Expect(err).ToNot(HaveOccurred())
140140
Expect(scriptContents).To(ContainSubstring("/opt/rke2-cis-script.sh"))
141141
})

controlplane/internal/contract/types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ func (p Path) IsParentOf(other Path) bool {
4141
if len(p) >= len(other) {
4242
return false
4343
}
44+
4445
for i := range p {
4546
if p[i] != other[i] {
4647
return false
4748
}
4849
}
50+
4951
return true
5052
}
5153

@@ -54,11 +56,13 @@ func (p Path) Equal(other Path) bool {
5456
if len(p) != len(other) {
5557
return false
5658
}
59+
5760
for i := range p {
5861
if p[i] != other[i] {
5962
return false
6063
}
6164
}
65+
6266
return true
6367
}
6468

@@ -88,9 +92,11 @@ func (i *Int64) Get(obj *unstructured.Unstructured) (*int64, error) {
8892
if err != nil {
8993
return nil, errors.Wrapf(err, "failed to get %s from object", "."+strings.Join(i.path, "."))
9094
}
95+
9196
if !ok {
9297
return nil, errors.Wrapf(ErrFieldNotFound, "path %s", "."+strings.Join(i.path, "."))
9398
}
99+
94100
return &value, nil
95101
}
96102

@@ -99,6 +105,7 @@ func (i *Int64) Set(obj *unstructured.Unstructured, value int64) error {
99105
if err := unstructured.SetNestedField(obj.UnstructuredContent(), value, i.path...); err != nil {
100106
return errors.Wrapf(err, "failed to set path %s of object %v", "."+strings.Join(i.path, "."), obj.GroupVersionKind())
101107
}
108+
102109
return nil
103110
}
104111

@@ -122,13 +129,22 @@ func (i *Int32) Get(obj *unstructured.Unstructured) (*int32, error) {
122129
if err != nil {
123130
return nil, errors.Wrapf(err, "failed to get %s from object", "."+strings.Join(path, "."))
124131
}
132+
125133
if !ok {
126134
paths = append(paths, "."+strings.Join(path, "."))
135+
127136
continue
128137
}
138+
139+
if value > 2147483647 || value < -2147483648 {
140+
return nil, errors.Wrapf(err, "int32 overflow: %d", value)
141+
}
142+
129143
int32Value := int32(value)
144+
130145
return &int32Value, nil
131146
}
147+
132148
return nil, errors.Wrapf(ErrFieldNotFound, "path %s", strings.Join(paths, ", "))
133149
}
134150

@@ -140,6 +156,7 @@ func (i *Int32) Set(obj *unstructured.Unstructured, value int64) error {
140156
if err := unstructured.SetNestedField(obj.UnstructuredContent(), value, i.paths[0]...); err != nil {
141157
return errors.Wrapf(err, "failed to set path %s of object %v", "."+strings.Join(i.paths[0], "."), obj.GroupVersionKind())
142158
}
159+
143160
return nil
144161
}
145162

@@ -159,9 +176,11 @@ func (b *Bool) Get(obj *unstructured.Unstructured) (*bool, error) {
159176
if err != nil {
160177
return nil, errors.Wrapf(err, "failed to get %s from object", "."+strings.Join(b.path, "."))
161178
}
179+
162180
if !ok {
163181
return nil, errors.Wrapf(ErrFieldNotFound, "path %s", "."+strings.Join(b.path, "."))
164182
}
183+
165184
return &value, nil
166185
}
167186

@@ -170,6 +189,7 @@ func (b *Bool) Set(obj *unstructured.Unstructured, value bool) error {
170189
if err := unstructured.SetNestedField(obj.UnstructuredContent(), value, b.path...); err != nil {
171190
return errors.Wrapf(err, "failed to set path %s of object %v", "."+strings.Join(b.path, "."), obj.GroupVersionKind())
172191
}
192+
173193
return nil
174194
}
175195

@@ -189,9 +209,11 @@ func (s *String) Get(obj *unstructured.Unstructured) (*string, error) {
189209
if err != nil {
190210
return nil, errors.Wrapf(err, "failed to get %s from object", "."+strings.Join(s.path, "."))
191211
}
212+
192213
if !ok {
193214
return nil, errors.Wrapf(ErrFieldNotFound, "path %s", "."+strings.Join(s.path, "."))
194215
}
216+
195217
return &value, nil
196218
}
197219

@@ -200,6 +222,7 @@ func (s *String) Set(obj *unstructured.Unstructured, value string) error {
200222
if err := unstructured.SetNestedField(obj.UnstructuredContent(), value, s.path...); err != nil {
201223
return errors.Wrapf(err, "failed to set path %s of object %v", "."+strings.Join(s.path, "."), obj.GroupVersionKind())
202224
}
225+
203226
return nil
204227
}
205228

@@ -219,6 +242,7 @@ func (i *Duration) Get(obj *unstructured.Unstructured) (*metav1.Duration, error)
219242
if err != nil {
220243
return nil, errors.Wrapf(err, "failed to get %s from object", "."+strings.Join(i.path, "."))
221244
}
245+
222246
if !ok {
223247
return nil, errors.Wrapf(ErrFieldNotFound, "path %s", "."+strings.Join(i.path, "."))
224248
}
@@ -236,5 +260,6 @@ func (i *Duration) Set(obj *unstructured.Unstructured, value metav1.Duration) er
236260
if err := unstructured.SetNestedField(obj.UnstructuredContent(), value.Duration.String(), i.path...); err != nil {
237261
return errors.Wrapf(err, "failed to set path %s of object %v", "."+strings.Join(i.path, "."), obj.GroupVersionKind())
238262
}
263+
239264
return nil
240265
}

controlplane/internal/controllers/rke2controlplane_controller.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444
"sigs.k8s.io/controller-runtime/pkg/log"
4545
"sigs.k8s.io/controller-runtime/pkg/source"
4646

47-
"github.com/rancher/cluster-api-provider-rke2/controlplane/internal/util/ssa"
4847
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4948
"sigs.k8s.io/cluster-api/controllers/clustercache"
5049
"sigs.k8s.io/cluster-api/controllers/remote"
@@ -57,6 +56,8 @@ import (
5756
"sigs.k8s.io/cluster-api/util/patch"
5857

5958
controlplanev1 "github.com/rancher/cluster-api-provider-rke2/controlplane/api/v1beta1"
59+
"github.com/rancher/cluster-api-provider-rke2/controlplane/internal/contract"
60+
"github.com/rancher/cluster-api-provider-rke2/controlplane/internal/util/ssa"
6061
"github.com/rancher/cluster-api-provider-rke2/pkg/kubeconfig"
6162
"github.com/rancher/cluster-api-provider-rke2/pkg/registration"
6263
"github.com/rancher/cluster-api-provider-rke2/pkg/rke2"
@@ -65,8 +66,10 @@ import (
6566
)
6667

6768
const (
69+
// rke2ManagerName is the name of the RKE2 manager deployment.
6870
rke2ManagerName = "rke2controlplane"
6971

72+
// rke2ControlPlaneKind is the kind of the RKE2 control plane.
7073
rke2ControlPlaneKind = "RKE2ControlPlane"
7174

7275
// dependentCertRequeueAfter is how long to wait before checking again to see if
@@ -500,6 +503,10 @@ func (r *RKE2ControlPlaneReconciler) reconcileNormal(
500503
return result, err
501504
}
502505

506+
if err := r.syncMachines(ctx, rke2.ControlPlane{}); err != nil {
507+
return ctrl.Result{}, errors.Wrap(err, "failed to sync Machines")
508+
}
509+
503510
controlPlaneMachines, err := r.managementClusterUncached.GetMachinesForCluster(
504511
ctx,
505512
util.ObjectKey(cluster),
@@ -1098,3 +1105,86 @@ func (r *RKE2ControlPlaneReconciler) getWorkloadCluster(ctx context.Context, clu
10981105

10991106
return workloadCluster, nil
11001107
}
1108+
1109+
// syncMachines updates Machines, InfrastructureMachines and Rke2Configs to propagate in-place mutable fields from KCP.
1110+
// Note: It also cleans up managed fields of all Machines so that Machines that were
1111+
// created/patched before (< v1.4.0)“ the controller adopted Server-Side-Apply (SSA) can also work with SSA.
1112+
// Note: For InfrastructureMachines and Rke2Configs it also drops ownership of "metadata.labels" and
1113+
// "metadata.annotations" from "manager" so that "rke2controlplane" can own these fields and can work with SSA.
1114+
// Otherwise, fields would be co-owned by our "old" "manager" and "rke2controlplane" and then we would not be
1115+
// able to e.g. drop labels and annotations.
1116+
func (r *RKE2ControlPlaneReconciler) syncMachines(ctx context.Context, controlPlane rke2.ControlPlane) error {
1117+
patchHelpers := map[string]*patch.Helper{}
1118+
1119+
for machineName := range controlPlane.Machines {
1120+
m := controlPlane.Machines[machineName]
1121+
// If the machine is already being deleted, we don't need to update it.
1122+
if !m.DeletionTimestamp.IsZero() {
1123+
continue
1124+
}
1125+
1126+
// Cleanup managed fields of all Machines.
1127+
// We do this so that Machines that were created/patched before the controller adopted Server-Side-Apply (SSA)
1128+
// (< v1.4.0) can also work with SSA. Otherwise, fields would be co-owned by our "old" "manager" and
1129+
// "rke2controlplane" and then we would not be able to e.g. drop labels and annotations.
1130+
if err := ssa.CleanUpManagedFieldsForSSAAdoption(ctx, r.Client, m, rke2ManagerName); err != nil {
1131+
return errors.Wrapf(err, "failed to update Machine: failed to adjust the managedFields of the Machine %s", klog.KObj(m))
1132+
}
1133+
// Update Machine to propagate in-place mutable fields from RCP.
1134+
updatedMachine, err := r.UpdateMachine(ctx, m, controlPlane.RCP, controlPlane.Cluster)
1135+
if err != nil {
1136+
return errors.Wrapf(err, "failed to update Machine: %s", klog.KObj(m))
1137+
}
1138+
1139+
controlPlane.Machines[machineName] = updatedMachine
1140+
// Since the machine is updated, re-create the patch helper so that any subsequent
1141+
// Patch calls use the correct base machine object to calculate the diffs.
1142+
// Example: reconcileControlPlaneConditions patches the machine objects in a subsequent call
1143+
// and, it should use the updated machine to calculate the diff.
1144+
// Note: If the patchHelpers are not re-computed based on the new updated machines, subsequent
1145+
// Patch calls will fail because the patch will be calculated based on an outdated machine and will error
1146+
// because of outdated resourceVersion.
1147+
patchHelper, err := patch.NewHelper(updatedMachine, r.Client)
1148+
if err != nil {
1149+
return errors.Wrapf(err, "failed to create patch helper for Machine %s", klog.KObj(updatedMachine))
1150+
}
1151+
1152+
patchHelpers[machineName] = patchHelper
1153+
1154+
labelsAndAnnotationsManagedFieldPaths := []contract.Path{
1155+
{"f:metadata", "f:annotations"},
1156+
{"f:metadata", "f:labels"},
1157+
}
1158+
infraMachine := controlPlane.InfraResources[machineName]
1159+
// Cleanup managed fields of all InfrastructureMachines to drop ownership of labels and annotations
1160+
// from "manager". We do this so that InfrastructureMachines that are created using the Create method
1161+
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
1162+
// and "rke2-kubeadmcontrolplane" and then we would not be able to e.g. drop labels and annotations.
1163+
if err := ssa.DropManagedFields(ctx, r.Client, infraMachine, rke2ManagerName, labelsAndAnnotationsManagedFieldPaths); err != nil {
1164+
return errors.Wrapf(err, "failed to clean up managedFields of InfrastructureMachine %s", klog.KObj(infraMachine))
1165+
}
1166+
// Update in-place mutating fields on InfrastructureMachine.
1167+
if err := r.UpdateExternalObject(ctx, infraMachine, controlPlane.RCP, controlPlane.Cluster); err != nil {
1168+
return errors.Wrapf(err, "failed to update InfrastructureMachine %s", klog.KObj(infraMachine))
1169+
}
1170+
1171+
rke2Config := controlPlane.Rke2Configs[machineName]
1172+
// Note: Set the GroupVersionKind because updateExternalObject depends on it.
1173+
rke2Config.SetGroupVersionKind(m.Spec.Bootstrap.ConfigRef.GroupVersionKind())
1174+
// Cleanup managed fields of all Rke2Configs to drop ownership of labels and annotations
1175+
// from "manager". We do this so that Rke2Configs that are created using the Create method
1176+
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
1177+
// and "rke2controlplane" and then we would not be able to e.g. drop labels and annotations.
1178+
if err := ssa.DropManagedFields(ctx, r.Client, rke2Config, rke2ManagerName, labelsAndAnnotationsManagedFieldPaths); err != nil {
1179+
return errors.Wrapf(err, "failed to clean up managedFields of KubeadmConfig %s", klog.KObj(rke2Config))
1180+
}
1181+
// Update in-place mutating fields on BootstrapConfig.
1182+
if err := r.UpdateExternalObject(ctx, rke2Config, controlPlane.RCP, controlPlane.Cluster); err != nil {
1183+
return errors.Wrapf(err, "failed to update Rke2Config %s", klog.KObj(rke2Config))
1184+
}
1185+
}
1186+
// Update the patch helpers.
1187+
controlPlane.SetPatchHelpers(patchHelpers)
1188+
1189+
return nil
1190+
}

controlplane/internal/controllers/rke2controlplane_controller_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
. "github.com/onsi/gomega"
88
bootstrapv1 "github.com/rancher/cluster-api-provider-rke2/bootstrap/api/v1beta1"
99
controlplanev1 "github.com/rancher/cluster-api-provider-rke2/controlplane/api/v1beta1"
10-
1110
// "github.com/rancher/cluster-api-provider-rke2/pkg/kubeconfig"
1211
"github.com/rancher/cluster-api-provider-rke2/pkg/rke2"
1312
corev1 "k8s.io/api/core/v1"

0 commit comments

Comments
 (0)