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 MachineHealthCheck webhook #6376

Merged
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
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