mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #13426 from feihujiang/aggregateErrorsWhenValidateParams
Aggregate errors when validate kubectl required parameters and labels
This commit is contained in:
commit
cfe2bf10f2
@ -28,6 +28,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
"k8s.io/kubernetes/pkg/util"
|
"k8s.io/kubernetes/pkg/util"
|
||||||
|
"k8s.io/kubernetes/pkg/util/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LabelOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
|
// LabelOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
|
||||||
@ -86,12 +87,13 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateNoOverwrites(meta *api.ObjectMeta, labels map[string]string) error {
|
func validateNoOverwrites(meta *api.ObjectMeta, labels map[string]string) error {
|
||||||
|
allErrs := []error{}
|
||||||
for key := range labels {
|
for key := range labels {
|
||||||
if value, found := meta.Labels[key]; found {
|
if value, found := meta.Labels[key]; found {
|
||||||
return fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value)
|
allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return errors.NewAggregate(allErrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLabels(spec []string) (map[string]string, []string, error) {
|
func parseLabels(spec []string) (map[string]string, []string, error) {
|
||||||
|
@ -24,6 +24,7 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
|
"k8s.io/kubernetes/pkg/util/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GeneratorParam is a parameter for a generator
|
// GeneratorParam is a parameter for a generator
|
||||||
@ -50,15 +51,16 @@ func IsZero(i interface{}) bool {
|
|||||||
|
|
||||||
// ValidateParams ensures that all required params are present in the params map
|
// ValidateParams ensures that all required params are present in the params map
|
||||||
func ValidateParams(paramSpec []GeneratorParam, params map[string]interface{}) error {
|
func ValidateParams(paramSpec []GeneratorParam, params map[string]interface{}) error {
|
||||||
|
allErrs := []error{}
|
||||||
for ix := range paramSpec {
|
for ix := range paramSpec {
|
||||||
if paramSpec[ix].Required {
|
if paramSpec[ix].Required {
|
||||||
value, found := params[paramSpec[ix].Name]
|
value, found := params[paramSpec[ix].Name]
|
||||||
if !found || IsZero(value) {
|
if !found || IsZero(value) {
|
||||||
return fmt.Errorf("Parameter: %s is required", paramSpec[ix].Name)
|
allErrs = append(allErrs, fmt.Errorf("Parameter: %s is required", paramSpec[ix].Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return errors.NewAggregate(allErrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeParams is a utility that creates generator parameters from a command line
|
// MakeParams is a utility that creates generator parameters from a command line
|
||||||
|
Loading…
Reference in New Issue
Block a user