Skip to content

Commit

Permalink
Infer the label matcher for app.kubernetes.io/name (#935)
Browse files Browse the repository at this point in the history
Signed-off-by: Pete Wall <pete.wall@grafana.com>
  • Loading branch information
petewall committed Nov 22, 2024
1 parent 869da92 commit 5d16003
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 71 deletions.
2 changes: 1 addition & 1 deletion charts/feature-integrations/docs/integrations/alloy.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| labelSelectors | object | `{"app.kubernetes.io/name":"alloy"}` | Discover Alloy instances based on label selectors. |
| labelSelectors | object | `{}` | Discover Alloy instances based on label selectors. Will automatically set a matcher for `app.kubernetes.io/name: <name>` unless set here. |

### Metric Processing Settings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Key | Type | Default | Description |
|-----|------|---------|-------------|
| field_selectors | list | `[]` | Discover cert-manager instances based on field selectors. |
| labelSelectors | object | `{"app.kubernetes.io/name":"cert-manager"}` | Discover cert-manager instances based on label selectors. |
| labelSelectors | object | `{}` | Discover cert-manager instances based on label selectors. Will automatically set a matcher for `app.kubernetes.io/name: <name>` unless set here. |
| namespaces | list | `[]` | Namespaces to look for cert-manager instances. |
| portName | string | `"http-metrics"` | Name of the port to scrape metrics from. |

Expand Down
4 changes: 2 additions & 2 deletions charts/feature-integrations/integrations/alloy-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
name: ""

# -- Discover Alloy instances based on label selectors.
# Will automatically set a matcher for `app.kubernetes.io/name: <name>` unless set here.
# @section -- Discovery Settings
labelSelectors:
app.kubernetes.io/name: alloy
labelSelectors: {}

# -- How frequently to scrape metrics from Alloy.
# @default -- `60s`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespaces: []
field_selectors: []

# -- Discover cert-manager instances based on label selectors.
# Will automatically set a matcher for `app.kubernetes.io/name: <name>` unless set here.
# @section -- Discovery Settings
labelSelectors:
app.kubernetes.io/name: cert-manager
labelSelectors: {}

# -- Name of the port to scrape metrics from.
# @section -- Discovery Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
"type": "object",
"properties": {
"labelSelectors": {
"type": "object",
"properties": {
"app.kubernetes.io/name": {
"type": "string"
}
}
"type": "object"
},
"maxCacheSize": {
"type": "null"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
"type": "string"
},
"labelSelectors": {
"type": "object",
"properties": {
"app.kubernetes.io/name": {
"type": "string"
}
}
"type": "object"
},
"maxCacheSize": {
"type": "null"
Expand Down
10 changes: 9 additions & 1 deletion charts/feature-integrations/templates/_integration_alloy.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,17 @@ declare "alloy_integration" {
{{- with $defaultValues | merge (deepCopy .instance) }}
{{- $metricAllowList := include "integrations.alloy.allowList" (dict "instance" . "Files" $.Files) | fromYamlArray }}
{{- $metricDenyList := .excludeMetrics }}

{{- $nameLabelDefined := false }}
{{- $labelSelectors := list }}
{{- range $k, $v := .labelSelectors }}
{{- $labelSelectors = append $labelSelectors (printf "%s=%s" $k $v) }}
{{- if eq $k "app.kubernetes.io/name" }}{{- $nameLabelDefined = true }}{{- end }}
{{- if $v }}
{{- $labelSelectors = append $labelSelectors (printf "%s=%s" $k $v) }}
{{- end }}
{{- end }}
{{- if not $nameLabelDefined }}
{{- $labelSelectors = append $labelSelectors (printf "app.kubernetes.io/name=%s" .name) }}
{{- end }}

alloy_integration_discovery {{ include "helper.alloy_name" .name | quote }} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ declare "cert_manager_integration" {
{{- with $defaultValues | merge (deepCopy .instance) }}
{{- $metricAllowList := .metricsTuning.includeMetrics }}
{{- $metricDenyList := .metricsTuning.excludeMetrics }}

{{- $nameLabelDefined := false }}
{{- $labelSelectors := list }}
{{- range $k, $v := .labelSelectors }}
{{- $labelSelectors = append $labelSelectors (printf "%s=%s" $k $v) }}
{{- if eq $k "app.kubernetes.io/name" }}{{- $nameLabelDefined = true }}{{- end }}
{{- if $v }}
{{- $labelSelectors = append $labelSelectors (printf "%s=%s" $k $v) }}
{{- end }}
{{- end }}
{{- if not $nameLabelDefined }}
{{- $labelSelectors = append $labelSelectors (printf "app.kubernetes.io/name=%s" .name) }}
{{- end }}
{{- $fieldSelectors := list }}
{{- range $k, $v := .fieldSelectors }}
Expand Down
33 changes: 29 additions & 4 deletions charts/feature-integrations/tests/alloy_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ tests:
alloy:
instances:
- name: alloy-metrics
labelSelectors:
app.kubernetes.io/name: alloy-metrics
asserts:
- isKind:
of: ConfigMap
Expand Down Expand Up @@ -299,11 +297,38 @@ tests:
- name: alloy-metrics
metricsTuning:
useIntegrationAllowList: true
labelSelectors:
app.kubernetes.io/name: alloy-metrics
asserts:
- isKind:
of: ConfigMap
- matchRegex:
path: data["metrics.alloy"]
pattern: alloy_component_controller_running_components
- it: should allow you to overwrite the name label
set:
deployAsConfigMap: true
alloy:
instances:
- name: alloy-metrics
labelSelectors:
app.kubernetes.io/name: my-alloy-instance
asserts:
- isKind:
of: ConfigMap
- matchRegex:
path: data["metrics.alloy"]
pattern: label_selectors = \["app.kubernetes.io/name=my-alloy-instance"\]
- it: should allow you to dismiss the name label
set:
deployAsConfigMap: true
alloy:
instances:
- name: alloy-metrics
labelSelectors:
app.kubernetes.io/name: null
app.kubernetes.io/instance: k8smon
asserts:
- isKind:
of: ConfigMap
- matchRegex:
path: data["metrics.alloy"]
pattern: label_selectors = \["app.kubernetes.io/instance=k8smon"\]
10 changes: 5 additions & 5 deletions charts/feature-integrations/tests/cert-manager_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tests:
deployAsConfigMap: true
cert-manager:
instances:
- name: my-cert-manager
- name: cert-manager
asserts:
- isKind:
of: ConfigMap
Expand All @@ -27,13 +27,13 @@ tests:
pull_frequency = "15m"
}
cert_manager.kubernetes "my_cert_manager" {
cert_manager.kubernetes "cert_manager" {
label_selectors = ["app.kubernetes.io/name=cert-manager"]
port_name = "http-metrics"
}
cert_manager.scrape "my_cert_manager" {
targets = cert_manager.kubernetes.my_cert_manager.output
cert_manager.scrape "cert_manager" {
targets = cert_manager.kubernetes.cert_manager.output
clustering = true
job_label = "integrations/cert-manager"
scrape_interval = "60s"
Expand Down Expand Up @@ -69,7 +69,7 @@ tests:
cert_manager.kubernetes "my_cert_manager" {
namespaces = ["kube-system"]
label_selectors = ["app.kubernetes.io/name=cert-manager"]
label_selectors = ["app.kubernetes.io/name=my-cert-manager"]
port_name = "http-metrics"
}
Expand Down
14 changes: 2 additions & 12 deletions charts/feature-integrations/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@
"type": "object",
"properties": {
"labelSelectors": {
"type": "object",
"properties": {
"app.kubernetes.io/name": {
"type": "string"
}
}
"type": "object"
},
"maxCacheSize": {
"type": "null"
Expand Down Expand Up @@ -120,12 +115,7 @@
"type": "string"
},
"labelSelectors": {
"type": "object",
"properties": {
"app.kubernetes.io/name": {
"type": "string"
}
}
"type": "object"
},
"maxCacheSize": {
"type": "null"
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5d16003

Please sign in to comment.