Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix the webhook creation for defaulting webhooks #1718

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions generate_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ scaffold_test_project() {
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
$kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false
$kb create webhook --group crew --version v1 --kind Admiral --defaulting
elif [ $project == "project-v2-multigroup" ] || [ $project == "project-v3-multigroup" ]; then
header_text 'Switching to multigroup layout ...'
$kb edit --multigroup=true
Expand All @@ -74,7 +75,9 @@ scaffold_test_project() {
$kb create api --group ship --version v1beta1 --kind Frigate --controller=true --resource=true --make=false
$kb create webhook --group ship --version v1beta1 --kind Frigate --conversion
$kb create api --group ship --version v1 --kind Destroyer --controller=true --resource=true --namespaced=false --make=false
$kb create webhook --group ship --version v1 --kind Destroyer --defaulting
$kb create api --group ship --version v2alpha1 --kind Cruiser --controller=true --resource=true --namespaced=false --make=false
$kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation
$kb create api --group sea-creatures --version v1beta1 --kind Kraken --controller=true --resource=true --make=false
$kb create api --group sea-creatures --version v1beta2 --kind Leviathan --controller=true --resource=true --make=false
$kb create api --group foo.policy --version v1 --kind HealthCheckPolicy --controller=true --resource=true --make=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ package {{ .Resource.Version }}
import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
{{- if or .Validating .Defaulting }}
{{- if or .Validating }}
"k8s.io/apimachinery/pkg/runtime"
{{- end }}
{{- if or .Validating .Defaulting }}
"sigs.k8s.io/controller-runtime/pkg/webhook"
{{- end }}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ package {{ .Resource.Version }}
import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
{{- if or .Validating .Defaulting }}
{{- if .Validating }}
"k8s.io/apimachinery/pkg/runtime"
{{- end }}
{{- if or .Validating .Defaulting }}
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
"sigs.k8s.io/controller-runtime/pkg/webhook"
{{- end }}
)
Expand Down
45 changes: 45 additions & 0 deletions testdata/project-v2-multigroup/apis/ship/v1/destroyer_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2020 The Kubernetes 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 v1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var destroyerlog = logf.Log.WithName("destroyer-resource")

func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// +kubebuilder:webhook:path=/mutate-ship-testproject-org-v1-destroyer,mutating=true,failurePolicy=fail,groups=ship.testproject.org,resources=destroyers,verbs=create;update,versions=v1,name=mdestroyer.kb.io

var _ webhook.Defaulter = &Destroyer{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Destroyer) Default() {
destroyerlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2020 The Kubernetes 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 v2alpha1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var cruiserlog = logf.Log.WithName("cruiser-resource")

func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:verbs=create;update,path=/validate-ship-testproject-org-v2alpha1-cruiser,mutating=false,failurePolicy=fail,groups=ship.testproject.org,resources=cruisers,versions=v2alpha1,name=vcruiser.kb.io

var _ webhook.Validator = &Cruiser{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Cruiser) ValidateCreate() error {
cruiserlog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Cruiser) ValidateUpdate(old runtime.Object) error {
cruiserlog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Cruiser) ValidateDelete() error {
cruiserlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions testdata/project-v2-multigroup/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ webhooks:
- UPDATE
resources:
- captains
- clientConfig:
caBundle: Cg==
service:
name: webhook-service
namespace: system
path: /mutate-ship-testproject-org-v1-destroyer
failurePolicy: Fail
name: mdestroyer.kb.io
rules:
- apiGroups:
- ship.testproject.org
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- destroyers

---
apiVersion: admissionregistration.k8s.io/v1beta1
Expand All @@ -50,3 +68,21 @@ webhooks:
- UPDATE
resources:
- captains
- clientConfig:
caBundle: Cg==
service:
name: webhook-service
namespace: system
path: /validate-ship-testproject-org-v2alpha1-cruiser
failurePolicy: Fail
name: vcruiser.kb.io
rules:
- apiGroups:
- ship.testproject.org
apiVersions:
- v2alpha1
operations:
- CREATE
- UPDATE
resources:
- cruisers
8 changes: 8 additions & 0 deletions testdata/project-v2-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Destroyer")
os.Exit(1)
}
if err = (&shipv1.Destroyer{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Destroyer")
os.Exit(1)
}
if err = (&shipcontroller.CruiserReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Cruiser"),
Expand All @@ -122,6 +126,10 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Cruiser")
os.Exit(1)
}
if err = (&shipv2alpha1.Cruiser{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Cruiser")
os.Exit(1)
}
if err = (&seacreaturescontroller.KrakenReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Kraken"),
Expand Down
45 changes: 45 additions & 0 deletions testdata/project-v2/api/v1/admiral_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2020 The Kubernetes 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 v1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var admirallog = logf.Log.WithName("admiral-resource")

func (r *Admiral) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// +kubebuilder:webhook:path=/mutate-crew-testproject-org-v1-admiral,mutating=true,failurePolicy=fail,groups=crew.testproject.org,resources=admirals,verbs=create;update,versions=v1,name=madmiral.kb.io

var _ webhook.Defaulter = &Admiral{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Admiral) Default() {
admirallog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}
18 changes: 18 additions & 0 deletions testdata/project-v2/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ metadata:
creationTimestamp: null
name: mutating-webhook-configuration
webhooks:
- clientConfig:
caBundle: Cg==
service:
name: webhook-service
namespace: system
path: /mutate-crew-testproject-org-v1-admiral
failurePolicy: Fail
name: madmiral.kb.io
rules:
- apiGroups:
- crew.testproject.org
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- admirals
- clientConfig:
caBundle: Cg==
service:
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Admiral")
os.Exit(1)
}
if err = (&crewv1.Admiral{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Admiral")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

setupLog.Info("starting manager")
Expand Down
45 changes: 45 additions & 0 deletions testdata/project-v3-multigroup/apis/ship/v1/destroyer_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2020 The Kubernetes 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 v1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var destroyerlog = logf.Log.WithName("destroyer-resource")

func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// +kubebuilder:webhook:path=/mutate-ship-testproject-org-v1-destroyer,mutating=true,failurePolicy=fail,groups=ship.testproject.org,resources=destroyers,verbs=create;update,versions=v1,name=mdestroyer.kb.io

var _ webhook.Defaulter = &Destroyer{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Destroyer) Default() {
destroyerlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}
Loading