...

Source file src/k8s.io/component-base/tracing/api/v1/config.go

Documentation: k8s.io/component-base/tracing/api/v1

     1  /*
     2  Copyright 2021 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 v1
    18  
    19  import (
    20  	"fmt"
    21  	"net/url"
    22  	"strings"
    23  
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	"k8s.io/component-base/featuregate"
    26  )
    27  
    28  var (
    29  	maxSamplingRatePerMillion = int32(1000000)
    30  )
    31  
    32  // ValidateTracingConfiguration validates the tracing configuration
    33  func ValidateTracingConfiguration(traceConfig *TracingConfiguration, featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList {
    34  	allErrs := field.ErrorList{}
    35  	if traceConfig == nil {
    36  		return allErrs
    37  	}
    38  	if traceConfig.SamplingRatePerMillion != nil {
    39  		allErrs = append(allErrs, validateSamplingRate(*traceConfig.SamplingRatePerMillion, fldPath.Child("samplingRatePerMillion"))...)
    40  	}
    41  	if traceConfig.Endpoint != nil {
    42  		allErrs = append(allErrs, validateEndpoint(*traceConfig.Endpoint, fldPath.Child("endpoint"))...)
    43  	}
    44  	return allErrs
    45  }
    46  
    47  func validateSamplingRate(rate int32, fldPath *field.Path) field.ErrorList {
    48  	errs := field.ErrorList{}
    49  	if rate < 0 {
    50  		errs = append(errs, field.Invalid(
    51  			fldPath, rate,
    52  			"sampling rate must be positive",
    53  		))
    54  	}
    55  	if rate > maxSamplingRatePerMillion {
    56  		errs = append(errs, field.Invalid(
    57  			fldPath, rate,
    58  			"sampling rate per million must be less than or equal to one million",
    59  		))
    60  	}
    61  	return errs
    62  }
    63  
    64  func validateEndpoint(endpoint string, fldPath *field.Path) field.ErrorList {
    65  	errs := field.ErrorList{}
    66  	if !strings.Contains(endpoint, "//") {
    67  		endpoint = "dns://" + endpoint
    68  	}
    69  	url, err := url.Parse(endpoint)
    70  	if err != nil {
    71  		errs = append(errs, field.Invalid(
    72  			fldPath, endpoint,
    73  			err.Error(),
    74  		))
    75  		return errs
    76  	}
    77  	switch url.Scheme {
    78  	case "dns":
    79  	case "unix":
    80  	case "unix-abstract":
    81  	default:
    82  		errs = append(errs, field.Invalid(
    83  			fldPath, endpoint,
    84  			fmt.Sprintf("unsupported scheme: %v.  Options are none, dns, unix, or unix-abstract.  See https://github.com/grpc/grpc/blob/master/doc/naming.md", url.Scheme),
    85  		))
    86  	}
    87  	return errs
    88  }
    89  

View as plain text