Skip to content

Commit

Permalink
Update to collector v0.24.0 (#251)
Browse files Browse the repository at this point in the history
Fixes #238
  • Loading branch information
jnodorp-jaconi authored Apr 22, 2021
1 parent 720400e commit 41e5738
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changes by Version
==================

0.24.0 (2021-04-20)
-------------------
* Bumped OpenTelemetry Collector to v0.24.0

0.23.0 (2021-04-04)
-------------------
* Bumped OpenTelemetry Collector to v0.23.0
Expand Down
74 changes: 74 additions & 0 deletions pkg/collector/upgrade/v0_24_0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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_24_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.24.0, failed to parse configuration: %w", err)
}

extensions, ok := cfg["extensions"].(map[interface{}]interface{})
if !ok {
// We do not need an upgrade if there are no extensions.
return otelcol, nil
}

for k, v := range extensions {
if strings.HasPrefix(k.(string), "health_check") {
switch extension := v.(type) {
case map[interface{}]interface{}:
if port, ok := extension["port"]; ok {
delete(extension, "port")
extension["endpoint"] = fmt.Sprintf("0.0.0.0:%d", port)
otelcol.Status.Messages = append(otelcol.Status.Messages, fmt.Sprintf("upgrade to v0.24.0 migrated the property 'port' to 'endpoint' for extension %q", k))
}
case string:
if len(extension) == 0 {
// This extension is using the default configuration.
continue
}
case nil:
// This extension is using the default configuration.
continue
default:
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)
}
}
}

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

otelcol.Spec.Config = string(res)
return otelcol, nil
}
68 changes: 68 additions & 0 deletions pkg/collector/upgrade/v0_24_0_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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"
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/upgrade"
)

func TestHealthCheckEndpointMigration(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: `extensions:
health_check:
health_check/1: ""
health_check/2:
endpoint: "localhost:13133"
health_check/3:
port: 13133
`,
},
}
existing.Status.Version = "0.23.0"

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

// verify
assert.Equal(t, `extensions:
health_check: null
health_check/1: ""
health_check/2:
endpoint: localhost:13133
health_check/3:
endpoint: 0.0.0.0:13133
`, res.Spec.Config)
assert.Equal(t, "upgrade to v0.24.0 migrated the property 'port' to 'endpoint' for extension \"health_check/3\"", res.Status.Messages[0])
}
4 changes: 4 additions & 0 deletions pkg/collector/upgrade/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var (
Version: *semver.MustParse("0.19.0"),
upgrade: upgrade0_19_0,
},
{
Version: *semver.MustParse("0.24.0"),
upgrade: upgrade0_24_0,
},
}

// Latest represents the latest version that we need to upgrade. This is not necessarily the latest known version.
Expand Down
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.23.0
opentelemetry-collector=0.24.0

# Represents the next release of the OpenTelemetry Operator.
operator=0.23.0
operator=0.24.0

0 comments on commit 41e5738

Please sign in to comment.