亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

kubectl插件里面,提示輸入?

kubectl插件里面,提示輸入?

Go
炎炎設計 2023-06-26 17:41:54
我正在編寫一個kubectl插件來對用戶進行身份驗證,我想在調用插件后提示用戶輸入密碼。據我了解,從 STDIN 獲取輸入相當簡單,但我很難看到寫入 STDOUT 的消息。目前我的代碼如下所示:在 cmd/kubectl-myauth.go 中:// This is mostly boilerplate, but it's needed for the MRE// https://stackoverflow.com/help/minimal-reproducible-examplepackage myauthimport (...)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}當從上下文外部運行時,這按照我期望的方式工作kubectl- 即,它打印字符串,提示輸入,然后繼續。然而,從上下文內部來看kubectl,我相信前兩個全大寫注釋([1] 和 [2])之間的打印內容正在被kubectlSTDOUT 上的監聽所吞沒。我可以通過打印到 STDERR 來解決這個問題,但這感覺......錯誤。有沒有辦法可以繞過kubectlSTDOUT 的消耗來與用戶通信?TL;DR:kubectl似乎正在吞噬kubectl插件的所有 STDOUT,但我想提示用戶輸入 - 有沒有一種簡單的方法可以做到這一點?
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

以下是步驟:

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

  • 重復sample-cli-plugintest-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 標準錯誤

測試 Fprintf 標準輸出

測試 Printf

測試 Fprintf o.IOStreams.Out


查看完整回答
反對 回復 2023-06-26
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號