k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubelet/app/options/globalflags.go (about)

     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 options
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  
    25  	"github.com/spf13/pflag"
    26  
    27  	// libs that provide registration functions
    28  	"k8s.io/component-base/logs"
    29  	"k8s.io/component-base/version/verflag"
    30  )
    31  
    32  // AddGlobalFlags explicitly registers flags that libraries (glog, verflag, etc.) register
    33  // against the global flagsets from "flag" and "github.com/spf13/pflag".
    34  // We do this in order to prevent unwanted flags from leaking into the Kubelet's flagset.
    35  func AddGlobalFlags(fs *pflag.FlagSet) {
    36  	addCadvisorFlags(fs)
    37  	addCredentialProviderFlags(fs)
    38  	verflag.AddFlags(fs)
    39  	logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
    40  }
    41  
    42  // normalize replaces underscores with hyphens
    43  // we should always use hyphens instead of underscores when registering kubelet flags
    44  func normalize(s string) string {
    45  	return strings.Replace(s, "_", "-", -1)
    46  }
    47  
    48  // register adds a flag to local that targets the Value associated with the Flag named globalName in global
    49  func register(global *flag.FlagSet, local *pflag.FlagSet, globalName string) {
    50  	if f := global.Lookup(globalName); f != nil {
    51  		pflagFlag := pflag.PFlagFromGoFlag(f)
    52  		pflagFlag.Name = normalize(pflagFlag.Name)
    53  		local.AddFlag(pflagFlag)
    54  	} else {
    55  		panic(fmt.Sprintf("failed to find flag in global flagset (flag): %s", globalName))
    56  	}
    57  }
    58  
    59  // registerDeprecated registers the flag with register, and then marks it deprecated
    60  func registerDeprecated(global *flag.FlagSet, local *pflag.FlagSet, globalName, deprecated string) {
    61  	register(global, local, globalName)
    62  	local.Lookup(normalize(globalName)).Deprecated = deprecated
    63  }
    64  
    65  // addCredentialProviderFlags adds flags from k8s.io/kubernetes/pkg/credentialprovider
    66  func addCredentialProviderFlags(fs *pflag.FlagSet) {
    67  	// lookup flags in global flag set and re-register the values with our flagset
    68  	local := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
    69  
    70  	fs.AddFlagSet(local)
    71  }