Skip to content

关于 ReplicaSet Controller expectations 的理解 #184

@z1cheng

Description

@z1cheng

非常感谢作者关于 Kubernetes 源码的分享,在阅读关于 ReplicaSet Controller 源码的文章时,发现和作者的理解有些不一致的地方:

  1. 关于 expectations 中 CreationObserved 和 DeletionObserved 方法其实都是对 add 或 del 值 -1
// CreationObserved atomically decrements the `add` expectation count of the given controller.
func (r *ControllerExpectations) CreationObserved(logger klog.Logger, controllerKey string) {
	r.LowerExpectations(logger, controllerKey, 1, 0)
}

// DeletionObserved atomically decrements the `del` expectation count of the given controller.
func (r *ControllerExpectations) DeletionObserved(logger klog.Logger, controllerKey string) {
	r.LowerExpectations(logger, controllerKey, 0, 1)
}

https://github.com/rfyiamcool/notes/blob/6e7be7db68d1dadca095de34a6abaf651cbf81a2/kubernetes_kube_replicaset_controller_code.md?plain=1#L181C1-L182C1

2.expectations 的作用
expectations 中 ExpectCreations 和 ExpectDeletions 都是在 manageReplicas 方法计算得到 add/del 数量后调用的,其含义是「预期创建/删除的数量」,这里的预期其实是 Pod informer 预期收到的创建/删除 Pod 的通知数量
举个例子,预期创建 Pod 为 10, 并且创建失败 3 个(skippedPods 为 3),也就是说在 Pod informer 预期应该收到 7 个创建 Pod 的回调通知,那每收到一个回调通知,会做一次 CreationObserved,假设此时只收到一个,那么在 SatisfiedExpectations Fulfilled 逻辑中 add 值为 6,也就返回 false,这就会跳过 manageReplicas 逻辑,因为此时的 Pod 数量和 spec 中的 replicas 数量不一致是符合预期的,实际上已经调用过 CreatePods 了,只是还在创建中,所以用 expectations 机制就是为了避免重复的创建/删除 Pod
这也就回答了您的问题「有个问题, 当 Fulfilled 返回 true, 也就是当前集群副本数跟预期状态一致时, 为什么还返回 true ?」

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions