1 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
以下是步驟:
git clone https://github.com/kubernetes/kubernetes.git
重復(fù)
sample-cli-plugin
為test-cli-plugin
(這涉及在暫存/發(fā)布下修復(fù) import-restrictions.yaml、rules-godeps.yaml 和 Rules.yaml - 也許沒(méi)有必要,但這樣更安全)將 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
}
相應(yīng)地修復(fù) BUILD 文件
構(gòu)建插件
跑步
make
復(fù)制
kubectl-test
到/usr/local/bin運(yùn)行編譯后的二進(jìn)制
kubectl
文件:
~/k8s/_output/bin$ ./kubectl 測(cè)試
測(cè)試 Fprintf 標(biāo)準(zhǔn)錯(cuò)誤
測(cè)試 Fprintf 標(biāo)準(zhǔn)輸出
測(cè)試 Printf
測(cè)試 Fprintf o.IOStreams.Out
- 1 回答
- 0 關(guān)注
- 148 瀏覽
添加回答
舉報(bào)