在kubectl插件中,提示输入?

时间:2019-06-27 17:51:39

标签: go kubernetes kubectl

我正在编写一个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上下文内部来看,我相信kubectl在STDOUT上监听会淹没前两个全大写注释([1]和[2])之间的内容。我可以通过打印到STDERR来解决此问题,但这感觉...不对。有没有一种方法可以绕过kubectl消耗STDOUT与用户进行交流?

TL; DR:kubectl似乎吞噬了kubectl插件的所有STDOUT,但我想提示用户输入-是否有一种简单的方法?

1 个答案:

答案 0 :(得分:1)

对不起,我没有比“为我工作”更好的答案了:-)以下是步骤:

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

  • sample-cli-plugin复制为test-cli-plugin(这涉及在阶段/发布下修复import-restrictions.yaml,rules-godeps.yaml和rules.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测试

     

测试Fprintf Stderr

     

测试Fprintf标准输出

     

测试Printf

     

测试Fprintf o.IOStreams.Out