Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add field.Path aggregation for Machine webhook #6368

Merged
merged 1 commit into from
Apr 6, 2022
Merged
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
11 changes: 6 additions & 5 deletions api/v1beta1/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ func (m *Machine) ValidateDelete() error {

func (m *Machine) validate(old *Machine) error {
var allErrs field.ErrorList
specPath := field.NewPath("spec")
if m.Spec.Bootstrap.ConfigRef == nil && m.Spec.Bootstrap.DataSecretName == nil {
allErrs = append(
allErrs,
field.Required(
field.NewPath("spec", "bootstrap", "data"),
specPath.Child("bootstrap", "data"),
"expected either spec.bootstrap.dataSecretName or spec.bootstrap.configRef to be populated",
),
)
Expand All @@ -105,7 +106,7 @@ func (m *Machine) validate(old *Machine) error {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "bootstrap", "configRef", "namespace"),
specPath.Child("bootstrap", "configRef", "namespace"),
m.Spec.Bootstrap.ConfigRef.Namespace,
"must match metadata.namespace",
),
Expand All @@ -116,7 +117,7 @@ func (m *Machine) validate(old *Machine) error {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "infrastructureRef", "namespace"),
specPath.Child("infrastructureRef", "namespace"),
m.Spec.InfrastructureRef.Namespace,
"must match metadata.namespace",
),
Expand All @@ -126,13 +127,13 @@ func (m *Machine) validate(old *Machine) error {
if old != nil && old.Spec.ClusterName != m.Spec.ClusterName {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "clusterName"), m.Spec.ClusterName, "field is immutable"),
field.Forbidden(specPath.Child("clusterName"), "field is immutable"),
)
}

if m.Spec.Version != nil {
if !version.KubeSemver.MatchString(*m.Spec.Version) {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "version"), *m.Spec.Version, "must be a valid semantic version"))
allErrs = append(allErrs, field.Invalid(specPath.Child("version"), *m.Spec.Version, "must be a valid semantic version"))
}
}

Expand Down