Skip to content

Commit

Permalink
Add field.Path aggregation for Machine webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Apr 4, 2022
1 parent df97d93 commit c029dd2
Showing 1 changed file with 6 additions and 5 deletions.
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

0 comments on commit c029dd2

Please sign in to comment.