Skip to content

Commit

Permalink
Added validation in webhook to validate prometheus config
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Varma committed Aug 11, 2021
1 parent 33cb042 commit 3d37148
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 58 deletions.
10 changes: 10 additions & 0 deletions api/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

ta "github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator/adapters"
)

// log is for logging in this package.
Expand Down Expand Up @@ -96,5 +98,13 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the target allocation deployment", r.Spec.Mode)
}

// validate Prometheus config for target allocation
if r.Spec.TargetAllocator.Enabled {
_, err := ta.ConfigToPromConfig(r.Spec.Config)
if err != nil {
return fmt.Errorf("the OpenTelemetry Spec configuration is incorrect, %s", err)
}
}

return nil
}
3 changes: 2 additions & 1 deletion pkg/collector/reconcile/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/pkg/collector"
"github.com/open-telemetry/opentelemetry-operator/pkg/naming"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator"
ta "github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator/adapters"
)

// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -84,7 +85,7 @@ func desiredTAConfigMap(params Params) (corev1.ConfigMap, error) {
labels := targetallocator.Labels(params.Instance)
labels["app.kubernetes.io/name"] = name

promConfig, err := GetPromConfig(params)
promConfig, err := ta.ConfigToPromConfig(params.Instance.Spec.Config)
if err != nil {
return corev1.ConfigMap{}, err
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/collector/reconcile/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

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

Expand Down Expand Up @@ -210,10 +209,7 @@ func TestExpectedConfigMap(t *testing.T) {
assert.True(t, exists)
assert.Equal(t, instanceUID, actual.OwnerReferences[0].UID)

config, err := adapters.ConfigFromString(params().Instance.Spec.Config)
assert.NoError(t, err)

parmConfig, err := ta.ConfigToPromConfig(config)
parmConfig, err := ta.ConfigToPromConfig(params().Instance.Spec.Config)
assert.NoError(t, err)

taConfig := make(map[interface{}]interface{})
Expand Down
4 changes: 0 additions & 4 deletions pkg/collector/reconcile/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ func Deployments(ctx context.Context, params Params) error {
}

if params.Instance.Spec.TargetAllocator.Enabled {
_, err := GetPromConfig(params)
if err != nil {
return fmt.Errorf("failed to parse Prometheus config: %v", err)
}
desired = append(desired, targetallocator.Deployment(params.Config, params.Log, params.Instance))
}

Expand Down
29 changes: 0 additions & 29 deletions pkg/collector/reconcile/helper.go

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/collector/reconcile/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ func Services(ctx context.Context, params Params) error {
}

if params.Instance.Spec.TargetAllocator.Enabled {
_, err := GetPromConfig(params)
if err != nil {
return fmt.Errorf("failed to parse Prometheus config: %v", err)
}
desired = append(desired, desiredTAService(params))
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/targetallocator/adapters/config_to_prom_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package adapters

import (
"fmt"

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

func errorNoComponent(component string) error {
Expand All @@ -27,7 +29,12 @@ func errorNotAMap(component string) error {
}

// ConfigToPromConfig converts the incoming configuration object into a the Prometheus receiver config.
func ConfigToPromConfig(config map[interface{}]interface{}) (map[interface{}]interface{}, error) {
func ConfigToPromConfig(cfg string) (map[interface{}]interface{}, error) {
config, err := adapters.ConfigFromString(cfg)
if err != nil {
return nil, err
}

receiversProperty, ok := config["receivers"]
if !ok {
return nil, errorNoComponent("receivers")
Expand Down
16 changes: 2 additions & 14 deletions pkg/targetallocator/adapters/config_to_prom_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters"
ta "github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator/adapters"
)

Expand All @@ -49,13 +47,8 @@ func TestExtractPromConfigFromConfig(t *testing.T) {
},
}

// prepare
config, err := adapters.ConfigFromString(configStr)
require.NoError(t, err)
require.NotEmpty(t, config)

// test
promConfig, err := ta.ConfigToPromConfig(config)
promConfig, err := ta.ConfigToPromConfig(configStr)
assert.NoError(t, err)

// verify
Expand All @@ -76,13 +69,8 @@ func TestExtractPromConfigFromNullConfig(t *testing.T) {
endpoint: 0.0.0.0:15268
`

// prepare
config, err := adapters.ConfigFromString(configStr)
require.NoError(t, err)
require.NotEmpty(t, config)

// test
promConfig, err := ta.ConfigToPromConfig(config)
promConfig, err := ta.ConfigToPromConfig(configStr)
assert.Equal(t, err, fmt.Errorf("%s property in the configuration doesn't contain valid %s", "prometheusConfig", "prometheusConfig"))

// verify
Expand Down

0 comments on commit 3d37148

Please sign in to comment.