Skip to content

Commit

Permalink
Error aggregation for Validation Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
k-keiichi-rh committed Aug 28, 2024
1 parent a6346d9 commit 1cd836a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions api/v1alpha1/fenceagentsremediation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package v1alpha1
import (
"fmt"

"github.com/pkg/errors"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -71,30 +70,35 @@ func (far *FenceAgentsRemediation) ValidateDelete() (admission.Warnings, error)
}

func validateFAR(farSpec *FenceAgentsRemediationSpec) (admission.Warnings, error) {
if _, err := validateAgentName(farSpec.Agent); err != nil {
return nil, err
}
return validateStrategy(farSpec.RemediationStrategy)
aggregated := errors.NewAggregate([]error{
validateAgentName(farSpec.Agent),
validateStrategy(farSpec.RemediationStrategy),
})

return nil, aggregated
}

func InitOutOfServiceTaintSupportedFlag(outOfServiceTaintSupported bool) {
isOutOfServiceTaintSupported = outOfServiceTaintSupported
}

func validateAgentName(agent string) (admission.Warnings, error) {
func validateAgentName(agent string) error {
exists, err := agentValidator.ValidateAgentName(agent)
if err != nil {
return nil, errors.WithMessagef(err, "Failed to validate fence agent: %s. You might want to try again.", agent)
return errors.NewAggregate([]error{
fmt.Errorf("Failed to validate fence agent: %s. You might want to try again.", agent),
err,
})
}
if !exists {
return nil, fmt.Errorf("unsupported fence agent: %s", agent)
return fmt.Errorf("unsupported fence agent: %s", agent)
}
return nil, nil
return nil
}

func validateStrategy(farRemStrategy RemediationStrategyType) (admission.Warnings, error) {
func validateStrategy(farRemStrategy RemediationStrategyType) error {
if farRemStrategy == OutOfServiceTaintRemediationStrategy && !isOutOfServiceTaintSupported {
return nil, fmt.Errorf("%s remediation strategy is not supported at kubernetes version lower than 1.26, please use a different remediation strategy", OutOfServiceTaintRemediationStrategy)
return fmt.Errorf("%s remediation strategy is not supported at kubernetes version lower than 1.26, please use a different remediation strategy", OutOfServiceTaintRemediationStrategy)
}
return nil, nil
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/onsi/gomega v1.30.0
github.com/openshift/api v0.0.0-20230621174358-ea40115b9fa6
github.com/openshift/client-go v0.0.0-20230626133714-296133fbf75e
github.com/pkg/errors v0.9.1
go.uber.org/zap v1.26.0
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
Expand Down Expand Up @@ -49,6 +48,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
Expand Down

0 comments on commit 1cd836a

Please sign in to comment.