|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package upgrade |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "gopkg.in/yaml.v2" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 23 | + |
| 24 | + "github.com/open-telemetry/opentelemetry-operator/api/v1alpha1" |
| 25 | + "github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters" |
| 26 | +) |
| 27 | + |
| 28 | +func upgrade0_24_0(cl client.Client, otelcol *v1alpha1.OpenTelemetryCollector) (*v1alpha1.OpenTelemetryCollector, error) { |
| 29 | + if len(otelcol.Spec.Config) == 0 { |
| 30 | + return otelcol, nil |
| 31 | + } |
| 32 | + |
| 33 | + cfg, err := adapters.ConfigFromString(otelcol.Spec.Config) |
| 34 | + if err != nil { |
| 35 | + return otelcol, fmt.Errorf("couldn't upgrade to v0.24.0, failed to parse configuration: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + extensions, ok := cfg["extensions"].(map[interface{}]interface{}) |
| 39 | + if !ok { |
| 40 | + // We do not need an upgrade if there are no extensions. |
| 41 | + return otelcol, nil |
| 42 | + } |
| 43 | + |
| 44 | + for k, v := range extensions { |
| 45 | + if strings.HasPrefix(k.(string), "health_check") { |
| 46 | + switch extension := v.(type) { |
| 47 | + case map[interface{}]interface{}: |
| 48 | + if port, ok := extension["port"]; ok { |
| 49 | + delete(extension, "port") |
| 50 | + extension["endpoint"] = fmt.Sprintf("0.0.0.0:%d", port) |
| 51 | + otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.24.0 migrated the property 'port' to 'endpoint' for extension %q", k)) |
| 52 | + } |
| 53 | + case string: |
| 54 | + if len(extension) == 0 { |
| 55 | + // This extension is using the default configuration. |
| 56 | + continue |
| 57 | + } |
| 58 | + case nil: |
| 59 | + // This extension is using the default configuration. |
| 60 | + continue |
| 61 | + default: |
| 62 | + return otelcol, fmt.Errorf("couldn't upgrade to v0.24.0, the extension %q is invalid (expected string or map but was %t)", k, v) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + res, err := yaml.Marshal(cfg) |
| 68 | + if err != nil { |
| 69 | + return otelcol, fmt.Errorf("couldn't upgrade to v0.24.0, failed to marshall back configuration: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + otelcol.Spec.Config = string(res) |
| 73 | + return otelcol, nil |
| 74 | +} |
0 commit comments