0
点赞
收藏
分享

微信扫一扫

helm源码分析之create命令


 欢迎关注我的公众号:

helm源码分析之create命令_绝对路径

 目前刚开始写一个月,一共写了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 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 条评论