-
Notifications
You must be signed in to change notification settings - Fork 459
/
v0_56_0.go
83 lines (71 loc) · 2.92 KB
/
v0_56_0.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)
func upgrade0_56_0(u VersionUpgrade, otelcol *v1alpha1.OpenTelemetryCollector) (*v1alpha1.OpenTelemetryCollector, error) {
// return if this does not use an autoscaler
if otelcol.Spec.Autoscaler == nil || otelcol.Spec.Autoscaler.MaxReplicas == nil {
return otelcol, nil
}
// Add minReplicas
one := int32(1)
if otelcol.Spec.Autoscaler.MinReplicas == nil {
otelcol.Spec.Autoscaler.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: v1beta1.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
}