helm源码分析之pull

发布一个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源码分析,学习知其然,知其所以然

------------------------------------------------------------------------------------------------------------

func newPullCmd(out io.Writer) *cobra.Command {//创建pull命令
	client := action.NewPull()//初始化结构体

	cmd := &cobra.Command{//创建cobra命令
		Use:     "pull [chart URL | repo/chartname] [...]",
		Short:   "download a chart from a repository and (optionally) unpack it in local directory",
		Aliases: []string{"fetch"},
		Long:    pullDesc,
		Args:    require.MinimumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			client.Settings = settings//设置settings
			if client.Version == "" && client.Devel {//如果没有指定version,则用开发版
				debug("setting version to >0.0.0-0")
				client.Version = ">0.0.0-0"
			}

			for i := 0; i < len(args); i++ {//遍历参数
				output, err := client.Run(args[i])//运行拉取
				if err != nil {
					return err
				}
				fmt.Fprint(out, output)//打印结果
			}
			return nil
		},
	}

	// Function providing dynamic auto-completion
	completion.RegisterValidArgsFunc(cmd, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) {
		if len(args) != 0 {
			return nil, completion.BashCompDirectiveNoFileComp
		}
		return compListCharts(toComplete, false)
	})

	f := cmd.Flags()
	f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")//devel选项
	f.BoolVar(&client.Untar, "untar", false, "if set to true, will untar the chart after downloading it")//untar选项
	f.BoolVar(&client.VerifyLater, "prov", false, "fetch the provenance file, but don't perform verification")//prov选项
	f.StringVar(&client.UntarDir, "untardir", ".", "if untar is specified, this flag specifies the name of the directory into which the chart is expanded")//untardir选项
	f.StringVarP(&client.DestDir, "destination", "d", ".", "location to write the chart. If this and tardir are specified, tardir is appended to this")//destinatio选项
	addChartPathOptionsFlags(f, &client.ChartPathOptions)//chartpath选项

	return cmd
}
type Pull struct {//pull结构体
	ChartPathOptions

	Settings *cli.EnvSettings // TODO: refactor this out of pkg/action

	Devel       bool
	Untar       bool
	VerifyLater bool
	UntarDir    string
	DestDir     string
}

// NewPull creates a new Pull object with the given configuration.
func NewPull() *Pull {//创建pull结构体
	return &Pull{}
}
func (p *Pull) Run(chartRef string) (string, error) {
	var out strings.Builder

	c := downloader.ChartDownloader{//构造downloader
		Out:     &out,
		Keyring: p.Keyring,
		Verify:  downloader.VerifyNever,
		Getters: getter.All(p.Settings),
		Options: []getter.Option{
			getter.WithBasicAuth(p.Username, p.Password),
			getter.WithTLSClientConfig(p.CertFile, p.KeyFile, p.CaFile),
		},
		RepositoryConfig: p.Settings.RepositoryConfig,
		RepositoryCache:  p.Settings.RepositoryCache,
	}

	if p.Verify {//设置verify
		c.Verify = downloader.VerifyAlways
	} else if p.VerifyLater {
		c.Verify = downloader.VerifyLater
	}

	// If untar is set, we fetch to a tempdir, then untar and copy after
	// verification.
	dest := p.DestDir//获取目标目录
	if p.Untar {//如果指定了untar
		var err error
		dest, err = ioutil.TempDir("", "helm-")//目标目录为临时目录
		if err != nil {
			return out.String(), errors.Wrap(err, "failed to untar")
		}
		defer os.RemoveAll(dest)
	}

	if p.RepoURL != "" {//如果repoUrl不为空
		chartURL, err := repo.FindChartInAuthRepoURL(p.RepoURL, p.Username, p.Password, chartRef, p.Version, p.CertFile, p.KeyFile, p.CaFile, getter.All(p.Settings))//获取charturl
		if err != nil {
			return out.String(), err
		}
		chartRef = chartURL
	}

	saved, v, err := c.DownloadTo(chartRef, p.Version, dest)//执行下载
	if err != nil {
		return out.String(), err
	}

	if p.Verify {//如果指定了verify,打印verify
		fmt.Fprintf(&out, "Verification: %v\n", v)
	}

	// After verification, untar the chart into the requested directory.
	if p.Untar {//如果指定了untar
		ud := p.UntarDir//获取untardir
		if !filepath.IsAbs(ud) {//如果untardir不是绝对路径
			ud = filepath.Join(p.DestDir, ud)//凭借untardir
		}
		// Let udCheck to check conflict file/dir without replacing ud when untarDir is the current directory(.).
		udCheck := ud
		if udCheck == "." {//如果untardir为当前目录
			_, udCheck = filepath.Split(chartRef)
		} else {
			_, chartName := filepath.Split(chartRef)
			udCheck = filepath.Join(udCheck, chartName)
		}
		if _, err := os.Stat(udCheck); err != nil {//判断目录是否存在
			if err := os.MkdirAll(udCheck, 0755); err != nil {//新创建目录
				return out.String(), errors.Wrap(err, "failed to untar (mkdir)")
			}

		} else {
			return out.String(), errors.Errorf("failed to untar: a file or directory with the name %s already exists", udCheck)
		}

		return out.String(), chartutil.ExpandFile(ud, saved)//展开文件
	}
	return out.String(), nil
}

 

 

 

 

 

 

 

 

 

 

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页