Skip to content

Commit

Permalink
fix: MetricPipeline should not apply input default when the input not…
Browse files Browse the repository at this point in the history
… enabled explicitly (#1712)
  • Loading branch information
hisarbalik authored Jan 7, 2025
1 parent 5eab4db commit fe1f89c
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 26 deletions.
2 changes: 1 addition & 1 deletion webhook/logpipeline/v1alpha1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (ld defaulter) applyDefaults(pipeline *telemetryv1alpha1.LogPipeline) {
pipeline.Spec.Input.Application.Enabled = &ld.ApplicationInputEnabled
}

if pipeline.Spec.Input.Application.KeepOriginalBody == nil {
if *pipeline.Spec.Input.Application.Enabled && pipeline.Spec.Input.Application.KeepOriginalBody == nil {
pipeline.Spec.Input.Application.KeepOriginalBody = &ld.ApplicationInputKeepOriginalBody
}
}
Expand Down
21 changes: 21 additions & 0 deletions webhook/logpipeline/v1alpha1/defaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ func TestDefault(t *testing.T) {
},
},
},
{
name: "should skip default ApplicationInput if set",
input: &telemetryv1alpha1.LogPipeline{
Spec: telemetryv1alpha1.LogPipelineSpec{
Input: telemetryv1alpha1.LogPipelineInput{
Application: &telemetryv1alpha1.LogPipelineApplicationInput{
Enabled: ptr.To(false),
},
},
},
},
expected: &telemetryv1alpha1.LogPipeline{
Spec: telemetryv1alpha1.LogPipelineSpec{
Input: telemetryv1alpha1.LogPipelineInput{
Application: &telemetryv1alpha1.LogPipelineApplicationInput{
Enabled: ptr.To(false),
},
},
},
},
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion webhook/logpipeline/v1beta1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (ld defaulter) applyDefaults(pipeline *telemetryv1beta1.LogPipeline) {
pipeline.Spec.Input.Runtime.Enabled = &ld.RuntimeInputEnabled
}

if pipeline.Spec.Input.Runtime.KeepOriginalBody == nil {
if *pipeline.Spec.Input.Runtime.Enabled && pipeline.Spec.Input.Runtime.KeepOriginalBody == nil {
pipeline.Spec.Input.Runtime.KeepOriginalBody = &ld.RuntimeInputKeepOriginalBody
}
}
Expand Down
25 changes: 23 additions & 2 deletions webhook/logpipeline/v1beta1/defaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestDefault(t *testing.T) {
expected *telemetryv1beta1.LogPipeline
}{
{
name: "should set default ApplicationInput if not set",
name: "should set default Runtime Input if not set",
input: &telemetryv1beta1.LogPipeline{
Spec: telemetryv1beta1.LogPipelineSpec{
Input: telemetryv1beta1.LogPipelineInput{
Expand All @@ -43,7 +43,7 @@ func TestDefault(t *testing.T) {
},
},
{
name: "should skip default ApplicationInput if set",
name: "should skip default Runtime Input if set",
input: &telemetryv1beta1.LogPipeline{
Spec: telemetryv1beta1.LogPipelineSpec{
Input: telemetryv1beta1.LogPipelineInput{
Expand All @@ -64,6 +64,27 @@ func TestDefault(t *testing.T) {
},
},
},
{
name: "should skip default Runtime Input if disabled",
input: &telemetryv1beta1.LogPipeline{
Spec: telemetryv1beta1.LogPipelineSpec{
Input: telemetryv1beta1.LogPipelineInput{
Runtime: &telemetryv1beta1.LogPipelineRuntimeInput{
Enabled: ptr.To(false),
},
},
},
},
expected: &telemetryv1beta1.LogPipeline{
Spec: telemetryv1beta1.LogPipelineSpec{
Input: telemetryv1beta1.LogPipelineInput{
Runtime: &telemetryv1beta1.LogPipelineRuntimeInput{
Enabled: ptr.To(false),
},
},
},
},
},
{
name: "should set default OTLPOutput if not set",
input: &telemetryv1beta1.LogPipeline{
Expand Down
28 changes: 20 additions & 8 deletions webhook/metricpipeline/v1alpha1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,31 @@ func (md defaulter) Default(ctx context.Context, obj runtime.Object) error {
}

func (md defaulter) applyDefaults(pipeline *telemetryv1alpha1.MetricPipeline) {
if pipeline.Spec.Input.Prometheus != nil && pipeline.Spec.Input.Prometheus.Namespaces == nil {
if prometheusInputEnabled(pipeline) && pipeline.Spec.Input.Prometheus.Namespaces == nil {
pipeline.Spec.Input.Prometheus.Namespaces = &telemetryv1alpha1.NamespaceSelector{
Exclude: md.ExcludeNamespaces,
}
}

if pipeline.Spec.Input.Istio != nil && pipeline.Spec.Input.Istio.Namespaces == nil {
if istioInputEnabled(pipeline) && pipeline.Spec.Input.Istio.Namespaces == nil {
pipeline.Spec.Input.Istio.Namespaces = &telemetryv1alpha1.NamespaceSelector{
Exclude: md.ExcludeNamespaces,
}
}

if pipeline.Spec.Output.OTLP != nil && pipeline.Spec.Output.OTLP.Protocol == "" {
pipeline.Spec.Output.OTLP.Protocol = md.DefaultOTLPOutputProtocol
}

if pipeline.Spec.Input.Runtime != nil && pipeline.Spec.Input.Runtime.Namespaces == nil {
if runtimeInputEnabled(pipeline) && pipeline.Spec.Input.Runtime.Namespaces == nil {
pipeline.Spec.Input.Runtime.Namespaces = &telemetryv1alpha1.NamespaceSelector{
Exclude: md.ExcludeNamespaces,
}
}

if pipeline.Spec.Input.Runtime != nil {
if runtimeInputEnabled(pipeline) {
md.applyRuntimeInputResourceDefaults(pipeline)
}

if pipeline.Spec.Output.OTLP != nil && pipeline.Spec.Output.OTLP.Protocol == "" {
pipeline.Spec.Output.OTLP.Protocol = md.DefaultOTLPOutputProtocol
}
}

func (md defaulter) applyRuntimeInputResourceDefaults(pipeline *telemetryv1alpha1.MetricPipeline) {
Expand Down Expand Up @@ -122,3 +122,15 @@ func (md defaulter) applyRuntimeInputResourceDefaults(pipeline *telemetryv1alpha
}
}
}

func prometheusInputEnabled(pipeline *telemetryv1alpha1.MetricPipeline) bool {
return pipeline.Spec.Input.Prometheus != nil && pipeline.Spec.Input.Prometheus.Enabled
}

func istioInputEnabled(pipeline *telemetryv1alpha1.MetricPipeline) bool {
return pipeline.Spec.Input.Istio != nil && pipeline.Spec.Input.Istio.Enabled
}

func runtimeInputEnabled(pipeline *telemetryv1alpha1.MetricPipeline) bool {
return pipeline.Spec.Input.Runtime != nil && pipeline.Spec.Input.Runtime.Enabled
}
80 changes: 77 additions & 3 deletions webhook/metricpipeline/v1alpha1/defaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,17 @@ func TestDefault(t *testing.T) {
input: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Prometheus: &telemetryv1alpha1.MetricPipelinePrometheusInput{},
Prometheus: &telemetryv1alpha1.MetricPipelinePrometheusInput{
Enabled: true,
},
},
},
},
expected: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Prometheus: &telemetryv1alpha1.MetricPipelinePrometheusInput{
Enabled: true,
Namespaces: &telemetryv1alpha1.NamespaceSelector{
Exclude: []string{"kyma-system", "kube-system", "istio-system", "compass-system"},
},
Expand All @@ -97,14 +100,17 @@ func TestDefault(t *testing.T) {
input: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Istio: &telemetryv1alpha1.MetricPipelineIstioInput{},
Istio: &telemetryv1alpha1.MetricPipelineIstioInput{
Enabled: true,
},
},
},
},
expected: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Istio: &telemetryv1alpha1.MetricPipelineIstioInput{
Enabled: true,
Namespaces: &telemetryv1alpha1.NamespaceSelector{
Exclude: []string{"kyma-system", "kube-system", "istio-system", "compass-system"},
},
Expand All @@ -119,14 +125,17 @@ func TestDefault(t *testing.T) {
input: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{},
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{
Enabled: true,
},
},
},
},
expected: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{
Enabled: true,
Namespaces: &telemetryv1alpha1.NamespaceSelector{
Exclude: []string{"kyma-system", "kube-system", "istio-system", "compass-system"},
},
Expand Down Expand Up @@ -175,6 +184,7 @@ func TestDefault(t *testing.T) {
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{
Enabled: true,
Resources: &telemetryv1alpha1.MetricPipelineRuntimeInputResources{
Pod: &telemetryv1alpha1.MetricPipelineRuntimeInputResource{
Enabled: ptr.To(false),
Expand All @@ -188,6 +198,7 @@ func TestDefault(t *testing.T) {
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{
Enabled: true,
Namespaces: &telemetryv1alpha1.NamespaceSelector{
Exclude: []string{"kyma-system", "kube-system", "istio-system", "compass-system"},
},
Expand Down Expand Up @@ -229,6 +240,69 @@ func TestDefault(t *testing.T) {
},
},
},
{
name: "should not set default for Prometheus input",
input: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Prometheus: &telemetryv1alpha1.MetricPipelinePrometheusInput{
Enabled: false,
},
},
},
},
expected: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Prometheus: &telemetryv1alpha1.MetricPipelinePrometheusInput{
Enabled: false,
},
},
},
},
},
{
name: "should not set defaults for Istio input",
input: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Istio: &telemetryv1alpha1.MetricPipelineIstioInput{
Enabled: false,
},
},
},
},
expected: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Istio: &telemetryv1alpha1.MetricPipelineIstioInput{
Enabled: false,
},
},
},
},
},
{
name: "should not set defaults for Runtime input",
input: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{
Enabled: false,
},
},
},
},
expected: &telemetryv1alpha1.MetricPipeline{
Spec: telemetryv1alpha1.MetricPipelineSpec{
Input: telemetryv1alpha1.MetricPipelineInput{
Runtime: &telemetryv1alpha1.MetricPipelineRuntimeInput{
Enabled: false,
},
},
},
},
},
}

for _, tt := range tests {
Expand Down
28 changes: 20 additions & 8 deletions webhook/metricpipeline/v1beta1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,31 @@ func (md defaulter) Default(ctx context.Context, obj runtime.Object) error {
}

func (md defaulter) applyDefaults(pipeline *telemetryv1beta1.MetricPipeline) {
if pipeline.Spec.Input.Prometheus != nil && pipeline.Spec.Input.Prometheus.Namespaces == nil {
if prometheusInputEnabled(pipeline) && pipeline.Spec.Input.Prometheus.Namespaces == nil {
pipeline.Spec.Input.Prometheus.Namespaces = &telemetryv1beta1.NamespaceSelector{
Exclude: md.ExcludeNamespaces,
}
}

if pipeline.Spec.Input.Istio != nil && pipeline.Spec.Input.Istio.Namespaces == nil {
if istioInputEnabled(pipeline) && pipeline.Spec.Input.Istio.Namespaces == nil {
pipeline.Spec.Input.Istio.Namespaces = &telemetryv1beta1.NamespaceSelector{
Exclude: md.ExcludeNamespaces,
}
}

if pipeline.Spec.Output.OTLP != nil && pipeline.Spec.Output.OTLP.Protocol == "" {
pipeline.Spec.Output.OTLP.Protocol = md.DefaultOTLPOutputProtocol
}

if pipeline.Spec.Input.Runtime != nil && pipeline.Spec.Input.Runtime.Namespaces == nil {
if runtimeInputEnabled(pipeline) && pipeline.Spec.Input.Runtime.Namespaces == nil {
pipeline.Spec.Input.Runtime.Namespaces = &telemetryv1beta1.NamespaceSelector{
Exclude: md.ExcludeNamespaces,
}
}

if pipeline.Spec.Input.Runtime != nil {
if runtimeInputEnabled(pipeline) {
md.applyRuntimeInputResourceDefaults(pipeline)
}

if pipeline.Spec.Output.OTLP != nil && pipeline.Spec.Output.OTLP.Protocol == "" {
pipeline.Spec.Output.OTLP.Protocol = md.DefaultOTLPOutputProtocol
}
}

func (md defaulter) applyRuntimeInputResourceDefaults(pipeline *telemetryv1beta1.MetricPipeline) {
Expand Down Expand Up @@ -122,3 +122,15 @@ func (md defaulter) applyRuntimeInputResourceDefaults(pipeline *telemetryv1beta1
}
}
}

func prometheusInputEnabled(pipeline *telemetryv1beta1.MetricPipeline) bool {
return pipeline.Spec.Input.Prometheus != nil && pipeline.Spec.Input.Prometheus.Enabled
}

func istioInputEnabled(pipeline *telemetryv1beta1.MetricPipeline) bool {
return pipeline.Spec.Input.Istio != nil && pipeline.Spec.Input.Istio.Enabled
}

func runtimeInputEnabled(pipeline *telemetryv1beta1.MetricPipeline) bool {
return pipeline.Spec.Input.Runtime != nil && pipeline.Spec.Input.Runtime.Enabled
}
Loading

0 comments on commit fe1f89c

Please sign in to comment.