Skip to content

Commit

Permalink
chore: move hpa cleanup into deleteHPA
Browse files Browse the repository at this point in the history
Signed-off-by: Ardika Bagus <[email protected]>
  • Loading branch information
ardikabs committed Dec 6, 2023
1 parent 03c193d commit cd2b31e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
4 changes: 2 additions & 2 deletions internal/infrastructure/kubernetes/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (i *Infra) createOrUpdate(ctx context.Context, r ResourceRender) error {
return errors.Wrapf(err, "failed to create or update service %s/%s", i.Namespace, r.Name())
}

if err := i.configureHPA(ctx, r); err != nil {
return errors.Wrapf(err, "failed to configure hpa %s/%s", i.Namespace, r.Name())
if err := i.createOrUpdateHPA(ctx, r); err != nil {
return errors.Wrapf(err, "failed to create or update hpa %s/%s", i.Namespace, r.Name())
}

Check warning on line 75 in internal/infrastructure/kubernetes/infra.go

View check run for this annotation

Codecov / codecov/patch

internal/infrastructure/kubernetes/infra.go#L74-L75

Added lines #L74 - L75 were not covered by tests

return nil
Expand Down
32 changes: 11 additions & 21 deletions internal/infrastructure/kubernetes/infra_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,19 @@ func (i *Infra) createOrUpdateDeployment(ctx context.Context, r ResourceRender)
})
}

// configureHPA configures HorizontalPodAutoscaler object in the kube api server based on the provided ResourceRender,
// there are two operations on this method:
// 1. if an HPA is defined and it doesn't exist then creates, or otherwise updates
// 2. if an HPA is not defined then delete the object irrespective of its existence
func (i *Infra) configureHPA(ctx context.Context, r ResourceRender) error {
// createOrUpdateHPA configures HorizontalPodAutoscaler object in the kube api server based on
// the provided ResourceRender, if it doesn't exist and updates it if it does,
// and delete hpa if not set.
func (i *Infra) createOrUpdateHPA(ctx context.Context, r ResourceRender) error {
hpa, err := r.HorizontalPodAutoscaler()
if err != nil {
return err
}

Check warning on line 102 in internal/infrastructure/kubernetes/infra_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/infrastructure/kubernetes/infra_resource.go#L101-L102

Added lines #L101 - L102 were not covered by tests

// when HorizontalPodAutoscaler is disabled,
// when HorizontalPodAutoscaler is not set,
// then delete the object in the kube api server if any.
if hpa == nil {
hpa = &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}

return i.Client.Delete(ctx, hpa)
return i.deleteHPA(ctx, r)
}

current := &autoscalingv2.HorizontalPodAutoscaler{}
Expand Down Expand Up @@ -195,13 +187,11 @@ func (i *Infra) deleteService(ctx context.Context, r ResourceRender) error {

// deleteHpa deletes the Horizontal Pod Autoscaler associated to its renderer, if it exists.
func (i *Infra) deleteHPA(ctx context.Context, r ResourceRender) error {
hpa, err := r.HorizontalPodAutoscaler()
if err != nil {
return err
}

if hpa == nil {
return nil
hpa := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}

return i.Client.Delete(ctx, hpa)
Expand Down

0 comments on commit cd2b31e

Please sign in to comment.