@@ -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(ctx *context.AutoscalingContext
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,16 @@ func (p *CapacityBufferPodListProcessor) Process(ctx *context.AutoscalingContext
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+ p .buffersRegistry .fakePodsUIDToBuffer = make (map [string ]* v1alpha1.CapacityBuffer , len (fakePods ))
114+ for _ , fakePod := range fakePods {
115+ if p .buffersRegistry != nil {
116+ p .buffersRegistry .fakePodsUIDToBuffer [string (fakePod .UID )] = buffer
117+ }
118+ }
119+ }
120+
121+ func (p * CapacityBufferPodListProcessor ) provision (buffer * v1alpha1.CapacityBuffer ) []* apiv1.Pod {
94122 if buffer .Status .PodTemplateRef == nil || buffer .Status .Replicas == nil {
95123 return []* apiv1.Pod {}
96124 }
@@ -102,7 +130,7 @@ func (p *CapacityBufferPodListProcessor) provision(buffer *api_v1.CapacityBuffer
102130 p .updateBufferStatus (buffer )
103131 return []* apiv1.Pod {}
104132 }
105- fakePods , err := makeFakePods (buffer . Name , & podTemplate .Template , int (* replicas ))
133+ fakePods , err := makeFakePods (buffer , & podTemplate .Template , int (* replicas ))
106134 if err != nil {
107135 common .UpdateBufferStatusToFailedProvisioing (buffer , "FailedToMakeFakePods" , fmt .Sprintf ("failed to create fake pods with error: %v" , err .Error ()))
108136 p .updateBufferStatus (buffer )
@@ -113,8 +141,8 @@ func (p *CapacityBufferPodListProcessor) provision(buffer *api_v1.CapacityBuffer
113141 return fakePods
114142}
115143
116- func (p * CapacityBufferPodListProcessor ) filterBuffersProvStrategy (buffers []* api_v1 .CapacityBuffer ) []* api_v1 .CapacityBuffer {
117- var filteredBuffers []* api_v1 .CapacityBuffer
144+ func (p * CapacityBufferPodListProcessor ) filterBuffersProvStrategy (buffers []* v1alpha1 .CapacityBuffer ) []* v1alpha1 .CapacityBuffer {
145+ var filteredBuffers []* v1alpha1 .CapacityBuffer
118146 for _ , buffer := range buffers {
119147
120148 if buffer .Status .ProvisioningStrategy != nil && p .provStrategies [* buffer .Status .ProvisioningStrategy ] {
@@ -124,24 +152,24 @@ func (p *CapacityBufferPodListProcessor) filterBuffersProvStrategy(buffers []*ap
124152 return filteredBuffers
125153}
126154
127- func (p * CapacityBufferPodListProcessor ) updateBufferStatus (buffer * api_v1 .CapacityBuffer ) {
155+ func (p * CapacityBufferPodListProcessor ) updateBufferStatus (buffer * v1alpha1 .CapacityBuffer ) {
128156 _ , err := p .client .UpdateCapacityBuffer (buffer )
129157 if err != nil {
130158 klog .Errorf ("Failed to update buffer status for buffer %v, error: %v" , buffer .Name , err .Error ())
131159 }
132160}
133161
134162// makeFakePods creates podCount number of copies of the sample pod
135- func makeFakePods (bufferName string , samplePodTemplate * apiv1.PodTemplateSpec , podCount int ) ([]* apiv1.Pod , error ) {
163+ func makeFakePods (buffer * v1alpha1. CapacityBuffer , samplePodTemplate * apiv1.PodTemplateSpec , podCount int ) ([]* apiv1.Pod , error ) {
136164 var fakePods []* apiv1.Pod
137165 samplePod := getPodFromTemplate (samplePodTemplate )
138- samplePod = withCapacityBufferFakePodAnnotation (samplePod )
139166 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 )
167+ fakePod := samplePod .DeepCopy ()
168+ fakePod = withCapacityBufferFakePodAnnotation (fakePod )
169+ fakePod .Name = fmt .Sprintf ("capacity-buffer-%s-%d" , buffer .Name , i )
170+ fakePod .UID = types .UID (fmt .Sprintf ("%s-%d" , string (buffer .UID ), i ))
171+ fakePod .Spec .NodeName = ""
172+ fakePods = append (fakePods , fakePod )
145173 }
146174 return fakePods , nil
147175}
@@ -150,15 +178,15 @@ func withCapacityBufferFakePodAnnotation(pod *apiv1.Pod) *apiv1.Pod {
150178 if pod .Annotations == nil {
151179 pod .Annotations = make (map [string ]string , 1 )
152180 }
153- pod .Annotations [FakeCapacityBufferPodAnnotationKey ] = FakeCapacityBufferPodAnnotationValue
181+ pod .Annotations [CapacityBufferFakePodAnnotationKey ] = CapacityBufferFakePodAnnotationValue
154182 return pod
155183}
156184
157185func isFakeCapacityBuffersPod (pod * apiv1.Pod ) bool {
158186 if pod .Annotations == nil {
159187 return false
160188 }
161- return pod .Annotations [FakeCapacityBufferPodAnnotationKey ] == FakeCapacityBufferPodAnnotationValue
189+ return pod .Annotations [CapacityBufferFakePodAnnotationKey ] == CapacityBufferFakePodAnnotationValue
162190}
163191
164192func getPodFromTemplate (template * apiv1.PodTemplateSpec ) * apiv1.Pod {
0 commit comments