mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Merge pull request #109850 from ardaguclu/validate-nooptdefval
Define NoOptDefVal for validate flag
This commit is contained in:
		@@ -407,6 +407,8 @@ func AddValidateFlags(cmd *cobra.Command) {
 | 
				
			|||||||
		"warn" will warn about unknown or duplicate fields without blocking the request if server-side field validation is enabled on the API server, and behave as "ignore" otherwise.
 | 
							"warn" will warn about unknown or duplicate fields without blocking the request if server-side field validation is enabled on the API server, and behave as "ignore" otherwise.
 | 
				
			||||||
		"false" or "ignore" will not perform any schema validation, silently dropping any unknown or duplicate fields.`,
 | 
							"false" or "ignore" will not perform any schema validation, silently dropping any unknown or duplicate fields.`,
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmd.Flags().Lookup("validate").NoOptDefVal = "strict"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AddFilenameOptionFlags(cmd *cobra.Command, options *resource.FilenameOptions, usage string) {
 | 
					func AddFilenameOptionFlags(cmd *cobra.Command, options *resource.FilenameOptions, usage string) {
 | 
				
			||||||
@@ -584,8 +586,6 @@ func GetValidationDirective(cmd *cobra.Command) (string, error) {
 | 
				
			|||||||
	b, err := strconv.ParseBool(validateFlag)
 | 
						b, err := strconv.ParseBool(validateFlag)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		switch validateFlag {
 | 
							switch validateFlag {
 | 
				
			||||||
		case cmd.Flag("validate").NoOptDefVal:
 | 
					 | 
				
			||||||
			return metav1.FieldValidationStrict, nil
 | 
					 | 
				
			||||||
		case "strict":
 | 
							case "strict":
 | 
				
			||||||
			return metav1.FieldValidationStrict, nil
 | 
								return metav1.FieldValidationStrict, nil
 | 
				
			||||||
		case "warn":
 | 
							case "warn":
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -517,7 +517,9 @@ func TestGetValidationDirective(t *testing.T) {
 | 
				
			|||||||
	for _, tc := range tests {
 | 
						for _, tc := range tests {
 | 
				
			||||||
		cmd := &cobra.Command{}
 | 
							cmd := &cobra.Command{}
 | 
				
			||||||
		AddValidateFlags(cmd)
 | 
							AddValidateFlags(cmd)
 | 
				
			||||||
 | 
							if tc.validateFlag != "" {
 | 
				
			||||||
			cmd.Flags().Set("validate", tc.validateFlag)
 | 
								cmd.Flags().Set("validate", tc.validateFlag)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		directive, err := GetValidationDirective(cmd)
 | 
							directive, err := GetValidationDirective(cmd)
 | 
				
			||||||
		if directive != tc.expectedDirective {
 | 
							if directive != tc.expectedDirective {
 | 
				
			||||||
			t.Errorf("validation directive, expected: %v, but got: %v", tc.expectedDirective, directive)
 | 
								t.Errorf("validation directive, expected: %v, but got: %v", tc.expectedDirective, directive)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -170,6 +170,12 @@ run_kubectl_create_validate_tests() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  create_and_use_new_namespace
 | 
					  create_and_use_new_namespace
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   ## test --validate no value expects default strict is used
 | 
				
			||||||
 | 
					   kube::log::status "Testing kubectl create --validate"
 | 
				
			||||||
 | 
					   # create and verify
 | 
				
			||||||
 | 
					   output_message=$(! kubectl create -f hack/testdata/invalid-deployment-unknown-and-duplicate-fields.yaml --validate 2>&1)
 | 
				
			||||||
 | 
					   has_one_of_error_message "${output_message}" 'strict decoding error' 'error validating data'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ## test --validate=true
 | 
					  ## test --validate=true
 | 
				
			||||||
  kube::log::status "Testing kubectl create --validate=true"
 | 
					  kube::log::status "Testing kubectl create --validate=true"
 | 
				
			||||||
  # create and verify
 | 
					  # create and verify
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user