...

Source file src/k8s.io/component-base/logs/api/v1/pflags.go

Documentation: k8s.io/component-base/logs/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  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/spf13/pflag"
    25  )
    26  
    27  // VModuleConfigurationPflag implements the pflag.Value interface for a
    28  // VModuleConfiguration. The value pointer must not be nil.
    29  func VModuleConfigurationPflag(value *VModuleConfiguration) pflag.Value {
    30  	return vmoduleConfigurationPFlag{value}
    31  }
    32  
    33  type vmoduleConfigurationPFlag struct {
    34  	value *VModuleConfiguration
    35  }
    36  
    37  // String returns the -vmodule parameter (comma-separated list of pattern=N).
    38  func (wrapper vmoduleConfigurationPFlag) String() string {
    39  	if wrapper.value == nil {
    40  		return ""
    41  	}
    42  	var patterns []string
    43  	for _, item := range *wrapper.value {
    44  		patterns = append(patterns, fmt.Sprintf("%s=%d", item.FilePattern, item.Verbosity))
    45  	}
    46  	return strings.Join(patterns, ",")
    47  }
    48  
    49  // Set parses the -vmodule parameter (comma-separated list of pattern=N).
    50  func (wrapper vmoduleConfigurationPFlag) Set(value string) error {
    51  	// This code mirrors https://github.com/kubernetes/klog/blob/9ad246211af1ed84621ee94a26fcce0038b69cd1/klog.go#L287-L313
    52  
    53  	for _, pat := range strings.Split(value, ",") {
    54  		if len(pat) == 0 {
    55  			// Empty strings such as from a trailing comma can be ignored.
    56  			continue
    57  		}
    58  		patLev := strings.Split(pat, "=")
    59  		if len(patLev) != 2 || len(patLev[0]) == 0 || len(patLev[1]) == 0 {
    60  			return fmt.Errorf("%q does not have the pattern=N format", pat)
    61  		}
    62  		pattern := patLev[0]
    63  		// 31 instead of 32 to ensure that it also fits into int32.
    64  		v, err := strconv.ParseUint(patLev[1], 10, 31)
    65  		if err != nil {
    66  			return fmt.Errorf("parsing verbosity in %q: %v", pat, err)
    67  		}
    68  		*wrapper.value = append(*wrapper.value, VModuleItem{FilePattern: pattern, Verbosity: VerbosityLevel(v)})
    69  	}
    70  	return nil
    71  }
    72  
    73  func (wrapper vmoduleConfigurationPFlag) Type() string {
    74  	return "pattern=N,..."
    75  }
    76  
    77  // VerbosityLevelPflag implements the pflag.Value interface for a verbosity
    78  // level value.
    79  func VerbosityLevelPflag(value *VerbosityLevel) pflag.Value {
    80  	return verbosityLevelPflag{value}
    81  }
    82  
    83  type verbosityLevelPflag struct {
    84  	value *VerbosityLevel
    85  }
    86  
    87  func (wrapper verbosityLevelPflag) String() string {
    88  	if wrapper.value == nil {
    89  		return "0"
    90  	}
    91  	return strconv.FormatInt(int64(*wrapper.value), 10)
    92  }
    93  
    94  func (wrapper verbosityLevelPflag) Get() interface{} {
    95  	if wrapper.value == nil {
    96  		return VerbosityLevel(0)
    97  	}
    98  	return *wrapper.value
    99  }
   100  
   101  func (wrapper verbosityLevelPflag) Set(value string) error {
   102  	// Limited to int32 for compatibility with klog.
   103  	v, err := strconv.ParseUint(value, 10, 31)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	*wrapper.value = VerbosityLevel(v)
   108  	return nil
   109  }
   110  
   111  func (wrapper verbosityLevelPflag) Type() string {
   112  	return "Level"
   113  }
   114  

View as plain text