kubectl源码分析之config delete-cluster

发布一个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源码分析,学习知其然,知其所以然
————————————————

//创建delete-cluster命令
func NewCmdConfigDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
	cmd := &cobra.Command{//创建cobra命令
		Use:                   "delete-cluster NAME",
		DisableFlagsInUseLine: true,
		Short:                 i18n.T("Delete the specified cluster from the kubeconfig"),
		Long:                  "Delete the specified cluster from the kubeconfig",
		Example:               deleteClusterExample,
		Run: func(cmd *cobra.Command, args []string) {
			cmdutil.CheckErr(runDeleteCluster(out, configAccess, cmd))//运行
		},
	}

	return cmd
}
//运行
func runDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
	config, err := configAccess.GetStartingConfig()//加载config
	if err != nil {
		return err
	}

	args := cmd.Flags().Args()//获取参数
	if len(args) != 1 {//参数不为1个报错
		cmd.Help()
		return nil
	}

	configFile := configAccess.GetDefaultFilename()//获取config文件名称
	if configAccess.IsExplicitFile() {//如果config文件使用--kubeconfig指定的
		configFile = configAccess.GetExplicitFile()// 获取指定的config文件名称
	}

	name := args[0]//获取cluster名称
	_, ok := config.Clusters[name]//判断cluster是否存在
	if !ok {// 如果不存在则报错
		return fmt.Errorf("cannot delete cluster %s, not in %s", name, configFile)
	}

	delete(config.Clusters, name)//从config中删除cluster

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

	fmt.Fprintf(out, "deleted cluster %s from %s\n", name, configFile)//打印结果

	return nil
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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