-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move docker infrastructure API v1beta1 webhooks to separate package
- Loading branch information
Showing
11 changed files
with
344 additions
and
211 deletions.
There are no files selected for viewing
73 changes: 0 additions & 73 deletions
73
test/infrastructure/docker/api/v1beta1/dockercluster_webhook.go
This file was deleted.
Oops, something went wrong.
95 changes: 0 additions & 95 deletions
95
test/infrastructure/docker/api/v1beta1/dockerclustertemplate_webhook.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
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 webhooks implements docker infrastructure webhooks. | ||
package webhooks |
92 changes: 92 additions & 0 deletions
92
test/infrastructure/docker/internal/webhooks/dockercluster_webhook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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 webhooks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1" | ||
) | ||
|
||
// DockerCluster implements a validating and defaulting webhook for DockerCluster. | ||
type DockerCluster struct{} | ||
|
||
func (webhook *DockerCluster) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&infrav1.DockerCluster{}). | ||
WithDefaulter(webhook). | ||
WithValidator(webhook). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-infrav1-dockercluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockerclusters,versions=infrav1,name=default.dockercluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;infrav1 | ||
|
||
var _ webhook.CustomDefaulter = &DockerCluster{} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type. | ||
func (webhook *DockerCluster) Default(_ context.Context, obj runtime.Object) error { | ||
cluster, ok := obj.(*infrav1.DockerCluster) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a DockerCluster but got a %T", obj)) | ||
} | ||
defaultDockerClusterSpec(&cluster.Spec) | ||
return nil | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-infrav1-dockercluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockerclusters,versions=infrav1,name=validation.dockercluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;infrav1 | ||
|
||
var _ webhook.CustomValidator = &DockerCluster{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *DockerCluster) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
cluster, ok := obj.(*infrav1.DockerCluster) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a DockerCluster but got a %T", obj)) | ||
} | ||
if allErrs := validateDockerClusterSpec(cluster.Spec); len(allErrs) > 0 { | ||
return nil, apierrors.NewInvalid(infrav1.GroupVersion.WithKind("DockerCluster").GroupKind(), cluster.Name, allErrs) | ||
} | ||
return nil, nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *DockerCluster) 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 (webhook *DockerCluster) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func defaultDockerClusterSpec(s *infrav1.DockerClusterSpec) { | ||
if s.ControlPlaneEndpoint.Port == 0 { | ||
s.ControlPlaneEndpoint.Port = 6443 | ||
} | ||
} | ||
|
||
func validateDockerClusterSpec(_ infrav1.DockerClusterSpec) field.ErrorList { | ||
return nil | ||
} |
116 changes: 116 additions & 0 deletions
116
test/infrastructure/docker/internal/webhooks/dockerclustertemplate_webhook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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 webhooks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"sigs.k8s.io/cluster-api/feature" | ||
infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1" | ||
) | ||
|
||
const dockerClusterTemplateImmutableMsg = "DockerClusterTemplate spec.template.spec field is immutable. Please create a new resource instead." | ||
|
||
// DockerClusterTemplate implements a validating and defaulting webhook for DockerClusterTemplate. | ||
type DockerClusterTemplate struct{} | ||
|
||
func (webhook *DockerClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&infrav1.DockerClusterTemplate{}). | ||
WithDefaulter(webhook). | ||
WithValidator(webhook). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-infrav1-dockerclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=infrav1,name=default.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;infrav1 | ||
|
||
var _ webhook.CustomDefaulter = &DockerClusterTemplate{} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type. | ||
func (webhook *DockerClusterTemplate) Default(_ context.Context, obj runtime.Object) error { | ||
clusterTemplate, ok := obj.(*infrav1.DockerClusterTemplate) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a DockerClusterTemplate but got a %T", obj)) | ||
} | ||
defaultDockerClusterSpec(&clusterTemplate.Spec.Template.Spec) | ||
return nil | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-infrav1-dockerclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=infrav1,name=validation.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;infrav1 | ||
|
||
var _ webhook.CustomValidator = &DockerClusterTemplate{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *DockerClusterTemplate) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
// NOTE: DockerClusterTemplate is behind ClusterTopology feature gate flag; the web hook | ||
// must prevent creating new objects in case the feature flag is disabled. | ||
if !feature.Gates.Enabled(feature.ClusterTopology) { | ||
return nil, field.Forbidden( | ||
field.NewPath("spec"), | ||
"can be set only if the ClusterTopology feature flag is enabled", | ||
) | ||
} | ||
|
||
clusterTemplate, ok := obj.(*infrav1.DockerClusterTemplate) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a DockerClusterTemplate but got a %T", obj)) | ||
} | ||
|
||
allErrs := validateDockerClusterSpec(clusterTemplate.Spec.Template.Spec) | ||
|
||
// Validate the metadata of the template. | ||
allErrs = append(allErrs, clusterTemplate.Spec.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...) | ||
|
||
if len(allErrs) > 0 { | ||
return nil, apierrors.NewInvalid(infrav1.GroupVersion.WithKind("DockerClusterTemplate").GroupKind(), clusterTemplate.Name, allErrs) | ||
} | ||
return nil, nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *DockerClusterTemplate) ValidateUpdate(_ context.Context, oldRaw, newRaw runtime.Object) (admission.Warnings, error) { | ||
var allErrs field.ErrorList | ||
oldTemplate, ok := oldRaw.(*infrav1.DockerClusterTemplate) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a DockerClusterTemplate but got a %T", oldRaw)) | ||
} | ||
newTemplate, ok := newRaw.(*infrav1.DockerClusterTemplate) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a DockerClusterTemplate but got a %T", newRaw)) | ||
} | ||
if !reflect.DeepEqual(newTemplate.Spec.Template.Spec, oldTemplate.Spec.Template.Spec) { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "template", "spec"), newTemplate, dockerClusterTemplateImmutableMsg)) | ||
} | ||
if len(allErrs) == 0 { | ||
return nil, nil | ||
} | ||
return nil, apierrors.NewInvalid(infrav1.GroupVersion.WithKind("DockerClusterTemplate").GroupKind(), newTemplate.Name, allErrs) | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *DockerClusterTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} |
Oops, something went wrong.