Files
capsule/pkg/runtime/client/apply.go
T
cc4fb45d70 feat: upstream enterprise preview (#1841)
feat: upstream enterprise preview

---------

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Co-authored-by: CorentinPtrl <pitrel.corentin@gmail.com>
2026-05-28 00:58:58 +02:00

57 lines
1.1 KiB
Go

// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func PatchApply(
ctx context.Context,
c client.Client,
obj client.Object,
fieldOwner string,
overwrite bool,
) error {
opts := []client.PatchOption{client.FieldOwner(fieldOwner)}
if overwrite {
opts = append(opts, client.ForceOwnership)
}
//nolint:staticcheck
return c.Patch(ctx, obj, client.Apply, opts...)
}
// Returns timestamp of last apply for a manager.
func LastApplyTimeForManager(obj *unstructured.Unstructured, manager string) *metav1.Time {
var latest *metav1.Time
for i := range obj.GetManagedFields() {
mf := obj.GetManagedFields()[i]
if mf.Manager != manager {
continue
}
if mf.Operation != metav1.ManagedFieldsOperationApply {
continue
}
if mf.Time == nil {
continue
}
if latest == nil || mf.Time.After(latest.Time) {
t := *mf.Time
latest = &t
}
}
return latest
}