helm源码分析之dependency list 命令

发布一个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 newDependencyCmd(out io.Writer) *cobra.Command {//创建dependency命令
	cmd := &cobra.Command{//创建cobra命令
		Use:     "dependency update|build|list",
		Aliases: []string{"dep", "dependencies"},
		Short:   "manage a chart's dependencies",
		Long:    dependencyDesc,
		Args:    require.NoArgs,
	}

	cmd.AddCommand(newDependencyListCmd(out))//添加list命令
	cmd.AddCommand(newDependencyUpdateCmd(out))//添加update命令
	cmd.AddCommand(newDependencyBuildCmd(out))//添加build命令

	return cmd
}
func newDependencyListCmd(out io.Writer) *cobra.Command {//创建dependency list命令
	client := action.NewDependency()//初始化结构体

	cmd := &cobra.Command{//创建cobra命令
		Use:     "list CHART",
		Aliases: []string{"ls"},
		Short:   "list the dependencies for the given chart",
		Long:    dependencyListDesc,
		Args:    require.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			chartpath := "."//设置chartpath
			if len(args) > 0 {//如果参数个数大于0个,则设置第0个参数为chartpath
				chartpath = filepath.Clean(args[0])
			}
			return client.List(chartpath, out)//运行命令
		},
	}
	return cmd
}
type Dependency struct {//dependency结构体
	Verify      bool
	Keyring     string
	SkipRefresh bool
}

// NewDependency creates a new Dependency object with the given configuration.
func NewDependency() *Dependency {//初始化结构体
	return &Dependency{}
}
func (d *Dependency) List(chartpath string, out io.Writer) error {//list denendency
	c, err := loader.Load(chartpath)//加载chart
	if err != nil {
		return err
	}

	if c.Metadata.Dependencies == nil {//如果chart没有dependency则打印告警,返回
		fmt.Fprintf(out, "WARNING: no dependencies at %s\n", filepath.Join(chartpath, "charts"))
		return nil
	}

	d.printDependencies(chartpath, out, c.Metadata.Dependencies)//打印依赖
	fmt.Fprintln(out)
  	d.printMissing(chartpath, out, c.Metadata.Dependencies)//打印丢失的依赖
	return nil
}
//打印依赖
func (d *Dependency) printDependencies(chartpath string, out io.Writer, reqs []*chart.Dependency) {
	table := uitable.New()//创建table
	table.MaxColWidth = 80//设置宽度
	table.AddRow("NAME", "VERSION", "REPOSITORY", "STATUS")// 添加头
	for _, row := range reqs {// 打印依赖
		table.AddRow(row.Name, row.Version, row.Repository, d.dependencyStatus(chartpath, row))
	}
	fmt.Fprintln(out, table)//打印表格
}
//打印丢失的依赖
func (d *Dependency) printMissing(chartpath string, out io.Writer, reqs []*chart.Dependency) {
	folder := filepath.Join(chartpath, "charts/*")//获取charts目录
	files, err := filepath.Glob(folder)//获取charts目录文件
	if err != nil {
		fmt.Fprintln(out, err)
		return
	}

	for _, f := range files {//遍历文件
		fi, err := os.Stat(f)//判断文件状态
		if err != nil {
			fmt.Fprintf(out, "Warning: %s\n", err)
		}
		// Skip anything that is not a directory and not a tgz file.
		if !fi.IsDir() && filepath.Ext(f) != ".tgz" {//判断文件是否是目录或tgz文件
			continue
		}
		c, err := loader.Load(f)//加载chart
		if err != nil {
			fmt.Fprintf(out, "WARNING: %q is not a chart.\n", f)
			continue
		}
		found := false
		for _, d := range reqs {//遍历依赖
			if d.Name == c.Name() {//如果依赖名称和chart名称相同则找到
				found = true
				break
			}
		}
		if !found {//没找到则打印告警
			fmt.Fprintf(out, "WARNING: %q is not in Chart.yaml.\n", f)
		}
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(k8s实战之helm源码分析)