欢迎关注我的公众号:
目前刚开始写一个月,一共写了18篇原创文章,文章目录如下:
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 RenameContextOptions struct {//rename-context结构体
configAccess clientcmd.ConfigAccess
contextName string
newName string
}
//创建rename-context命令
func NewCmdConfigRenameContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
options := &RenameContextOptions{configAccess: configAccess}//初始化结构体
cmd := &cobra.Command{//创建cobra命令
Use: renameContextUse,
DisableFlagsInUseLine: true,
Short: renameContextShort,
Long: renameContextLong,
Example: renameContextExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.Complete(cmd, args, out))//准备
cmdutil.CheckErr(options.Validate())//校验
cmdutil.CheckErr(options.RunRenameContext(out))//运行
},
}
return cmd
}
//准备
func (o *RenameContextOptions) Complete(cmd *cobra.Command, args []string, out io.Writer) error {
if len(args) != 2 {//参数必须是2个
return helpErrorf(cmd, "Unexpected args: %v", args)
}
o.contextName = args[0]//设置原名称
o.newName = args[1]//设置新名称
return nil
}
// Validate makes sure that provided values for command-line options are valid
//校验
func (o RenameContextOptions) Validate() error {
if len(o.newName) == 0 {//新名称不能为空
return errors.New("You must specify a new non-empty context name")
}
return nil
}
//运行
func (o RenameContextOptions) RunRenameContext(out io.Writer) error {
config, err := o.configAccess.GetStartingConfig()//加载config
if err != nil {
return err
}
configFile := o.configAccess.GetDefaultFilename()//获取config文件路径
if o.configAccess.IsExplicitFile() {//如果指定了kubecofnig
configFile = o.configAccess.GetExplicitFile()//获取kubeconfig指定的文件路径
}
context, exists := config.Contexts[o.contextName]//判断context是否存在
if !exists {//不存在报错
return fmt.Errorf("cannot rename the context %q, it's not in %s", o.contextName, configFile)
}
_, newExists := config.Contexts[o.newName]//判断新名称是否存在
if newExists {//如果存在报错
return fmt.Errorf("cannot rename the context %q, the context %q already exists in %s", o.contextName, o.newName, configFile)
}
config.Contexts[o.newName] = context//设置新context
delete(config.Contexts, o.contextName)//删除旧context
if config.CurrentContext == o.contextName {//如果旧context是正在使用的context
config.CurrentContext = o.newName//正在使用的context设置为新名称
}
if err := clientcmd.ModifyConfig(o.configAccess, *config, true); err != nil {//把配置写回文件
return err
}
fmt.Fprintf(out, "Context %q renamed to %q.\n", o.contextName, o.newName)// 打印结果
return nil
}