forked from kubernetes-sigs/cluster-api-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes-sigs#3099 from sbueringer/pr-implement-…
…vm-naming-strategy ✨ Implement VirtualMachine namingStrategy
- Loading branch information
Showing
14 changed files
with
504 additions
and
22 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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
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
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
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,108 @@ | ||
/* | ||
Copyright 2024 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 vmware is the package for webhooks of vmware resources. | ||
package vmware | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation" | ||
"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" | ||
|
||
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" | ||
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/services/vmoperator" | ||
) | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-vspheremachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachinetemplates,versions=v1beta1,name=validation.vspheremachinetemplate.vmware.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 | ||
|
||
// VSphereMachineTemplateWebhook implements a validation webhook for VSphereMachineTemplate. | ||
type VSphereMachineTemplateWebhook struct{} | ||
|
||
var _ webhook.CustomValidator = &VSphereMachineTemplateWebhook{} | ||
|
||
func (webhook *VSphereMachineTemplateWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&vmwarev1.VSphereMachineTemplate{}). | ||
WithValidator(webhook). | ||
Complete() | ||
} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereMachineTemplateWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
vSphereMachineTemplate, ok := obj.(*vmwarev1.VSphereMachineTemplate) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereMachineTemplate but got a %T", obj)) | ||
} | ||
return webhook.validate(ctx, nil, vSphereMachineTemplate) | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereMachineTemplateWebhook) ValidateUpdate(ctx context.Context, _, newRaw runtime.Object) (admission.Warnings, error) { | ||
vSphereMachineTemplate, ok := newRaw.(*vmwarev1.VSphereMachineTemplate) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereMachineTemplate but got a %T", newRaw)) | ||
} | ||
return webhook.validate(ctx, nil, vSphereMachineTemplate) | ||
} | ||
|
||
func (webhook *VSphereMachineTemplateWebhook) validate(_ context.Context, _, newVSphereMachineTemplate *vmwarev1.VSphereMachineTemplate) (admission.Warnings, error) { | ||
var allErrs field.ErrorList | ||
|
||
// Validate namingStrategy | ||
namingStrategy := newVSphereMachineTemplate.Spec.Template.Spec.NamingStrategy | ||
if namingStrategy != nil && | ||
namingStrategy.Template != nil { | ||
name, err := vmoperator.GenerateVirtualMachineName("machine", namingStrategy) | ||
templateFldPath := field.NewPath("spec", "template", "spec", "namingStrategy", "template") | ||
if err != nil { | ||
allErrs = append(allErrs, | ||
field.Invalid( | ||
templateFldPath, | ||
*namingStrategy.Template, | ||
fmt.Sprintf("invalid VirtualMachine name template: %v", err), | ||
), | ||
) | ||
} else { | ||
// Note: This validates that the resulting name is a valid Kubernetes object name. | ||
for _, err := range validation.IsDNS1123Subdomain(name) { | ||
allErrs = append(allErrs, | ||
field.Invalid( | ||
templateFldPath, | ||
*namingStrategy.Template, | ||
fmt.Sprintf("invalid VirtualMachine name template, generated name is not a valid Kubernetes object name: %v", err), | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
if len(allErrs) > 0 { | ||
return nil, apierrors.NewInvalid(vmwarev1.GroupVersion.WithKind("VSphereMachineTemplate").GroupKind(), newVSphereMachineTemplate.Name, allErrs) | ||
} | ||
return nil, nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} |
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,99 @@ | ||
/* | ||
Copyright 2024 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 vmware | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"k8s.io/utils/ptr" | ||
|
||
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" | ||
) | ||
|
||
func TestVSphereMachineTemplate_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
namingStrategy *vmwarev1.VirtualMachineNamingStrategy | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Should succeed if namingStrategy not set", | ||
namingStrategy: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Should succeed if namingStrategy.template not set", | ||
namingStrategy: &vmwarev1.VirtualMachineNamingStrategy{ | ||
Template: nil, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Should succeed if namingStrategy.template is set to the fallback value", | ||
namingStrategy: &vmwarev1.VirtualMachineNamingStrategy{ | ||
Template: ptr.To[string]("{{ .machine.name }}"), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Should succeed if namingStrategy.template is set to the Windows example", | ||
namingStrategy: &vmwarev1.VirtualMachineNamingStrategy{ | ||
Template: ptr.To[string]("{{ if le (len .machine.name) 20 }}{{ .machine.name }}{{else}}{{ trimSuffix \"-\" (trunc 14 .machine.name) }}-{{ trunc -5 .machine.name }}{{end}}"), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Should fail if namingStrategy.template is set to an invalid template", | ||
namingStrategy: &vmwarev1.VirtualMachineNamingStrategy{ | ||
Template: ptr.To[string]("{{ invalid"), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Should fail if namingStrategy.template is set to a valid template that renders an invalid name", | ||
namingStrategy: &vmwarev1.VirtualMachineNamingStrategy{ | ||
Template: ptr.To[string]("-{{ .machine.name }}"), // Leading - is not valid for names. | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
vSphereMachineTemplate := &vmwarev1.VSphereMachineTemplate{ | ||
Spec: vmwarev1.VSphereMachineTemplateSpec{ | ||
Template: vmwarev1.VSphereMachineTemplateResource{ | ||
Spec: vmwarev1.VSphereMachineSpec{ | ||
NamingStrategy: tc.namingStrategy, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
webhook := &VSphereMachineTemplateWebhook{} | ||
_, err := webhook.validate(context.Background(), nil, vSphereMachineTemplate) | ||
if tc.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.