当前位置: 首页 > 工具软件 > kubectl-slice > 使用案例 >

kubectl源码分析之create deployment

林鸿彩
2023-12-01

发布一个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 DeploymentOpts struct {//option结构体
	CreateSubcommandOptions *CreateSubcommandOptions
}
//创建create deployment命令
func NewCmdCreateDeployment(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
	options := &DeploymentOpts{//初始化结构体
		CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
	}

	cmd := &cobra.Command{//创建cobra命令
		Use:                   "deployment NAME --image=image [--dry-run]",
		DisableFlagsInUseLine: true,
		Aliases:               []string{"deploy"},
		Short:                 i18n.T("Create a deployment with the specified name."),
		Long:                  deploymentLong,
		Example:               deploymentExample,
		Run: func(cmd *cobra.Command, args []string) {
			cmdutil.CheckErr(options.Complete(f, cmd, args))//准备运行
			cmdutil.CheckErr(options.Run())//运行命令
		},
	}

	options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)//设置print 选项

	cmdutil.AddApplyAnnotationFlags(cmd)//设置save-config选项
	cmdutil.AddValidateFlags(cmd)//设置validate选项
	cmdutil.AddGeneratorFlags(cmd, "")//设置generator选项
	cmd.Flags().StringSlice("image", []string{}, "Image name to run.")//设置image选项
	cmd.MarkFlagRequired("image")//必须
	return cmd
}
//准备运行
func (o *DeploymentOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
	name, err := NameFromCommandArgs(cmd, args)//获取名称
	if err != nil {
		return err
	}

	clientset, err := f.KubernetesClientSet()//获取clientset
	if err != nil {
		return err
	}

	generatorName := cmdutil.GetFlagString(cmd, "generator")//后去generator 名称

	if len(generatorName) == 0 {//如果generator名称为空
		generatorName = generateversioned.DeploymentBasicAppsV1GeneratorName//设置默认的generator名称
		generatorNameTemp, err := generateversioned.FallbackGeneratorNameIfNecessary(generatorName, clientset.Discovery(), o.CreateSubcommandOptions.ErrOut)//回退generator,如果有必要
		if err != nil {
			return err
		}
		if generatorNameTemp != generatorName {如果generator不相同则发警告
			cmdutil.Warning(o.CreateSubcommandOptions.ErrOut, generatorName, generatorNameTemp)
		} else {
			generatorName = generatorNameTemp
		}
	}

	imageNames := cmdutil.GetFlagStringSlice(cmd, "image")//获取image选项
	generator, ok := generatorFromName(generatorName, imageNames, name)//根据generator名称生成generator
	if !ok {
		return errUnsupportedGenerator(cmd, generatorName)
	}

	return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)//运行createsubcommandoption complete方法
}
func generatorFromName(
	generatorName string,
	imageNames []string,
	deploymentName string,
) (generate.StructuredGenerator, bool) {

	switch generatorName {//判断generator名称生成相应generator
	case generateversioned.DeploymentBasicAppsV1GeneratorName:
		generator := &generateversioned.DeploymentBasicAppsGeneratorV1{
			BaseDeploymentGenerator: generateversioned.BaseDeploymentGenerator{
				Name:   deploymentName,
				Images: imageNames,
			},
		}
		return generator, true

	case generateversioned.DeploymentBasicAppsV1Beta1GeneratorName:
		generator := &generateversioned.DeploymentBasicAppsGeneratorV1Beta1{
			BaseDeploymentGenerator: generateversioned.BaseDeploymentGenerator{
				Name:   deploymentName,
				Images: imageNames,
			},
		}
		return generator, true

	case generateversioned.DeploymentBasicV1Beta1GeneratorName:
		generator := &generateversioned.DeploymentBasicGeneratorV1{
			BaseDeploymentGenerator: generateversioned.BaseDeploymentGenerator{
				Name:   deploymentName,
				Images: imageNames,
			},
		}
		return generator, true
	}

	return nil, false
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 类似资料: