Skip to content

Commit 08e316e

Browse files
authored
Added Webhooks for IBMPowerVSClusterTemplate (#1174)
Moved webhooks to latest API v1beta2
1 parent 9c33955 commit 08e316e

File tree

7 files changed

+223
-0
lines changed

7 files changed

+223
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta2
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
26+
ctrl "sigs.k8s.io/controller-runtime"
27+
logf "sigs.k8s.io/controller-runtime/pkg/log"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook"
29+
)
30+
31+
// log is for logging in this package.
32+
var ibmpowervsclustertemplatelog = logf.Log.WithName("ibmpowervsclustertemplate-resource")
33+
34+
func (r *IBMPowerVSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
35+
return ctrl.NewWebhookManagedBy(mgr).
36+
For(r).
37+
Complete()
38+
}
39+
40+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,verbs=create;update,versions=v1beta2,name=mibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
41+
42+
var _ webhook.Defaulter = &IBMPowerVSClusterTemplate{}
43+
44+
// Default implements webhook.Defaulter so a webhook will be registered for the type.
45+
func (r *IBMPowerVSClusterTemplate) Default() {
46+
ibmpowervsclustertemplatelog.Info("default", "name", r.Name)
47+
}
48+
49+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,versions=v1beta2,name=vibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
50+
51+
var _ webhook.Validator = &IBMPowerVSClusterTemplate{}
52+
53+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
54+
func (r *IBMPowerVSClusterTemplate) ValidateCreate() error {
55+
ibmpowervsclustertemplatelog.Info("validate create", "name", r.Name)
56+
57+
return nil
58+
}
59+
60+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
61+
func (r *IBMPowerVSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
62+
ibmpowervsclustertemplatelog.Info("validate update", "name", r.Name)
63+
old, ok := oldRaw.(*IBMPowerVSClusterTemplate)
64+
if !ok {
65+
return apierrors.NewBadRequest(fmt.Sprintf("expected an IBMPowerVSClusterTemplate but got a %T", oldRaw))
66+
}
67+
if !reflect.DeepEqual(r.Spec, old.Spec) {
68+
return apierrors.NewBadRequest("IBMPowerVSClusterTemplate.Spec is immutable")
69+
}
70+
return nil
71+
}
72+
73+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
74+
func (r *IBMPowerVSClusterTemplate) ValidateDelete() error {
75+
ibmpowervsclustertemplatelog.Info("validate delete", "name", r.Name)
76+
77+
return nil
78+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta2
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
func TestIBMPowerVSClusterTemplate_ValidateUpdate(t *testing.T) {
26+
g := NewWithT(t)
27+
28+
tests := []struct {
29+
name string
30+
newTemplate *IBMPowerVSClusterTemplate
31+
oldTemplate *IBMPowerVSClusterTemplate
32+
wantErr bool
33+
}{
34+
{
35+
name: "IBMPowerVSClusterTemplate with immutable spec",
36+
newTemplate: &IBMPowerVSClusterTemplate{
37+
Spec: IBMPowerVSClusterTemplateSpec{
38+
Template: IBMPowerVSClusterTemplateResource{
39+
Spec: IBMPowerVSClusterSpec{
40+
ServiceInstanceID: "test-instance1",
41+
},
42+
},
43+
},
44+
},
45+
oldTemplate: &IBMPowerVSClusterTemplate{
46+
Spec: IBMPowerVSClusterTemplateSpec{
47+
Template: IBMPowerVSClusterTemplateResource{
48+
Spec: IBMPowerVSClusterSpec{
49+
ServiceInstanceID: "test-instance1",
50+
},
51+
},
52+
},
53+
},
54+
wantErr: false,
55+
},
56+
{
57+
name: " IBMPowerVSClusterTemplate with mutable spec",
58+
newTemplate: &IBMPowerVSClusterTemplate{
59+
Spec: IBMPowerVSClusterTemplateSpec{
60+
Template: IBMPowerVSClusterTemplateResource{
61+
Spec: IBMPowerVSClusterSpec{
62+
ServiceInstanceID: "test-instance1",
63+
},
64+
},
65+
},
66+
},
67+
oldTemplate: &IBMPowerVSClusterTemplate{
68+
Spec: IBMPowerVSClusterTemplateSpec{
69+
Template: IBMPowerVSClusterTemplateResource{
70+
Spec: IBMPowerVSClusterSpec{
71+
ServiceInstanceID: "test-instance2",
72+
},
73+
},
74+
},
75+
},
76+
wantErr: true,
77+
},
78+
}
79+
for _, test := range tests {
80+
t.Run(test.name, func(t *testing.T) {
81+
err := test.newTemplate.ValidateUpdate(test.oldTemplate)
82+
if test.wantErr {
83+
g.Expect(err).To(HaveOccurred())
84+
} else {
85+
g.Expect(err).NotTo(HaveOccurred())
86+
}
87+
})
88+
}
89+
}

api/v1beta2/suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func setup() {
7272
if err := (&IBMVPCMachineTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
7373
panic(fmt.Sprintf("Unable to setup IBMVPCMachineTemplate webhook: %v", err))
7474
}
75+
if err := (&IBMPowerVSClusterTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
76+
panic(fmt.Sprintf("Unable to setup IBMPowerVSClusterTemplate webhook: %v", err))
77+
}
78+
7579
go func() {
7680
fmt.Println("Starting the manager")
7781
if err := testEnv.StartManager(ctx); err != nil {

cloud/scope/suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func setup() {
7575
if err := (&infrav1beta2.IBMVPCMachineTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
7676
panic(fmt.Sprintf("Unable to setup IBMVPCMachineTemplate webhook: %v", err))
7777
}
78+
if err := (&infrav1beta2.IBMPowerVSClusterTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
79+
panic(fmt.Sprintf("Unable to setup IBMPowerVSClusterTemplate webhook: %v", err))
80+
}
7881
go func() {
7982
fmt.Println("Starting the manager")
8083
if err := testEnv.StartManager(ctx); err != nil {

config/webhook/manifests.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ webhooks:
2626
resources:
2727
- ibmpowervsclusters
2828
sideEffects: None
29+
- admissionReviewVersions:
30+
- v1
31+
- v1beta1
32+
clientConfig:
33+
service:
34+
name: webhook-service
35+
namespace: system
36+
path: /mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate
37+
failurePolicy: Fail
38+
name: mibmpowervsclustertemplate.kb.io
39+
rules:
40+
- apiGroups:
41+
- infrastructure.cluster.x-k8s.io
42+
apiVersions:
43+
- v1beta2
44+
operations:
45+
- CREATE
46+
- UPDATE
47+
resources:
48+
- ibmpowervsclustertemplates
49+
sideEffects: None
2950
- admissionReviewVersions:
3051
- v1
3152
- v1beta1
@@ -180,6 +201,27 @@ webhooks:
180201
resources:
181202
- ibmpowervsclusters
182203
sideEffects: None
204+
- admissionReviewVersions:
205+
- v1
206+
- v1beta1
207+
clientConfig:
208+
service:
209+
name: webhook-service
210+
namespace: system
211+
path: /validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate
212+
failurePolicy: Fail
213+
name: vibmpowervsclustertemplate.kb.io
214+
rules:
215+
- apiGroups:
216+
- infrastructure.cluster.x-k8s.io
217+
apiVersions:
218+
- v1beta2
219+
operations:
220+
- CREATE
221+
- UPDATE
222+
resources:
223+
- ibmpowervsclustertemplates
224+
sideEffects: None
183225
- admissionReviewVersions:
184226
- v1
185227
- v1beta1

controllers/suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func setup() {
7676
if err := (&infrav1beta2.IBMVPCMachineTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
7777
panic(fmt.Sprintf("Unable to setup IBMVPCMachineTemplate webhook: %v", err))
7878
}
79+
if err := (&infrav1beta2.IBMPowerVSClusterTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
80+
panic(fmt.Sprintf("Unable to setup IBMPowerVSClusterTemplate webhook: %v", err))
81+
}
7982
go func() {
8083
fmt.Println("Starting the manager")
8184
if err := testEnv.StartManager(ctx); err != nil {

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ func setupWebhooks(mgr ctrl.Manager) {
300300
setupLog.Error(err, "unable to create webhook", "webhook", "IBMPowerVSImage")
301301
os.Exit(1)
302302
}
303+
if err := (&infrav1beta2.IBMPowerVSClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
304+
setupLog.Error(err, "unable to create webhook", "webhook", "IBMPowerVSClusterTemplate")
305+
os.Exit(1)
306+
}
303307
}
304308

305309
func setupChecks(mgr ctrl.Manager) {

0 commit comments

Comments
 (0)