Refactor to simplify factory Validator
Kubernetes-commit: 5e8648b9f1cdbcbb10f22fd46b6ac40dbc471de2
This commit is contained in:
parent
b70c6c3bb8
commit
e5c05deb7b
|
@ -86,18 +86,17 @@ type ApplyOptions struct {
|
||||||
|
|
||||||
DeleteOptions *delete.DeleteOptions
|
DeleteOptions *delete.DeleteOptions
|
||||||
|
|
||||||
ServerSideApply bool
|
ServerSideApply bool
|
||||||
ForceConflicts bool
|
ForceConflicts bool
|
||||||
FieldManager string
|
FieldManager string
|
||||||
Selector string
|
Selector string
|
||||||
DryRunStrategy cmdutil.DryRunStrategy
|
DryRunStrategy cmdutil.DryRunStrategy
|
||||||
FieldValidationVerifier *resource.QueryParamVerifier
|
Prune bool
|
||||||
Prune bool
|
PruneResources []prune.Resource
|
||||||
PruneResources []prune.Resource
|
cmdBaseName string
|
||||||
cmdBaseName string
|
All bool
|
||||||
All bool
|
Overwrite bool
|
||||||
Overwrite bool
|
OpenAPIPatch bool
|
||||||
OpenAPIPatch bool
|
|
||||||
|
|
||||||
ValidationDirective string
|
ValidationDirective string
|
||||||
Validator validation.Schema
|
Validator validation.Schema
|
||||||
|
@ -255,7 +254,6 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValidationVerifier := resource.NewQueryParamVerifier(dynamicClient, flags.Factory.OpenAPIGetter(), resource.QueryParamFieldValidation)
|
|
||||||
fieldManager := GetApplyFieldManagerFlag(cmd, serverSideApply)
|
fieldManager := GetApplyFieldManagerFlag(cmd, serverSideApply)
|
||||||
|
|
||||||
// allow for a success message operation to be specified at print time
|
// allow for a success message operation to be specified at print time
|
||||||
|
@ -287,7 +285,7 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
validator, err := flags.Factory.Validator(validationDirective, fieldValidationVerifier)
|
validator, err := flags.Factory.Validator(validationDirective)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,8 +51,7 @@ type CreateOptions struct {
|
||||||
PrintFlags *genericclioptions.PrintFlags
|
PrintFlags *genericclioptions.PrintFlags
|
||||||
RecordFlags *genericclioptions.RecordFlags
|
RecordFlags *genericclioptions.RecordFlags
|
||||||
|
|
||||||
DryRunStrategy cmdutil.DryRunStrategy
|
DryRunStrategy cmdutil.DryRunStrategy
|
||||||
FieldValidationVerifier *resource.QueryParamVerifier
|
|
||||||
|
|
||||||
ValidationDirective string
|
ValidationDirective string
|
||||||
|
|
||||||
|
@ -205,11 +204,6 @@ func (o *CreateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []s
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
||||||
dynamicClient, err := f.DynamicClient()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
o.FieldValidationVerifier = resource.NewQueryParamVerifier(dynamicClient, f.OpenAPIGetter(), resource.QueryParamFieldValidation)
|
|
||||||
|
|
||||||
o.ValidationDirective, err = cmdutil.GetValidationDirective(cmd)
|
o.ValidationDirective, err = cmdutil.GetValidationDirective(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -244,7 +238,7 @@ func (o *CreateOptions) RunCreate(f cmdutil.Factory, cmd *cobra.Command) error {
|
||||||
return RunEditOnCreate(f, o.PrintFlags, o.RecordFlags, o.IOStreams, cmd, &o.FilenameOptions, o.fieldManager)
|
return RunEditOnCreate(f, o.PrintFlags, o.RecordFlags, o.IOStreams, cmd, &o.FilenameOptions, o.fieldManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, err := f.Validator(o.ValidationDirective, o.FieldValidationVerifier)
|
schema, err := f.Validator(o.ValidationDirective)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,9 +76,8 @@ type ReplaceOptions struct {
|
||||||
DeleteFlags *delete.DeleteFlags
|
DeleteFlags *delete.DeleteFlags
|
||||||
DeleteOptions *delete.DeleteOptions
|
DeleteOptions *delete.DeleteOptions
|
||||||
|
|
||||||
DryRunStrategy cmdutil.DryRunStrategy
|
DryRunStrategy cmdutil.DryRunStrategy
|
||||||
FieldValidationVerifier *resource.QueryParamVerifier
|
validationDirective string
|
||||||
validationDirective string
|
|
||||||
|
|
||||||
PrintObj func(obj runtime.Object) error
|
PrintObj func(obj runtime.Object) error
|
||||||
|
|
||||||
|
@ -164,7 +163,6 @@ func (o *ReplaceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
o.FieldValidationVerifier = resource.NewQueryParamVerifier(dynamicClient, f.OpenAPIGetter(), resource.QueryParamFieldValidation)
|
|
||||||
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
||||||
|
|
||||||
printer, err := o.PrintFlags.ToPrinter()
|
printer, err := o.PrintFlags.ToPrinter()
|
||||||
|
@ -198,7 +196,7 @@ func (o *ReplaceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, err := f.Validator(o.validationDirective, o.FieldValidationVerifier)
|
schema, err := f.Validator(o.validationDirective)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -524,7 +524,7 @@ func (f *TestFactory) UnstructuredClientForMapping(mapping *meta.RESTMapping) (r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validator returns a validation schema
|
// Validator returns a validation schema
|
||||||
func (f *TestFactory) Validator(validateDirective string, verifier *resource.QueryParamVerifier) (validation.Schema, error) {
|
func (f *TestFactory) Validator(validateDirective string) (validation.Schema, error) {
|
||||||
return validation.NullSchema{}, nil
|
return validation.NullSchema{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,8 +68,7 @@ type EditOptions struct {
|
||||||
WindowsLineEndings bool
|
WindowsLineEndings bool
|
||||||
|
|
||||||
cmdutil.ValidateOptions
|
cmdutil.ValidateOptions
|
||||||
ValidationDirective string
|
ValidationDirective string
|
||||||
FieldValidationVerifier *resource.QueryParamVerifier
|
|
||||||
|
|
||||||
OriginalResult *resource.Result
|
OriginalResult *resource.Result
|
||||||
|
|
||||||
|
@ -217,12 +216,6 @@ func (o *EditOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Comm
|
||||||
return o.PrintFlags.ToPrinter()
|
return o.PrintFlags.ToPrinter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dynamicClient, err := f.DynamicClient()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
o.FieldValidationVerifier = resource.NewQueryParamVerifier(dynamicClient, f.OpenAPIGetter(), resource.QueryParamFieldValidation)
|
|
||||||
|
|
||||||
o.ValidationDirective, err = cmdutil.GetValidationDirective(cmd)
|
o.ValidationDirective, err = cmdutil.GetValidationDirective(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -322,7 +315,7 @@ func (o *EditOptions) Run() error {
|
||||||
klog.V(4).Infof("User edited:\n%s", string(edited))
|
klog.V(4).Infof("User edited:\n%s", string(edited))
|
||||||
|
|
||||||
// Apply validation
|
// Apply validation
|
||||||
schema, err := o.f.Validator(o.ValidationDirective, o.FieldValidationVerifier)
|
schema, err := o.f.Validator(o.ValidationDirective)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return preservedFile(err, file, o.ErrOut)
|
return preservedFile(err, file, o.ErrOut)
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ type Factory interface {
|
||||||
UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
||||||
|
|
||||||
// Returns a schema that can validate objects stored on disk.
|
// Returns a schema that can validate objects stored on disk.
|
||||||
Validator(validationDirective string, verifier *resource.QueryParamVerifier) (validation.Schema, error)
|
Validator(validationDirective string) (validation.Schema, error)
|
||||||
// OpenAPISchema returns the parsed openapi schema definition
|
// OpenAPISchema returns the parsed openapi schema definition
|
||||||
OpenAPISchema() (openapi.Resources, error)
|
OpenAPISchema() (openapi.Resources, error)
|
||||||
// OpenAPIGetter returns a getter for the openapi schema document
|
// OpenAPIGetter returns a getter for the openapi schema document
|
||||||
|
|
|
@ -142,7 +142,7 @@ func (f *factoryImpl) UnstructuredClientForMapping(mapping *meta.RESTMapping) (r
|
||||||
return restclient.RESTClientFor(cfg)
|
return restclient.RESTClientFor(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *factoryImpl) Validator(validationDirective string, verifier *resource.QueryParamVerifier) (validation.Schema, error) {
|
func (f *factoryImpl) Validator(validationDirective string) (validation.Schema, error) {
|
||||||
// client-side schema validation is only performed
|
// client-side schema validation is only performed
|
||||||
// when the validationDirective is strict.
|
// when the validationDirective is strict.
|
||||||
// If the directive is warn, we rely on the ParamVerifyingSchema
|
// If the directive is warn, we rely on the ParamVerifyingSchema
|
||||||
|
@ -162,6 +162,13 @@ func (f *factoryImpl) Validator(validationDirective string, verifier *resource.Q
|
||||||
openapivalidation.NewSchemaValidation(resources),
|
openapivalidation.NewSchemaValidation(resources),
|
||||||
validation.NoDoubleKeySchema{},
|
validation.NoDoubleKeySchema{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamicClient, err := f.DynamicClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Create the FieldValidationVerifier for use in the ParamVerifyingSchema.
|
||||||
|
verifier := resource.NewQueryParamVerifier(dynamicClient, f.OpenAPIGetter(), resource.QueryParamFieldValidation)
|
||||||
return validation.NewParamVerifyingSchema(schema, verifier, string(validationDirective)), nil
|
return validation.NewParamVerifyingSchema(schema, verifier, string(validationDirective)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue