kubectl源码分析之config set-context

发布一个k8s部署视频:https://edu.csdn.net/course/detail/26967

课程内容:各种k8s部署方式。包括minikube部署,kubeadm部署,kubeasz部署,rancher部署,k3s部署。包括开发测试环境部署k8s,和生产环境部署k8s。

腾讯课堂连接地址https://ke.qq.com/course/478827?taid=4373109931462251&tuin=ba64518

第二个视频发布  https://edu.csdn.net/course/detail/27109

腾讯课堂连接地址https://ke.qq.com/course/484107?tuin=ba64518

介绍主要的k8s资源的使用配置和命令。包括configmap,pod,service,replicaset,namespace,deployment,daemonset,ingress,pv,pvc,sc,role,rolebinding,clusterrole,clusterrolebinding,secret,serviceaccount,statefulset,job,cronjob,podDisruptionbudget,podSecurityPolicy,networkPolicy,resourceQuota,limitrange,endpoint,event,conponentstatus,node,apiservice,controllerRevision等。

第三个视频发布:https://edu.csdn.net/course/detail/27574

详细介绍helm命令,学习helm chart语法,编写helm chart。深入分析各项目源码,学习编写helm插件

第四个课程发布:https://edu.csdn.net/course/detail/28488

本课程将详细介绍k8s所有命令,以及命令的go源码分析,学习知其然,知其所以然
————————————————

type createContextOptions struct {//setcontext结构体
	configAccess clientcmd.ConfigAccess
	name         string
	currContext  bool
	cluster      cliflag.StringFlag
	authInfo     cliflag.StringFlag
	namespace    cliflag.StringFlag
}
//创建set-context命令
func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
	options := &createContextOptions{configAccess: configAccess}//初始化结构体

	cmd := &cobra.Command{//创建cobra命令
		Use:                   fmt.Sprintf("set-context [NAME | --current] [--%v=cluster_nickname] [--%v=user_nickname] [--%v=namespace]", clientcmd.FlagClusterName, clientcmd.FlagAuthInfoName, clientcmd.FlagNamespace),
		DisableFlagsInUseLine: true,
		Short:                 i18n.T("Sets a context entry in kubeconfig"),
		Long:                  createContextLong,
		Example:               createContextExample,
		Run: func(cmd *cobra.Command, args []string) {
			cmdutil.CheckErr(options.complete(cmd))//准备
			name, exists, err := options.run()//运行
			cmdutil.CheckErr(err)
			if exists {
				fmt.Fprintf(out, "Context %q modified.\n", name)//打印结果
			} else {
				fmt.Fprintf(out, "Context %q created.\n", name)
			}
		},
	}

	cmd.Flags().BoolVar(&options.currContext, "current", options.currContext, "Modify the current context")//current选项
	cmd.Flags().Var(&options.cluster, clientcmd.FlagClusterName, clientcmd.FlagClusterName+" for the context entry in kubeconfig")//cluster选项
	cmd.Flags().Var(&options.authInfo, clientcmd.FlagAuthInfoName, clientcmd.FlagAuthInfoName+" for the context entry in kubeconfig")//user选项
	cmd.Flags().Var(&options.namespace, clientcmd.FlagNamespace, clientcmd.FlagNamespace+" for the context entry in kubeconfig")//namespace选项

	return cmd
}
//准备
func (o *createContextOptions) complete(cmd *cobra.Command) error {
	args := cmd.Flags().Args()//获取参数
	if len(args) > 1 {//参数不能大于1个
		return helpErrorf(cmd, "Unexpected args: %v", args)
	}
	if len(args) == 1 {//如果参数为1个,设置name
		o.name = args[0]
	}
	return nil
}
//运行
func (o createContextOptions) run() (string, bool, error) {
	err := o.validate()//校验
	if err != nil {
		return "", false, err
	}

	config, err := o.configAccess.GetStartingConfig()//加载config
	if err != nil {
		return "", false, err
	}

	name := o.name
	if o.currContext {//如果指定了current
		if len(config.CurrentContext) == 0 {//如果当前config currentcontext为空报错
			return "", false, errors.New("no current context is set")
		}
		name = config.CurrentContext//设置name为currentContext
	}

	startingStanza, exists := config.Contexts[name]//判断context是否存在
	if !exists {//不存在则创建
		startingStanza = clientcmdapi.NewContext()
	}
	context := o.modifyContext(*startingStanza)//修改context
	config.Contexts[name] = &context//设置context

	if err := clientcmd.ModifyConfig(o.configAccess, *config, true); err != nil {//把配置写会文件
		return name, exists, err
	}

	return name, exists, nil
}
//校验
func (o createContextOptions) validate() error {
	if len(o.name) == 0 && !o.currContext {//名称和currnetContext不能同时为空
		return errors.New("you must specify a non-empty context name or --current")
	}
	if len(o.name) > 0 && o.currContext {//名称和currentcontext不能同时指定
		return errors.New("you cannot specify both a context name and --current")
	}

	return nil
}
//修改context
func (o *createContextOptions) modifyContext(existingContext clientcmdapi.Context) clientcmdapi.Context {
	modifiedContext := existingContext

	if o.cluster.Provided() {//如果cluster有值设置cluster
		modifiedContext.Cluster = o.cluster.Value()
	}
	if o.authInfo.Provided() {//如果authinfo有值,设置authinfo
		modifiedContext.AuthInfo = o.authInfo.Value()
	}
	if o.namespace.Provided() {//如果namespace有值设置namespace
		modifiedContext.Namespace = o.namespace.Value()
	}

	return modifiedContext
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(kubectl源码分析之config set-context)