2021-01-13 01:27:41 +00:00
|
|
|
// Copyright 2021 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package kubeclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2021-02-05 01:02:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/errors"
|
2021-01-13 01:27:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Middleware interface {
|
|
|
|
Handle(ctx context.Context, rt RoundTrip)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Middleware = MiddlewareFunc(nil)
|
|
|
|
|
|
|
|
type MiddlewareFunc func(ctx context.Context, rt RoundTrip)
|
|
|
|
|
|
|
|
func (f MiddlewareFunc) Handle(ctx context.Context, rt RoundTrip) {
|
|
|
|
f(ctx, rt)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Middleware = Middlewares{}
|
|
|
|
|
|
|
|
type Middlewares []Middleware
|
|
|
|
|
|
|
|
func (m Middlewares) Handle(ctx context.Context, rt RoundTrip) {
|
|
|
|
for _, middleware := range m {
|
|
|
|
middleware := middleware
|
|
|
|
middleware.Handle(ctx, rt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoundTrip interface {
|
|
|
|
Verb() Verb
|
|
|
|
Namespace() string // this is the only valid way to check namespace, Object.GetNamespace() will almost always be empty
|
|
|
|
NamespaceScoped() bool
|
|
|
|
Resource() schema.GroupVersionResource
|
|
|
|
Subresource() string
|
2021-02-05 01:02:59 +00:00
|
|
|
MutateRequest(f func(obj Object) error)
|
|
|
|
MutateResponse(f func(obj Object) error)
|
2021-01-13 01:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Object interface {
|
|
|
|
runtime.Object // generic access to TypeMeta
|
|
|
|
metav1.Object // generic access to ObjectMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ RoundTrip = &request{}
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
verb Verb
|
|
|
|
namespace string
|
|
|
|
resource schema.GroupVersionResource
|
2021-02-05 01:02:59 +00:00
|
|
|
reqFuncs, respFuncs []func(obj Object) error
|
2021-01-13 01:27:41 +00:00
|
|
|
subresource string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) Verb() Verb {
|
|
|
|
return r.verb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) Namespace() string {
|
|
|
|
return r.namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint: gochecknoglobals
|
|
|
|
var namespaceGVR = corev1.SchemeGroupVersion.WithResource("namespaces")
|
|
|
|
|
|
|
|
func (r *request) NamespaceScoped() bool {
|
|
|
|
if r.Resource() == namespaceGVR {
|
|
|
|
return false // always consider namespaces to be cluster scoped
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(r.Namespace()) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) Resource() schema.GroupVersionResource {
|
|
|
|
return r.resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) Subresource() string {
|
|
|
|
return r.subresource
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:02:59 +00:00
|
|
|
func (r *request) MutateRequest(f func(obj Object) error) {
|
2021-01-13 01:27:41 +00:00
|
|
|
r.reqFuncs = append(r.reqFuncs, f)
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:02:59 +00:00
|
|
|
func (r *request) MutateResponse(f func(obj Object) error) {
|
2021-01-13 01:27:41 +00:00
|
|
|
r.respFuncs = append(r.respFuncs, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mutationResult struct {
|
|
|
|
origGVK, newGVK schema.GroupVersionKind
|
|
|
|
gvkChanged, mutated bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) mutateRequest(obj Object) (*mutationResult, error) {
|
|
|
|
origGVK := obj.GetObjectKind().GroupVersionKind()
|
|
|
|
if origGVK.Empty() {
|
|
|
|
return nil, fmt.Errorf("invalid empty orig GVK for %T: %#v", obj, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
origObj, ok := obj.DeepCopyObject().(Object)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid deep copy semantics for %T: %#v", obj, r)
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:02:59 +00:00
|
|
|
var errs []error
|
2021-01-13 01:27:41 +00:00
|
|
|
for _, reqFunc := range r.reqFuncs {
|
|
|
|
reqFunc := reqFunc
|
2021-02-05 01:02:59 +00:00
|
|
|
if err := reqFunc(obj); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := errors.NewAggregate(errs); err != nil {
|
|
|
|
return nil, fmt.Errorf("request mutation failed: %w", err)
|
2021-01-13 01:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newGVK := obj.GetObjectKind().GroupVersionKind()
|
|
|
|
if newGVK.Empty() {
|
|
|
|
return nil, fmt.Errorf("invalid empty new GVK for %T: %#v", obj, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mutationResult{
|
|
|
|
origGVK: origGVK,
|
|
|
|
newGVK: newGVK,
|
|
|
|
gvkChanged: origGVK != newGVK,
|
|
|
|
mutated: len(r.respFuncs) != 0 || !apiequality.Semantic.DeepEqual(origObj, obj),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) mutateResponse(obj Object) (bool, error) {
|
|
|
|
origObj, ok := obj.DeepCopyObject().(Object)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("invalid deep copy semantics for %T: %#v", obj, r)
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:02:59 +00:00
|
|
|
var errs []error
|
2021-01-13 01:27:41 +00:00
|
|
|
for _, respFunc := range r.respFuncs {
|
|
|
|
respFunc := respFunc
|
2021-02-05 01:02:59 +00:00
|
|
|
if err := respFunc(obj); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := errors.NewAggregate(errs); err != nil {
|
|
|
|
return false, fmt.Errorf("response mutation failed: %w", err)
|
2021-01-13 01:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutated := !apiequality.Semantic.DeepEqual(origObj, obj)
|
|
|
|
return mutated, nil
|
|
|
|
}
|