istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/validate/validate_values.go (about)

     1  // Copyright Istio Authors
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validate
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"google.golang.org/protobuf/types/known/structpb"
    21  
    22  	"istio.io/istio/operator/pkg/apis/istio/v1alpha1"
    23  	"istio.io/istio/operator/pkg/util"
    24  )
    25  
    26  // DefaultValuesValidations maps a data path to a validation function.
    27  var DefaultValuesValidations = map[string]ValidatorFunc{
    28  	"global.proxy.includeIPRanges":     validateIPRangesOrStar,
    29  	"global.proxy.excludeIPRanges":     validateIPRangesOrStar,
    30  	"global.proxy.includeInboundPorts": validateStringList(validatePortNumberString),
    31  	"global.proxy.excludeInboundPorts": validateStringList(validatePortNumberString),
    32  	"meshConfig":                       validateMeshConfig,
    33  }
    34  
    35  // CheckValues validates the values in the given tree, which follows the Istio values.yaml schema.
    36  func CheckValues(root any) util.Errors {
    37  	v := reflect.ValueOf(root)
    38  	if root == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
    39  		return nil
    40  	}
    41  	vs, err := util.ToYAMLGeneric(root)
    42  	if err != nil {
    43  		return util.Errors{err}
    44  	}
    45  	val := &v1alpha1.Values{}
    46  	if err := util.UnmarshalWithJSONPB(string(vs), val, false); err != nil {
    47  		return util.Errors{err}
    48  	}
    49  	return ValuesValidate(DefaultValuesValidations, root.(*structpb.Struct).AsMap(), nil)
    50  }
    51  
    52  // ValuesValidate validates the values of the tree using the supplied Func
    53  func ValuesValidate(validations map[string]ValidatorFunc, node any, path util.Path) (errs util.Errors) {
    54  	pstr := path.String()
    55  	scope.Debugf("ValuesValidate %s", pstr)
    56  	vf := validations[pstr]
    57  	if vf != nil {
    58  		errs = util.AppendErrs(errs, vf(path, node))
    59  	}
    60  
    61  	nn, ok := node.(map[string]any)
    62  	if !ok {
    63  		// Leaf, nothing more to recurse.
    64  		return errs
    65  	}
    66  	for k, v := range nn {
    67  		errs = util.AppendErrs(errs, ValuesValidate(validations, v, append(path, k)))
    68  	}
    69  
    70  	return errs
    71  }