Skip to content
Open
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
31 changes: 18 additions & 13 deletions api/v1alpha1/helmchartproxy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"context"
"fmt"
"net/url"
"time"
Expand All @@ -27,7 +28,6 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

Expand All @@ -44,12 +44,12 @@ func (r *HelmChartProxy) SetupWebhookWithManager(mgr ctrl.Manager) error {

//+kubebuilder:webhook:path=/mutate-addons-cluster-x-k8s-io-v1alpha1-helmchartproxy,mutating=true,failurePolicy=fail,sideEffects=None,groups=addons.cluster.x-k8s.io,resources=helmchartproxies,verbs=create;update,versions=v1alpha1,name=helmchartproxy.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &HelmChartProxy{}
var _ admission.CustomDefaulter = &HelmChartProxy{}

const helmTimeout = 10 * time.Minute

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (p *HelmChartProxy) Default() {
// Default implements admission.CustomDefaulter so a webhook will be registered for the type.
func (p *HelmChartProxy) Default(ctx context.Context, obj runtime.Object) error {
helmchartproxylog.Info("default", "name", p.Name)

if p.Spec.ReleaseNamespace == "" {
Expand All @@ -64,15 +64,16 @@ func (p *HelmChartProxy) Default() {
if p.Spec.Options.Timeout == nil {
p.Spec.Options.Timeout = &metav1.Duration{Duration: helmTimeout}
}
return nil
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-addons-cluster-x-k8s-io-v1alpha1-helmchartproxy,mutating=false,failurePolicy=fail,sideEffects=None,groups=addons.cluster.x-k8s.io,resources=helmchartproxies,verbs=create;update,versions=v1alpha1,name=vhelmchartproxy.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &HelmChartProxy{}
var _ admission.CustomValidator = &HelmChartProxy{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (p *HelmChartProxy) ValidateCreate() (admission.Warnings, error) {
// ValidateCreate implements admission.CustomValidator so a webhook will be registered for the type.
func (p *HelmChartProxy) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
helmchartproxylog.Info("validate create", "name", p.Name)

if err := isUrlValid(p.Spec.RepoURL); err != nil {
Expand All @@ -82,14 +83,18 @@ func (p *HelmChartProxy) ValidateCreate() (admission.Warnings, error) {
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (p *HelmChartProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
// ValidateUpdate implements admission.CustomValidator so a webhook will be registered for the type.
func (p *HelmChartProxy) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
helmchartproxylog.Info("validate update", "name", p.Name)

var allErrs field.ErrorList
old, ok := oldRaw.(*HelmChartProxy)
old, ok := oldObj.(*HelmChartProxy)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmChartProxy but got a %T", oldObj))
}
p, ok = newObj.(*HelmChartProxy)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmChartProxy but got a %T", old))
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmChartProxy but got a %T", newObj))
}

if err := isUrlValid(p.Spec.RepoURL); err != nil {
Expand All @@ -113,8 +118,8 @@ func (p *HelmChartProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warnin
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (p *HelmChartProxy) ValidateDelete() (admission.Warnings, error) {
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type.
func (p *HelmChartProxy) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
helmchartproxylog.Info("validate delete", "name", p.Name)

// TODO(user): fill in your validation logic upon object deletion.
Expand Down
31 changes: 18 additions & 13 deletions api/v1alpha1/helmreleaseproxy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"context"
"fmt"
"reflect"

Expand All @@ -25,7 +26,6 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

Expand All @@ -42,38 +42,43 @@ func (r *HelmReleaseProxy) SetupWebhookWithManager(mgr ctrl.Manager) error {

//+kubebuilder:webhook:path=/mutate-addons-cluster-x-k8s-io-v1alpha1-helmreleaseproxy,mutating=true,failurePolicy=fail,sideEffects=None,groups=addons.cluster.x-k8s.io,resources=helmreleaseproxies,verbs=create;update,versions=v1alpha1,name=helmreleaseproxy.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &HelmReleaseProxy{}
var _ admission.CustomDefaulter = &HelmReleaseProxy{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (p *HelmReleaseProxy) Default() {
// Default implements admission.CustomDefaulter so a webhook will be registered for the type.
func (p *HelmReleaseProxy) Default(ctx context.Context, obj runtime.Object) error {
helmreleaseproxylog.Info("default", "name", p.Name)

if p.Spec.ReleaseNamespace == "" {
p.Spec.ReleaseNamespace = "default"
}
return nil
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-addons-cluster-x-k8s-io-v1alpha1-helmreleaseproxy,mutating=false,failurePolicy=fail,sideEffects=None,groups=addons.cluster.x-k8s.io,resources=helmreleaseproxies,verbs=create;update,versions=v1alpha1,name=vhelmreleaseproxy.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &HelmReleaseProxy{}
var _ admission.CustomValidator = &HelmReleaseProxy{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (p *HelmReleaseProxy) ValidateCreate() (admission.Warnings, error) {
// ValidateCreate implements admission.CustomValidator so a webhook will be registered for the type.
func (p *HelmReleaseProxy) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
helmreleaseproxylog.Info("validate create", "name", p.Name)

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

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (p *HelmReleaseProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
// ValidateUpdate implements admission.CustomValidator so a webhook will be registered for the type.
func (p *HelmReleaseProxy) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
helmreleaseproxylog.Info("validate update", "name", p.Name)

var allErrs field.ErrorList
old, ok := oldRaw.(*HelmReleaseProxy)
old, ok := oldObj.(*HelmReleaseProxy)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmReleaseProxy but got a %T", old))
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmReleaseProxy but got a %T", oldObj))
}
p, ok = newObj.(*HelmReleaseProxy)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmReleaseProxy but got a %T", newObj))
}

if !reflect.DeepEqual(p.Spec.RepoURL, old.Spec.RepoURL) {
Expand Down Expand Up @@ -113,8 +118,8 @@ func (p *HelmReleaseProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warn
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmReleaseProxy) ValidateDelete() (admission.Warnings, error) {
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type.
func (r *HelmReleaseProxy) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
helmreleaseproxylog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
Expand Down
Loading