@@ -193,3 +193,172 @@ func TestUpdateHPAsDisregardingIncompatibleHPA(t *testing.T) {
193193 // we expect an event when disregardIncompatibleHPAs=false
194194 require .Len (t , eventRecorder .Events , 1 )
195195}
196+
197+ func TestEqualHPA (t * testing.T ) {
198+ for _ , tc := range []struct {
199+ name string
200+ hpa1 autoscaling.HorizontalPodAutoscaler
201+ hpa2 autoscaling.HorizontalPodAutoscaler
202+ equal bool
203+ }{
204+ {
205+ name : "Identical HPAs are equal" ,
206+ hpa1 : autoscaling.HorizontalPodAutoscaler {
207+ ObjectMeta : metav1.ObjectMeta {
208+ Name : "hpa1" ,
209+ Namespace : "default" ,
210+ },
211+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
212+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
213+ Kind : "Deployment" ,
214+ Name : "app" ,
215+ APIVersion : "apps/v1" ,
216+ },
217+ MinReplicas : & []int32 {1 }[0 ],
218+ MaxReplicas : 10 ,
219+ },
220+ },
221+ hpa2 : autoscaling.HorizontalPodAutoscaler {
222+ ObjectMeta : metav1.ObjectMeta {
223+ Name : "hpa1" ,
224+ Namespace : "default" ,
225+ },
226+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
227+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
228+ Kind : "Deployment" ,
229+ Name : "app" ,
230+ APIVersion : "apps/v1" ,
231+ },
232+ MinReplicas : & []int32 {1 }[0 ],
233+ MaxReplicas : 10 ,
234+ },
235+ },
236+ equal : true ,
237+ },
238+ {
239+ name : "Only kubectl-last-applied diff on HPAs are equal" ,
240+ hpa1 : autoscaling.HorizontalPodAutoscaler {
241+ ObjectMeta : metav1.ObjectMeta {
242+ Name : "hpa1" ,
243+ Namespace : "default" ,
244+ Annotations : map [string ]string {
245+ kubectlLastAppliedAnnotation : "old value" ,
246+ },
247+ },
248+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
249+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
250+ Kind : "Deployment" ,
251+ Name : "app" ,
252+ APIVersion : "apps/v1" ,
253+ },
254+ MinReplicas : & []int32 {1 }[0 ],
255+ MaxReplicas : 10 ,
256+ },
257+ },
258+ hpa2 : autoscaling.HorizontalPodAutoscaler {
259+ ObjectMeta : metav1.ObjectMeta {
260+ Name : "hpa1" ,
261+ Namespace : "default" ,
262+ Annotations : map [string ]string {
263+ kubectlLastAppliedAnnotation : "new value" ,
264+ },
265+ },
266+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
267+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
268+ Kind : "Deployment" ,
269+ Name : "app" ,
270+ APIVersion : "apps/v1" ,
271+ },
272+ MinReplicas : & []int32 {1 }[0 ],
273+ MaxReplicas : 10 ,
274+ },
275+ },
276+ equal : true ,
277+ },
278+ {
279+ name : "diff in annotations are not equal" ,
280+ hpa1 : autoscaling.HorizontalPodAutoscaler {
281+ ObjectMeta : metav1.ObjectMeta {
282+ Name : "hpa1" ,
283+ Namespace : "default" ,
284+ Annotations : map [string ]string {
285+ "annotation" : "old value" ,
286+ },
287+ },
288+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
289+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
290+ Kind : "Deployment" ,
291+ Name : "app" ,
292+ APIVersion : "apps/v1" ,
293+ },
294+ MinReplicas : & []int32 {1 }[0 ],
295+ MaxReplicas : 10 ,
296+ },
297+ },
298+ hpa2 : autoscaling.HorizontalPodAutoscaler {
299+ ObjectMeta : metav1.ObjectMeta {
300+ Name : "hpa1" ,
301+ Namespace : "default" ,
302+ Annotations : map [string ]string {
303+ "annotation" : "new value" ,
304+ },
305+ },
306+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
307+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
308+ Kind : "Deployment" ,
309+ Name : "app" ,
310+ APIVersion : "apps/v1" ,
311+ },
312+ MinReplicas : & []int32 {1 }[0 ],
313+ MaxReplicas : 10 ,
314+ },
315+ },
316+ equal : false ,
317+ },
318+ {
319+ name : "diff in labels are equal" ,
320+ hpa1 : autoscaling.HorizontalPodAutoscaler {
321+ ObjectMeta : metav1.ObjectMeta {
322+ Name : "hpa1" ,
323+ Namespace : "default" ,
324+ Labels : map [string ]string {
325+ "label" : "old-value" ,
326+ },
327+ },
328+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
329+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
330+ Kind : "Deployment" ,
331+ Name : "app" ,
332+ APIVersion : "apps/v1" ,
333+ },
334+ MinReplicas : & []int32 {1 }[0 ],
335+ MaxReplicas : 10 ,
336+ },
337+ },
338+ hpa2 : autoscaling.HorizontalPodAutoscaler {
339+ ObjectMeta : metav1.ObjectMeta {
340+ Name : "hpa1" ,
341+ Namespace : "default" ,
342+ Labels : map [string ]string {
343+ "label" : "new-value" ,
344+ },
345+ },
346+ Spec : autoscaling.HorizontalPodAutoscalerSpec {
347+ ScaleTargetRef : autoscaling.CrossVersionObjectReference {
348+ Kind : "Deployment" ,
349+ Name : "app" ,
350+ APIVersion : "apps/v1" ,
351+ },
352+ MinReplicas : & []int32 {1 }[0 ],
353+ MaxReplicas : 10 ,
354+ },
355+ },
356+ equal : true ,
357+ },
358+ } {
359+ t .Run (tc .name , func (t * testing.T ) {
360+ require .Equal (t , tc .equal , equalHPA (tc .hpa1 , tc .hpa2 ))
361+ })
362+
363+ }
364+ }
0 commit comments