Skip to content

Commit

Permalink
Get HPA by Label selector (#625)
Browse files Browse the repository at this point in the history
* Get HPA by Label selector

* Dont check for possible NotFound error
  • Loading branch information
Richard87 authored May 16, 2024
1 parent 51013af commit 936c1b9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions api/deployments/component_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployments

import (
"context"
"fmt"
"strings"

deploymentModels "github.com/equinor/radix-api/api/deployments/models"
Expand All @@ -18,7 +19,6 @@ import (
crdUtils "github.com/equinor/radix-operator/pkg/apis/utils"
v2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -87,7 +87,7 @@ func (deploy *deployHandler) getComponent(ctx context.Context, component v1.Radi
}

if component.GetType() == v1.RadixComponentTypeComponent {
hpaSummary, err := deploy.getHpaSummary(ctx, component, envNs)
hpaSummary, err := deploy.getHpaSummary(ctx, component, ra.Name, envNs)
if err != nil {
return nil, err
}
Expand All @@ -96,14 +96,19 @@ func (deploy *deployHandler) getComponent(ctx context.Context, component v1.Radi
return deploymentComponent, nil
}

func (deploy *deployHandler) getHpaSummary(ctx context.Context, component v1.RadixCommonDeployComponent, envNs string) (*deploymentModels.HorizontalScalingSummary, error) {
hpa, err := deploy.accounts.UserAccount.Client.AutoscalingV2().HorizontalPodAutoscalers(envNs).Get(ctx, component.GetName(), metav1.GetOptions{})
func (deploy *deployHandler) getHpaSummary(ctx context.Context, component v1.RadixCommonDeployComponent, appName, envNs string) (*deploymentModels.HorizontalScalingSummary, error) {
selector := labelselector.ForComponent(appName, component.GetName()).String()
hpas, err := deploy.accounts.UserAccount.Client.AutoscalingV2().HorizontalPodAutoscalers(envNs).List(ctx, metav1.ListOptions{LabelSelector: selector})
if err != nil {
if errors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
if len(hpas.Items) == 0 {
return nil, nil
}
if len(hpas.Items) > 1 {
return nil, fmt.Errorf("found more than 1 HPA for component %s", component.GetName())
}
hpa := &hpas.Items[0]

minReplicas := int32(1)
if hpa.Spec.MinReplicas != nil {
Expand Down

0 comments on commit 936c1b9

Please sign in to comment.