diff --git a/.golangci.yml b/.golangci.yml index 0e6b1ef0c878..2d48069ddafb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -269,10 +269,6 @@ linters: - linters: - staticcheck text: 'SA1019: .*(res|result|i|j)\.Requeue is deprecated: Use `RequeueAfter` instead' - # CR v0.22 deprecated client.Apply, will be fixed via: https://github.com/kubernetes-sigs/cluster-api/issues/12695 - - linters: - - staticcheck - text: 'SA1019: client.Apply is deprecated: Use client.Client.Apply' # TODO: var-naming: avoid meaningless package names by revive # * test/infrastructure/docker/internal/docker/types/ # * bootstrap/kubeadm/types/ diff --git a/controllers/crdmigrator/crd_migrator.go b/controllers/crdmigrator/crd_migrator.go index 4fdf83e8da49..b5238dd6c1aa 100644 --- a/controllers/crdmigrator/crd_migrator.go +++ b/controllers/crdmigrator/crd_migrator.go @@ -412,7 +412,7 @@ func (r *CRDMigrator) reconcileStorageVersionMigration(ctx context.Context, crd if migrationConfig.UseStatusForStorageVersionMigration { err = r.Client.Status().Patch(ctx, u, client.Apply, client.FieldOwner("crdmigrator")) } else { - err = r.Client.Patch(ctx, u, client.Apply, client.FieldOwner("crdmigrator")) + err = r.Client.Apply(ctx, client.ApplyConfigurationFromUnstructured(u), client.FieldOwner("crdmigrator")) } // If we got a NotFound error, the object no longer exists so no need to update it. // If we got a Conflict error, another client wrote the object already so no need to update it. diff --git a/controllers/crdmigrator/crd_migrator_test.go b/controllers/crdmigrator/crd_migrator_test.go index 1000ac050eb7..ba8667fb216e 100644 --- a/controllers/crdmigrator/crd_migrator_test.go +++ b/controllers/crdmigrator/crd_migrator_test.go @@ -195,10 +195,10 @@ func TestReconcile(t *testing.T) { // Deploy test-cluster-1 and test-cluster-2. testClusterT1 := unstructuredTestCluster("test-cluster-1", t1v1beta1.GroupVersion.WithKind("TestCluster")) g.Expect(unstructured.SetNestedField(testClusterT1.Object, "foo-value", "spec", "foo")).To(Succeed()) - g.Expect(managerT1.GetClient().Patch(ctx, testClusterT1, client.Apply, fieldOwner)).To(Succeed()) + g.Expect(managerT1.GetClient().Apply(ctx, client.ApplyConfigurationFromUnstructured(testClusterT1), fieldOwner)).To(Succeed()) testClusterT1 = unstructuredTestCluster("test-cluster-2", t1v1beta1.GroupVersion.WithKind("TestCluster")) g.Expect(unstructured.SetNestedField(testClusterT1.Object, "foo-value", "spec", "foo")).To(Succeed()) - g.Expect(managerT1.GetClient().Patch(ctx, testClusterT1, client.Apply, fieldOwner)).To(Succeed()) + g.Expect(managerT1.GetClient().Apply(ctx, client.ApplyConfigurationFromUnstructured(testClusterT1), fieldOwner)).To(Succeed()) validateManagedFields(t, g, "v1beta1", map[string][]string{ "test-cluster-1": {"test.cluster.x-k8s.io/v1beta1"}, "test-cluster-2": {"test.cluster.x-k8s.io/v1beta1"}, @@ -226,11 +226,11 @@ func TestReconcile(t *testing.T) { // Set an additional field with a different field manager and v1beta2 apiVersion in test-cluster-2 testClusterT2 := unstructuredTestCluster("test-cluster-2", t2v1beta2.GroupVersion.WithKind("TestCluster")) g.Expect(unstructured.SetNestedField(testClusterT2.Object, "bar-value", "spec", "bar")).To(Succeed()) - g.Expect(managerT2.GetClient().Patch(ctx, testClusterT2, client.Apply, client.FieldOwner("different-unit-test-client"))).To(Succeed()) + g.Expect(managerT2.GetClient().Apply(ctx, client.ApplyConfigurationFromUnstructured(testClusterT2), client.FieldOwner("different-unit-test-client"))).To(Succeed()) // Deploy test-cluster-3. testClusterT2 = unstructuredTestCluster("test-cluster-3", t2v1beta2.GroupVersion.WithKind("TestCluster")) g.Expect(unstructured.SetNestedField(testClusterT2.Object, "foo-value", "spec", "foo")).To(Succeed()) - g.Expect(managerT2.GetClient().Patch(ctx, testClusterT2, client.Apply, fieldOwner)).To(Succeed()) + g.Expect(managerT2.GetClient().Apply(ctx, client.ApplyConfigurationFromUnstructured(testClusterT2), fieldOwner)).To(Succeed()) // At this point we have clusters with all combinations of managedField apiVersions. validateManagedFields(t, g, "v1beta2", map[string][]string{ "test-cluster-1": {"test.cluster.x-k8s.io/v1beta1"}, @@ -323,7 +323,7 @@ func TestReconcile(t *testing.T) { // Try to patch the test-clusters CRs with SSA. testClusterT4 := unstructuredTestCluster(clusterName, t4v1beta2.GroupVersion.WithKind("TestCluster")) g.Expect(unstructured.SetNestedField(testClusterT4.Object, "new-foo-value", "spec", "foo")).To(Succeed()) - err = managerT4.GetClient().Patch(ctx, testClusterT4, client.Apply, fieldOwner) + err = managerT4.GetClient().Apply(ctx, client.ApplyConfigurationFromUnstructured(testClusterT4), fieldOwner) // If managedField cleanup was skipped before, the SSA patch will fail for the clusters which still have v1beta1 managedFields. if skipCRDMigrationPhases.Has(CleanupManagedFieldsPhase) && (clusterName == "test-cluster-1" || clusterName == "test-cluster-2") { diff --git a/internal/controllers/topology/cluster/structuredmerge/dryrun.go b/internal/controllers/topology/cluster/structuredmerge/dryrun.go index 3e1231b14b97..4db1e34dc534 100644 --- a/internal/controllers/topology/cluster/structuredmerge/dryrun.go +++ b/internal/controllers/topology/cluster/structuredmerge/dryrun.go @@ -81,7 +81,7 @@ func dryRunSSAPatch(ctx context.Context, dryRunCtx *dryRunSSAPatchInput) (bool, } // Do a server-side apply dry-run with modifiedUnstructured to get the updated object. - err = dryRunCtx.client.Patch(ctx, dryRunCtx.modifiedUnstructured, client.Apply, client.DryRunAll, client.FieldOwner(TopologyManagerName), client.ForceOwnership) + err = dryRunCtx.client.Apply(ctx, client.ApplyConfigurationFromUnstructured(dryRunCtx.modifiedUnstructured), client.DryRunAll, client.FieldOwner(TopologyManagerName), client.ForceOwnership) if err != nil { // This catches errors like metadata.uid changes. return false, false, nil, errors.Wrap(err, "server side apply dry-run failed for modified object") @@ -107,7 +107,7 @@ func dryRunSSAPatch(ctx context.Context, dryRunCtx *dryRunSSAPatchInput) (bool, // Note: Otherwise we would get the following error: // "failed to request dry-run server side apply: metadata.managedFields must be nil" dryRunCtx.originalUnstructured.SetManagedFields(nil) - err = dryRunCtx.client.Patch(ctx, dryRunCtx.originalUnstructured, client.Apply, client.DryRunAll, client.FieldOwner(TopologyManagerName), client.ForceOwnership) + err = dryRunCtx.client.Apply(ctx, client.ApplyConfigurationFromUnstructured(dryRunCtx.originalUnstructured), client.DryRunAll, client.FieldOwner(TopologyManagerName), client.ForceOwnership) if err != nil { return false, false, nil, errors.Wrap(err, "server side apply dry-run failed for original object") } diff --git a/internal/controllers/topology/cluster/structuredmerge/serversidepathhelper.go b/internal/controllers/topology/cluster/structuredmerge/serversidepathhelper.go index 5a8b04c67f3c..2e029c966d73 100644 --- a/internal/controllers/topology/cluster/structuredmerge/serversidepathhelper.go +++ b/internal/controllers/topology/cluster/structuredmerge/serversidepathhelper.go @@ -141,11 +141,11 @@ func (h *serverSidePatchHelper) Patch(ctx context.Context) error { log := ctrl.LoggerFrom(ctx) log.V(5).Info("Patching object", "intent", h.modified) - options := []client.PatchOption{ + options := []client.ApplyOption{ client.FieldOwner(TopologyManagerName), // NOTE: we are using force ownership so in case of conflicts the topology controller // overwrite values and become sole manager. client.ForceOwnership, } - return h.client.Patch(ctx, h.modified, client.Apply, options...) + return h.client.Apply(ctx, client.ApplyConfigurationFromUnstructured(h.modified), options...) } diff --git a/internal/util/ssa/patch.go b/internal/util/ssa/patch.go index 1c89f35325d2..9b22a943a130 100644 --- a/internal/util/ssa/patch.go +++ b/internal/util/ssa/patch.go @@ -99,11 +99,11 @@ func Patch(ctx context.Context, c client.Client, fieldManager string, modified c } } - patchOptions := []client.PatchOption{ + applyOptions := []client.ApplyOption{ client.ForceOwnership, client.FieldOwner(fieldManager), } - if err := c.Patch(ctx, modifiedUnstructured, client.Apply, patchOptions...); err != nil { + if err := c.Apply(ctx, client.ApplyConfigurationFromUnstructured(modifiedUnstructured), applyOptions...); err != nil { return errors.Wrapf(err, "failed to apply %s %s", gvk.Kind, klog.KObj(modifiedUnstructured)) }