Skip to content

Commit

Permalink
Add 0.36.0 upgrade routine (#465)
Browse files Browse the repository at this point in the history
* Add 0.36.0 upgrade routine
  • Loading branch information
VineethReddy02 authored Oct 27, 2021
1 parent ca4c187 commit f6e2932
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
126 changes: 126 additions & 0 deletions pkg/collector/upgrade/v0_36_0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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 (
"fmt"
"strings"

"gopkg.in/yaml.v2"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/open-telemetry/opentelemetry-operator/api/collector/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters"
)

func upgrade0_36_0(cl client.Client, otelcol *v1alpha1.OpenTelemetryCollector) (*v1alpha1.OpenTelemetryCollector, error) {
if len(otelcol.Spec.Config) == 0 {
return otelcol, nil
}

cfg, err := adapters.ConfigFromString(otelcol.Spec.Config)
if err != nil {
return otelcol, fmt.Errorf("couldn't upgrade to v0.36.0, failed to parse configuration: %w", err)
}

// upgrading the receivers
receivers, ok := cfg["receivers"].(map[interface{}]interface{})
if !ok {
// no receivers? no need to fail because of that
return otelcol, nil
}

for k1, v1 := range receivers {
// from the changelog https://github.com/open-telemetry/opentelemetry-collector/blob/main/CHANGELOG.md#-breaking-changes--2
// Here is the upstream PR https://github.com/open-telemetry/opentelemetry-collector/pull/4063

// Change tls config key from tls_settings to tls in otlp.protocols.grpc
if strings.HasPrefix(k1.(string), "otlp") {
otlpConfig, ok := v1.(map[interface{}]interface{})
if !ok {
// no otlpConfig? no need to fail because of that
return otelcol, nil
}
for k2, v2 := range otlpConfig {
// protocols config
if k2 == "protocols" {
protocConfig, ok := v2.(map[interface{}]interface{})
if !ok {
// no protocolConfig? no need to fail because of that
return otelcol, nil
}
for k3, v3 := range protocConfig {
// grpc config
if k3 == "grpc" || k3 == "http" {
grpcHttpConfig, ok := v3.(map[interface{}]interface{})
if !ok {
// no grpcHttpConfig? no need to fail because of that
return otelcol, nil
}
for k4, v4 := range grpcHttpConfig {
// change tls_settings to tls
if k4.(string) == "tls_settings" {
grpcHttpConfig["tls"] = v4
delete(grpcHttpConfig, "tls_settings")
otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.36.0 has changed the tls_settings field name to tls in %s protocol of %s receiver", k3, k1))
}
}
}
}
}
}
}
}
cfg["receivers"] = receivers

// upgrading the exporters
exporters, ok := cfg["exporters"].(map[interface{}]interface{})
if !ok {
// no exporters? no need to fail because of that
return otelcol, nil
}

for k1, v1 := range exporters {
// from the changelog https://github.com/open-telemetry/opentelemetry-collector/blob/main/CHANGELOG.md#-breaking-changes--2
// Here is the upstream PR https://github.com/open-telemetry/opentelemetry-collector/pull/4063

// Move all tls config into separate field i,e, tls.*
if strings.HasPrefix(k1.(string), "otlp") {
otlpConfig, ok := v1.(map[interface{}]interface{})
if !ok {
// no otlpConfig? no need to fail because of that
return otelcol, nil
}
tlsConfig := make(map[interface{}]interface{}, 5)
for key, value := range otlpConfig {
if key == "ca_file" || key == "cert_file" || key == "key_file" || key == "min_version" || key == "max_version" ||
key == "insecure" || key == "insecure_skip_verify" || key == "server_name_override" {
tlsConfig[key] = value
delete(otlpConfig, key)
}
otlpConfig["tls"] = tlsConfig
otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.36.0 move tls config i.e. ca_file, key_file, cert_file, min_version, max_version to tls.* in %s exporter", k1))
}
}
}
cfg["exporters"] = exporters

res, err := yaml.Marshal(cfg)
if err != nil {
return otelcol, fmt.Errorf("couldn't upgrade to v0.36.0, failed to marshall back configuration: %w", err)
}
otelcol.Spec.Config = string(res)
return otelcol, nil
}
122 changes: 122 additions & 0 deletions pkg/collector/upgrade/v0_36_0_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/open-telemetry/opentelemetry-operator/api/collector/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
"github.com/open-telemetry/opentelemetry-operator/pkg/collector/upgrade"
)

func Test0_36_0Upgrade(t *testing.T) {
// prepare
nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"}
existing := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: nsn.Name,
Namespace: nsn.Namespace,
Labels: map[string]string{
"app.kubernetes.io/managed-by": "opentelemetry-operator",
},
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Config: `
receivers:
otlp/mtls:
protocols:
grpc:
endpoint: mysite.local:55690
tls_settings:
client_ca_file: client.pem
cert_file: server.crt
key_file: server.key
http:
endpoint: mysite.local:55690
tls_settings:
client_ca_file: client.pem
cert_file: server.crt
key_file: server.key
exporters:
otlp:
endpoint: "example.com"
ca_file: /var/lib/mycert.pem
insecure: true
key_file: keyfile
min_version: "1.0.0"
max_version: "2.0.2"
insecure_skip_verify: true
server_name_override: hii
service:
pipelines:
traces:
receivers: [otlp/mtls]
exporters: [otlp]
`,
},
}
existing.Status.Version = "0.35.0"

// test
res, err := upgrade.ManagedInstance(context.Background(), logger, version.Get(), nil, existing)
assert.NoError(t, err)

// verify
assert.Equal(t, `exporters:
otlp:
endpoint: example.com
tls:
ca_file: /var/lib/mycert.pem
insecure: true
insecure_skip_verify: true
key_file: keyfile
max_version: 2.0.2
min_version: 1.0.0
server_name_override: hii
receivers:
otlp/mtls:
protocols:
grpc:
endpoint: mysite.local:55690
tls:
cert_file: server.crt
client_ca_file: client.pem
key_file: server.key
http:
endpoint: mysite.local:55690
tls:
cert_file: server.crt
client_ca_file: client.pem
key_file: server.key
service:
pipelines:
traces:
exporters:
- otlp
receivers:
- otlp/mtls
`, res.Spec.Config)
assert.Equal(t, fmt.Sprintf("upgrade to v0.36.0 has changed the tls_settings field name to tls in %s protocol of %s receiver", "grpc", "otlp/mtls"), res.Status.Messages[0])
assert.Equal(t, fmt.Sprintf("upgrade to v0.36.0 has changed the tls_settings field name to tls in %s protocol of %s receiver", "http", "otlp/mtls"), res.Status.Messages[1])
assert.Equal(t, fmt.Sprintf("upgrade to v0.36.0 move tls config i.e. ca_file, key_file, cert_file, min_version, max_version to tls.* in %s exporter", "otlp"), res.Status.Messages[2])
}
4 changes: 4 additions & 0 deletions pkg/collector/upgrade/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ var (
Version: *semver.MustParse("0.31.0"),
upgrade: upgrade0_31_0,
},
{
Version: *semver.MustParse("0.36.0"),
upgrade: upgrade0_36_0,
},
}

// Latest represents the latest version that we need to upgrade. This is not necessarily the latest known version.
Expand Down

0 comments on commit f6e2932

Please sign in to comment.