Skip to content

Commit

Permalink
Move replicas calculation in seperate function
Browse files Browse the repository at this point in the history
Signed-off-by: binjip978 <pdp.eleven11@gmail.com>
  • Loading branch information
binjip978 committed Apr 27, 2022
1 parent 3b6440c commit dbc0877
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pkg/collector/reconcile/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/pkg/collector"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator"
)
Expand Down Expand Up @@ -99,19 +100,7 @@ func expectedDeployments(ctx context.Context, params Params, expected []appsv1.D
}

if params.Instance.Spec.MaxReplicas != nil {
currentReplicas := existing.Status.Replicas
if params.Instance.Spec.Replicas != nil {
// if replicas (minReplicas from HPA perspective) is bigger than
// current status use it.
if *params.Instance.Spec.Replicas > currentReplicas {
currentReplicas = *params.Instance.Spec.Replicas
}
// honor hpa max replicas parameter
if currentReplicas > *params.Instance.Spec.MaxReplicas {
currentReplicas = *params.Instance.Spec.MaxReplicas
}
}

currentReplicas := currentReplicasWithHPA(params.Instance.Spec, existing.Status.Replicas)
updated.Spec.Replicas = &currentReplicas
}

Expand Down Expand Up @@ -160,3 +149,16 @@ func deleteDeployments(ctx context.Context, params Params, expected []appsv1.Dep

return nil
}

// currentReplicasWithHPA calculates deployment replicas if HPA is enabled
func currentReplicasWithHPA(spec v1alpha1.OpenTelemetryCollectorSpec, curr int32) int32 {
if *spec.Replicas > curr {
return *spec.Replicas
}

if curr > *spec.MaxReplicas {
return *spec.MaxReplicas
}

return curr
}
18 changes: 18 additions & 0 deletions pkg/collector/reconcile/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,21 @@ func TestExpectedDeployments(t *testing.T) {

})
}

func TestCurrentReplicasWithHPA(t *testing.T) {
minReplicas := int32(2)
maxReplicas := int32(5)
spec := v1alpha1.OpenTelemetryCollectorSpec{
Replicas: &minReplicas,
MaxReplicas: &maxReplicas,
}

res := currentReplicasWithHPA(spec, 10)
assert.Equal(t, int32(5), res)

res = currentReplicasWithHPA(spec, 1)
assert.Equal(t, int32(2), res)

res = currentReplicasWithHPA(spec, 3)
assert.Equal(t, int32(3), res)
}

0 comments on commit dbc0877

Please sign in to comment.