strict.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package strict
  14. import (
  15. "github.com/pkg/errors"
  16. "k8s.io/apimachinery/pkg/runtime/schema"
  17. "k8s.io/klog"
  18. "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
  19. "k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
  20. "sigs.k8s.io/yaml"
  21. )
  22. // VerifyUnmarshalStrict takes a YAML byte slice and a GroupVersionKind and verifies if the YAML
  23. // schema is known and if it unmarshals with strict mode.
  24. //
  25. // TODO(neolit123): The returned error here is currently ignored everywhere and a klog warning is thrown instead.
  26. // We don't want to turn this into an actual error yet. Eventually this can be controlled with an optional CLI flag.
  27. func VerifyUnmarshalStrict(bytes []byte, gvk schema.GroupVersionKind) error {
  28. var (
  29. iface interface{}
  30. err error
  31. )
  32. iface, err = scheme.Scheme.New(gvk)
  33. if err != nil {
  34. iface, err = componentconfigs.Scheme.New(gvk)
  35. if err != nil {
  36. err := errors.Errorf("unknown configuration %#v for scheme definitions in %q and %q",
  37. gvk, scheme.Scheme.Name(), componentconfigs.Scheme.Name())
  38. klog.Warning(err.Error())
  39. return err
  40. }
  41. }
  42. if err := yaml.UnmarshalStrict(bytes, iface); err != nil {
  43. err := errors.Wrapf(err, "error unmarshaling configuration %#v", gvk)
  44. klog.Warning(err.Error())
  45. return err
  46. }
  47. return nil
  48. }