Skip to content

Commit 3dcb7be

Browse files
authored
Merge pull request #199 from erikgb/migrate-to-apply
Migrate to controller-runtime Apply funcs
2 parents 20e3901 + 17102ba commit 3dcb7be

File tree

4 files changed

+8
-46
lines changed

4 files changed

+8
-46
lines changed

controllers/namespace_controller.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ func (r *NamespaceReconciler) propagateMeta(ctx context.Context, ns, parent *cor
134134
ac := corev1ac.Namespace(ns.Name).
135135
WithLabels(labels).
136136
WithAnnotations(annotations)
137-
ns, p, err := newNamespacePatch(ac)
138-
if err != nil {
139-
return err
140-
}
141-
if err := r.Patch(ctx, ns, p, fieldOwner, client.ForceOwnership); err != nil {
137+
if err := r.Apply(ctx, ac, fieldOwner, client.ForceOwnership); err != nil {
142138
return fmt.Errorf("failed to propagate labels/annotations for namespace %s: %w", ns.Name, err)
143139
}
144140
return nil
@@ -269,7 +265,8 @@ func (r *NamespaceReconciler) propagateUpdate(ctx context.Context, res *unstruct
269265
return nil
270266
}
271267

272-
if err := r.Patch(ctx, c2, applyPatch{c2}, fieldOwner, client.ForceOwnership); err != nil {
268+
ac := client.ApplyConfigurationFromUnstructured(c2)
269+
if err := r.Apply(ctx, ac, fieldOwner, client.ForceOwnership); err != nil {
273270
return fmt.Errorf("failed to apply %s/%s: %w", c2.GetNamespace(), c2.GetName(), err)
274271
}
275272

controllers/propagate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ func (r *PropagateController) propagateUpdate(ctx context.Context, obj, parent *
290290
}
291291

292292
if !equality.Semantic.DeepDerivative(clone, obj) {
293-
if err := r.Patch(ctx, clone, applyPatch{clone}, fieldOwner, client.ForceOwnership); err != nil {
293+
ac := client.ApplyConfigurationFromUnstructured(clone)
294+
if err := r.Apply(ctx, ac, fieldOwner, client.ForceOwnership); err != nil {
294295
return fmt.Errorf("failed to apply %s/%s: %w", clone.GetNamespace(), clone.GetName(), err)
295296
}
296297
logger.Info("applied", "from", parent.GetNamespace())
@@ -336,7 +337,8 @@ func (r *PropagateController) propagateUpdate(ctx context.Context, obj, parent *
336337
continue
337338
}
338339

339-
if err := r.Patch(ctx, clone, applyPatch{clone}, fieldOwner, client.ForceOwnership); err != nil {
340+
ac := client.ApplyConfigurationFromUnstructured(clone)
341+
if err := r.Apply(ctx, ac, fieldOwner, client.ForceOwnership); err != nil {
340342
return fmt.Errorf("failed to apply %s/%s: %w", clone.GetNamespace(), clone.GetName(), err)
341343
}
342344
logger.Info("applied a child resource", "subnamespace", child.Name)

controllers/ssa_client.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ package controllers
22

33
import (
44
"context"
5-
"encoding/json"
65

7-
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
8-
accuratev2ac "github.com/cybozu-go/accurate/internal/applyconfigurations/accurate/v2"
9-
corev1 "k8s.io/api/core/v1"
106
"k8s.io/apimachinery/pkg/types"
117
"k8s.io/apimachinery/pkg/util/sets"
12-
corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
138
"k8s.io/client-go/util/csaupgrade"
149
"sigs.k8s.io/controller-runtime/pkg/client"
1510
)
@@ -34,31 +29,3 @@ func upgradeManagedFields(ctx context.Context, c client.Client, obj client.Objec
3429
// No work to be done - already upgraded
3530
return nil
3631
}
37-
38-
func newSubNamespacePatch(ac *accuratev2ac.SubNamespaceApplyConfiguration) (*accuratev2.SubNamespace, client.Patch, error) {
39-
sn := &accuratev2.SubNamespace{}
40-
sn.Name = *ac.Name
41-
sn.Namespace = *ac.Namespace
42-
43-
return sn, applyPatch{ac}, nil
44-
}
45-
46-
func newNamespacePatch(ac *corev1ac.NamespaceApplyConfiguration) (*corev1.Namespace, client.Patch, error) {
47-
ns := &corev1.Namespace{}
48-
ns.Name = *ac.Name
49-
50-
return ns, applyPatch{ac}, nil
51-
}
52-
53-
type applyPatch struct {
54-
// must use any type until apply configurations implements a common interface
55-
patch any
56-
}
57-
58-
func (p applyPatch) Type() types.PatchType {
59-
return types.ApplyPatchType
60-
}
61-
62-
func (p applyPatch) Data(_ client.Object) ([]byte, error) {
63-
return json.Marshal(p.patch)
64-
}

controllers/subnamespace_controller.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ func (r *SubNamespaceReconciler) reconcileNS(ctx context.Context, sn *accuratev2
163163
return err
164164
}
165165

166-
sn, p, err := newSubNamespacePatch(ac)
167-
if err != nil {
168-
return err
169-
}
170-
return r.Status().Patch(ctx, sn, p, fieldOwner, client.ForceOwnership)
166+
return r.Status().Apply(ctx, ac, fieldOwner, client.ForceOwnership)
171167
}
172168

173169
// SetupWithManager sets up the controller with the Manager.

0 commit comments

Comments
 (0)