|
| 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 | + |
| 23 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" |
| 24 | + "github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters" |
| 25 | +) |
| 26 | + |
| 27 | +func upgrade0_57_2(u VersionUpgrade, otelcol *v1alpha1.OpenTelemetryCollector) (*v1alpha1.OpenTelemetryCollector, error) { |
| 28 | + |
| 29 | + if len(otelcol.Spec.Config) == 0 { |
| 30 | + return otelcol, nil |
| 31 | + } |
| 32 | + |
| 33 | + otelCfg, err := adapters.ConfigFromString(otelcol.Spec.Config) |
| 34 | + if err != nil { |
| 35 | + return otelcol, fmt.Errorf("couldn't upgrade to v0.57.2, failed to parse configuration: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + //Remove deprecated port field from config. (https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/10853) |
| 39 | + extensionsConfig, ok := otelCfg["extensions"].(map[interface{}]interface{}) |
| 40 | + if !ok { |
| 41 | + // In case there is no extensions config. |
| 42 | + return otelcol, nil |
| 43 | + } |
| 44 | + |
| 45 | + for keyExt, valExt := range extensionsConfig { |
| 46 | + if strings.HasPrefix(keyExt.(string), "health_check") { |
| 47 | + switch extensions := valExt.(type) { |
| 48 | + case map[interface{}]interface{}: |
| 49 | + if port, ok := extensions["port"]; ok { |
| 50 | + endpointV := extensions["endpoint"] |
| 51 | + extensions["endpoint"] = fmt.Sprintf("%s:%s", endpointV, port) |
| 52 | + delete(extensions, "port") |
| 53 | + |
| 54 | + otelCfg["extensions"] = extensionsConfig |
| 55 | + res, err := yaml.Marshal(otelCfg) |
| 56 | + if err != nil { |
| 57 | + return otelcol, fmt.Errorf("couldn't upgrade to v0.57.2, failed to marshall back configuration: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + otelcol.Spec.Config = string(res) |
| 61 | + u.Recorder.Event(otelcol, "Normal", "Upgrade", fmt.Sprintf("upgrade to v0.57.2 has deprecated port for healthcheck extension %q", keyExt)) |
| 62 | + } |
| 63 | + default: |
| 64 | + return otelcol, fmt.Errorf("couldn't upgrade to v0.57.2, the extension %q is invalid (expected string or map but was %t)", keyExt, valExt) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return otelcol, nil |
| 69 | +} |
0 commit comments