Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions controllers/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ func (r *NamespaceReconciler) propagateMeta(ctx context.Context, ns, parent *cor
ac := corev1ac.Namespace(ns.Name).
WithLabels(labels).
WithAnnotations(annotations)
ns, p, err := newNamespacePatch(ac)
if err != nil {
return err
}
if err := r.Patch(ctx, ns, p, fieldOwner, client.ForceOwnership); err != nil {
if err := r.Apply(ctx, ac, fieldOwner, client.ForceOwnership); err != nil {
return fmt.Errorf("failed to propagate labels/annotations for namespace %s: %w", ns.Name, err)
}
return nil
Expand Down Expand Up @@ -269,7 +265,8 @@ func (r *NamespaceReconciler) propagateUpdate(ctx context.Context, res *unstruct
return nil
}

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

Expand Down
6 changes: 4 additions & 2 deletions controllers/propagate.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ func (r *PropagateController) propagateUpdate(ctx context.Context, obj, parent *
}

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

if err := r.Patch(ctx, clone, applyPatch{clone}, fieldOwner, client.ForceOwnership); err != nil {
ac := client.ApplyConfigurationFromUnstructured(clone)
if err := r.Apply(ctx, ac, fieldOwner, client.ForceOwnership); err != nil {
return fmt.Errorf("failed to apply %s/%s: %w", clone.GetNamespace(), clone.GetName(), err)
}
logger.Info("applied a child resource", "subnamespace", child.Name)
Expand Down
33 changes: 0 additions & 33 deletions controllers/ssa_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ package controllers

import (
"context"
"encoding/json"

accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
accuratev2ac "github.com/cybozu-go/accurate/internal/applyconfigurations/accurate/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
"k8s.io/client-go/util/csaupgrade"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -34,31 +29,3 @@ func upgradeManagedFields(ctx context.Context, c client.Client, obj client.Objec
// No work to be done - already upgraded
return nil
}

func newSubNamespacePatch(ac *accuratev2ac.SubNamespaceApplyConfiguration) (*accuratev2.SubNamespace, client.Patch, error) {
sn := &accuratev2.SubNamespace{}
sn.Name = *ac.Name
sn.Namespace = *ac.Namespace

return sn, applyPatch{ac}, nil
}

func newNamespacePatch(ac *corev1ac.NamespaceApplyConfiguration) (*corev1.Namespace, client.Patch, error) {
ns := &corev1.Namespace{}
ns.Name = *ac.Name

return ns, applyPatch{ac}, nil
}

type applyPatch struct {
// must use any type until apply configurations implements a common interface
patch any
}

func (p applyPatch) Type() types.PatchType {
return types.ApplyPatchType
}

func (p applyPatch) Data(_ client.Object) ([]byte, error) {
return json.Marshal(p.patch)
}
6 changes: 1 addition & 5 deletions controllers/subnamespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ func (r *SubNamespaceReconciler) reconcileNS(ctx context.Context, sn *accuratev2
return err
}

sn, p, err := newSubNamespacePatch(ac)
if err != nil {
return err
}
return r.Status().Patch(ctx, sn, p, fieldOwner, client.ForceOwnership)
return r.Status().Apply(ctx, ac, fieldOwner, client.ForceOwnership)
}

// SetupWithManager sets up the controller with the Manager.
Expand Down