kubectl源码分析之config get-contexts
欢迎关注我的公众号:
目前刚开始写一个月,一共写了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 GetContextsOptions struct {//get-contexts结构体configAccess clientcmd.ConfigAccessnameOnly boolshowHeaders boolcontextNames []stringgenericclioptions.IOStreams
}
//创建get-contexts命令
func NewCmdConfigGetContexts(streams genericclioptions.IOStreams, configAccess clientcmd.ConfigAccess) *cobra.Command {options := &GetContextsOptions{//初始化结构体configAccess: configAccess,IOStreams: streams,}cmd := &cobra.Command{//创建cobra命令Use: "get-contexts [(-o|--output=)name)]",DisableFlagsInUseLine: true,Short: i18n.T("Describe one or many contexts"),Long: getContextsLong,Example: getContextsExample,Run: func(cmd *cobra.Command, args []string) {validOutputTypes := sets.NewString("", "json", "yaml", "wide", "name", "custom-columns", "custom-columns-file", "go-template", "go-template-file", "jsonpath", "jsonpath-file")//有效的输出格式supportedOutputTypes := sets.NewString("", "name")//支持的输出格式outputFormat := cmdutil.GetFlagString(cmd, "output")//获取输出格式if !validOutputTypes.Has(outputFormat) {//判断输出是否有效cmdutil.CheckErr(fmt.Errorf("output must be one of '' or 'name': %v", outputFormat))}if !supportedOutputTypes.Has(outputFormat) {.//判断输出是否支持fmt.Fprintf(options.Out, "--output %v is not available in kubectl config get-contexts; resetting to default output format\n", outputFormat)cmd.Flags().Set("output", "")}cmdutil.CheckErr(options.Complete(cmd, args))//准备cmdutil.CheckErr(options.RunGetContexts())//运行},}cmd.Flags().Bool("no-headers", false, "When using the default or custom-column output format, don't print headers (default print headers).")//no-headers选项cmd.Flags().StringP("output", "o", "", "Output format. One of: name")//output选项return cmd
}
//准备
func (o *GetContextsOptions) Complete(cmd *cobra.Command, args []string) error {o.contextNames = args//设置contextNameso.nameOnly = false//设置nameOnlyif cmdutil.GetFlagString(cmd, "output") == "name" {//如果输出格式是name,设置nameOnly为trueo.nameOnly = true}o.showHeaders = true//设置showHeadersif cmdutil.GetFlagBool(cmd, "no-headers") || o.nameOnly {//如果no-headers或nameOnly为true,设置shouHeaders为falseo.showHeaders = false}return nil
}
//运行
func (o GetContextsOptions) RunGetContexts() error {config, err := o.configAccess.GetStartingConfig()//加载configif err != nil {return err}out, found := o.Out.(*tabwriter.Writer)//把out转为tabwriterif !found {//转换失败out = printers.GetNewTabWriter(o.Out)//包装tabwriterdefer out.Flush()}// Build a list of context names to print, and warn if any requested contexts are not found.// Do this before printing the headers so it doesn't look ugly.allErrs := []error{}toPrint := []string{}if len(o.contextNames) == 0 {//如果contextName为空for name := range config.Contexts {//append所有contexts name到toPrinttoPrint = append(toPrint, name)}} else {for _, name := range o.contextNames {//遍历contextNames_, ok := config.Contexts[name]//判断context是否存在if ok {//如果存在append到toPrinttoPrint = append(toPrint, name)} else {//不存在append错误allErrs = append(allErrs, fmt.Errorf("context %v not found", name))}}}if o.showHeaders {//如果showHeaders为trueerr = printContextHeaders(out, o.nameOnly)// 打印头if err != nil {allErrs = append(allErrs, err)}}sort.Strings(toPrint)//对context进行排序for _, name := range toPrint {//遍历toPrinterr = printContext(name, config.Contexts[name], out, o.nameOnly, config.CurrentContext == name)//打印contextif err != nil {allErrs = append(allErrs, err)}}return utilerrors.NewAggregate(allErrs)
}
//打印头
func printContextHeaders(out io.Writer, nameOnly bool) error {columnNames := []string{"CURRENT", "NAME", "CLUSTER", "AUTHINFO", "NAMESPACE"}if nameOnly {//如果nameOnly取第一个columnNames = columnNames[:1]}_, err := fmt.Fprintf(out, "%s\n", strings.Join(columnNames, "\t"))//打印头return err
}//打印context
func printContext(name string, context *clientcmdapi.Context, w io.Writer, nameOnly, current bool) error {if nameOnly {//如果nameOnly打印name返回_, err := fmt.Fprintf(w, "%s\n", name)return err}prefix := " "if current {// 如果是当前context,打印*号prefix = "*"}_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", prefix, name, context.Cluster, context.AuthInfo, context.Namespace)//打印contextreturn err
}
kubectl源码分析之config get-contexts相关推荐
- kubectl源码分析之config delete-context
欢迎关注我的公众号: 目前刚开始写一个月,一共写了18篇原创文章,文章目录如下: istio多集群探秘,部署了50次多集群后我得出的结论 istio多集群链路追踪,附实操视频 istio防故障利器,你 ...
- CI源码分析(一)—config配置文件模块
(一) 使用方式 (a) 系统级配置 语言.字符编码.session.cookie等配置项 文件位置: application/config/config.php 加载方式: 自动加载 调用方式: $ ...
- Storm-kafka源码分析之Config相关类
要创建一个KafkaSpout对象,必须要传入一个SpoutConfig对象,KafkaSpout的构造函数定义如下: public KafkaSpout(SpoutConfig spoutConf) ...
- kubectl源码分析之cordon and uncordon
欢迎关注我的公众号: 目前刚开始写一个月,一共写了18篇原创文章,文章目录如下: istio多集群探秘,部署了50次多集群后我得出的结论 istio多集群链路追踪,附实操视频 istio防故障利器,你 ...
- kubectl源码分析之auth reconcile
欢迎关注我的公众号: 目前刚开始写一个月,一共写了18篇原创文章,文章目录如下: istio多集群探秘,部署了50次多集群后我得出的结论 istio多集群链路追踪,附实操视频 istio防故障利器,你 ...
- kubectl源码分析之taint
欢迎关注我的公众号: 目前刚开始写一个月,一共写了18篇原创文章,文章目录如下: istio多集群探秘,部署了50次多集群后我得出的结论 istio多集群链路追踪,附实操视频 istio防故障利器,你 ...
- 【SA8295P 源码分析】08 - XBL Loader 加载 SMSS、XBL Config、SHRM、CDT 、DDR、APDP、RamDump、OEM_MISC、AOP、QSEE过程分析
[SA8295P 源码分析]08 - XBL Loader 加载 SMSS.XBL Config.SHRM.CDT .DDR.APDP.RamDump.OEM_MISC.AOP.QSEE Dev Co ...
- 【SA8295P 源码分析】14 - Passthrough配置文件 /mnt/vm/images/linux-la.config 内容分析
系列文章汇总见:<[SA8295P 源码分析]00 - 系列文章链接汇总> 本文链接:<[SA8295P 源码分析]14 - Passthrough配置文件 /mnt/vm/imag ...
- kubeadm源码分析(内含kubernetes离线包,三步安装)
k8s离线安装包 三步安装,简单到难以置信 kubeadm源码分析 说句实在话,kubeadm的代码写的真心一般,质量不是很高. 几个关键点来先说一下kubeadm干的几个核心的事: kubeadm ...
- kubeadm源码分析(kubernetes离线安装包,三步安装)
k8s离线安装包 三步安装,简单到难以置信 kubeadm源码分析 说句实在话,kubeadm的代码写的真心一般,质量不是很高. 几个关键点来先说一下kubeadm干的几个核心的事: kubeadm ...
最新文章
- java访问glusterfs_GlusterFS: 访问权限设置
- java jsp setAttribute setParameter 区别
- OkHttp 3.x 源码解析之Interceptor 拦截器
- 除了HTML、CSS与JS,现在WASM也是标准Web语言
- 2M线路保护实现与应用
- 《程序员代码面试指南》第三章 二叉树问题 二叉树节点间的最大距离问题
- glob.glob() + os.path.join() :找到文件路径,拼接路径
- 【AI视野·今日NLP 自然语言处理论文速览 第二十四期】Thu, 30 Sep 2021
- mysql -connector/j驱动下载
- 如何将c语言中的文件,急求如何将下列C语言程序数据存储到文件中?
- QCC3x系列蓝牙芯片
- CentOS 7 systemd添加自定义系统服务
- 编写脚本程序程序,将当前目录下所有的.txt文件更名为.doc文件。
- jquery的插件有哪些
- 力扣报错 error: <identifier> expected
- Qt Excel进行新增、删除、修改读取从入门到精通
- css 背景渐变 图像_交叉渐变背景图像
- UNIX环境高级编程——1.UNIX基础知识
- C++ 命令行CMD 的控制与源代码
- 三丰三坐标编程基本步骤_零基础学习工业机器人PLC编程的基本步骤