Skip to content

Commit

Permalink
Merge branch 'main' into tt-hase
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Oct 11, 2023
2 parents 67a9504 + f24677d commit 063b961
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions controllers/tortoise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
}

defer func() {
if tortoise == nil {
logger.Error(reterr, "get error during the reconciliation, but cannot record the event because tortoise object is nil", "tortoise", req.NamespacedName)
return
}

tortoise = r.TortoiseService.RecordReconciliationFailure(tortoise, reterr, now)
_, err = r.TortoiseService.UpdateTortoiseStatus(ctx, tortoise, now, false)
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *Service) InitializeHPA(ctx context.Context, tortoise *autoscalingv1beta
return tortoise, fmt.Errorf("create hpa: %w", err)
}

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

return tortoise, nil
}
Expand Down Expand Up @@ -354,6 +354,8 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context
}
// No need to edit container resource phase.

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))

return tortoise, nil
}

Expand All @@ -368,11 +370,18 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context
if err != nil {
return tortoise, fmt.Errorf("initialize hpa: %w", err)
}

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))
return tortoise, nil
}
return tortoise, fmt.Errorf("failed to get hpa on tortoise: %w", err)
}

// make sure it's managed by tortoise
if v, ok := hpa.Annotations[annotation.ManagedByTortoiseAnnotation]; !ok || v != "true" {
return tortoise, fmt.Errorf("the HPA %s/%s is specified in tortoise, but not managed by tortoise", hpa.Namespace, hpa.Name)
}

var newhpa *v2.HorizontalPodAutoscaler
var isHpaEdited bool
newhpa, tortoise, isHpaEdited = c.addHPAMetricsFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa, now)
Expand All @@ -396,6 +405,8 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context
return tortoise, err
}

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))

return tortoise, nil
}

Expand Down
22 changes: 16 additions & 6 deletions pkg/hpa/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,9 +1179,11 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
},
initialHPA: &v2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "hpa",
Namespace: "default",
Annotations: map[string]string{},
Name: "hpa",
Namespace: "default",
Annotations: map[string]string{
annotation.ManagedByTortoiseAnnotation: "true",
},
},
Spec: v2.HorizontalPodAutoscalerSpec{
MinReplicas: ptrInt32(1),
Expand Down Expand Up @@ -1281,6 +1283,9 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "hpa",
Namespace: "default",
Annotations: map[string]string{
annotation.ManagedByTortoiseAnnotation: "true",
},
},
Spec: v2.HorizontalPodAutoscalerSpec{
MinReplicas: ptrInt32(1),
Expand Down Expand Up @@ -1415,9 +1420,11 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
},
initialHPA: &v2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "hpa",
Namespace: "default",
Annotations: map[string]string{},
Name: "hpa",
Namespace: "default",
Annotations: map[string]string{
annotation.ManagedByTortoiseAnnotation: "true",
},
},
Spec: v2.HorizontalPodAutoscalerSpec{
MinReplicas: ptrInt32(1),
Expand Down Expand Up @@ -1528,6 +1535,9 @@ func TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "hpa",
Namespace: "default",
Annotations: map[string]string{
annotation.ManagedByTortoiseAnnotation: "true",
},
},
Spec: v2.HorizontalPodAutoscalerSpec{
MinReplicas: ptrInt32(1),
Expand Down

0 comments on commit 063b961

Please sign in to comment.