gpt4 book ai didi

go - 在 kubectl 插件中,提示输入?

转载 作者:数据小太阳 更新时间:2023-10-29 03:19:03 25 4
gpt4 key购买 nike

我正在编写一个 kubectl 插件来对用户进行身份验证,我想在调用插件后提示用户输入密码。据我了解,从 STDIN 获取输入相当简单,但我很难看到写入 STDOUT 的消息。目前我的代码如下所示:

在 cmd/kubectl-myauth.go 中:

// This is mostly boilerplate, but it's needed for the MRE
// https://stackoverflow.com/help/minimal-reproducible-example
package myauth
import (...)
func main() {
pflag.CommandLine = pflag.NewFlagSet("kubectl-myauth", pflag.ExitOnError)
root := cmd.NewCmdAuthOp(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}

在 pkg/cmd/auth.go 中:

package cmd
...
type AuthOpOptions struct {
configFlags *genericclioptions.ConfigFlags
resultingContext *api.Context
rawConfig api.Config
args []string
...
genericclioptions.IOStreams
}
func NewAuthOpOptions(streams genericclioptions.IOStreams) *AuthOpOptions {
return &AuthOpOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
func NewCmdAuthOp(streams genericclioptions.IOStreams) *cobra.Command {
o := NewAuthOpOptions(streams)
cmd := &cobra.Command{
RunE: func(c *cobra.Command, args []string) error {
return o.Run()
},
}
return cmd
}
func (o *AuthOpOptions) Run() error {
pass, err := getPassword(o)
if err != nil {
return err
}
// Do Auth Stuff
// Eventually print an ExecCredential to STDOUT
return nil
}
func getPassword(o *AuthOpOptions) (string, error) {
var reader *bufio.Reader
reader = nil
pass := ""
for pass == "" {
// THIS IS AN IMPORTANT LINE [1]
fmt.Fprintf(o.IOStreams.Out, "Password with which to authenticate:\n")
// THE REST OF THIS IS STILL IMPORTANT, BUT LESS SO [2]
if reader == nil {
// The first time through, initialize the reader
reader = bufio.NewReader(o.IOStreams.In)
}
pass, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
pass = strings.Trim(pass, "\r\n")
if pass == "" {
// ALSO THIS LINE IS IMPORTANT [3]
fmt.Fprintf(o.IOStreams.Out, `Read password was empty string.
Please input a valid password.
`)
}
}
return pass, nil
}

当从 kubectl 上下文外部运行时,这按照我期望的方式工作 - 即,它打印字符串,提示输入,然后继续。然而,在 kubectl 上下文中,我相信前两个全大写注释([1] 和 [2])之间的打印被 kubectl 监听标准输出。我可以通过打印到 STDERR 来解决这个问题,但这感觉……不对。有没有一种方法可以绕过 kubectl 使用 STDOUT 与用户通信?

TL;DR:kubectl 似乎吞没了 kubectl 插件的所有 STDOUT,但我想提示用户输入 - 有没有简单的方法这个?

最佳答案

抱歉,我没有比“对我有用”更好的答案了:-)以下是步骤:

  • git clone https://github.com/kubernetes/kubernetes.git

  • sample-cli-plugin 复制为 test-cli-plugin(这涉及修复 import-restrictions.yaml、rules-godeps.yaml 和规则。 yaml 在暂存/发布下——也许没有必要,但这样更安全)

  • 将 kubectl-ns.go 更改为 kubectl-test.go:

package main

import (
"os"

"github.com/spf13/pflag"

"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/test-cli-plugin/pkg/cmd"
)

func main() {
flags := pflag.NewFlagSet("kubectl-test", pflag.ExitOnError)
pflag.CommandLine = flags

root := cmd.NewCmdTest(genericclioptions.IOStreams{In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
  • 将 ns.go 更改为 test.go:
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"
)

type TestOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
}

func NewTestOptions(streams genericclioptions.IOStreams) *TestOptions {
return &TestOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}

func NewCmdTest(streams genericclioptions.IOStreams) *cobra.Command {
o := NewTestOptions(streams)

cmd := &cobra.Command{
Use: "test",
Short: "Test plugin",
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
o.Run()
return nil
},
}

return cmd
}

func (o *TestOptions) Run() error {
fmt.Fprintf(os.Stderr, "Testing Fprintf Stderr\n")
fmt.Fprintf(os.Stdout, "Testing Fprintf Stdout\n")
fmt.Printf("Testing Printf\n")
fmt.Fprintf(o.IOStreams.Out, "Testing Fprintf o.IOStreams.Out\n")
return nil
}
  • 相应地修复 BUILD 文件
  • 构建插件
  • 运行make
  • 复制kubectl-test到/usr/local/bin
  • 运行编译后的 kubectl 二进制文件:

~/k8s/_output/bin$ ./kubectl test

Testing Fprintf Stderr

Testing Fprintf Stdout

Testing Printf

Testing Fprintf o.IOStreams.Out

关于go - 在 kubectl 插件中,提示输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com