mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-25 16:07:24 +00:00
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>
This commit is contained in:
co-authored by
CorentinPtrl
parent
7a65ab7afc
commit
cc4fb45d70
@@ -5,60 +5,26 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
apierr "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
func CreateOrPatch(
|
||||
func PatchApply(
|
||||
ctx context.Context,
|
||||
c client.Client,
|
||||
obj client.Object,
|
||||
fieldOwner string,
|
||||
overwrite bool,
|
||||
) error {
|
||||
gvks, _, err := c.Scheme().ObjectKinds(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(gvks) == 0 {
|
||||
return fmt.Errorf("no GVK found for object %T", obj)
|
||||
}
|
||||
|
||||
obj.GetObjectKind().SetGroupVersionKind(gvks[0])
|
||||
|
||||
//nolint:forcetypeassert
|
||||
actual := obj.DeepCopyObject().(client.Object)
|
||||
|
||||
key := client.ObjectKeyFromObject(obj)
|
||||
|
||||
err = c.Get(ctx, key, actual)
|
||||
|
||||
notFound := apierr.IsNotFound(err)
|
||||
if err != nil && !notFound {
|
||||
return err
|
||||
}
|
||||
|
||||
if !notFound {
|
||||
obj.SetResourceVersion(actual.GetResourceVersion())
|
||||
} else {
|
||||
obj.SetResourceVersion("")
|
||||
}
|
||||
|
||||
patchOpts := []client.PatchOption{
|
||||
client.FieldOwner(fieldOwner),
|
||||
}
|
||||
|
||||
opts := []client.PatchOption{client.FieldOwner(fieldOwner)}
|
||||
if overwrite {
|
||||
patchOpts = append(patchOpts, client.ForceOwnership)
|
||||
opts = append(opts, client.ForceOwnership)
|
||||
}
|
||||
|
||||
//nolint:staticcheck
|
||||
return c.Patch(ctx, obj, client.Apply, patchOpts...)
|
||||
return c.Patch(ctx, obj, client.Apply, opts...)
|
||||
}
|
||||
|
||||
// Returns timestamp of last apply for a manager.
|
||||
|
||||
Reference in New Issue
Block a user