@@ -43,6 +43,7 @@ import (
4343 "k8s.io/apimachinery/pkg/runtime"
4444 "k8s.io/apimachinery/pkg/runtime/schema"
4545 "k8s.io/apimachinery/pkg/types"
46+ "k8s.io/apimachinery/pkg/util/rand"
4647 appsv1applyconfigurations "k8s.io/client-go/applyconfigurations/apps/v1"
4748 autoscaling1applyconfigurations "k8s.io/client-go/applyconfigurations/autoscaling/v1"
4849 corev1applyconfigurations "k8s.io/client-go/applyconfigurations/core/v1"
@@ -378,6 +379,52 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
378379 Expect (actual .ManagedFields ).To (HaveLen (1 ))
379380 Expect (actual .ManagedFields [0 ].Manager ).To (Equal ("test-owner" ))
380381 })
382+
383+ Context ("with the FieldValidation option" , func () {
384+ It ("should log warnings with FieldValidation equal to Warn" , func (ctx SpecContext ) {
385+ restCfg := rest .CopyConfig (cfg )
386+ var testLog bytes.Buffer
387+ restCfg .WarningHandler = rest .NewWarningWriter (& testLog , rest.WarningWriterOptions {})
388+
389+ warnClient , err := client .New (restCfg , client.Options {FieldValidation : metav1 .FieldValidationWarn })
390+ Expect (err ).NotTo (HaveOccurred ())
391+ Expect (warnClient ).NotTo (BeNil ())
392+
393+ unstrContent , err := runtime .DefaultUnstructuredConverter .ToUnstructured (
394+ corev1applyconfigurations .ConfigMap ("test-cm-" + rand .String (3 ), "default" ).
395+ WithData (map [string ]string {"foo" : "bar" }),
396+ )
397+ Expect (err ).NotTo (HaveOccurred ())
398+ unstrContent ["additionalField" ] = "test"
399+ cm := & unstructured.Unstructured {Object : unstrContent }
400+
401+ err = warnClient .Create (ctx , cm )
402+ Expect (err ).NotTo (HaveOccurred ())
403+ Expect (testLog .String ()).To (ContainSubstring (`Warning: unknown field "additionalField"` ))
404+
405+ })
406+ It ("should fail write operation if FieldValidation equals Strict" , func (ctx SpecContext ) {
407+ restCfg := rest .CopyConfig (cfg )
408+ var testLog bytes.Buffer
409+ restCfg .WarningHandler = rest .NewWarningWriter (& testLog , rest.WarningWriterOptions {})
410+ strictClient , err := client .New (restCfg , client.Options {FieldValidation : metav1 .FieldValidationStrict })
411+ Expect (err ).NotTo (HaveOccurred ())
412+
413+ unstrContent , err := runtime .DefaultUnstructuredConverter .ToUnstructured (
414+ corev1applyconfigurations .ConfigMap ("test-cm-" + rand .String (3 ), "default" ).
415+ WithData (map [string ]string {"foo" : "bar" }),
416+ )
417+ Expect (err ).NotTo (HaveOccurred ())
418+ unstrContent ["additionalField" ] = "test"
419+ cm := & unstructured.Unstructured {Object : unstrContent }
420+
421+ err = strictClient .Create (ctx , cm )
422+ Expect (err ).To (HaveOccurred ())
423+ Expect (err ).To (MatchError (ContainSubstring ("unknown field \" additionalField\" " )))
424+ Expect (err ).To (MatchError (ContainSubstring ("strict decoding error" )))
425+ Expect (testLog .String ()).To (BeEmpty ())
426+ })
427+ })
381428 })
382429
383430 Describe ("Create" , func () {
0 commit comments