...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict/strict.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // VerifyUnmarshalStrict takes a slice of schems, a JSON/YAML byte slice and a GroupVersionKind
    28  // and verifies if the schema is known and if the byte slice unmarshals with strict mode.
    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