-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Horizontal Pod Autoscaler to scale on OpenTelemetry Collector … (
#984) * Change Horizontal Pod Autoscaler to scale on OpenTelemetry Collector object Signed-off-by: Kevin Earls <kearls@redhat.com> * Update doc; move upgrade code out of reconciler Signed-off-by: Kevin Earls <kearls@redhat.com> * Fixed doc Signed-off-by: Kevin Earls <kearls@redhat.com> * Respond to comments, implement test Signed-off-by: Kevin Earls <kearls@redhat.com> * respond to comments Signed-off-by: Kevin Earls <kearls@redhat.com>
- Loading branch information
1 parent
5ef2c0f
commit 0232b80
Showing
19 changed files
with
307 additions
and
15 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
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
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 |
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,80 @@ | ||
// 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 ( | ||
"context" | ||
"fmt" | ||
|
||
autoscalingv1 "k8s.io/api/autoscaling/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" | ||
"github.com/open-telemetry/opentelemetry-operator/pkg/naming" | ||
) | ||
|
||
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 | ||
} | ||
|
||
// Add minReplicas | ||
one := int32(1) | ||
otelcol.Spec.MinReplicas = &one | ||
|
||
// Find the existing HPA for this collector and upgrade it if necessary | ||
listOptions := []client.ListOption{ | ||
client.InNamespace(otelcol.Namespace), | ||
client.MatchingLabels(map[string]string{ | ||
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", otelcol.Namespace, otelcol.Name), | ||
"app.kubernetes.io/managed-by": "opentelemetry-operator", | ||
}), | ||
} | ||
|
||
hpaList := &autoscalingv1.HorizontalPodAutoscalerList{} | ||
ctx := context.Background() | ||
if err := u.Client.List(ctx, hpaList, listOptions...); err != nil { | ||
return nil, fmt.Errorf("couldn't upgrade to v0.56.0, failed trying to find HPA instances: %w", err) | ||
} | ||
|
||
errors := []error{} | ||
for i := range hpaList.Items { | ||
existing := hpaList.Items[i] | ||
// If there is an autoscaler based on Deployment, replace it with one based on OpenTelemetryCollector | ||
if existing.Spec.ScaleTargetRef.Kind == "Deployment" { | ||
updated := existing.DeepCopy() | ||
updated.Spec.ScaleTargetRef = autoscalingv1.CrossVersionObjectReference{ | ||
Kind: "OpenTelemetryCollector", | ||
Name: naming.OpenTelemetryCollectorName(otelcol.Name), | ||
APIVersion: v1alpha1.GroupVersion.String(), | ||
} | ||
patch := client.MergeFrom(&existing) | ||
err := u.Client.Patch(ctx, updated, patch) | ||
if err != nil { | ||
errors = append(errors, err) | ||
} | ||
} | ||
} | ||
|
||
if len(errors) != 0 { | ||
return nil, fmt.Errorf("couldn't upgrade to v0.56.0, failed to recreate autoscaler: %v", errors) | ||
} | ||
|
||
u.Log.Info("in upgrade0_56_0", "Otel Instance", otelcol.Name, "Upgrade version", u.Version.String()) | ||
u.Recorder.Event(otelcol, "Normal", "Upgrade", "upgraded to v0.56.0, added minReplicas. recreated HPA instance") | ||
|
||
return otelcol, nil | ||
} |
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,59 @@ | ||
// 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 ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/tools/record" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" | ||
"github.com/open-telemetry/opentelemetry-operator/internal/version" | ||
"github.com/open-telemetry/opentelemetry-operator/pkg/collector/upgrade" | ||
) | ||
|
||
func Test0_56_0Upgrade(t *testing.T) { | ||
one := int32(1) | ||
three := int32(3) | ||
|
||
collectorInstance := v1alpha1.OpenTelemetryCollector{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "OpenTelemetryCollector", | ||
APIVersion: "v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-instance", | ||
Namespace: "somewhere", | ||
}, | ||
Spec: v1alpha1.OpenTelemetryCollectorSpec{ | ||
Replicas: &one, | ||
MaxReplicas: &three, | ||
}, | ||
} | ||
|
||
collectorInstance.Status.Version = "0.55.0" | ||
versionUpgrade := &upgrade.VersionUpgrade{ | ||
Log: logger, | ||
Version: version.Get(), | ||
Client: k8sClient, | ||
Recorder: record.NewFakeRecorder(upgrade.RecordBufferSize), | ||
} | ||
upgradedInstance, err := versionUpgrade.ManagedInstance(context.Background(), collectorInstance) | ||
assert.NoError(t, err) | ||
assert.Equal(t, one, *upgradedInstance.Spec.MinReplicas) | ||
} |
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,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 | ||
|
Oops, something went wrong.