@@ -31,6 +31,7 @@ import (
3131 "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/common"
3232 buffersfilter "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/filters"
3333 ca_context "k8s.io/autoscaler/cluster-autoscaler/context"
34+ "k8s.io/autoscaler/cluster-autoscaler/utils/drain"
3435)
3536
3637// Pods annotation keys and values for fake pods created by capacity buffer pod list processor
@@ -42,11 +43,12 @@ const (
4243// CapacityBufferPodListProcessor processes the pod lists before scale up
4344// and adds buffres api virtual pods.
4445type CapacityBufferPodListProcessor struct {
45- client * client.CapacityBufferClient
46- statusFilter buffersfilter.Filter
47- podTemplateGenFilter buffersfilter.Filter
48- provStrategies map [string ]bool
49- buffersRegistry * capacityBuffersFakePodsRegistry
46+ client * client.CapacityBufferClient
47+ statusFilter buffersfilter.Filter
48+ podTemplateGenFilter buffersfilter.Filter
49+ provStrategies map [string ]bool
50+ buffersRegistry * capacityBuffersFakePodsRegistry
51+ forceSafeToEvictFakePods bool
5052}
5153
5254// capacityBuffersFakePodsRegistry a struct that keeps the status of capacity buffer
@@ -66,7 +68,7 @@ func NewDefaultCapacityBuffersFakePodsRegistry() *capacityBuffersFakePodsRegistr
6668}
6769
6870// NewCapacityBufferPodListProcessor creates a new CapacityRequestPodListProcessor.
69- func NewCapacityBufferPodListProcessor (client * client.CapacityBufferClient , provStrategies []string , buffersRegistry * capacityBuffersFakePodsRegistry ) * CapacityBufferPodListProcessor {
71+ func NewCapacityBufferPodListProcessor (client * client.CapacityBufferClient , provStrategies []string , buffersRegistry * capacityBuffersFakePodsRegistry , forceSafeToEvictFakePods bool ) * CapacityBufferPodListProcessor {
7072 provStrategiesMap := map [string ]bool {}
7173 for _ , ps := range provStrategies {
7274 provStrategiesMap [ps ] = true
@@ -77,9 +79,10 @@ func NewCapacityBufferPodListProcessor(client *client.CapacityBufferClient, prov
7779 common .ReadyForProvisioningCondition : common .ConditionTrue ,
7880 common .ProvisioningCondition : common .ConditionTrue ,
7981 }),
80- podTemplateGenFilter : buffersfilter .NewPodTemplateGenerationChangedFilter (client ),
81- provStrategies : provStrategiesMap ,
82- buffersRegistry : buffersRegistry ,
82+ podTemplateGenFilter : buffersfilter .NewPodTemplateGenerationChangedFilter (client ),
83+ provStrategies : provStrategiesMap ,
84+ buffersRegistry : buffersRegistry ,
85+ forceSafeToEvictFakePods : forceSafeToEvictFakePods ,
8386 }
8487}
8588
@@ -138,7 +141,7 @@ func (p *CapacityBufferPodListProcessor) provision(buffer *v1alpha1.CapacityBuff
138141 p .updateBufferStatus (buffer )
139142 return []* apiv1.Pod {}
140143 }
141- fakePods , err := makeFakePods (buffer , & podTemplate .Template , int (* replicas ))
144+ fakePods , err := makeFakePods (buffer , & podTemplate .Template , int (* replicas ), p . forceSafeToEvictFakePods )
142145 if err != nil {
143146 common .UpdateBufferStatusToFailedProvisioing (buffer , "FailedToMakeFakePods" , fmt .Sprintf ("failed to create fake pods with error: %v" , err .Error ()))
144147 p .updateBufferStatus (buffer )
@@ -152,7 +155,6 @@ func (p *CapacityBufferPodListProcessor) provision(buffer *v1alpha1.CapacityBuff
152155func (p * CapacityBufferPodListProcessor ) filterBuffersProvStrategy (buffers []* v1alpha1.CapacityBuffer ) []* v1alpha1.CapacityBuffer {
153156 var filteredBuffers []* v1alpha1.CapacityBuffer
154157 for _ , buffer := range buffers {
155-
156158 if buffer .Status .ProvisioningStrategy != nil && p .provStrategies [* buffer .Status .ProvisioningStrategy ] {
157159 filteredBuffers = append (filteredBuffers , buffer )
158160 }
@@ -168,15 +170,18 @@ func (p *CapacityBufferPodListProcessor) updateBufferStatus(buffer *v1alpha1.Cap
168170}
169171
170172// makeFakePods creates podCount number of copies of the sample pod
171- func makeFakePods (buffer * v1alpha1.CapacityBuffer , samplePodTemplate * apiv1.PodTemplateSpec , podCount int ) ([]* apiv1.Pod , error ) {
173+ func makeFakePods (buffer * v1alpha1.CapacityBuffer , samplePodTemplate * apiv1.PodTemplateSpec , podCount int , forceSafeToEvictFakePods bool ) ([]* apiv1.Pod , error ) {
172174 var fakePods []* apiv1.Pod
173175 samplePod := getPodFromTemplate (samplePodTemplate , buffer .Namespace )
176+ samplePod .Spec .NodeName = ""
177+ samplePod = withCapacityBufferFakePodAnnotation (samplePod )
178+ if forceSafeToEvictFakePods {
179+ samplePod = withSafeToEvictAnnotation (samplePod )
180+ }
174181 for i := 1 ; i <= podCount ; i ++ {
175182 fakePod := samplePod .DeepCopy ()
176- fakePod = withCapacityBufferFakePodAnnotation (fakePod )
177183 fakePod .Name = fmt .Sprintf ("capacity-buffer-%s-%d" , buffer .Name , i )
178184 fakePod .UID = types .UID (fmt .Sprintf ("%s-%d" , string (buffer .UID ), i ))
179- fakePod .Spec .NodeName = ""
180185 fakePods = append (fakePods , fakePod )
181186 }
182187 return fakePods , nil
@@ -190,7 +195,15 @@ func withCapacityBufferFakePodAnnotation(pod *apiv1.Pod) *apiv1.Pod {
190195 return pod
191196}
192197
193- func isFakeCapacityBuffersPod (pod * apiv1.Pod ) bool {
198+ func withSafeToEvictAnnotation (pod * apiv1.Pod ) * apiv1.Pod {
199+ if pod .Annotations == nil {
200+ pod .Annotations = make (map [string ]string , 1 )
201+ }
202+ pod .Annotations [drain .PodSafeToEvictKey ] = "true"
203+ return pod
204+ }
205+
206+ func IsFakeCapacityBuffersPod (pod * apiv1.Pod ) bool {
194207 if pod .Annotations == nil {
195208 return false
196209 }
0 commit comments