Skip to content

Commit 777ed92

Browse files
authored
fix: apply webhook configurations correctly (#178)
* fix: apply webhook configurations correctly * feat: release v0.24.0
1 parent 23fac2e commit 777ed92

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.23.4-dev
1+
v0.24.0

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/google/uuid v1.6.0
1212
github.com/onsi/ginkgo/v2 v2.27.2
1313
github.com/onsi/gomega v1.38.2
14-
github.com/openmcp-project/controller-utils/api v0.23.4
14+
github.com/openmcp-project/controller-utils/api v0.24.0
1515
github.com/spf13/pflag v1.0.10
1616
github.com/stretchr/testify v1.11.1
1717
go.uber.org/zap v1.27.0

pkg/init/webhooks/init.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"k8s.io/apimachinery/pkg/runtime"
1111
"sigs.k8s.io/controller-runtime/pkg/client"
1212
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
13-
"sigs.k8s.io/controller-runtime/pkg/webhook"
1413
)
1514

1615
var (
@@ -66,11 +65,21 @@ func GenerateCertificate(ctx context.Context, c client.Client, options ...CertOp
6665
return err
6766
}
6867

68+
// APITypes defines an API type along with whether it has a validator and/or defaulter webhook.
69+
type APITypes struct {
70+
// Obj is the API type object.
71+
Obj client.Object
72+
// Validator indicates whether the type has a validating webhook.
73+
Validator bool
74+
// Defaulter indicates whether the type has a mutating webhook.
75+
Defaulter bool
76+
}
77+
6978
func Install(
7079
ctx context.Context,
7180
c client.Client,
7281
scheme *runtime.Scheme,
73-
apiTypes []client.Object,
82+
apiTypes []APITypes,
7483
options ...InstallOption,
7584
) error {
7685
opts := &installOptions{
@@ -104,16 +113,14 @@ func Install(
104113
}
105114
}
106115

107-
for _, o := range apiTypes {
108-
_, isCustomValidator := o.(webhook.CustomValidator)
109-
if isCustomValidator {
110-
if err := applyValidatingWebhook(ctx, opts, o); err != nil {
116+
for _, t := range apiTypes {
117+
if t.Validator {
118+
if err := applyValidatingWebhook(ctx, opts, t.Obj); err != nil {
111119
return err
112120
}
113121
}
114-
_, isCustomDefaulter := o.(webhook.CustomDefaulter)
115-
if isCustomDefaulter {
116-
if err := applyMutatingWebhook(ctx, opts, o); err != nil {
122+
if t.Defaulter {
123+
if err := applyMutatingWebhook(ctx, opts, t.Obj); err != nil {
117124
return err
118125
}
119126
}
@@ -127,7 +134,7 @@ func Uninstall(
127134
ctx context.Context,
128135
c client.Client,
129136
scheme *runtime.Scheme,
130-
apiTypes []client.Object,
137+
apiTypes []APITypes,
131138
options ...InstallOption,
132139
) error {
133140
opts := &installOptions{
@@ -148,16 +155,14 @@ func Uninstall(
148155
}
149156
}
150157

151-
for _, o := range apiTypes {
152-
_, isCustomValidator := o.(webhook.CustomValidator)
153-
if isCustomValidator {
154-
if err := removeValidatingWebhook(ctx, opts, o); err != nil {
158+
for _, t := range apiTypes {
159+
if t.Validator {
160+
if err := removeValidatingWebhook(ctx, opts, t.Obj); err != nil {
155161
return err
156162
}
157163
}
158-
_, isCustomDefaulter := o.(webhook.CustomDefaulter)
159-
if isCustomDefaulter {
160-
if err := removeMutatingWebhook(ctx, opts, o); err != nil {
164+
if t.Defaulter {
165+
if err := removeMutatingWebhook(ctx, opts, t.Obj); err != nil {
161166
return err
162167
}
163168
}

pkg/init/webhooks/init_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,14 @@ func Test_Install(t *testing.T) {
271271
t.Fatal(err)
272272
}
273273

274-
testErr := Install(ctx, c, c.Scheme(), []client.Object{&TestObj{}}, tC.options...)
274+
apiTypes := []APITypes{
275+
{
276+
Obj: &TestObj{},
277+
Validator: true,
278+
Defaulter: true,
279+
},
280+
}
281+
testErr := Install(ctx, c, c.Scheme(), apiTypes, tC.options...)
275282

276283
if err := tC.validate(ctx, c, t, testErr); err != nil {
277284
t.Fatal(err)

0 commit comments

Comments
 (0)