-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from muvaf/webhook-chains
Add validator and mutator chain executors to be used by provider webhooks
- Loading branch information
Showing
4 changed files
with
435 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2022 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// WithMutationFns allows you to initiate the mutator with given list of mutator | ||
// functions. | ||
func WithMutationFns(fns ...MutateFn) MutatorOption { | ||
return func(m *Mutator) { | ||
m.MutationChain = fns | ||
} | ||
} | ||
|
||
// MutatorOption configures given Mutator. | ||
type MutatorOption func(*Mutator) | ||
|
||
// MutateFn is a single mutating function that can be used by Mutator. | ||
type MutateFn func(ctx context.Context, obj runtime.Object) error | ||
|
||
// NewMutator returns a new instance of Mutator that can be used as CustomDefaulter. | ||
func NewMutator(opts ...MutatorOption) *Mutator { | ||
m := &Mutator{ | ||
MutationChain: []MutateFn{}, | ||
} | ||
for _, f := range opts { | ||
f(m) | ||
} | ||
return m | ||
} | ||
|
||
// Mutator satisfies CustomDefaulter interface with an ordered MutateFn list. | ||
type Mutator struct { | ||
MutationChain []MutateFn | ||
} | ||
|
||
// Default executes the MutatorFns in given order. Its name might sound misleading | ||
// since defaulting seems to be the first use case used by controller-runtime | ||
// but MutatorFns can make any changes on given resource. | ||
func (m *Mutator) Default(ctx context.Context, obj runtime.Object) error { | ||
for _, f := range m.MutationChain { | ||
if err := f(ctx, obj); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2022 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/test" | ||
) | ||
|
||
// Mutator has to satisfy CustomDefaulter interface so that it can be used by | ||
// controller-runtime Manager. | ||
var _ webhook.CustomDefaulter = &Mutator{} | ||
|
||
func TestDefault(t *testing.T) { | ||
type args struct { | ||
obj runtime.Object | ||
fns []MutateFn | ||
} | ||
type want struct { | ||
err error | ||
} | ||
cases := map[string]struct { | ||
reason string | ||
args | ||
want | ||
}{ | ||
"Success": { | ||
reason: "Functions without errors should be executed successfully", | ||
args: args{ | ||
fns: []MutateFn{ | ||
func(_ context.Context, _ runtime.Object) error { | ||
return nil | ||
}, | ||
}, | ||
}, | ||
}, | ||
"Failure": { | ||
reason: "Functions with errors should return with error", | ||
args: args{ | ||
fns: []MutateFn{ | ||
func(_ context.Context, _ runtime.Object) error { | ||
return errBoom | ||
}, | ||
}, | ||
}, | ||
want: want{ | ||
err: errBoom, | ||
}, | ||
}, | ||
} | ||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
v := NewMutator(WithMutationFns(tc.fns...)) | ||
err := v.Default(context.TODO(), tc.args.obj) | ||
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { | ||
t.Errorf("\n%s\nDefault(...): -want, +got\n%s\n", tc.reason, diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright 2022 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// WithValidateCreationFns initializes the Validator with given set of creation | ||
// validation functions. | ||
func WithValidateCreationFns(fns ...ValidateCreateFn) ValidatorOption { | ||
return func(v *Validator) { | ||
v.CreationChain = fns | ||
} | ||
} | ||
|
||
// WithValidateUpdateFns initializes the Validator with given set of update | ||
// validation functions. | ||
func WithValidateUpdateFns(fns ...ValidateUpdateFn) ValidatorOption { | ||
return func(v *Validator) { | ||
v.UpdateChain = fns | ||
} | ||
} | ||
|
||
// WithValidateDeletionFns initializes the Validator with given set of deletion | ||
// validation functions. | ||
func WithValidateDeletionFns(fns ...ValidateDeleteFn) ValidatorOption { | ||
return func(v *Validator) { | ||
v.DeletionChain = fns | ||
} | ||
} | ||
|
||
// ValidatorOption allows you to configure given Validator. | ||
type ValidatorOption func(*Validator) | ||
|
||
// ValidateCreateFn is function type for creation validation. | ||
type ValidateCreateFn func(ctx context.Context, obj runtime.Object) error | ||
|
||
// ValidateUpdateFn is function type for update validation. | ||
type ValidateUpdateFn func(ctx context.Context, oldObj, newObj runtime.Object) error | ||
|
||
// ValidateDeleteFn is function type for deletion validation. | ||
type ValidateDeleteFn func(ctx context.Context, obj runtime.Object) error | ||
|
||
// NewValidator returns a new Validator with no-op defaults. | ||
func NewValidator(opts ...ValidatorOption) *Validator { | ||
vc := &Validator{ | ||
CreationChain: []ValidateCreateFn{}, | ||
UpdateChain: []ValidateUpdateFn{}, | ||
DeletionChain: []ValidateDeleteFn{}, | ||
} | ||
for _, f := range opts { | ||
f(vc) | ||
} | ||
return vc | ||
} | ||
|
||
// Validator runs the given validation chains in order. | ||
type Validator struct { | ||
CreationChain []ValidateCreateFn | ||
UpdateChain []ValidateUpdateFn | ||
DeletionChain []ValidateDeleteFn | ||
} | ||
|
||
// ValidateCreate runs functions in creation chain in order. | ||
func (vc *Validator) ValidateCreate(ctx context.Context, obj runtime.Object) error { | ||
for _, f := range vc.CreationChain { | ||
if err := f(ctx, obj); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateUpdate runs functions in update chain in order. | ||
func (vc *Validator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error { | ||
for _, f := range vc.UpdateChain { | ||
if err := f(ctx, oldObj, newObj); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateDelete runs functions in deletion chain in order. | ||
func (vc *Validator) ValidateDelete(ctx context.Context, obj runtime.Object) error { | ||
for _, f := range vc.DeletionChain { | ||
if err := f(ctx, obj); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.