Skip to content

Commit

Permalink
prometheus.operator.*: fix panic when port not defined in CRD (#4813)
Browse files Browse the repository at this point in the history
* prometheus.operator.*: fix panic when port not defined in CRD

* add tests

* changelog

* deprecated errors
  • Loading branch information
captncraig authored Aug 15, 2023
1 parent 315468d commit 718622b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Main (unreleased)

- Fix issue on Windows where DNS short names were unresolvable. (@rfratto)

- Fix panic in `prometheus.operator.*` when no Port supplied in Monitor crds. (@captncraig)

v0.35.4 (2023-08-14)
--------------------

Expand Down
33 changes: 17 additions & 16 deletions component/prometheus/operator/configgen/config_gen_podmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,29 @@ func (cg *ConfigGenerator) GeneratePodMonitorConfig(m *promopv1.PodMonitor, ep p
Regex: regex,
})
} else if ep.TargetPort != nil { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
//nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String())
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
if ep.TargetPort.StrVal != "" { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String()) //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
if ep.TargetPort.StrVal != "" { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_name"},
Action: "keep",
Regex: regex,
})
}
} else if ep.TargetPort.IntVal != 0 { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String()) //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_name"},
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_number"},
Action: "keep",
Regex: regex,
})
}
} else if ep.TargetPort.IntVal != 0 { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String()) //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_number"},
Action: "keep",
Regex: regex,
})
}

// Relabel namespace and pod and service labels into proper labels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) {
Name: "podmonitor",
},
},
ep: promopv1.PodMetricsEndpoint{
Port: "metrics",
},
ep: promopv1.PodMetricsEndpoint{},
expectedRelabels: util.Untab(`
- target_label: __meta_foo
replacement: bar
Expand All @@ -53,9 +51,6 @@ func TestGeneratePodMonitorConfig(t *testing.T) {
- source_labels: [__meta_kubernetes_pod_phase]
regex: (Failed|Succeeded)
action: drop
- source_labels: [__meta_kubernetes_pod_container_port_name]
regex: metrics
action: keep
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
- source_labels: [__meta_kubernetes_pod_container_name]
Expand All @@ -64,8 +59,6 @@ func TestGeneratePodMonitorConfig(t *testing.T) {
target_label: pod
- target_label: job
replacement: operator/podmonitor
- target_label: endpoint
replacement: metrics
`),
expected: &config.ScrapeConfig{
JobName: "podMonitor/operator/podmonitor/0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,29 @@ func (cg *ConfigGenerator) GenerateServiceMonitorConfig(m *promopv1.ServiceMonit
Regex: regex,
})
} else if ep.TargetPort != nil { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
//nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String())
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
if ep.TargetPort.StrVal != "" {
if ep.TargetPort.StrVal != "" { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String()) //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
if ep.TargetPort.StrVal != "" {
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_name"},
Action: "keep",
Regex: regex,
})
}
} else if ep.TargetPort.IntVal != 0 { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String()) //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_name"},
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_number"},
Action: "keep",
Regex: regex,
})
}
} else if ep.TargetPort.IntVal != 0 { //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
regex, err := relabel.NewRegexp(ep.TargetPort.String()) //nolint:staticcheck // Ignore SA1019 this field is marked as deprecated.
if err != nil {
return nil, fmt.Errorf("parsing TargetPort as regex: %w", err)
}
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{"__meta_kubernetes_pod_container_port_number"},
Action: "keep",
Regex: regex,
})
}

sourceLabels := model.LabelNames{"__meta_kubernetes_endpoint_address_target_kind", "__meta_kubernetes_endpoint_address_target_name"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@ func TestGenerateServiceMonitorConfig(t *testing.T) {
Name: "svcmonitor",
},
},
ep: promopv1.Endpoint{
Port: "metrics",
},
ep: promopv1.Endpoint{},
expectedRelabels: util.Untab(`
- target_label: __meta_foo
replacement: bar
- source_labels: [job]
target_label: __tmp_prometheus_job_name
- source_labels: [__meta_kubernetes_endpoint_port_name]
regex: metrics
action: keep
- source_labels: [__meta_kubernetes_endpoint_address_target_kind, __meta_kubernetes_endpoint_address_target_name]
regex: Node;(.*)
target_label: node
Expand All @@ -76,9 +71,6 @@ func TestGenerateServiceMonitorConfig(t *testing.T) {
- source_labels: [__meta_kubernetes_service_name]
target_label: job
replacement: ${1}
- target_label: endpoint
replacement: metrics
action: replace
`),
expected: &config.ScrapeConfig{
JobName: "serviceMonitor/operator/svcmonitor/0",
Expand Down

0 comments on commit 718622b

Please sign in to comment.