kubernetes源码分析-deployment分析

  1. deployemt启动注册:

​ cmd/kube-controller-manager/app/controllermanager.go

func NewControllerInitializers(loopMode ControllerLoopMode) map[string]InitFunc {
	.....
	controllers["deployment"] = startDeploymentController
	....
}

func startDeploymentController(ctx ControllerContext) (http.Handler, bool, error) {
	if !ctx.AvailableResources[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] {
		return nil, false, nil
	}
	dc, err := deployment.NewDeploymentController(
		ctx.InformerFactory.Apps().V1().Deployments(),
		ctx.InformerFactory.Apps().V1().ReplicaSets(),
		ctx.InformerFactory.Core().V1().Pods(),
		ctx.ClientBuilder.ClientOrDie("deployment-controller"),
	)
	if err != nil {
		return nil, true, fmt.Errorf("error creating Deployment controller: %v", err)
	}
	go dc.Run(int(ctx.ComponentConfig.DeploymentController.ConcurrentDeploymentSyncs), ctx.Stop)
	return nil, true, nil
}

NewControllerInitializers 注册 startDeploymentController

  1. NewDeploymentController 定义在pkg/controller/deployment/deployment_controller.go
// NewDeploymentController creates a new DeploymentController.
func NewDeploymentController(dInformer appsinformers.DeploymentInformer, rsInformer appsinformers.ReplicaSetInformer, podInformer coreinformers.PodInformer, client clientset.Interface) (*DeploymentController, error) {
	 ......

	dc.syncHandler = dc.syncDeployment
	dc.enqueueDeployment = dc.enqueue

	......
	return dc, nil
}

// Run begins watching and syncing.
func (dc *DeploymentController) Run(workers int, stopCh <-chan struct{}) {
	defer utilruntime.HandleCrash()
	defer dc.queue.ShutDown()

	klog.Infof("Starting deployment controller")
	defer klog.Infof("Shutting down deployment controller")

	if !cache.WaitForNamedCacheSync("deployment", stopCh, dc.dListerSynced, dc.rsListerSynced, dc.podListerSynced) {
		return
	}

	for i := 0; i < workers; i++ {
		go wait.Until(dc.worker, time.Second, stopCh)
	}

	<-stopCh
}

调用run启动, handler处理响应, 实际通过dc.syncHandler = dc.syncDeployment 指向syncDeployment

  1. syncDeployment定义:

    // syncDeployment will sync the deployment with the given key.
    // This function is not meant to be invoked concurrently with the same key.
    func (dc *DeploymentController) syncDeployment(key string) error {
    	startTime := time.Now()
    	klog.V(4).Infof("Started syncing deployment %q (%v)", key, startTime)
    	defer func() {
    		klog.V(4).Infof("Finished syncing deployment %q (%v)", key, time.Since(startTime))
    	}()
    
    	namespace, name, err := cache.SplitMetaNamespaceKey(key)
    	if err != nil {
    		return err
    	}
    	deployment, err := dc.dLister.Deployments(namespace).Get(name)
    	if errors.IsNotFound(err) {
    		klog.V(2).Infof("Deployment %v has been deleted", key)
    		return nil
    	}
    	if err != nil {
    		return err
    	}
    
    	// Deep-copy otherwise we are mutating our cache.
    	// TODO: Deep-copy only when needed.
    	d := deployment.DeepCopy()
    
    	everything := metav1.LabelSelector{}
    	if reflect.DeepEqual(d.Spec.Selector, &everything) {
    		dc.eventRecorder.Eventf(d, v1.EventTypeWarning, "SelectingAll", "This deployment is selecting all pods. A non-empty selector is required.")
    		if d.Status.ObservedGeneration < d.Generation {
    			d.Status.ObservedGeneration = d.Generation
    			dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(context.TODO(), d, metav1.UpdateOptions{})
    		}
    		return nil
    	}
    
    	// List ReplicaSets owned by this Deployment, while reconciling ControllerRef
    	// through adoption/orphaning.
    	rsList, err := dc.getReplicaSetsForDeployment(d)
    	if err != nil {
    		return err
    	}
    	// List all Pods owned by this Deployment, grouped by their ReplicaSet.
    	// Current uses of the podMap are:
    	//
    	// * check if a Pod is labeled correctly with the pod-template-hash label.
    	// * check that no old Pods are running in the middle of Recreate Deployments.
    	podMap, err := dc.getPodMapForDeployment(d, rsList)
    	if err != nil {
    		return err
    	}
    
    	if d.DeletionTimestamp != nil {
    		return dc.syncStatusOnly(d, rsList)
    	}
    
    	// Update deployment conditions with an Unknown condition when pausing/resuming
    	// a deployment. In this way, we can be sure that we won't timeout when a user
    	// resumes a Deployment with a set progressDeadlineSeconds.
    	if err = dc.checkPausedConditions(d); err != nil {
    		return err
    	}
    
    	if d.Spec.Paused {
    		return dc.sync(d, rsList)
    	}
    
    	// rollback is not re-entrant in case the underlying replica sets are updated with a new
    	// revision so we should ensure that we won't proceed to update replica sets until we
    	// make sure that the deployment has cleaned up its rollback spec in subsequent enqueues.
    	if getRollbackTo(d) != nil {
    		return dc.rollback(d, rsList)
    	}
    
    	scalingEvent, err := dc.isScalingEvent(d, rsList)
    	if err != nil {
    		return err
    	}
    	if scalingEvent {
    		return dc.sync(d, rsList)
    	}
    
    	switch d.Spec.Strategy.Type {
    	case apps.RecreateDeploymentStrategyType:
    		return dc.rolloutRecreate(d, rsList, podMap)
    	case apps.RollingUpdateDeploymentStrategyType:
    		return dc.rolloutRolling(d, rsList)
    	}
    	return fmt.Errorf("unexpected deployment strategy type: %s", d.Spec.Strategy.Type)
    }
    
  2. Deployment删除

    	if d.DeletionTimestamp != nil {
    		return dc.syncStatusOnly(d, rsList)
    	}
    
    

    通过标记deletiontimestamp判断, 删除调用syncStatusOnly 更新状态:

    // syncStatusOnly only updates Deployments Status and doesn't take any mutating actions.
    func (dc *DeploymentController) syncStatusOnly(d *apps.Deployment, rsList []*apps.ReplicaSet) error {
    	newRS, oldRSs, err := dc.getAllReplicaSetsAndSyncRevision(d, rsList, false)
    	if err != nil {
    		return err
    	}
    
    	allRSs := append(oldRSs, newRS)
    	return dc.syncDeploymentStatus(allRSs, newRS, d)
    }
    

    注释// syncStatusOnly only updates Deployments Status and doesn't take any mutating actions. 说明只更新状态, 不进行实际的删除操作

    getAllReplicaSetsAndSyncRevision 获取所有的replicationset以及版本信息列表

    将rs的列表的new以及old生成为所有的列表allrss

    dc.syncDeploymentStatus(allRSs, newRS, d) 同步状态信息

    // syncDeploymentStatus checks if the status is up
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值