Skip to content

Commit

Permalink
chore: added e2e tests for extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Ankit152 <[email protected]>
  • Loading branch information
Ankit152 committed Nov 24, 2024
1 parent c62b817 commit 6751006
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 123 deletions.
16 changes: 16 additions & 0 deletions .chloggen/service-extension.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: support for creating a service for extensions when ports are specified.

# One or more tracking issues related to the change
issues: [3460]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
6 changes: 5 additions & 1 deletion apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,14 @@ func (c *Config) GetExtensionPorts(logger logr.Logger) ([]corev1.ServicePort, er
return c.getPortsForComponentKinds(logger, KindExtension)
}

func (c *Config) GetAllPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
func (c *Config) GetReceiverAndExporterPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter)
}

func (c *Config) GetAllPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter, KindExtension)
}

func (c *Config) GetEnvironmentVariables(logger logr.Logger) ([]corev1.EnvVar, error) {
return c.getEnvironmentVariablesForComponentKinds(logger, KindReceiver)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/manifests/collector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
const (
headlessLabel = "operator.opentelemetry.io/collector-headless-service"
monitoringLabel = "operator.opentelemetry.io/collector-monitoring-service"
extensionService = "operator.opentelemetry.io/collector-extension-service"
serviceTypeLabel = "operator.opentelemetry.io/collector-service-type"
valueExists = "Exists"
)
Expand Down Expand Up @@ -111,9 +110,8 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) {
}

func ExtensionService(params manifests.Params) (*corev1.Service, error) {
name := naming.Service(params.OtelCol.Name)
name := naming.ExtensionService(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels[extensionService] = valueExists
labels[serviceTypeLabel] = ExtensionServiceType.String()

annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
Expand Down Expand Up @@ -154,7 +152,7 @@ func Service(params manifests.Params) (*corev1.Service, error) {
return nil, err
}

ports, err := params.OtelCol.Spec.Config.GetAllPorts(params.Log)
ports, err := params.OtelCol.Spec.Config.GetReceiverAndExporterPorts(params.Log)
if err != nil {
return nil, err
}
Expand Down
283 changes: 165 additions & 118 deletions internal/manifests/collector/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

func TestExtractPortNumbersAndNames(t *testing.T) {
Expand Down Expand Up @@ -322,158 +324,203 @@ func TestMonitoringService(t *testing.T) {
}

func TestExtensionService(t *testing.T) {
t.Run("when the extension has http endpoint", func(t *testing.T) {
params := manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{
"http": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
testCases := []struct {
name string
params manifests.Params
expectedPorts []v1.ServicePort
}{
{
name: "when the extension has http endpoint",
params: manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{
"http": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
},
},
},
},
},
},
},
}

actual, err := ExtensionService(params)
assert.NotNil(t, actual)
assert.NoError(t, err)
})

t.Run("when the extension has grpc endpoint", func(t *testing.T) {
params := manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
expectedPorts: []v1.ServicePort{
{
Name: "jaeger-query",
Port: 16686,
TargetPort: intstr.IntOrString{
IntVal: 16686,
},
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{
"grpc": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
},
{
name: "when the extension has grpc endpoint",
params: manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{
"http": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
},
},
},
},
},
},
},
}

actual, err := ExtensionService(params)
assert.NotNil(t, actual)
assert.NoError(t, err)
})

t.Run("when the extension has both http and grpc endpoint", func(t *testing.T) {
params := manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
expectedPorts: []v1.ServicePort{
{
Name: "jaeger-query",
Port: 16686,
TargetPort: intstr.IntOrString{
IntVal: 16686,
},
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{
"http": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
"grpc": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
},
{
name: "when the extension has both http and grpc endpoint",
params: manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{
"http": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
"grpc": map[string]interface{}{
"endpoint": "0.0.0.0:16686",
},
},
},
},
},
},
},
},
}

actual, err := ExtensionService(params)
assert.NotNil(t, actual)
assert.NoError(t, err)
})

t.Run("when the extension has no extensions defined", func(t *testing.T) {
params := manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
expectedPorts: []v1.ServicePort{
{
Name: "jaeger-query",
Port: 16686,
TargetPort: intstr.IntOrString{
IntVal: 16686,
},
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{},
},
},
{
name: "when the extension has no extensions defined",
params: manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{},
},
},
},
},
},
}

actual, err := ExtensionService(params)
assert.Nil(t, actual)
assert.NoError(t, err)
})

t.Run("when the extension has no endpoint defined", func(t *testing.T) {
params := manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{},
expectedPorts: []v1.ServicePort{},
},
{
name: "when the extension has no endpoint defined",
params: manifests.Params{
Config: config.Config{},
Log: logger,
OtelCol: v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: []string{"jaeger_query"},
},
Extensions: &v1beta1.AnyConfig{
Object: map[string]interface{}{
"jaeger_query": map[string]interface{}{},
},
},
},
},
},
},
}
expectedPorts: []v1.ServicePort{
{
Name: "jaeger-query",
Port: 16686,
TargetPort: intstr.IntOrString{
IntVal: 16686,
},
},
},
},
}

actual, err := ExtensionService(params)
assert.NotNil(t, actual)
assert.NoError(t, err)
})
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
actual, err := ExtensionService(tc.params)
assert.NoError(t, err)

if len(tc.expectedPorts) > 0 {
assert.NotNil(t, actual)
assert.Equal(t, actual.Name, naming.ExtensionService(tc.params.OtelCol.Name))
// ports assertion
assert.Equal(t, len(tc.expectedPorts), len(actual.Spec.Ports))
assert.Equal(t, tc.expectedPorts[0].Name, actual.Spec.Ports[0].Name)
assert.Equal(t, tc.expectedPorts[0].Port, actual.Spec.Ports[0].Port)
assert.Equal(t, tc.expectedPorts[0].TargetPort.IntVal, actual.Spec.Ports[0].TargetPort.IntVal)
} else {
// no ports, no service
assert.Nil(t, actual)
}
})
}
}

func service(name string, ports []v1beta1.PortsSpec) v1.Service {
Expand Down
Loading

0 comments on commit 6751006

Please sign in to comment.