sig.k8s.io/controller-runtime/pkg/client/config/config.go中一些定义
var (
kubeconfig, apiServerURL string
)
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "",
"Paths to a kubeconfig. Only required if out-of-cluster.")
}我的项目mybinary,与眼镜蛇
var rootCmd = &cobra.Command{
Use: "mybinary",
Run: func(cmd *cobra.Command, args []string) {
somefunc()
}
}
func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
rootCmd.InitDefaultHelpFlag()
}如果我想使用mybinary --kubeconfig somevalue来设置在config.go上定义的参数kubeconfig,我需要怎么做
发布于 2020-09-24 08:53:20
你有很多选择,但我认为目前你最好的选择可能是使用眼镜蛇的PersistentPreRun函数:
var rootCmd = &cobra.Command{
// ...
PersistentPreRun: func(cmd *cobra.Command, args []string) {
flag.CommandLine.Parse([]string{"-kubeconfig", yourVariableHere})
},
// ...
}也就是说,在您的根命令或从它派生的任何命令(只要它们不覆盖PreRun)运行之前,您将调用等同于调用flag.Parse()的方法,就好像有人已经运行了:
your-program -kubeconfig <string>其中的值来自提供给您自己的眼镜蛇标志的参数。如果正确的话,您可以使用cfgFile参数,或者添加一个--kubeconfig变量。
如果您使用的是viper,那么它似乎有自己的粘合剂来与基本的flag库混合,但我没有研究过这一点。我确实在config.go上看到了这样的评论:
// TODO: Fix this to allow double vendoring this library but still register flags on behalf of users如果修复,它将提供一种不同于调用flag.CommandLine.Parse的方法。
https://stackoverflow.com/questions/64023424
复制相似问题