Skip to content

Commit

Permalink
Merge branch 'kubernetes-sigs:main' into add-fake-client-interception
Browse files Browse the repository at this point in the history
  • Loading branch information
ludydoo committed Apr 25, 2023
2 parents f264518 + a24b949 commit 3b534a4
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 679 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.20'
cache: false
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.1
version: v1.52.1
working-directory: ${{matrix.working-directory}}
36 changes: 31 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,42 @@ linters-settings:
- pkg: sigs.k8s.io/controller-runtime
alias: ctrl
staticcheck:
go: "1.19"
go: "1.20"
stylecheck:
go: "1.19"
go: "1.20"
depguard:
include-go-root: true
packages:
- io/ioutil # https://go.dev/doc/go1.16#ioutil
revive:
rules:
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: exported
- name: if-return
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: superfluous-else
- name: unreachable-code
- name: redefines-builtin-id
#
# Rules in addition to the recommended configuration above.
#
- name: bool-literal-in-expr
- name: constant-logical-expr

issues:
max-same-issues: 0
Expand Down Expand Up @@ -133,9 +162,6 @@ issues:
- linters:
- gosec
text: "G304: Potential file inclusion via variable"
- linters:
- revive
text: "package-comments: should have a package comment"
- linters:
- dupl
path: _test\.go
Expand Down
17 changes: 9 additions & 8 deletions examples/builtins/validatingwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"

logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// +kubebuilder:webhook:path=/validate-v1-pod,mutating=false,failurePolicy=fail,groups="",resources=pods,verbs=create;update,versions=v1,name=vpod.kb.io
Expand All @@ -32,34 +33,34 @@ import (
type podValidator struct{}

// validate admits a pod if a specific annotation exists.
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) error {
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
log := logf.FromContext(ctx)
pod, ok := obj.(*corev1.Pod)
if !ok {
return fmt.Errorf("expected a Pod but got a %T", obj)
return nil, fmt.Errorf("expected a Pod but got a %T", obj)
}

log.Info("Validating Pod")
key := "example-mutating-admission-webhook"
anno, found := pod.Annotations[key]
if !found {
return fmt.Errorf("missing annotation %s", key)
return nil, fmt.Errorf("missing annotation %s", key)
}
if anno != "foo" {
return fmt.Errorf("annotation %s did not have value %q", key, "foo")
return nil, fmt.Errorf("annotation %s did not have value %q", key, "foo")
}

return nil
return nil, nil
}

func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return v.validate(ctx, obj)
}

func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error {
func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
return v.validate(ctx, newObj)
}

func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error {
func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return v.validate(ctx, obj)
}
23 changes: 12 additions & 11 deletions examples/crd/pkg/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// ChaosPodSpec defines the desired state of ChaosPod
Expand Down Expand Up @@ -66,41 +67,41 @@ type ChaosPodList struct {
var _ webhook.Validator = &ChaosPod{}

// ValidateCreate implements webhookutil.validator so a webhook will be registered for the type
func (c *ChaosPod) ValidateCreate() error {
func (c *ChaosPod) ValidateCreate() (admission.Warnings, error) {
log.Info("validate create", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
return fmt.Errorf(".spec.nextStop must be later than current time")
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
}
return nil
return nil, nil
}

// ValidateUpdate implements webhookutil.validator so a webhook will be registered for the type
func (c *ChaosPod) ValidateUpdate(old runtime.Object) error {
func (c *ChaosPod) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
log.Info("validate update", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
return fmt.Errorf(".spec.nextStop must be later than current time")
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
}

oldC, ok := old.(*ChaosPod)
if !ok {
return fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
return nil, fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
}
if c.Spec.NextStop.After(oldC.Spec.NextStop.Add(time.Hour)) {
return fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
return nil, fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
}
return nil
return nil, nil
}

// ValidateDelete implements webhookutil.validator so a webhook will be registered for the type
func (c *ChaosPod) ValidateDelete() error {
func (c *ChaosPod) ValidateDelete() (admission.Warnings, error) {
log.Info("validate delete", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
return fmt.Errorf(".spec.nextStop must be later than current time")
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
}
return nil
return nil, nil
}

// +kubebuilder:webhook:path=/mutate-chaosapps-metamagical-io-v1-chaospod,mutating=true,failurePolicy=fail,groups=chaosapps.metamagical.io,resources=chaospods,verbs=create;update,versions=v1,name=mchaospod.kb.io
Expand Down
17 changes: 9 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module sigs.k8s.io/controller-runtime

go 1.19
go 1.20

require (
github.com/evanphx/json-patch/v5 v5.6.0
Expand All @@ -10,7 +10,7 @@ require (
github.com/google/go-cmp v0.5.9
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.6
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_golang v1.15.0
github.com/prometheus/client_model v0.3.0
go.uber.org/goleak v1.2.1
go.uber.org/zap v1.24.0
Expand All @@ -29,7 +29,7 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
Expand All @@ -48,23 +48,24 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down

0 comments on commit 3b534a4

Please sign in to comment.