Skip to content

Commit 0922dbb

Browse files
authored
Merge branch 'main' into tt-hase
2 parents 853b1f8 + f24677d commit 0922dbb

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

controllers/tortoise_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
9999
}
100100

101101
defer func() {
102+
if tortoise == nil {
103+
logger.Error(reterr, "get error during the reconciliation, but cannot record the event because tortoise object is nil", "tortoise", req.NamespacedName)
104+
return
105+
}
106+
102107
tortoise = r.TortoiseService.RecordReconciliationFailure(tortoise, reterr, now)
103108
_, err = r.TortoiseService.UpdateTortoiseStatus(ctx, tortoise, now, false)
104109
if err != nil {

pkg/hpa/service.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *Service) InitializeHPA(ctx context.Context, tortoise *autoscalingv1beta
7272
return tortoise, fmt.Errorf("create hpa: %w", err)
7373
}
7474

75-
c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPACreated", fmt.Sprintf("Initialized a HPA %s/%s", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler))
75+
c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPACreated", fmt.Sprintf("Initialized a HPA %s/%s for a created tortoise", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler))
7676

7777
return tortoise, nil
7878
}
@@ -354,6 +354,8 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context
354354
}
355355
// No need to edit container resource phase.
356356

357+
c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPADeleted", fmt.Sprintf("Deleted a HPA %s/%s because tortoise has no resource to scale horizontally", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler))
358+
357359
return tortoise, nil
358360
}
359361

@@ -368,11 +370,18 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context
368370
if err != nil {
369371
return tortoise, fmt.Errorf("initialize hpa: %w", err)
370372
}
373+
374+
c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPACreated", fmt.Sprintf("Initialized a HPA %s/%s because tortoise has resource to scale horizontally", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler))
371375
return tortoise, nil
372376
}
373377
return tortoise, fmt.Errorf("failed to get hpa on tortoise: %w", err)
374378
}
375379

380+
// make sure it's managed by tortoise
381+
if v, ok := hpa.Annotations[annotation.ManagedByTortoiseAnnotation]; !ok || v != "true" {
382+
return tortoise, fmt.Errorf("the HPA %s/%s is specified in tortoise, but not managed by tortoise", hpa.Namespace, hpa.Name)
383+
}
384+
376385
var newhpa *v2.HorizontalPodAutoscaler
377386
var isHpaEdited bool
378387
newhpa, tortoise, isHpaEdited = c.addHPAMetricsFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa, now)
@@ -396,6 +405,8 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context
396405
return tortoise, err
397406
}
398407

408+
c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPAUpdated", fmt.Sprintf("Updated a HPA %s/%s because the autoscaling policy is changed in the tortoise", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler))
409+
399410
return tortoise, nil
400411
}
401412

pkg/hpa/service_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,9 +1179,11 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
11791179
},
11801180
initialHPA: &v2.HorizontalPodAutoscaler{
11811181
ObjectMeta: metav1.ObjectMeta{
1182-
Name: "hpa",
1183-
Namespace: "default",
1184-
Annotations: map[string]string{},
1182+
Name: "hpa",
1183+
Namespace: "default",
1184+
Annotations: map[string]string{
1185+
annotation.ManagedByTortoiseAnnotation: "true",
1186+
},
11851187
},
11861188
Spec: v2.HorizontalPodAutoscalerSpec{
11871189
MinReplicas: ptrInt32(1),
@@ -1281,6 +1283,9 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
12811283
ObjectMeta: metav1.ObjectMeta{
12821284
Name: "hpa",
12831285
Namespace: "default",
1286+
Annotations: map[string]string{
1287+
annotation.ManagedByTortoiseAnnotation: "true",
1288+
},
12841289
},
12851290
Spec: v2.HorizontalPodAutoscalerSpec{
12861291
MinReplicas: ptrInt32(1),
@@ -1415,9 +1420,11 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
14151420
},
14161421
initialHPA: &v2.HorizontalPodAutoscaler{
14171422
ObjectMeta: metav1.ObjectMeta{
1418-
Name: "hpa",
1419-
Namespace: "default",
1420-
Annotations: map[string]string{},
1423+
Name: "hpa",
1424+
Namespace: "default",
1425+
Annotations: map[string]string{
1426+
annotation.ManagedByTortoiseAnnotation: "true",
1427+
},
14211428
},
14221429
Spec: v2.HorizontalPodAutoscalerSpec{
14231430
MinReplicas: ptrInt32(1),
@@ -1528,6 +1535,9 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
15281535
ObjectMeta: metav1.ObjectMeta{
15291536
Name: "hpa",
15301537
Namespace: "default",
1538+
Annotations: map[string]string{
1539+
annotation.ManagedByTortoiseAnnotation: "true",
1540+
},
15311541
},
15321542
Spec: v2.HorizontalPodAutoscalerSpec{
15331543
MinReplicas: ptrInt32(1),

0 commit comments

Comments
 (0)