Skip to content

Commit

Permalink
Bumped OpenTelemetry Collector to v0.19.0 (#179)
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Jan 28, 2021
1 parent b70d1e7 commit 5281b5a
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes by Version
==================

0.19.0 (2021-01-27)
-------------------
* Bumped OpenTelemetry Collector to v0.19.0


0.18.1 (2021-01-25)
-------------------
* Fixed testing image from being used in non-test artifacts (fixes #170) ([#171](https://github.com/open-telemetry/opentelemetry-operator/pull/171), [@gramidt](https://github.com/gramidt))
Expand Down
121 changes: 121 additions & 0 deletions pkg/collector/upgrade/v0_19_0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters"
)

func upgrade0_19_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.19.0, failed to parse configuration: %w", err)
}

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

for k, v := range processors {
// from the changelog https://github.com/open-telemetry/opentelemetry-collector/releases/tag/v0.19.0

// Remove deprecated queued_retry processor
if strings.HasPrefix(k.(string), "queued_retry") {
delete(processors, k)
otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.19.0 removed the processor %q", k))
continue
}

// Remove deprecated configs from resource processor: type (set "opencensus.type" key in "attributes.upsert" map instead) and labels (use "attributes.upsert" instead).
if strings.HasPrefix(k.(string), "resource") {
switch processor := v.(type) {
case map[interface{}]interface{}:
// type becomes an attribute.upsert with key opencensus.type
if typ, found := processor["type"]; found {
var attributes []map[string]string
if attrs, found := processor["attributes"]; found {
if attributes, ok = attrs.([]map[string]string); !ok {
return otelcol, fmt.Errorf("couldn't upgrade to v0.19.0, the attributes list for processors %q couldn't be parsed based on the previous value. Type: %t, value: %v", k, attrs, attrs)
}
}
attr := map[string]string{}
attr["key"] = "opencensus.type"
attr["value"] = typ.(string)
attr["action"] = "upsert"
attributes = append(attributes, attr)

processor["attributes"] = attributes
delete(processor, "type")
otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.19.0 migrated the property 'type' for processor %q", k))
}

// handle labels
if labels, found := processor["labels"]; found {
var attributes []map[string]string
if attrs, found := processor["attributes"]; found {
if attributes, ok = attrs.([]map[string]string); !ok {
return otelcol, fmt.Errorf("couldn't upgrade to v0.19.0, the attributes list for processors %q couldn't be parsed based on the previous value. Type: %t, value: %v", k, attrs, attrs)
}
}

if ls, ok := labels.(map[interface{}]interface{}); ok {
for labelK, labelV := range ls {
attr := map[string]string{}
attr["key"] = labelK.(string)
attr["value"] = labelV.(string)
attr["action"] = "upsert"
attributes = append(attributes, attr)
}
}

processor["attributes"] = attributes
delete(processor, "labels")
otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.19.0 migrated the property 'labels' for processor %q", k))
}

processors[k] = processor
case string:
if len(processor) == 0 {
// this processor is using the default configuration
continue
}
default:
return otelcol, fmt.Errorf("couldn't upgrade to v0.19.0, the processor %q is invalid (neither a string nor map)", k)
}
}
}

cfg["processors"] = processors
res, err := yaml.Marshal(cfg)
if err != nil {
return otelcol, fmt.Errorf("couldn't upgrade to v0.19.0, failed to marshall back configuration: %w", err)
}

otelcol.Spec.Config = string(res)
return otelcol, nil
}
143 changes: 143 additions & 0 deletions pkg/collector/upgrade/v0_19_0_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// 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"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

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

func TestRemoveQueuedRetryProcessor(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: `processors:
queued_retry:
otherprocessor:
queued_retry/second:
compression: "on"
reconnection_delay: 15
num_workers: 123`,
},
}
existing.Status.Version = "0.18.0"

// sanity check
require.Contains(t, existing.Spec.Config, "queued_retry")
require.Contains(t, existing.Spec.Config, "queued_retry/second")
require.Contains(t, existing.Spec.Config, "num_workers: 123") // checking one property is sufficient

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

// verify
assert.NotContains(t, res.Spec.Config, "queued_retry:")
assert.Contains(t, res.Spec.Config, "otherprocessor:")
assert.NotContains(t, res.Spec.Config, "queued_retry/second:")
assert.NotContains(t, res.Spec.Config, "num_workers: 123") // checking one property is sufficient
assert.Contains(t, res.Status.Messages[0], "upgrade to v0.19.0 removed the processor")
}

func TestMigrateResourceType(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: `processors:
resource:
type: some-type
`,
},
}
existing.Status.Version = "0.18.0"

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

// verify
assert.Equal(t, `processors:
resource:
attributes:
- action: upsert
key: opencensus.type
value: some-type
`, res.Spec.Config)
assert.Contains(t, res.Status.Messages[0], "upgrade to v0.19.0 migrated the property 'type' for processor")
}

func TestMigrateLabels(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: `processors:
resource:
labels:
cloud.zone: zone-1
host.name: k8s-node
`,
},
}
existing.Status.Version = "0.18.0"

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

actual, err := adapters.ConfigFromString(res.Spec.Config)
require.NoError(t, err)
actualProcessors := actual["processors"].(map[interface{}]interface{})
actualProcessor := actualProcessors["resource"].(map[interface{}]interface{})
actualAttrs := actualProcessor["attributes"].([]interface{})

// verify
assert.Len(t, actualAttrs, 2)
assert.Nil(t, actualProcessor["labels"])
assert.Contains(t, res.Status.Messages[0], "upgrade to v0.19.0 migrated the property 'labels' for processor")
}
4 changes: 4 additions & 0 deletions pkg/collector/upgrade/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var (
Version: *semver.MustParse("0.15.0"),
upgrade: upgrade0_15_0,
},
{
Version: *semver.MustParse("0.19.0"),
upgrade: upgrade0_19_0,
},
}

// Latest represents the latest version that we need to upgrade. This is not necessarily the latest known version.
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/smoke-simplest/00-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ spec:
protocols:
grpc:
processors:
queued_retry:
exporters:
logging:
Expand All @@ -18,5 +17,5 @@ spec:
pipelines:
traces:
receivers: [jaeger]
processors: [queued_retry]
processors: []
exporters: [logging]
4 changes: 2 additions & 2 deletions versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# by default with the OpenTelemetry Operator. This would usually be the latest
# stable OpenTelemetry version. When you update this file, make sure to update the
# the docs as well.
opentelemetry-collector=0.18.0
opentelemetry-collector=0.19.0

# Represents the next release of the OpenTelemetry Operator.
operator=0.18.1
operator=0.19.0

0 comments on commit 5281b5a

Please sign in to comment.