@@ -26,7 +26,7 @@ import (
2626 "k8s.io/klog/v2"
2727
2828 apiv1 "k8s.io/api/core/v1"
29- api_v1 "k8s.io/autoscaler/cluster-autoscaler/apis/capacitybuffer/autoscaling.x-k8s.io/v1alpha1"
29+ v1alpha1 "k8s.io/autoscaler/cluster-autoscaler/apis/capacitybuffer/autoscaling.x-k8s.io/v1alpha1"
3030 client "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/client"
3131 "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/common"
3232 buffersfilter "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/filters"
@@ -35,8 +35,8 @@ import (
3535
3636// Pods annotation keys and values for fake pods created by capacity buffer pod list processor
3737const (
38- FakeCapacityBufferPodAnnotationKey = "podType"
39- FakeCapacityBufferPodAnnotationValue = "capacityBufferFakePod"
38+ CapacityBufferFakePodAnnotationKey = "podType"
39+ CapacityBufferFakePodAnnotationValue = "capacityBufferFakePod"
4040)
4141
4242// CapacityBufferPodListProcessor processes the pod lists before scale up
@@ -46,10 +46,27 @@ type CapacityBufferPodListProcessor struct {
4646 statusFilter buffersfilter.Filter
4747 podTemplateGenFilter buffersfilter.Filter
4848 provStrategies map [string ]bool
49+ buffersRegistry * capacityBuffersFakePodsRegistry
50+ }
51+
52+ // capacityBuffersFakePodsRegistry a struct that keeps the status of capacity buffer
53+ // the fake pods generated for adding buffer event later
54+ type capacityBuffersFakePodsRegistry struct {
55+ fakePodsUIDToBuffer map [string ]* v1alpha1.CapacityBuffer
56+ }
57+
58+ // NewCapacityBuffersFakePodsRegistry returns a new pointer to empty capacityBuffersFakePodsRegistry
59+ func NewCapacityBuffersFakePodsRegistry (fakePodsToBuffers map [string ]* v1alpha1.CapacityBuffer ) * capacityBuffersFakePodsRegistry {
60+ return & capacityBuffersFakePodsRegistry {fakePodsUIDToBuffer : fakePodsToBuffers }
61+ }
62+
63+ // NewDefaultCapacityBuffersFakePodsRegistry returns a new pointer to empty capacityBuffersFakePodsRegistry
64+ func NewDefaultCapacityBuffersFakePodsRegistry () * capacityBuffersFakePodsRegistry {
65+ return & capacityBuffersFakePodsRegistry {fakePodsUIDToBuffer : map [string ]* v1alpha1.CapacityBuffer {}}
4966}
5067
5168// NewCapacityBufferPodListProcessor creates a new CapacityRequestPodListProcessor.
52- func NewCapacityBufferPodListProcessor (client * client.CapacityBufferClient , provStrategies []string ) * CapacityBufferPodListProcessor {
69+ func NewCapacityBufferPodListProcessor (client * client.CapacityBufferClient , provStrategies []string , buffersRegistry * capacityBuffersFakePodsRegistry ) * CapacityBufferPodListProcessor {
5370 provStrategiesMap := map [string ]bool {}
5471 for _ , ps := range provStrategies {
5572 provStrategiesMap [ps ] = true
@@ -62,6 +79,7 @@ func NewCapacityBufferPodListProcessor(client *client.CapacityBufferClient, prov
6279 }),
6380 podTemplateGenFilter : buffersfilter .NewPodTemplateGenerationChangedFilter (client ),
6481 provStrategies : provStrategiesMap ,
82+ buffersRegistry : buffersRegistry ,
6583 }
6684}
6785
@@ -79,6 +97,7 @@ func (p *CapacityBufferPodListProcessor) Process(autoscalingCtx *ca_context.Auto
7997 totalFakePods := []* apiv1.Pod {}
8098 for _ , buffer := range buffers {
8199 fakePods := p .provision (buffer )
100+ p .updateCapacityBufferRegistry (fakePods , buffer )
82101 totalFakePods = append (totalFakePods , fakePods ... )
83102 }
84103 klog .V (2 ).Infof ("Capacity pod processor injecting %v fake pods provisioning %v capacity buffers" , len (totalFakePods ), len (buffers ))
@@ -90,7 +109,17 @@ func (p *CapacityBufferPodListProcessor) Process(autoscalingCtx *ca_context.Auto
90109func (p * CapacityBufferPodListProcessor ) CleanUp () {
91110}
92111
93- func (p * CapacityBufferPodListProcessor ) provision (buffer * api_v1.CapacityBuffer ) []* apiv1.Pod {
112+ func (p * CapacityBufferPodListProcessor ) updateCapacityBufferRegistry (fakePods []* apiv1.Pod , buffer * v1alpha1.CapacityBuffer ) {
113+ if p .buffersRegistry == nil {
114+ return
115+ }
116+ p .buffersRegistry .fakePodsUIDToBuffer = make (map [string ]* v1alpha1.CapacityBuffer , len (fakePods ))
117+ for _ , fakePod := range fakePods {
118+ p .buffersRegistry .fakePodsUIDToBuffer [string (fakePod .UID )] = buffer
119+ }
120+ }
121+
122+ func (p * CapacityBufferPodListProcessor ) provision (buffer * v1alpha1.CapacityBuffer ) []* apiv1.Pod {
94123 if buffer .Status .PodTemplateRef == nil || buffer .Status .Replicas == nil {
95124 return []* apiv1.Pod {}
96125 }
@@ -102,7 +131,7 @@ func (p *CapacityBufferPodListProcessor) provision(buffer *api_v1.CapacityBuffer
102131 p .updateBufferStatus (buffer )
103132 return []* apiv1.Pod {}
104133 }
105- fakePods , err := makeFakePods (buffer . Name , & podTemplate .Template , int (* replicas ))
134+ fakePods , err := makeFakePods (buffer , & podTemplate .Template , int (* replicas ))
106135 if err != nil {
107136 common .UpdateBufferStatusToFailedProvisioing (buffer , "FailedToMakeFakePods" , fmt .Sprintf ("failed to create fake pods with error: %v" , err .Error ()))
108137 p .updateBufferStatus (buffer )
@@ -113,8 +142,8 @@ func (p *CapacityBufferPodListProcessor) provision(buffer *api_v1.CapacityBuffer
113142 return fakePods
114143}
115144
116- func (p * CapacityBufferPodListProcessor ) filterBuffersProvStrategy (buffers []* api_v1 .CapacityBuffer ) []* api_v1 .CapacityBuffer {
117- var filteredBuffers []* api_v1 .CapacityBuffer
145+ func (p * CapacityBufferPodListProcessor ) filterBuffersProvStrategy (buffers []* v1alpha1 .CapacityBuffer ) []* v1alpha1 .CapacityBuffer {
146+ var filteredBuffers []* v1alpha1 .CapacityBuffer
118147 for _ , buffer := range buffers {
119148
120149 if buffer .Status .ProvisioningStrategy != nil && p .provStrategies [* buffer .Status .ProvisioningStrategy ] {
@@ -124,24 +153,24 @@ func (p *CapacityBufferPodListProcessor) filterBuffersProvStrategy(buffers []*ap
124153 return filteredBuffers
125154}
126155
127- func (p * CapacityBufferPodListProcessor ) updateBufferStatus (buffer * api_v1 .CapacityBuffer ) {
156+ func (p * CapacityBufferPodListProcessor ) updateBufferStatus (buffer * v1alpha1 .CapacityBuffer ) {
128157 _ , err := p .client .UpdateCapacityBuffer (buffer )
129158 if err != nil {
130159 klog .Errorf ("Failed to update buffer status for buffer %v, error: %v" , buffer .Name , err .Error ())
131160 }
132161}
133162
134163// makeFakePods creates podCount number of copies of the sample pod
135- func makeFakePods (bufferName string , samplePodTemplate * apiv1.PodTemplateSpec , podCount int ) ([]* apiv1.Pod , error ) {
164+ func makeFakePods (buffer * v1alpha1. CapacityBuffer , samplePodTemplate * apiv1.PodTemplateSpec , podCount int ) ([]* apiv1.Pod , error ) {
136165 var fakePods []* apiv1.Pod
137166 samplePod := getPodFromTemplate (samplePodTemplate )
138- samplePod = withCapacityBufferFakePodAnnotation (samplePod )
139167 for i := 1 ; i <= podCount ; i ++ {
140- newPod := samplePod .DeepCopy ()
141- newPod .Name = fmt .Sprintf ("capacity-buffer-%s-%d" , bufferName , i )
142- newPod .UID = types .UID (fmt .Sprintf ("%s-%d" , string (bufferName ), i ))
143- newPod .Spec .NodeName = ""
144- fakePods = append (fakePods , newPod )
168+ fakePod := samplePod .DeepCopy ()
169+ fakePod = withCapacityBufferFakePodAnnotation (fakePod )
170+ fakePod .Name = fmt .Sprintf ("capacity-buffer-%s-%d" , buffer .Name , i )
171+ fakePod .UID = types .UID (fmt .Sprintf ("%s-%d" , string (buffer .UID ), i ))
172+ fakePod .Spec .NodeName = ""
173+ fakePods = append (fakePods , fakePod )
145174 }
146175 return fakePods , nil
147176}
@@ -150,15 +179,15 @@ func withCapacityBufferFakePodAnnotation(pod *apiv1.Pod) *apiv1.Pod {
150179 if pod .Annotations == nil {
151180 pod .Annotations = make (map [string ]string , 1 )
152181 }
153- pod .Annotations [FakeCapacityBufferPodAnnotationKey ] = FakeCapacityBufferPodAnnotationValue
182+ pod .Annotations [CapacityBufferFakePodAnnotationKey ] = CapacityBufferFakePodAnnotationValue
154183 return pod
155184}
156185
157186func isFakeCapacityBuffersPod (pod * apiv1.Pod ) bool {
158187 if pod .Annotations == nil {
159188 return false
160189 }
161- return pod .Annotations [FakeCapacityBufferPodAnnotationKey ] == FakeCapacityBufferPodAnnotationValue
190+ return pod .Annotations [CapacityBufferFakePodAnnotationKey ] == CapacityBufferFakePodAnnotationValue
162191}
163192
164193func getPodFromTemplate (template * apiv1.PodTemplateSpec ) * apiv1.Pod {
0 commit comments