2022-09-18 09:12:29 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateClient[T metav1.Object] interface {
|
|
|
|
Update(context.Context, T, metav1.UpdateOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteClient[T metav1.Object] interface {
|
|
|
|
Delete(context.Context, string, metav1.DeleteOptions) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteCollectionClient[T metav1.Object] interface {
|
|
|
|
DeleteCollection(context.Context, metav1.DeleteOptions, metav1.ListOptions) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetClient[T metav1.Object] interface {
|
|
|
|
Get(context.Context, string, metav1.GetOptions) (T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type WatchClient[T metav1.Object] interface {
|
|
|
|
Watch(context.Context, metav1.ListOptions) (watch.Interface, 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]
|
|
|
|
UpdateClient[T]
|
|
|
|
DeleteClient[T]
|
|
|
|
DeleteCollectionClient[T]
|
|
|
|
GetClient[T]
|
|
|
|
WatchClient[T]
|
|
|
|
PatchClient[T]
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if reflect.DeepEqual(obj, mutated) {
|
|
|
|
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 {
|
|
|
|
if reflect.DeepEqual(obj, mutated) {
|
|
|
|
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]
|
|
|
|
}, S StatusClient[T]](ctx context.Context, obj T, setter S, build func(T) error,
|
|
|
|
) (T, error) {
|
|
|
|
mutated := obj.DeepCopy()
|
|
|
|
if err := build(mutated); err != nil {
|
|
|
|
var d T
|
|
|
|
return d, err
|
|
|
|
} else {
|
|
|
|
if reflect.DeepEqual(obj, mutated) {
|
|
|
|
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
|
|
|
|
}
|