Skip to content

Commit

Permalink
Add validating webhook for Release objects
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Pavlov <[email protected]>
  • Loading branch information
Kshatrix committed Dec 3, 2024
1 parent 81b8ce0 commit 4dd408f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ resources:
kind: Release
path: github.com/Mirantis/hmc/api/v1alpha1
version: v1alpha1
webhooks:
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down
4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,9 @@ func setupWebhooks(mgr ctrl.Manager, currentNamespace string) error {
setupLog.Error(err, "unable to create webhook", "webhook", "ProviderTemplate")
return err
}
if err := (&hmcwebhook.ReleaseValidator{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Release")
return err
}
return nil
}
57 changes: 57 additions & 0 deletions internal/webhook/release_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024
//
// 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 webhook

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

hmcv1alpha1 "github.com/Mirantis/hmc/api/v1alpha1"
)

type ReleaseValidator struct {
client.Client
}

// SetupWebhookWithManager will setup the manager to manage the webhooks
func (v *ReleaseValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
v.Client = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
For(&hmcv1alpha1.Release{}).
WithValidator(v).
Complete()
}

var _ webhook.CustomValidator = &ReleaseValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (*ReleaseValidator) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (*ReleaseValidator) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (*ReleaseValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}
29 changes: 29 additions & 0 deletions internal/webhook/release_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024
//
// 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 webhook

import (
. "github.com/onsi/ginkgo/v2"
)

var _ = Describe("Release Webhook", func() {
Context("When creating Release under Validating Webhook", func() {
It("Should deny if a required field is empty", func() {
})

It("Should admit if all required fields are provided", func() {
})
})
})

0 comments on commit 4dd408f

Please sign in to comment.