Skip to content

Commit

Permalink
Add field.Path aggregation for MachineHealthCheck webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 authored and Yuvaraj Kakaraparthi committed Apr 20, 2022
1 parent 5cc8e57 commit 8bc8d1a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions api/v1beta1/machinehealthcheck_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,35 @@ func (m *MachineHealthCheck) ValidateDelete() error {

func (m *MachineHealthCheck) validate(old *MachineHealthCheck) error {
var allErrs field.ErrorList
specPath := field.NewPath("spec")

// Validate selector parses as Selector
selector, err := metav1.LabelSelectorAsSelector(&m.Spec.Selector)
if err != nil {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "selector"), m.Spec.Selector, err.Error()),
field.Invalid(specPath.Child("selector"), m.Spec.Selector, err.Error()),
)
}

// Validate that the selector isn't empty.
if selector != nil && selector.Empty() {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "selector"), m.Spec.Selector, "selector must not be empty"),
field.Required(specPath.Child("selector"), "selector must not be empty"),
)
}

if clusterName, ok := m.Spec.Selector.MatchLabels[ClusterLabelName]; ok && clusterName != m.Spec.ClusterName {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "selector"), m.Spec.Selector, "cannot specify a cluster selector other than the one specified by ClusterName"))
field.Invalid(specPath.Child("selector"), m.Spec.Selector, "cannot specify a cluster selector other than the one specified by ClusterName"))
}

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"),
)
}

Expand All @@ -140,15 +141,15 @@ func (m *MachineHealthCheck) validate(old *MachineHealthCheck) error {
m.Spec.NodeStartupTimeout.Seconds() < minNodeStartupTimeout.Seconds() {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "nodeStartupTimeout"), m.Spec.NodeStartupTimeout.Seconds(), "must be at least 30s"),
field.Invalid(specPath.Child("nodeStartupTimeout"), m.Spec.NodeStartupTimeout.Seconds(), "must be at least 30s"),
)
}

if m.Spec.MaxUnhealthy != nil {
if _, err := intstr.GetScaledValueFromIntOrPercent(m.Spec.MaxUnhealthy, 0, false); err != nil {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "maxUnhealthy"), m.Spec.MaxUnhealthy, fmt.Sprintf("must be either an int or a percentage: %v", err.Error())),
field.Invalid(specPath.Child("maxUnhealthy"), m.Spec.MaxUnhealthy, fmt.Sprintf("must be either an int or a percentage: %v", err.Error())),
)
}
}
Expand All @@ -157,7 +158,7 @@ func (m *MachineHealthCheck) validate(old *MachineHealthCheck) error {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "remediationTemplate", "namespace"),
specPath.Child("remediationTemplate", "namespace"),
m.Spec.RemediationTemplate.Namespace,
"must match metadata.namespace",
),
Expand Down

0 comments on commit 8bc8d1a

Please sign in to comment.