Skip to content
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.23.4-dev
v0.24.0
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.27.2
github.com/onsi/gomega v1.38.2
github.com/openmcp-project/controller-utils/api v0.23.4
github.com/openmcp-project/controller-utils/api v0.24.0
github.com/spf13/pflag v1.0.10
github.com/stretchr/testify v1.11.1
go.uber.org/zap v1.27.0
Expand Down
39 changes: 22 additions & 17 deletions pkg/init/webhooks/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

var (
Expand Down Expand Up @@ -66,11 +65,21 @@ func GenerateCertificate(ctx context.Context, c client.Client, options ...CertOp
return err
}

// APITypes defines an API type along with whether it has a validator and/or defaulter webhook.
type APITypes struct {
// Obj is the API type object.
Obj client.Object
// Validator indicates whether the type has a validating webhook.
Validator bool
// Defaulter indicates whether the type has a mutating webhook.
Defaulter bool
}

func Install(
ctx context.Context,
c client.Client,
scheme *runtime.Scheme,
apiTypes []client.Object,
apiTypes []APITypes,
options ...InstallOption,
) error {
opts := &installOptions{
Expand Down Expand Up @@ -104,16 +113,14 @@ func Install(
}
}

for _, o := range apiTypes {
_, isCustomValidator := o.(webhook.CustomValidator)
if isCustomValidator {
if err := applyValidatingWebhook(ctx, opts, o); err != nil {
for _, t := range apiTypes {
if t.Validator {
if err := applyValidatingWebhook(ctx, opts, t.Obj); err != nil {
return err
}
}
_, isCustomDefaulter := o.(webhook.CustomDefaulter)
if isCustomDefaulter {
if err := applyMutatingWebhook(ctx, opts, o); err != nil {
if t.Defaulter {
if err := applyMutatingWebhook(ctx, opts, t.Obj); err != nil {
return err
}
}
Expand All @@ -127,7 +134,7 @@ func Uninstall(
ctx context.Context,
c client.Client,
scheme *runtime.Scheme,
apiTypes []client.Object,
apiTypes []APITypes,
options ...InstallOption,
) error {
opts := &installOptions{
Expand All @@ -148,16 +155,14 @@ func Uninstall(
}
}

for _, o := range apiTypes {
_, isCustomValidator := o.(webhook.CustomValidator)
if isCustomValidator {
if err := removeValidatingWebhook(ctx, opts, o); err != nil {
for _, t := range apiTypes {
if t.Validator {
if err := removeValidatingWebhook(ctx, opts, t.Obj); err != nil {
return err
}
}
_, isCustomDefaulter := o.(webhook.CustomDefaulter)
if isCustomDefaulter {
if err := removeMutatingWebhook(ctx, opts, o); err != nil {
if t.Defaulter {
if err := removeMutatingWebhook(ctx, opts, t.Obj); err != nil {
return err
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/init/webhooks/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,14 @@ func Test_Install(t *testing.T) {
t.Fatal(err)
}

testErr := Install(ctx, c, c.Scheme(), []client.Object{&TestObj{}}, tC.options...)
apiTypes := []APITypes{
{
Obj: &TestObj{},
Validator: true,
Defaulter: true,
},
}
testErr := Install(ctx, c, c.Scheme(), apiTypes, tC.options...)

if err := tC.validate(ctx, c, t, testErr); err != nil {
t.Fatal(err)
Expand Down
Loading