Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
randytqwjp committed Dec 6, 2024
1 parent ed22342 commit bddb139
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ status:
message: "recommended size matches current size"
- status: "True"
type: ScalingActive
message: "the HPA was unable to compute the replica count"
message: "the HPA was able to compute the replica count"
currentMetrics:
- containerResource:
container: app
Expand Down
10 changes: 4 additions & 6 deletions internal/controller/tortoise_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func updateResourcesInTestCaseFile(path string, resource resources) error {
}

if resource.hpa != nil {
err = writeToFile(filepath.Join(path, "hpa.yaml"), removeUnnecessaryFieldsFromHPA(resource.hpa))
err = writeToFile(filepath.Join(path, "hpa.yaml"), resource.hpa)
if err != nil {
return err
}
Expand All @@ -195,11 +195,6 @@ func updateResourcesInTestCaseFile(path string, resource resources) error {
return nil
}

func removeUnnecessaryFieldsFromHPA(hpa *v2.HorizontalPodAutoscaler) *v2.HorizontalPodAutoscaler {
//hpa.Status = v2.HorizontalPodAutoscalerStatus{}
return hpa
}

func removeUnnecessaryFieldsFromDeployment(deployment *v1.Deployment) *v1.Deployment {
// remove all default values

Expand Down Expand Up @@ -496,6 +491,9 @@ var _ = Describe("Test TortoiseController", func() {
It("HPA scalingactive condition false", func() {
runTest(filepath.Join("testdata", "reconcile-automatic-emergency-mode-hpa-condition"))
})
It("HPA scalingactive no metrics", func() {
runTest(filepath.Join("testdata", "reconcile-automatic-emergency-mode-hpa-no-metrics"))
})
})
Context("DeletionPolicy is handled correctly", func() {
It("[DeletionPolicy = DeleteAll] delete HPA and VPA when Tortoise is deleted", func() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ func (c *Service) CheckHpaMetricStatus(ctx context.Context, currenthpa *v2.Horiz
for _, condition := range conditions {
if condition.Type == "ScalingActive" && condition.Status == "False" && condition.Reason == "FailedGetResourceMetric" {
//switch to Emergency mode since no metrics
logger.Info("HPA failed to get resource metrics, switch to emergency mode")
return false
}
}
Expand All @@ -807,9 +808,11 @@ func (c *Service) CheckHpaMetricStatus(ctx context.Context, currenthpa *v2.Horiz
if len(currentMetrics) > 0 {
for _, currentMetric := range currentMetrics {
if !currentMetric.ContainerResource.Current.Value.IsZero() {
//Can still get metrics for some containers, scale based on those
return true
}
}
logger.Info("HPA all metrics return 0, switch to emergency mode")
return false
}

Expand Down

0 comments on commit bddb139

Please sign in to comment.