-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autoscale option to enable support for Horizontal Pod Autoscaling (…
…#746) * add autoscale, minReplicas maxReplicas to collector types Signed-off-by: Sergei Semenchuk <[email protected]> * manage hpa with min/max replicas if autoscale is enablad Signed-off-by: Sergei Semenchuk <[email protected]> * fix linter Signed-off-by: Sergei Semenchuk <[email protected]> * use status if autoscale is true Signed-off-by: Sergei Semenchuk <[email protected]> * add validation for autoscale Signed-off-by: Sergei Semenchuk <[email protected]> * remove minReplicas Signed-off-by: Sergei Semenchuk <[email protected]> * remove autoscale parameter, trigger HPA if maxReplicas is not nil Signed-off-by: Sergei Semenchuk <[email protected]> * use collector replicas for minReplicas in HPA if HPA status is currently lower Signed-off-by: Sergei Semenchuk <[email protected]> * remove nil check Signed-off-by: Sergei Semenchuk <[email protected]>
- Loading branch information
Showing
14 changed files
with
449 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
autoscalingv1 "k8s.io/api/autoscaling/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" | ||
"github.com/open-telemetry/opentelemetry-operator/internal/config" | ||
"github.com/open-telemetry/opentelemetry-operator/pkg/naming" | ||
) | ||
|
||
const defaultCPUTarget int32 = 90 | ||
|
||
func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) autoscalingv1.HorizontalPodAutoscaler { | ||
labels := Labels(otelcol) | ||
labels["app.kubernetes.io/name"] = naming.Collector(otelcol) | ||
|
||
annotations := Annotations(otelcol) | ||
cpuTarget := defaultCPUTarget | ||
|
||
return autoscalingv1.HorizontalPodAutoscaler{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: naming.Collector(otelcol), | ||
Namespace: otelcol.Namespace, | ||
Labels: labels, | ||
Annotations: annotations, | ||
}, | ||
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{ | ||
ScaleTargetRef: autoscalingv1.CrossVersionObjectReference{ | ||
APIVersion: "apps/v1", | ||
Kind: "Deployment", | ||
Name: naming.Collector(otelcol), | ||
}, | ||
MinReplicas: otelcol.Spec.Replicas, | ||
MaxReplicas: *otelcol.Spec.MaxReplicas, | ||
TargetCPUUtilizationPercentage: &cpuTarget, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" | ||
"github.com/open-telemetry/opentelemetry-operator/internal/config" | ||
. "github.com/open-telemetry/opentelemetry-operator/pkg/collector" | ||
) | ||
|
||
func TestHPA(t *testing.T) { | ||
// prepare | ||
var minReplicas int32 = 3 | ||
var maxReplicas int32 = 5 | ||
|
||
otelcol := v1alpha1.OpenTelemetryCollector{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-instance", | ||
}, | ||
Spec: v1alpha1.OpenTelemetryCollectorSpec{ | ||
Replicas: &minReplicas, | ||
MaxReplicas: &maxReplicas, | ||
}, | ||
} | ||
|
||
cfg := config.New() | ||
hpa := HorizontalPodAutoscaler(cfg, logger, otelcol) | ||
|
||
// verify | ||
assert.Equal(t, "my-instance-collector", hpa.Name) | ||
assert.Equal(t, "my-instance-collector", hpa.Labels["app.kubernetes.io/name"]) | ||
assert.Equal(t, int32(3), *hpa.Spec.MinReplicas) | ||
assert.Equal(t, int32(5), hpa.Spec.MaxReplicas) | ||
assert.Equal(t, int32(90), *hpa.Spec.TargetCPUUtilizationPercentage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.