kubectl源码分析之version

 欢迎关注我的公众号:

 目前刚开始写一个月,一共写了18篇原创文章,文章目录如下:

istio多集群探秘,部署了50次多集群后我得出的结论

istio多集群链路追踪,附实操视频

istio防故障利器,你知道几个,istio新手不要读,太难!

istio业务权限控制,原来可以这么玩

istio实现非侵入压缩,微服务之间如何实现压缩

不懂envoyfilter也敢说精通istio系列-http-rbac-不要只会用AuthorizationPolicy配置权限

不懂envoyfilter也敢说精通istio系列-02-http-corsFilter-不要只会vs

不懂envoyfilter也敢说精通istio系列-03-http-csrf filter-再也不用再代码里写csrf逻辑了

不懂envoyfilter也敢说精通istio系列http-jwt_authn-不要只会RequestAuthorization

不懂envoyfilter也敢说精通istio系列-05-fault-filter-故障注入不止是vs

不懂envoyfilter也敢说精通istio系列-06-http-match-配置路由不只是vs

不懂envoyfilter也敢说精通istio系列-07-负载均衡配置不止是dr

不懂envoyfilter也敢说精通istio系列-08-连接池和断路器

不懂envoyfilter也敢说精通istio系列-09-http-route filter

不懂envoyfilter也敢说精通istio系列-network filter-redis proxy

不懂envoyfilter也敢说精通istio系列-network filter-HttpConnectionManager

不懂envoyfilter也敢说精通istio系列-ratelimit-istio ratelimit完全手册

 

————————————————

type Options struct {//version结构体
	ClientOnly bool
	Short      bool
	Output     string

	discoveryClient discovery.CachedDiscoveryInterface

	genericclioptions.IOStreams
}
func NewOptions(ioStreams genericclioptions.IOStreams) *Options {
	return &Options{//初始化结构体
		IOStreams: ioStreams,
	}

}

// NewCmdVersion returns a cobra command for fetching versions
//创建version命令
func NewCmdVersion(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
	o := NewOptions(ioStreams)//初始化结构体
	cmd := &cobra.Command{//创建cobra命令
		Use:     "version",
		Short:   i18n.T("Print the client and server version information"),
		Long:    "Print the client and server version information for the current context",
		Example: versionExample,
		Run: func(cmd *cobra.Command, args []string) {
			cmdutil.CheckErr(o.Complete(f, cmd))//准备
			cmdutil.CheckErr(o.Validate())//校验
			cmdutil.CheckErr(o.Run())//运行
		},
	}
	cmd.Flags().BoolVar(&o.ClientOnly, "client", o.ClientOnly, "Client version only (no server required).")//client选项
	cmd.Flags().BoolVar(&o.Short, "short", o.Short, "Print just the version number.")//short选项
	cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")//output选项
	return cmd
}
//准备
func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
	var err error
	if o.ClientOnly {//如果指定client,返回
		return nil
	}
	o.discoveryClient, err = f.ToDiscoveryClient()//设置discoveryClient
	// if we had an empty rest.Config, continue and just print out client information.
	// if we had an error other than being unable to build a rest.Config, fail.
	if err != nil && !clientcmd.IsEmptyConfig(err) {
		return err
	}
	return nil
}

// Validate validates the provided options
func (o *Options) Validate() error {//校验
	if o.Output != "" && o.Output != "yaml" && o.Output != "json" {//输出格式必须是空,yaml或json
		return errors.New(`--output must be 'yaml' or 'json'`)
	}

	return nil

}
//运行
func (o *Options) Run() error {
	var (
		serverVersion *apimachineryversion.Info
		serverErr     error
		versionInfo   Version
	)

	clientVersion := version.Get()//获取客户端版本
	versionInfo.ClientVersion = &clientVersion//设置versionInfo

	if !o.ClientOnly && o.discoveryClient != nil {//如果非clientonly
		// Always request fresh data from the server
		o.discoveryClient.Invalidate()//使缓存失效
		serverVersion, serverErr = o.discoveryClient.ServerVersion()//获取服务端版本
		versionInfo.ServerVersion = serverVersion//设置versionInfo
	}

	switch o.Output {//判断输出格式
	case "":// 如果是空
		if o.Short {//如果指定了short
			fmt.Fprintf(o.Out, "Client Version: %s\n", clientVersion.GitVersion)//打印client版本
			if serverVersion != nil {
				fmt.Fprintf(o.Out, "Server Version: %s\n", serverVersion.GitVersion)//打印服务端版本
			}
		} else {
			fmt.Fprintf(o.Out, "Client Version: %s\n", fmt.Sprintf("%#v", clientVersion))//打印client版本
			if serverVersion != nil {
				fmt.Fprintf(o.Out, "Server Version: %s\n", fmt.Sprintf("%#v", *serverVersion))//打印服务端版本
			}
		}
	case "yaml"://如果是yaml
		marshalled, err := yaml.Marshal(&versionInfo)//把obj转成yaml
		if err != nil {
			return err
		}
		fmt.Fprintln(o.Out, string(marshalled))//打印yaml
	case "json"://如果是json
		marshalled, err := json.MarshalIndent(&versionInfo, "", "  ")//把obj转成json
		if err != nil {
			return err
		}
		fmt.Fprintln(o.Out, string(marshalled))//打印json
	default:
		// There is a bug in the program if we hit this case.
		// However, we follow a policy of never panicking.
		return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", o.Output)//报错
	}

	return serverErr
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hxpjava1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值