2022-09-18 09:12:29 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-24 10:01:49 +00:00
|
|
|
datautils "github.com/kyverno/kyverno/pkg/utils/data"
|
2022-09-18 09:12:29 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-09-29 11:42:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2022-09-18 09:12:29 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2022-09-29 11:42:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2022-09-18 09:12:29 +00:00
|
|
|
)
|
|
|
|
|
2022-09-29 11:42:50 +00:00
|
|
|
type CreateClient[T metav1.Object] interface {
|
|
|
|
Create(context.Context, T, metav1.CreateOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:12:36 +00:00
|
|
|
type GetClient[T metav1.Object] interface {
|
|
|
|
Get(context.Context, string, metav1.GetOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:42:50 +00:00
|
|
|
type UpdateClient[T metav1.Object] interface {
|
|
|
|
Update(context.Context, T, metav1.UpdateOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:12:36 +00:00
|
|
|
type DeleteClient interface {
|
2022-09-29 11:42:50 +00:00
|
|
|
Delete(context.Context, string, metav1.DeleteOptions) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type PatchClient[T metav1.Object] interface {
|
|
|
|
Patch(context.Context, string, types.PatchType, []byte, metav1.PatchOptions, ...string) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectClient[T metav1.Object] interface {
|
|
|
|
CreateClient[T]
|
|
|
|
GetClient[T]
|
2023-06-22 11:12:36 +00:00
|
|
|
UpdateClient[T]
|
|
|
|
DeleteClient
|
2022-09-29 11:42:50 +00:00
|
|
|
PatchClient[T]
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:12:36 +00:00
|
|
|
type DeleteCollectionClient interface {
|
|
|
|
DeleteCollection(context.Context, metav1.DeleteOptions, metav1.ListOptions) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type WatchClient interface {
|
|
|
|
Watch(context.Context, metav1.ListOptions) (watch.Interface, error)
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:42:50 +00:00
|
|
|
type ListClient[T any] interface {
|
|
|
|
List(context.Context, metav1.ListOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusClient[T metav1.Object] interface {
|
|
|
|
UpdateStatus(context.Context, T, metav1.UpdateOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectListClient[T metav1.Object, L any] interface {
|
|
|
|
ObjectClient[T]
|
|
|
|
ListClient[L]
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectStatusClient[T metav1.Object] interface {
|
|
|
|
ObjectClient[T]
|
|
|
|
StatusClient[T]
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectListStatusClient[T metav1.Object, L any] interface {
|
|
|
|
ObjectClient[T]
|
|
|
|
ListClient[L]
|
|
|
|
StatusClient[T]
|
|
|
|
}
|
|
|
|
|
2022-09-18 09:12:29 +00:00
|
|
|
type Object[T any] interface {
|
|
|
|
*T
|
|
|
|
metav1.Object
|
|
|
|
DeepCopy() *T
|
|
|
|
}
|
|
|
|
|
|
|
|
type Getter[T any] interface {
|
|
|
|
Get(string) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Setter[T any] interface {
|
|
|
|
Create(context.Context, T, metav1.CreateOptions) (T, error)
|
|
|
|
Update(context.Context, T, metav1.UpdateOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Deleter interface {
|
|
|
|
Delete(context.Context, string, metav1.DeleteOptions) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOrNew[T any, R Object[T], G Getter[R]](name string, getter G) (R, error) {
|
|
|
|
obj, err := getter.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
if apierrors.IsNotFound(err) {
|
|
|
|
obj = new(T)
|
|
|
|
obj.SetName(name)
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:24:47 +00:00
|
|
|
func CreateOrUpdate[T any, R Object[T], G Getter[R], S Setter[R]](ctx context.Context, name string, getter G, setter S, build func(R) error) (R, error) {
|
2022-09-18 09:12:29 +00:00
|
|
|
if obj, err := GetOrNew[T, R](name, getter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
mutated := obj.DeepCopy()
|
|
|
|
if err := build(mutated); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
if obj.GetResourceVersion() == "" {
|
2022-09-30 11:24:47 +00:00
|
|
|
return setter.Create(ctx, mutated, metav1.CreateOptions{})
|
2022-09-18 09:12:29 +00:00
|
|
|
} else {
|
2023-03-24 10:01:49 +00:00
|
|
|
if datautils.DeepEqual(obj, mutated) {
|
2022-09-18 09:12:29 +00:00
|
|
|
return mutated, nil
|
|
|
|
} else {
|
2022-09-30 11:24:47 +00:00
|
|
|
return setter.Update(ctx, mutated, metav1.UpdateOptions{})
|
2022-09-18 09:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 11:23:02 +00:00
|
|
|
type DeepCopy[T any] interface {
|
|
|
|
DeepCopy() T
|
|
|
|
}
|
|
|
|
|
|
|
|
func Update[T interface {
|
|
|
|
metav1.Object
|
|
|
|
DeepCopy[T]
|
|
|
|
}, S UpdateClient[T]](ctx context.Context, obj T, setter S, build func(T) error,
|
|
|
|
) (T, error) {
|
2022-09-18 09:12:29 +00:00
|
|
|
mutated := obj.DeepCopy()
|
|
|
|
if err := build(mutated); err != nil {
|
2022-10-03 11:23:02 +00:00
|
|
|
var d T
|
|
|
|
return d, err
|
2022-09-18 09:12:29 +00:00
|
|
|
} else {
|
2023-03-24 10:01:49 +00:00
|
|
|
if datautils.DeepEqual(obj, mutated) {
|
2022-09-18 09:12:29 +00:00
|
|
|
return mutated, nil
|
|
|
|
} else {
|
2022-09-30 11:24:47 +00:00
|
|
|
return setter.Update(ctx, mutated, metav1.UpdateOptions{})
|
2022-09-18 09:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 05:52:54 +00:00
|
|
|
func UpdateStatus[T interface {
|
|
|
|
metav1.Object
|
|
|
|
DeepCopy[T]
|
2024-04-12 07:08:15 +00:00
|
|
|
}, S ObjectStatusClient[T]](ctx context.Context, obj T, setter S, build func(T) error,
|
2022-10-17 05:52:54 +00:00
|
|
|
) (T, error) {
|
2024-04-12 07:08:15 +00:00
|
|
|
var objNew T
|
|
|
|
objNew, err := setter.Get(ctx, obj.GetName(), metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return objNew, err
|
|
|
|
}
|
|
|
|
mutated := objNew.DeepCopy()
|
2022-10-17 05:52:54 +00:00
|
|
|
if err := build(mutated); err != nil {
|
|
|
|
var d T
|
|
|
|
return d, err
|
|
|
|
} else {
|
2023-03-24 10:01:49 +00:00
|
|
|
if datautils.DeepEqual(obj, mutated) {
|
2022-10-17 05:52:54 +00:00
|
|
|
return mutated, nil
|
|
|
|
} else {
|
|
|
|
return setter.UpdateStatus(ctx, mutated, metav1.UpdateOptions{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:24:47 +00:00
|
|
|
func Cleanup[T any, R Object[T]](ctx context.Context, actual []R, expected []R, deleter Deleter) error {
|
2022-12-21 22:33:51 +00:00
|
|
|
keep := sets.New[string]()
|
2022-09-18 09:12:29 +00:00
|
|
|
for _, obj := range expected {
|
|
|
|
keep.Insert(obj.GetName())
|
|
|
|
}
|
|
|
|
for _, obj := range actual {
|
|
|
|
if !keep.Has(obj.GetName()) {
|
2022-09-30 11:24:47 +00:00
|
|
|
if err := deleter.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
|
2022-09-18 09:12:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|