Skip to content

Commit

Permalink
OpAMP Bridge: Applier support for OTel CRD v1beta1 API Version
Browse files Browse the repository at this point in the history
**Description**: Updates the opamp bridge's config applier interface and client to support opentelemetry.io/v1beta1, explicitly removing support for applying OpenTelemetryCollector configurations of the opentelemetry.io/v1alph1 version

**Link to tracking Issue(s)**: open-telemetry#2985

**Testing**: Update

Documentation: n/a
  • Loading branch information
gdfast committed Jun 27, 2024
1 parent b4bfdb8 commit 9c7b7b9
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 151 deletions.
4 changes: 2 additions & 2 deletions cmd/operator-opamp-bridge/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"k8s.io/utils/clock"
"sigs.k8s.io/yaml"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/config"
"github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/metrics"
"github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/operator"
Expand Down Expand Up @@ -136,7 +136,7 @@ func (agent *Agent) generateCollectorPoolHealth() (map[string]*protobufs.Compone
}

// getCollectorSelector destructures the collectors scale selector if present, if uses the labelmap from the operator.
func (agent *Agent) getCollectorSelector(col v1alpha1.OpenTelemetryCollector) map[string]string {
func (agent *Agent) getCollectorSelector(col v1beta1.OpenTelemetryCollector) map[string]string {
if len(col.Status.Scale.Selector) > 0 {
selMap := map[string]string{}
for _, kvPair := range strings.Split(col.Status.Scale.Selector, ",") {
Expand Down
53 changes: 36 additions & 17 deletions cmd/operator-opamp-bridge/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/config"
"github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/operator"
)
Expand Down Expand Up @@ -147,15 +148,15 @@ type mockOpampClient struct {
settings types.StartSettings
}

func (m *mockOpampClient) SetCustomCapabilities(customCapabilities *protobufs.CustomCapabilities) error {
func (m *mockOpampClient) SetCustomCapabilities(_ *protobufs.CustomCapabilities) error {
return nil
}

func (m *mockOpampClient) SendCustomMessage(message *protobufs.CustomMessage) (messageSendingChannel chan struct{}, err error) {
func (m *mockOpampClient) SendCustomMessage(_ *protobufs.CustomMessage) (messageSendingChannel chan struct{}, err error) {
return nil, nil
}

func (m *mockOpampClient) RequestConnectionSettings(request *protobufs.ConnectionSettingsRequest) error {
func (m *mockOpampClient) RequestConnectionSettings(_ *protobufs.ConnectionSettingsRequest) error {
return nil
}

Expand Down Expand Up @@ -201,6 +202,7 @@ func (m *mockOpampClient) SetPackageStatuses(_ *protobufs.PackageStatuses) error
func getFakeApplier(t *testing.T, conf *config.Config, lists ...runtimeClient.ObjectList) *operator.Client {
schemeBuilder := runtime.NewSchemeBuilder(func(s *runtime.Scheme) error {
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.OpenTelemetryCollector{}, &v1alpha1.OpenTelemetryCollectorList{})
s.AddKnownTypes(v1beta1.GroupVersion, &v1beta1.OpenTelemetryCollector{}, &v1beta1.OpenTelemetryCollectorList{})
s.AddKnownTypes(v1.SchemeGroupVersion, &v1.Pod{}, &v1.PodList{})
metav1.AddToGroupVersion(s, v1alpha1.GroupVersion)
return nil
Expand Down Expand Up @@ -414,14 +416,17 @@ func TestAgent_getHealth(t *testing.T) {
agent.clock = fakeClock
err := agent.Start()
defer agent.Shutdown()

require.NoError(t, err, "should be able to start agent")
if len(tt.args.configs) > 0 {
require.True(t, len(tt.args.configs) == len(tt.want), "must have an equal amount of configs and checks.")
require.Len(t, tt.args.configs, len(tt.want), "must have an equal amount of configs and checks.")
} else {
require.Len(t, tt.want, 1, "must have exactly one want if no config is supplied.")
require.Equal(t, tt.want[0], agent.getHealth())
}

for i, configMap := range tt.args.configs {
var data *types.MessageData
data, err := getMessageDataFromConfigFile(configMap)
require.NoError(t, err, "should be able to load data")
agent.onMessage(tt.args.ctx, data)
Expand Down Expand Up @@ -495,7 +500,8 @@ func TestAgent_onMessage(t *testing.T) {
"name: " + testCollectorName,
"namespace: " + testNamespace,
"send_batch_size: 10000",
"receivers: [otlp]",
"receivers:",
"- otlp",
"status:",
},
},
Expand Down Expand Up @@ -523,7 +529,8 @@ func TestAgent_onMessage(t *testing.T) {
"name: " + testCollectorName,
"namespace: " + testNamespace,
"send_batch_size: 10000",
"receivers: [otlp]",
"receivers:",
"- otlp",
"status:",
},
},
Expand All @@ -549,7 +556,7 @@ func TestAgent_onMessage(t *testing.T) {
status: &protobufs.RemoteConfigStatus{
LastRemoteConfigHash: []byte(invalidYamlConfigHash),
Status: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_FAILED,
ErrorMessage: "error converting YAML to JSON: yaml: line 23: could not find expected ':'",
ErrorMessage: "failed to unmarshal config into v1beta1 API Version: error converting YAML to JSON: yaml: line 23: could not find expected ':'",
},
},
},
Expand All @@ -571,7 +578,8 @@ func TestAgent_onMessage(t *testing.T) {
"name: " + testCollectorName,
"namespace: " + testNamespace,
"send_batch_size: 10000",
"receivers: [otlp]",
"receivers:",
"- otlp",
"status:",
},
},
Expand Down Expand Up @@ -656,7 +664,9 @@ func TestAgent_onMessage(t *testing.T) {
"name: " + testCollectorName,
"namespace: " + testNamespace,
"send_batch_size: 10000",
"processors: [memory_limiter, batch]",
"processors:",
"- memory_limiter",
"- batch",
"replicas: 3",
"status:",
},
Expand Down Expand Up @@ -709,7 +719,7 @@ func TestAgent_onMessage(t *testing.T) {
nextStatus: &protobufs.RemoteConfigStatus{
LastRemoteConfigHash: []byte(invalidYamlConfigHash), // The new hash should be of the bad config
Status: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_FAILED,
ErrorMessage: "error converting YAML to JSON: yaml: line 23: could not find expected ':'",
ErrorMessage: "failed to unmarshal config into v1beta1 API Version: error converting YAML to JSON: yaml: line 23: could not find expected ':'",
},
},
},
Expand Down Expand Up @@ -757,7 +767,9 @@ func TestAgent_onMessage(t *testing.T) {
"name: " + otherCollectorName,
"namespace: " + testNamespace,
"send_batch_size: 10000",
"processors: [memory_limiter, batch]",
"processors:",
"- memory_limiter",
"- batch",
"status:",
},
},
Expand Down Expand Up @@ -805,14 +817,17 @@ func TestAgent_onMessage(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := &mockOpampClient{}

conf := config.NewConfig(logr.Discard())
loadErr := config.LoadFromFile(conf, tt.fields.configFile)
require.NoError(t, loadErr, "should be able to load config")

applier := getFakeApplier(t, conf)
agent := NewAgent(l, applier, conf, mockClient)
err := agent.Start()
defer agent.Shutdown()
require.NoError(t, err, "should be able to start agent")

data, err := getMessageDataFromConfigFile(tt.args.configFile)
require.NoError(t, err, "should be able to load data")
agent.onMessage(tt.args.ctx, data)
Expand All @@ -824,17 +839,20 @@ func TestAgent_onMessage(t *testing.T) {
}
assert.NotNilf(t, effectiveConfig.ConfigMap.GetConfigMap(), "configmap should have data")
for colNameNamespace, expectedContents := range tt.want.contents {
assert.Contains(t, effectiveConfig.ConfigMap.GetConfigMap(), colNameNamespace)
configFileMap := effectiveConfig.ConfigMap.GetConfigMap()
require.Contains(t, configFileMap, colNameNamespace)
configFileString := string(configFileMap[colNameNamespace].GetBody())
for _, content := range expectedContents {
asString := string(effectiveConfig.ConfigMap.GetConfigMap()[colNameNamespace].GetBody())
assert.Contains(t, asString, content)
assert.Contains(t, configFileString, content, "config should contain %s", content)
}
}
assert.Equal(t, tt.want.status, mockClient.lastStatus)

if tt.args.nextConfigFile == nil {
// Nothing left to do!
return
}

nextData, err := getMessageDataFromConfigFile(tt.args.nextConfigFile)
require.NoError(t, err, "should be able to load updated data")
agent.onMessage(tt.args.ctx, nextData)
Expand All @@ -843,10 +861,11 @@ func TestAgent_onMessage(t *testing.T) {
assert.Equal(t, nextEffectiveConfig, mockClient.lastEffectiveConfig, "client's config should be updated")
assert.NotNilf(t, nextEffectiveConfig.ConfigMap.GetConfigMap(), "configmap should have updated data")
for colNameNamespace, expectedContents := range tt.want.nextContents {
assert.Contains(t, nextEffectiveConfig.ConfigMap.GetConfigMap(), colNameNamespace)
configFileMap := nextEffectiveConfig.ConfigMap.GetConfigMap()
require.Contains(t, configFileMap, colNameNamespace)
configFileString := string(configFileMap[colNameNamespace].GetBody())
for _, content := range expectedContents {
asString := string(nextEffectiveConfig.ConfigMap.GetConfigMap()[colNameNamespace].GetBody())
assert.Contains(t, asString, content)
assert.Contains(t, configFileString, content)
}
}
assert.Equal(t, tt.want.nextStatus, mockClient.lastStatus)
Expand Down
3 changes: 2 additions & 1 deletion cmd/operator-opamp-bridge/agent/testdata/basic.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
apiVersion: opentelemetry.io/v1beta1
kind: OpenTelemetryCollector
metadata:
name: simplest
labels:
"opentelemetry.io/opamp-managed": "true"
spec:
config: |
config:
receivers:
otlp:
protocols:
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-opamp-bridge/agent/testdata/invalid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
labels:
"opentelemetry.io/opamp-managed": "true"
spec:
config: |
config:
receivers:
otlp:
protocols:
Expand Down
3 changes: 2 additions & 1 deletion cmd/operator-opamp-bridge/agent/testdata/updated.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
apiVersion: opentelemetry.io/v1beta1
kind: OpenTelemetryCollector
metadata:
name: simplest
labels:
"opentelemetry.io/opamp-managed": "test-bridge"
spec:
config: |
config:
receivers:
otlp:
protocols:
Expand Down
Loading

0 comments on commit 9c7b7b9

Please sign in to comment.