kubectl源码分析之config delete-cluster
  TEZNKK3IfmPf 2023年11月15日 24 0


//创建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
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月15日   16   0   0 文件名
  TEZNKK3IfmPf   2023年11月15日   35   0   0 ioside云原生
  TEZNKK3IfmPf   2023年11月15日   23   0   0 C++权限控制
TEZNKK3IfmPf