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

Change horizontal pod autoscaler to use otelcol scale subresource #941

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c5f0e1d
Change horizontal pod autoscaler to use otelcol scale subresource
kevinearls Jun 23, 2022
04abf43
Fix name
kevinearls Jun 23, 2022
e6e73da
Change horizontal pod autoscaler to use otelcol scale subresource
kevinearls Jun 23, 2022
6ba5e93
Fix name
kevinearls Jun 23, 2022
23d00f0
Merge branch 'hpa-use-scale-subresource' of github.com:kevinearls/ope…
kevinearls Jun 29, 2022
949a29f
Moved autoscale values into their own spec, added scale up test
kevinearls Jun 29, 2022
f088fea
Added a period to a comment to make the linter stop complaining
kevinearls Jun 29, 2022
34f44f7
Reran make generate after linting
kevinearls Jun 29, 2022
4974844
Fix autoscale test
kevinearls Jun 29, 2022
88575f6
Updated to autoscaling/v2beta2 as v1 is not available in k8s 1.24
kevinearls Jun 30, 2022
1d45856
Install metrics-server on kind clusters for autoscale tests
kevinearls Jul 1, 2022
2149091
Merge branch 'main' into hpa-use-scale-subresource
kevinearls Jul 1, 2022
dc55641
Fix more lint complaints
kevinearls Jul 1, 2022
36d2dda
Only scale up to 5 replicas in test
kevinearls Jul 1, 2022
41b581a
Fix test
kevinearls Jul 1, 2022
c22f6b4
Update test so it will work correctly in CI
kevinearls Jul 4, 2022
b4a72d1
Revert to autoscaling v1
kevinearls Jul 4, 2022
33d9c48
Reduce maxReplicas count to get kuttl tests to work in CI
kevinearls Jul 6, 2022
2f5ade5
Back out addition of AutoScaleSpec to CRD
kevinearls Jul 6, 2022
0bc348b
Fixed api-docs
kevinearls Jul 6, 2022
3ce81aa
Restore accidentally delete test, clean up whitespace
kevinearls Jul 6, 2022
aeb4a97
Document that this script is not needed for minikube
kevinearls Jul 7, 2022
96917a2
Fix scaledown
kevinearls Jul 7, 2022
2ef9467
Update webhook to validate minReplicas when needed
kevinearls Jul 11, 2022
ba0423b
Fix tests
kevinearls Jul 11, 2022
ef318ef
Initial upgrade code for instances with an HPA
kevinearls Jul 13, 2022
27fb26f
Make sure old Deploment based autoscalers get updated
kevinearls Jul 15, 2022
c733b6e
Fix lint error
kevinearls Jul 15, 2022
81aa971
Respond to comments
kevinearls Jul 15, 2022
f3d190b
Fixed api-docs
kevinearls Jul 15, 2022
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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ e2e:
$(KUTTL) test

.PHONY: prepare-e2e
prepare-e2e: kuttl set-test-image-vars set-image-controller container container-target-allocator start-kind load-image-all
prepare-e2e: kuttl set-test-image-vars set-image-controller container container-target-allocator start-kind install-metrics-server load-image-all
mkdir -p tests/_build/crds tests/_build/manifests
$(KUSTOMIZE) build config/default -o tests/_build/manifests/01-opentelemetry-operator.yaml
$(KUSTOMIZE) build config/crd -o tests/_build/crds/
Expand Down Expand Up @@ -184,6 +184,10 @@ container-target-allocator:
start-kind:
kind create cluster --config $(KIND_CONFIG)

.PHONY: install-metrics-server
install-metrics-server:
./hack/install-metrics-server.sh

.PHONY: load-image-all
load-image-all: load-image-operator load-image-target-allocator

Expand Down
4 changes: 4 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type OpenTelemetryCollectorSpec struct {
// +optional
Replicas *int32 `json:"replicas,omitempty"`

// MinReplicas sets a lower bound to the autoscaling feature. It must be at least 1
// +optional
MinReplicas *int32 `json:"minReplicas,omitempty"`

// MaxReplicas sets an upper bound to the autoscaling feature. If MaxReplicas is set autoscaling is enabled.
// +optional
MaxReplicas *int32 `json:"maxReplicas,omitempty"`
Copy link
Member

@pavolloffay pavolloffay Jul 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we can change the CRD. In this case, a field is removed.

Expand Down
9 changes: 9 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
if r.Spec.Replicas != nil && *r.Spec.Replicas > *r.Spec.MaxReplicas {
return fmt.Errorf("the OpenTelemetry Spec autoscale configuration is incorrect, replicas must not be greater than maxReplicas")
}

if r.Spec.MinReplicas != nil && *r.Spec.MinReplicas > *r.Spec.MaxReplicas {
return fmt.Errorf("the OpenTelemetry Spec autoscale configuration is incorrect, minReplicas must not be greater than maxReplicas")
}

if r.Spec.MinReplicas != nil && *r.Spec.MinReplicas < int32(1) {
return fmt.Errorf("the OpenTelemetry Spec autoscale configuration is incorrect, minReplicas should be one or more")
}

}

return nil
Expand Down
5 changes: 5 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ spec:
If MaxReplicas is set autoscaling is enabled.
format: int32
type: integer
minReplicas:
description: MinReplicas sets a lower bound to the autoscaling feature. It
must be at least 1
format: int32
type: integer
mode:
description: Mode represents how the collector should be deployed
(deployment, daemonset, statefulset or sidecar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ spec:
If MaxReplicas is set autoscaling is enabled.
format: int32
type: integer
minReplicas:
description: MinReplicas sets a lower bound to the autoscaling feature. It
must be at least 1
format: int32
type: integer
mode:
description: Mode represents how the collector should be deployed
(deployment, daemonset, statefulset or sidecar)
Expand Down
9 changes: 9 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,15 @@ OpenTelemetryCollectorSpec defines the desired state of OpenTelemetryCollector.
<i>Format</i>: int32<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>minReplicas</b></td>
<td>integer</td>
<td>
MinReplicas sets a lower bound to the autoscaling feature. It must be at least 1<br/>
<br/>
<i>Format</i>: int32<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>mode</b></td>
<td>enum</td>
Expand Down
7 changes: 7 additions & 0 deletions hack/install-metrics-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Install metrics-server on kind clusters for autoscale tests. Note: This is not needed for minikube,
# you can just add --addons "metrics-server" to the start command.
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl patch deployment -n kube-system metrics-server --type "json" -p '[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": --kubelet-insecure-tls}]'
kubectl wait --for=condition=available deployment/metrics-server -n kube-system --timeout=5m
9 changes: 5 additions & 4 deletions pkg/collector/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al

return autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: naming.Collector(otelcol),
Name: naming.HorizontalPodAutoscaler(otelcol),
Namespace: otelcol.Namespace,
Labels: labels,
Annotations: annotations,
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscalingv1.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: naming.Collector(otelcol),
APIVersion: v1alpha1.GroupVersion.String(),
Kind: "OpenTelemetryCollector",
Name: naming.OpenTelemetryCollector(otelcol),
},

MinReplicas: otelcol.Spec.Replicas,
MaxReplicas: *otelcol.Spec.MaxReplicas,
TargetCPUUtilizationPercentage: &cpuTarget,
Expand Down
5 changes: 0 additions & 5 deletions pkg/collector/reconcile/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ func expectedDeployments(ctx context.Context, params Params, expected []appsv1.D
updated.ObjectMeta.Labels[k] = v
}

if params.Instance.Spec.MaxReplicas != nil && desired.Labels["app.kubernetes.io/component"] == "opentelemetry-collector" {
currentReplicas := currentReplicasWithHPA(params.Instance.Spec, existing.Status.Replicas)
updated.Spec.Replicas = &currentReplicas
}

// Selector is an immutable field, if set, we cannot modify it otherwise we will face reconciliation error.
updated.Spec.Selector = existing.Spec.Selector.DeepCopy()

Expand Down
19 changes: 18 additions & 1 deletion pkg/collector/reconcile/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ 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/naming"
)

// +kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -52,6 +54,7 @@ func HorizontalPodAutoscalers(ctx context.Context, params Params) error {
}

func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expected []autoscalingv1.HorizontalPodAutoscaler) error {
one := int32(1)
for _, obj := range expected {
desired := obj

Expand Down Expand Up @@ -81,9 +84,23 @@ func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expect
}

updated.OwnerReferences = desired.OwnerReferences
updated.Spec.MinReplicas = params.Instance.Spec.Replicas
if params.Instance.Spec.MaxReplicas != nil {
updated.Spec.MaxReplicas = *params.Instance.Spec.MaxReplicas
if params.Instance.Spec.MinReplicas != nil {
updated.Spec.MinReplicas = params.Instance.Spec.MinReplicas
} else {
updated.Spec.MinReplicas = &one
}
}

// If the deployment has an autoscaler based on Deployment, replace it with one based on OpenTelemetryCollector
// Note: this is for version 0.56.0 and can eventually be removed
if existing.Spec.ScaleTargetRef.Kind == "Deployment" {
updated.Spec.ScaleTargetRef = autoscalingv1.CrossVersionObjectReference{
Kind: "OpenTelemetryCollector",
Name: naming.OpenTelemetryCollectorName(params.Instance.Name),
APIVersion: v1alpha1.GroupVersion.String(),
}
}

for k, v := range desired.Annotations {
Expand Down
32 changes: 32 additions & 0 deletions pkg/collector/upgrade/v0_56_0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 upgrade

import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)

func upgrade0_56_0(u VersionUpgrade, otelcol *v1alpha1.OpenTelemetryCollector) (*v1alpha1.OpenTelemetryCollector, error) {
// return if this does not use an autoscaler
if otelcol.Spec.MaxReplicas == nil {
return otelcol, nil
}

u.Log.Info("in upgrade0_56_0", "Otel Instance", otelcol.Name, "Upgrade version", u.Version.String())
one := int32(1)
otelcol.Spec.MinReplicas = &one

return otelcol, nil
}
24 changes: 24 additions & 0 deletions pkg/collector/upgrade/v0_56_0_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 upgrade_test

import (
"fmt"
"testing"
)

func Test0_56_0Upgrade(t *testing.T) {
fmt.Println("NYI")
}
4 changes: 4 additions & 0 deletions pkg/collector/upgrade/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ var (
Version: *semver.MustParse("0.43.0"),
upgrade: upgrade0_43_0,
},
{
Version: *semver.MustParse("0.56.0"),
upgrade: upgrade0_56_0,
},
}

// Latest represents the latest version that we need to upgrade. This is not necessarily the latest known version.
Expand Down
15 changes: 15 additions & 0 deletions pkg/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func Collector(otelcol v1alpha1.OpenTelemetryCollector) string {
return DNSName(Truncate("%s-collector", 63, otelcol.Name))
}

// HorizontalPodAutoscaler builds the autoscaler name based on the instance.
func HorizontalPodAutoscaler(otelcol v1alpha1.OpenTelemetryCollector) string {
return DNSName(Truncate("%s-collector", 63, otelcol.Name))
}

// HorizontalPodAutoscaler builds the collector (deployment/daemonset) name based on the instance.
func OpenTelemetryCollector(otelcol v1alpha1.OpenTelemetryCollector) string {
return DNSName(Truncate("%s", 63, otelcol.Name))
}

// HorizontalPodAutoscaler builds the collector (deployment/daemonset) name based on the instance.
func OpenTelemetryCollectorName(otelcolName string) string {
return DNSName(Truncate("%s", 63, otelcolName))
}

// TargetAllocator returns the TargetAllocator deployment resource name.
func TargetAllocator(otelcol v1alpha1.OpenTelemetryCollector) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol.Name))
Expand Down
16 changes: 16 additions & 0 deletions tests/e2e/autoscale/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: simplest-collector
status:
readyReplicas: 1

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: simplest-collector
spec:
minReplicas: 1
maxReplicas: 2

32 changes: 32 additions & 0 deletions tests/e2e/autoscale/00-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
name: simplest
spec:
minReplicas: 1
maxReplicas: 2
resources:
limits:
cpu: 500m
memory: 128Mi
requests:
cpu: 5m
memory: 64Mi

config: |
receivers:
otlp:
protocols:
grpc:
http:
processors:

exporters:
logging:

service:
pipelines:
traces:
receivers: [otlp]
processors: []
exporters: [logging]
18 changes: 18 additions & 0 deletions tests/e2e/autoscale/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: batch/v1
kind: Job
metadata:
name: tracegen
status:
conditions:
- status: "True"
type: Complete

---
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector

metadata:
name: simplest
status:
scale:
replicas: 2
19 changes: 19 additions & 0 deletions tests/e2e/autoscale/01-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: batch/v1
kind: Job
metadata:
name: tracegen
spec:
template:
spec:
containers:
- name: tracegen
image: ghcr.io/open-telemetry/opentelemetry-collector-contrib/tracegen:latest
command:
- "./tracegen"
args:
- -otlp-endpoint=simplest-collector-headless:4317
- -otlp-insecure
- -duration=1m
- -workers=20
restartPolicy: Never
backoffLimit: 4