...
1
16
17 package strict
18
19 import (
20 "github.com/pkg/errors"
21
22 "k8s.io/apimachinery/pkg/runtime"
23 "k8s.io/apimachinery/pkg/runtime/schema"
24 "k8s.io/apimachinery/pkg/runtime/serializer/json"
25 )
26
27
28
29 func VerifyUnmarshalStrict(schemes []*runtime.Scheme, gvk schema.GroupVersionKind, bytes []byte) error {
30 var scheme *runtime.Scheme
31 for _, s := range schemes {
32 if _, err := s.New(gvk); err == nil {
33 scheme = s
34 break
35 }
36 }
37 if scheme == nil {
38 return errors.Errorf("unknown configuration %#v", gvk)
39 }
40
41 opt := json.SerializerOptions{Yaml: true, Pretty: false, Strict: true}
42 serializer := json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, opt)
43 _, _, err := serializer.Decode(bytes, &gvk, nil)
44 if err != nil {
45 return errors.Wrapf(err, "error unmarshaling configuration %#v", gvk)
46 }
47
48 return nil
49 }
50
View as plain text