helm源码分析之create命令

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:46   3254   0

发布一个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 createOptions struct {//create结构体
 starter    string // --starter
 name       string
 starterDir string
}
func newCreateCmd(out io.Writer) *cobra.Command {//创建create命令
 o := &createOptions{}//初始化结构体

 cmd := &cobra.Command{//创建cobra命令
  Use:   "create NAME",
  Short: "create a new chart with the given name",
  Long:  createDesc,
  Args:  require.ExactArgs(1),
  RunE: func(cmd *cobra.Command, args []string) error {
   o.name = args[0]//设置name
   o.starterDir = helmpath.DataPath("starters")//设置starter目录
   return o.run(out)//运行
  },
 }

 cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "The name or absolute path to Helm starter scaffold")//starter选项
 return cmd
}
func (o *createOptions) run(out io.Writer) error {//运行
 fmt.Fprintf(out, "Creating %s\n", o.name)//打印

 chartname := filepath.Base(o.name)//获取chart名称
 cfile := &chart.Metadata{//构造chart元数据
  Name:        chartname,
  Description: "A Helm chart for Kubernetes",
  Type:        "application",
  Version:     "0.1.0",
  AppVersion:  "0.1.0",
  APIVersion:  chart.APIVersionV2,
 }

 if o.starter != "" {//如果starter不为空
  // Create from the starter
  lstarter := filepath.Join(o.starterDir, o.starter)//获取starter路径
  // If path is absolute, we don't want to prefix it with helm starters folder
  if filepath.IsAbs(o.starter) {//如果是绝对路径,设置lstarter
   lstarter = o.starter
  }
  return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter)//从starter创建chart
 }

 _, err := chartutil.Create(chartname, filepath.Dir(o.name))//创建chart
 return err
}
//从starter创建chart
func CreateFrom(chartfile *chart.Metadata, dest, src string) error {
 schart, err := loader.Load(src)//加载starter chart
 if err != nil {
  return errors.Wrapf(err, "could not load %s", src)
 }

 schart.Metadata = chartfile//设置chart元数据

 var updatedTemplates []*chart.File

 for _, template := range schart.Templates {//遍历templates
  newData := transform(string(template.Data), schart.Name())//转换template chart名称
  updatedTemplates = append(updatedTemplates, &chart.File{Name: template.Name, Data: newData})
 }

 schart.Templates = updatedTemplates//设置templates
 b, err := yaml.Marshal(schart.Values)//把values转换成yaml
 if err != nil {
  return errors.Wrap(err, "reading values file")
 }

 var m map[string]interface{}
 if err := yaml.Unmarshal(transform(string(b), schart.Name()), &m); err != nil {//转换values里的chart名称,然后转成map
  return errors.Wrap(err, "transforming values file")
 }
 schart.Values = m//设置values

 // SaveDir looks for the file values.yaml when saving rather than the values
 // key in order to preserve the comments in the YAML. The name placeholder
 // needs to be replaced on that file.
 for _, f := range schart.Raw {//遍历chart文件
  if f.Name == ValuesfileName {
   f.Data = transform(string(f.Data), schart.Name())//转换文件中chart的名称
  }
 }

 return SaveDir(schart, dest)//保存chart
}
func Create(name, dir string) (string, error) {//创建chart
 path, err := filepath.Abs(dir)//获取chart绝对路径
 if err != nil {
  return path, err
 }

 if fi, err := os.Stat(path); err != nil {//判断路径是否存在
  return path, err
 } else if !fi.IsDir() {//判断路径是否是目录
  return path, errors.Errorf("no such directory %s", path)
 }

 cdir := filepath.Join(path, name)//获取chart目录
 if fi, err := os.Stat(cdir); err == nil && !fi.IsDir() {//判断chart目录是否存在并且是目录
  return cdir, errors.Errorf("file %s already exists and is not a directory", cdir)
 }

 files := []struct {//设置要生成的chart文件
  path    string
  content []byte
 }{
  {
   // Chart.yaml
   path:    filepath.Join(cdir, ChartfileName),
   content: []byte(fmt.Sprintf(defaultChartfile, name)),
  },
  {
   // values.yaml
   path:    filepath.Join(cdir, ValuesfileName),
   content: []byte(fmt.Sprintf(defaultValues, name)),
  },
  {
   // .helmignore
   path:    filepath.Join(cdir, IgnorefileName),
   content: []byte(defaultIgnore),
  },
  {
   // ingress.yaml
   path:    filepath.Join(cdir, IngressFileName),
   content: transform(defaultIngress, name),
  },
  {
   // deployment.yaml
   path:    filepath.Join(cdir, DeploymentName),
   content: transform(defaultDeployment, name),
  },
  {
   // service.yaml
   path:    filepath.Join(cdir, ServiceName),
   content: transform(defaultService, name),
  },
  {
   // serviceaccount.yaml
   path:    filepath.Join(cdir, ServiceAccountName),
   content: transform(defaultServiceAccount, name),
  },
  {
   // NOTES.txt
   path:    filepath.Join(cdir, NotesName),
   content: transform(defaultNotes, name),
  },
  {
   // _helpers.tpl
   path:    filepath.Join(cdir, HelpersName),
   content: transform(defaultHelpers, name),
  },
  {
   // test-connection.yaml
   path:    filepath.Join(cdir, TestConnectionName),
   content: transform(defaultTestConnection, name),
  },
 }

 for _, file := range files {//遍历文件
  if _, err := os.Stat(file.path); err == nil {//如果文件存在则跳过
   // File exists and is okay. Skip it.
   continue
  }
  if err := writeFile(file.path, file.content); err != nil {//写入文件到磁盘
   return cdir, err
  }
 }
 // Need to add the ChartsDir explicitly as it does not contain any file OOTB
 if err := os.MkdirAll(filepath.Join(cdir, ChartsDir), 0755); err != nil {//添加charts目录
  return cdir, err
 }
 return cdir, nil
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP