Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set replicas to MaxReplicas if HPA is enabled #833

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 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 @@ -98,14 +99,8 @@ func expectedDeployments(ctx context.Context, params Params, expected []appsv1.D
updated.ObjectMeta.Labels[k] = v
}

// if autoscale is enabled, use replicas from current Status
if params.Instance.Spec.MaxReplicas != nil {
currentReplicas := existing.Status.Replicas
// if replicas (minReplicas from HPA perspective) is bigger than
// current status use it.
if params.Instance.Spec.Replicas != nil && *params.Instance.Spec.Replicas > currentReplicas {
currentReplicas = *params.Instance.Spec.Replicas
}
currentReplicas := currentReplicasWithHPA(params.Instance.Spec, existing.Status.Replicas)
updated.Spec.Replicas = &currentReplicas
}

Expand Down Expand Up @@ -154,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 curr < *spec.Replicas {
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)
}