diff --git a/CHANGELOG.md b/CHANGELOG.md index f6bbb53c1d..d91925e3f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - fix(openshift): changed allowed fsgroups in SecurityContextConstraints [#2717] - fix(openshift): set securityContexts for otelcol-logs-collector [#2717] - fix: obey proxy settings in otelcol [#2719] +- feat(metrics): simplify custom application metrics [#2716] [#2686]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2686 [#2687]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2687 @@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#2712]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2712 [#2717]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2717 [#2719]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2719 +[#2716]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2716 [Unreleased]: https://github.com/SumoLogic/sumologic-kubernetes-collection/compare/v3.0.0-beta.0...main ## [v3.0.0-beta.0] diff --git a/deploy/helm/sumologic/conf/metrics/otelcol/config.yaml b/deploy/helm/sumologic/conf/metrics/otelcol/config.yaml index f74bd91bf3..1a36cd94ea 100644 --- a/deploy/helm/sumologic/conf/metrics/otelcol/config.yaml +++ b/deploy/helm/sumologic/conf/metrics/otelcol/config.yaml @@ -15,37 +15,7 @@ receivers: data_format = "prometheusremotewrite" path_tag = true paths = [ - "/prometheus.metrics", - "/prometheus.metrics.apiserver", - "/prometheus.metrics.applications.activemq", - "/prometheus.metrics.applications.apache", - "/prometheus.metrics.applications.cassandra", - "/prometheus.metrics.applications.couchbase", - "/prometheus.metrics.applications.elasticsearch", - "/prometheus.metrics.applications.haproxy", - "/prometheus.metrics.applications.jmx", - "/prometheus.metrics.applications.kafka", - "/prometheus.metrics.applications.memcached", - "/prometheus.metrics.applications.mongodb", - "/prometheus.metrics.applications.mysql", - "/prometheus.metrics.applications.nginx", - "/prometheus.metrics.applications.nginx-ingress", - "/prometheus.metrics.applications.postgresql", - "/prometheus.metrics.applications.rabbitmq", - "/prometheus.metrics.applications.redis", - "/prometheus.metrics.applications.sqlserver", - "/prometheus.metrics.applications.squidproxy", - "/prometheus.metrics.applications.tomcat", - "/prometheus.metrics.applications.varnish", - "/prometheus.metrics.container", - "/prometheus.metrics.controller-manager", - "/prometheus.metrics.control-plane.coredns", - "/prometheus.metrics.control-plane.kube-etcd", - "/prometheus.metrics.kubelet", - "/prometheus.metrics.node", - "/prometheus.metrics.operator.rule", - "/prometheus.metrics.scheduler", - "/prometheus.metrics.state" +{{ include "metric.remotewrites" . | indent 10 }} ] extensions: health_check: {} diff --git a/deploy/helm/sumologic/templates/_helpers.tpl b/deploy/helm/sumologic/templates/_helpers.tpl index 2dff1c4d8d..9868cacbdc 100644 --- a/deploy/helm/sumologic/templates/_helpers.tpl +++ b/deploy/helm/sumologic/templates/_helpers.tpl @@ -1549,3 +1549,23 @@ Example Usage: value: {{ .Values.sumologic.noProxy }} {{- end -}} {{- end -}} + + +{{/* +Generate list of remoteWrite endpoints for telegraf configuration + +'{{ include "metric.remotewrites" . }}' +*/}} +{{- define "metric.remotewrites" -}} +{{- $remoteWrites := list -}} +{{- $kps := get .Values "kube-prometheus-stack" -}} +{{- range $remoteWrite := $kps.prometheus.prometheusSpec.remoteWrite }} +{{- $remoteWrites = append $remoteWrites ($remoteWrite.url | trimPrefix "http://$(METADATA_METRICS_SVC).$(NAMESPACE).svc.cluster.local.:9888" | quote) -}} +{{- end }} +{{- range $remoteWrite := $kps.prometheus.prometheusSpec.additionalRemoteWrite }} +{{- $remoteWrites = append $remoteWrites ($remoteWrite.url | trimPrefix "http://$(METADATA_METRICS_SVC).$(NAMESPACE).svc.cluster.local.:9888" | quote) -}} +{{- end -}} +{{- $remoteWrites := uniq $remoteWrites -}} +{{- $remoteWrites := sortAlpha $remoteWrites -}} +{{ $remoteWrites | join ",\n" }} +{{- end -}} diff --git a/deploy/helm/sumologic/templates/metrics/prometheus/servicemonitors.yaml b/deploy/helm/sumologic/templates/metrics/prometheus/servicemonitors.yaml new file mode 100644 index 0000000000..1122f3fac8 --- /dev/null +++ b/deploy/helm/sumologic/templates/metrics/prometheus/servicemonitors.yaml @@ -0,0 +1,42 @@ +{{- $kps := get .Values "kube-prometheus-stack" -}} +{{- if or (and (typeIs "" $kps.enabled) (.Values.sumologic.metrics.enabled)) (and (typeIs "bool" $kps.enabled) ($kps.enabled)) -}} +--- +{{- if .Values.sumologic.metrics.serviceMonitors }} +apiVersion: v1 +kind: List +items: +{{- range .Values.sumologic.metrics.serviceMonitors }} + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: {{ .name }} + namespace: {{ template "kube-prometheus-stack.namespace" $ }} + labels: + app: {{ template "kube-prometheus-stack.name" $ }}-prometheus +{{ include "kube-prometheus-stack.labels" $ | indent 8 }} + {{- if .additionalLabels }} +{{ toYaml .additionalLabels | indent 8 }} + {{- end }} + spec: + endpoints: +{{ toYaml .endpoints | indent 8 }} + {{- if .jobLabel }} + jobLabel: {{ .jobLabel }} + {{- end }} + {{- if .namespaceSelector }} + namespaceSelector: +{{ toYaml .namespaceSelector | indent 8 }} + {{- end }} + selector: +{{ toYaml .selector | indent 8 }} + {{- if .targetLabels }} + targetLabels: +{{ toYaml .targetLabels | indent 8 }} + {{- end }} + {{- if .podTargetLabels }} + podTargetLabels: +{{ toYaml .podTargetLabels | indent 8 }} + {{- end }} +{{- end }} +{{- end }} +{{- end }} diff --git a/deploy/helm/sumologic/values.yaml b/deploy/helm/sumologic/values.yaml index 50268ad8b8..3e686dc75d 100644 --- a/deploy/helm/sumologic/values.yaml +++ b/deploy/helm/sumologic/values.yaml @@ -431,6 +431,117 @@ sumologic: ## Add custom annotations only to metrics sts pods podAnnotations: {} + ## Prometheus serviceMonitors related to Sumo Logic services + ## They are applied only if kube-prometheus-stack is enabled + serviceMonitors: + - name: collection-sumologic-fluentd-logs + additionalLabels: + sumologic.com/app: fluentd-logs + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-logs + sumologic.com/scrape: "true" + - name: collection-sumologic-otelcol-logs + additionalLabels: + sumologic.com/app: otelcol-logs + endpoints: + - port: otelcol-metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-logs + sumologic.com/scrape: "true" + - name: collection-sumologic-fluentd-metrics + additionalLabels: + sumologic.com/app: fluentd-metrics + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-metrics + sumologic.com/scrape: "true" + - name: collection-sumologic-otelcol-metrics + additionalLabels: + sumologic.com/app: otelcol-metrics + endpoints: + - port: otelcol-metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-metrics + sumologic.com/scrape: "true" + - name: collection-sumologic-fluentd-events + additionalLabels: + sumologic.com/app: fluentd-events + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-events + sumologic.com/scrape: "true" + - name: collection-fluent-bit + additionalLabels: + sumologic.com/app: collection-fluent-bit + endpoints: + - port: http + path: /api/v1/metrics/prometheus + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + app.kubernetes.io/name: fluent-bit + sumologic.com/scrape: "true" + - name: collection-sumologic-otelcol-logs-collector + additionalLabels: + sumologic.com/app: otelcol-logs-collector + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: otelcol-logs-collector + sumologic.com/scrape: "true" + - name: collection-sumologic-otelcol-traces + additionalLabels: + sumologic.com/app: otelcol + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/component: traces + sumologic.com/scrape: "true" + - name: collection-sumologic-prometheus + endpoints: + - port: web + path: /metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + operated-prometheus: "true" + ### Traces configuration ## Set the enabled flag to false to disable traces ingestion. traces: @@ -1723,114 +1834,7 @@ kube-prometheus-stack: # cpu: 100m # memory: 30Mi prometheus: - additionalServiceMonitors: - - name: collection-sumologic-fluentd-logs - additionalLabels: - sumologic.com/app: fluentd-logs - endpoints: - - port: metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/app: fluentd-logs - sumologic.com/scrape: "true" - - name: collection-sumologic-otelcol-logs - additionalLabels: - sumologic.com/app: otelcol-logs - endpoints: - - port: otelcol-metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/app: fluentd-logs - sumologic.com/scrape: "true" - - name: collection-sumologic-fluentd-metrics - additionalLabels: - sumologic.com/app: fluentd-metrics - endpoints: - - port: metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/app: fluentd-metrics - sumologic.com/scrape: "true" - - name: collection-sumologic-otelcol-metrics - additionalLabels: - sumologic.com/app: otelcol-metrics - endpoints: - - port: otelcol-metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/app: fluentd-metrics - sumologic.com/scrape: "true" - - name: collection-sumologic-fluentd-events - additionalLabels: - sumologic.com/app: fluentd-events - endpoints: - - port: metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/app: fluentd-events - sumologic.com/scrape: "true" - - name: collection-fluent-bit - additionalLabels: - sumologic.com/app: collection-fluent-bit - endpoints: - - port: http - path: /api/v1/metrics/prometheus - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - app.kubernetes.io/name: fluent-bit - sumologic.com/scrape: "true" - - name: collection-sumologic-otelcol-logs-collector - additionalLabels: - sumologic.com/app: otelcol-logs-collector - endpoints: - - port: metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/app: otelcol-logs-collector - sumologic.com/scrape: "true" - - name: collection-sumologic-otelcol-traces - additionalLabels: - sumologic.com/app: otelcol - endpoints: - - port: metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - sumologic.com/component: traces - sumologic.com/scrape: "true" - - name: collection-sumologic-prometheus - endpoints: - - port: web - path: /metrics - namespaceSelector: - matchNames: - - $(NAMESPACE) - selector: - matchLabels: - operated-prometheus: "true" + additionalServiceMonitors: [] prometheusSpec: ## Prometheus default scrape interval, default from upstream Kube Prometheus Stack Helm chart ## NOTE changing the scrape interval to be >1m can result in metrics @@ -1939,6 +1943,13 @@ kube-prometheus-stack: target_label: pod replacement: $1 action: replace + ## Sets _sumo_forward_ label to true + - source_labels: [__name__] + separator: ; + regex: (.*) + target_label: _sumo_forward_ + replacement: "true" + action: replace - source_labels: [__meta_kubernetes_service_name] separator: ; regex: (.*) @@ -3315,7 +3326,16 @@ kube-prometheus-stack: sourceLabels: [__name__] ## additionalRemoteWrite is appended to remoteWrite - additionalRemoteWrite: [] + additionalRemoteWrite: + ## Forward every metric which has _sumo_forward_ label set to true + - url: http://$(METADATA_METRICS_SVC).$(NAMESPACE).svc.cluster.local.:9888/prometheus.metrics.applications.custom + remoteTimeout: 5s + writeRelabelConfigs: + - action: keep + regex: ^true$ + sourceLabels: [_sumo_forward_] + - action: labeldrop + regex: _sumo_forward_ serviceMonitor: selfMonitor: false diff --git a/docs/collecting-application-metrics.md b/docs/collecting-application-metrics.md new file mode 100644 index 0000000000..b4a01f8ee0 --- /dev/null +++ b/docs/collecting-application-metrics.md @@ -0,0 +1,211 @@ +# Collecting application metrics + +This document covers multiple different use cases related to scraping custom application metrics exposed in Prometheus format. + +## Practical scenarios + +### Scraping metrics + +#### Application metrics are exposed (one endpoint scenario) + +If there is only one endpoint in the Pod you want to scrape metrics from, you can use annotations. +Add the following annotations to your Pod definition: + +```yaml +# ... +annotations: + prometheus.io/port: 8000 # Port which metrics should be scraped from + prometheus.io/scrape: true # Set if metrics should be scraped from this Pod + prometheus.io/path: "/metrics" # Path which metrics should be scraped from +``` + +**NOTE:** If you add more than one annotation with the same name, only the last one will be used. + +#### Application metrics are exposed (multiple enpoints scenario) + +If you want to scrape metrics from multiple endpoints in a single Pod, +you need a Service which points to the Pod and also to configure `kube-prometheus-stack.prometheus.additionalServiceMonitors` +in your `user-values.yaml`: + +```yaml +kube-prometheus-stack: + prometheus: + additionalServiceMonitors: + - name: + endpoints: + - port: "" + path: + relabelings: + ## Sets _sumo_forward_ label to true + - sourceLabels: [__name__] + separator: ; + regex: (.*) + targetLabel: _sumo_forward_ + replacement: "true" + action: replace + namespaceSelector: + matchNames: + - + selector: + matchLabels: + : + : +``` + +**Note** For advanced serviceMonitor configuration, please look at the [Prometheus documentation][prometheus_service_monitors] + +> **Note** If you not set `_sumo_forward_` label you will have to configure `additionalRemoteWrite`: +> +> ```yaml +> kube-prometheus-stack: +> prometheus: +> prometheusSpec: +> additionalRemoteWrite: +> ## This is required to keep default configuration. It's copy of values.yaml content +> - url: http://$(METADATA_METRICS_SVC).$(NAMESPACE).svc.cluster.local.:9888/prometheus.metrics.applications.custom +> remoteTimeout: 5s +> writeRelabelConfigs: +> - action: keep +> regex: ^true$ +> sourceLabels: [_sumo_forward_] +> - action: labeldrop +> regex: _sumo_forward_ +> ## This is your custom remoteWrite configuration +> - url: http://$(METADATA_METRICS_SVC).$(NAMESPACE).svc.cluster.local.:9888/prometheus.metrics. +> writeRelabelConfigs: +> - action: keep +> regex: ||... +> sourceLabels: [__name__] +> ``` +> +> We recommend using a regex validator, for example [https://regex101.com/] + +[prometheus_service_monitors]: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor +[https://regex101.com/]: https://regex101.com/ + +##### Example + +Let's consider a Pod which exposes the following metrics: + +```txt +my_metric_cpu +my_metric_memory +``` + +on the following endpoints: + +```txt +:3000/metrics +:3001/custom-endpoint +``` + +The Pod's definition looks like the following: + +```yaml +apiVersion: v1 +kind: Pod +metadata: + labels: + app: my-custom-app + name: my-custom-app-56fdc95c9c-r5pvc + namespace: my-custom-app-namespace + # ... +spec: + containers: + - ports: + - containerPort: 3000 + protocol: TCP + - containerPort: 3001 + protocol: TCP + # ... +``` + +There is also a Service which exposes Pod ports: + +```yaml +apiVersion: v1 +kind: Service +metadata: + labels: + app: my-custom-app-service + managedFields: + name: my-custom-app-service + namespace: my-custom-app-namespace +spec: + ports: + - name: "some-port" + port: 3000 + protocol: TCP + targetPort: 3000 + - name: "another-port" + port: 3001 + protocol: TCP + targetPort: 3001 + selector: + app: my-custom-app +``` + +In order to scrape metrics from the above objects, the following configuration should be applied to `user-values.yaml`: + +```yaml +kube-prometheus-stack: + prometheus: + additionalServiceMonitors: + - name: my-custom-app-service-monitor + endpoints: + - port: some-port + path: /metrics + relabelings: + ## Sets _sumo_forward_ label to true + - sourceLabels: [__name__] + separator: ; + regex: (.*) + targetLabel: _sumo_forward_ + replacement: "true" + action: replace + - port: another-port + path: /custom-endpoint + relabelings: + ## Sets _sumo_forward_ label to true + - sourceLabels: [__name__] + separator: ; + regex: (.*) + targetLabel: _sumo_forward_ + replacement: "true" + action: replace + namespaceSelector: + matchNames: + - my-custom-app-namespace + selector: + matchLabels: + app: my-custom-app-service +``` + +#### Application metrics are not exposed + +In case you want to scrape metrics from application which do not expose them, you can use telegraf operator. +It will scrape metrics according to configuration and expose them on port `9273` so Prometheus will be able to scrape them. + +For example to expose metrics from nginx Pod, you can use the following annotations: + +``` +annotations: + telegraf.influxdata.com/inputs: |+ + [[inputs.nginx]] + urls = ["http://localhost/nginx_status"] + telegraf.influxdata.com/class: sumologic-prometheus + telegraf.influxdata.com/limits-cpu: '750m' +``` + +`sumologic-prometheus` defines the way telegraf operator will expose the metrics. +They are going to be exposed in prometheus format on port `9273` and `/metrics` path. + +**NOTE** If you apply annotations on Pod which is subject of other object, e.g. DaemonSet, it won't take affect. +In such case, the annotation should be added to Pod specification in DeamonSet template. + +After restart, the Pod should have additional `telegraf` container. + +To scrape and forward exposed metrics to Sumo Logic, please follow one of the following scenarios: + +- [Application metrics are exposed (one endpoint scenario)](#application-metrics-are-exposed-one-endpoint-scenario) +- [Application metrics are exposed (multiple enpoints scenario)](#application-metrics-are-exposed-multiple-enpoints-scenario) diff --git a/docs/v3-migration-doc.md b/docs/v3-migration-doc.md index a7885c5ec3..bb2a46a071 100644 --- a/docs/v3-migration-doc.md +++ b/docs/v3-migration-doc.md @@ -5,8 +5,9 @@ - [Requirements](#requirements) - [Manual steps](#manual-steps) - [Upgrade kube-prometheus-stack](#upgrade-kube-prometheus-stack) - - [Replace special configuration values marked by 'replace' suffix](#replace-special-configuration-values-marked-by-replace-suffix) + - [Replace special configuration values marked by 'replace' suffix](#replace-special-configuration-values-marked-by-replace-suffix) - [Otelcol StatefulSets](#otelcol-statefulsets) + - [Additional Service Monitors](#additional-service-monitors) Based on the feedback from our users, we will be introducing several changes to the Sumo Logic Kubernetes Collection solution. @@ -40,8 +41,8 @@ In this document we detail the changes as well as the exact steps for migration. - moved `fluentd.logs.containers.sourceHost` to `sumologic.logs.container.sourceHost` - moved `fluentd.logs.containers.sourceName` to `sumologic.logs.container.sourceName` - moved `fluentd.logs.contianers.sourceCategory` to `sumologic.logs.container.sourceCategory` - - moved `fluentd.logs.containers.sourceCategoryPrefix` to `sumologic.logs.container.sourceCategoryPrefix` - - moved `fluentd.logs.contianers.sourceCategoryReplaceDash` to `sumologic.logs.container.sourceCategoryReplaceDash` + - moved `fluentd.logs.containers.sourceCategoryPrefix` to `sumologic.logs.container.sourceCategoryPrefix` + - moved `fluentd.logs.contianers.sourceCategoryReplaceDash` to `sumologic.logs.container.sourceCategoryReplaceDash` - moved `fluentd.logs.containers.excludeContainerRegex` to `sumologic.logs.container.excludeContainerRegex` - moved `fluentd.logs.containers.excludeHostRegex` to `sumologic.logs.container.excludeHostRegex` - moved `fluentd.logs.containers.excludeNamespaceRegex` to `sumologic.logs.container.excludeNamespaceRegex` @@ -85,6 +86,9 @@ In this document we detail the changes as well as the exact steps for migration. - Upgrading Tailing Sidecar Operator helm chart to v0.5.5. There is no breaking change if using annotations only. +- Adding `sumologic.metrics.serviceMonitors` to avoid copying values for + `kube-prometheus-stack.prometheus.additionalServiceMonitors` configuration + ## How to upgrade ### Requirements @@ -146,7 +150,7 @@ Upgrade of kube-prometheus-stack is a breaking change and requires manual steps: - In case of overriding any of the `repository` property under the `kube-prometheus-stack` property, please follow the `kube-prometheus-stack` [migration doc][kube-prometheus-stack-image-migration] on that. -### Replace special configuration values marked by 'replace' suffix +#### Replace special configuration values marked by 'replace' suffix Mechanism to replace special configuration values for traces marked by 'replace' suffix was removed and following special values in configuration are no longer automatically replaced and they need to be changed: @@ -176,6 +180,12 @@ If you're using `otelcol` as the logs/metrics metadata provider, please run one kubectl delete sts --namespace=my-namespace --cascade=false my-release-sumologic-otelcol-metrics ``` +#### Additional Service Monitors + +If you're using `kube-prometheus-stack.prometheus.additionalServiceMonitors`, +you have to remove all Sumo Logic related service monitors from the list, because they are now covered by +`sumologic.metrics.serviceMonitors` configuration. This will make your configuration more clear. + ### Known issues #### Cannot delete pod if using Tailing Sidecar Operator diff --git a/tests/helm/go.mod b/tests/helm/go.mod index 23f90ed29a..0daecc1fa2 100644 --- a/tests/helm/go.mod +++ b/tests/helm/go.mod @@ -6,6 +6,7 @@ require ( github.com/bmatcuk/doublestar/v4 v4.4.0 github.com/gruntwork-io/go-commons v0.8.0 github.com/gruntwork-io/terratest v0.41.3 + github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.61.1 github.com/stretchr/testify v1.8.1 gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.25.4 @@ -52,11 +53,11 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/urfave/cli v1.22.2 // indirect golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect - golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/net v0.2.0 // indirect golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect - golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect - golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/sys v0.2.0 // indirect + golang.org/x/term v0.2.0 // indirect + golang.org/x/text v0.4.0 // indirect golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.28.0 // indirect @@ -65,7 +66,7 @@ require ( k8s.io/client-go v0.25.4 // indirect k8s.io/klog/v2 v2.80.1 // indirect k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect - k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect + k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect sigs.k8s.io/yaml v1.3.0 // indirect diff --git a/tests/helm/go.sum b/tests/helm/go.sum index 34f596418b..0d18138000 100644 --- a/tests/helm/go.sum +++ b/tests/helm/go.sum @@ -98,7 +98,6 @@ github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0/go.mod h1:f4zRH github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -256,6 +255,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pquerna/otp v1.2.0 h1:/A3+Jn+cagqayeR3iHs/L62m5ue7710D35zl1zJ1kok= github.com/pquerna/otp v1.2.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg= +github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.61.1 h1:ViIkBYnAUumtx9D7PiVPc1n8kNvwm+WMepDZWTZCBPc= +github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.61.1/go.mod h1:j51242bf6LQwvJ1JPKWApzTnifmCwcQq0i1p29ylWiM= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= @@ -265,7 +266,6 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= @@ -376,8 +376,8 @@ golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLd golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -454,11 +454,11 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -467,8 +467,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -695,13 +695,12 @@ k8s.io/apimachinery v0.25.4 h1:CtXsuaitMESSu339tfhVXhQrPET+EiWnIY1rcurKnAc= k8s.io/apimachinery v0.25.4/go.mod h1:jaF9C/iPNM1FuLl7Zuy5b9v+n35HGSh6AQ4HYRkCqwo= k8s.io/client-go v0.25.4 h1:3RNRDffAkNU56M/a7gUfXaEzdhZlYhoW8dgViGy5fn8= k8s.io/client-go v0.25.4/go.mod h1:8trHCAC83XKY0wsBIpbirZU4NTUpbuhc2JnI7OruGZw= -k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 h1:MQ8BAZPZlWk3S9K4a9NCkIFQtZShWqoha7snGixVgEA= k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1/go.mod h1:C/N6wCaBHeBHkHUesQOQy2/MZqGgMAFPqGsGQLdbZBU= -k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed h1:jAne/RjBTyawwAy0utX5eqigAwz/lQhTmy+Hr/Cpue4= -k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= +k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 h1:GfD9OzL11kvZN5iArC6oTS7RTj7oJOIfnislxYlqTj8= +k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/tests/helm/kube-prometheus-stack/servicemonitors/basic-sumo.input.yaml b/tests/helm/kube-prometheus-stack/servicemonitors/basic-sumo.input.yaml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/helm/kube-prometheus-stack/servicemonitors/basic-sumo.output.yaml b/tests/helm/kube-prometheus-stack/servicemonitors/basic-sumo.output.yaml new file mode 100644 index 0000000000..eb7ec1fd27 --- /dev/null +++ b/tests/helm/kube-prometheus-stack/servicemonitors/basic-sumo.output.yaml @@ -0,0 +1,239 @@ +--- +# Source: sumologic/templates/metrics/prometheus/servicemonitors.yaml +apiVersion: v1 +kind: List +items: + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-fluentd-logs + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: fluentd-logs + spec: + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-logs + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-otelcol-logs + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: otelcol-logs + spec: + endpoints: + - port: otelcol-metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-logs + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-fluentd-metrics + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: fluentd-metrics + spec: + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-metrics + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-otelcol-metrics + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: otelcol-metrics + spec: + endpoints: + - port: otelcol-metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-metrics + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-fluentd-events + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: fluentd-events + spec: + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-events + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-fluent-bit + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: collection-fluent-bit + spec: + endpoints: + - path: /api/v1/metrics/prometheus + port: http + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + app.kubernetes.io/name: fluent-bit + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-otelcol-logs-collector + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: otelcol-logs-collector + spec: + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: otelcol-logs-collector + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-otelcol-traces + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + sumologic.com/app: otelcol + spec: + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/component: traces + sumologic.com/scrape: "true" + - apiVersion: monitoring.coreos.com/v1 + kind: ServiceMonitor + metadata: + name: collection-sumologic-prometheus + namespace: sumologic + labels: + app: sumologic-prometheus + + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + app.kubernetes.io/part-of: sumologic + chart: sumologic-%CURRENT_CHART_VERSION% + release: "RELEASE-NAME" + heritage: "Helm" + spec: + endpoints: + - path: /metrics + port: web + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + operated-prometheus: "true" diff --git a/tests/helm/metadata_metrics_otc/static/basic.output.yaml b/tests/helm/metadata_metrics_otc/static/basic.output.yaml index e91a11b72b..0adc38fbef 100644 --- a/tests/helm/metadata_metrics_otc/static/basic.output.yaml +++ b/tests/helm/metadata_metrics_otc/static/basic.output.yaml @@ -232,6 +232,7 @@ data: "/prometheus.metrics.applications.apache", "/prometheus.metrics.applications.cassandra", "/prometheus.metrics.applications.couchbase", + "/prometheus.metrics.applications.custom", "/prometheus.metrics.applications.elasticsearch", "/prometheus.metrics.applications.haproxy", "/prometheus.metrics.applications.jmx", @@ -249,9 +250,9 @@ data: "/prometheus.metrics.applications.tomcat", "/prometheus.metrics.applications.varnish", "/prometheus.metrics.container", - "/prometheus.metrics.controller-manager", "/prometheus.metrics.control-plane.coredns", "/prometheus.metrics.control-plane.kube-etcd", + "/prometheus.metrics.controller-manager", "/prometheus.metrics.kubelet", "/prometheus.metrics.node", "/prometheus.metrics.operator.rule", diff --git a/tests/helm/prometheus_test.go b/tests/helm/prometheus_test.go new file mode 100644 index 0000000000..be8a7b0ec6 --- /dev/null +++ b/tests/helm/prometheus_test.go @@ -0,0 +1,114 @@ +package helm + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestServiceMonitors(t *testing.T) { + t.Parallel() + allTemplatePaths := []string{ + "templates/metrics/prometheus/servicemonitors.yaml", + "charts/kube-prometheus-stack/templates/prometheus/servicemonitors.yaml", + } + + testCases := []struct { + Name string + ValuesYaml string + ExpectedNames []string + TemplatePaths []string + }{ + { + Name: "additionalServiceMonitor", + ValuesYaml: ` +kube-prometheus-stack: + prometheus: + additionalServiceMonitors: + - name: collection-sumologic-fluentd-logs-test + additionalLabels: + sumologic.com/app: fluentd-logs + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-logs + sumologic.com/scrape: "true" +`, + ExpectedNames: []string{ + "collection-sumologic-fluentd-logs", + "collection-sumologic-otelcol-logs", + "collection-sumologic-fluentd-metrics", + "collection-sumologic-otelcol-metrics", + "collection-sumologic-fluentd-events", + "collection-fluent-bit", + "collection-sumologic-otelcol-logs-collector", + "collection-sumologic-otelcol-traces", + "collection-sumologic-prometheus", + "collection-sumologic-fluentd-logs-test", + }, + TemplatePaths: allTemplatePaths, + }, + { + Name: "onlyAdditionalServiceMonitor", + ValuesYaml: ` +kube-prometheus-stack: + prometheus: + additionalServiceMonitors: + - name: collection-sumologic-fluentd-logs-test + additionalLabels: + sumologic.com/app: fluentd-logs + endpoints: + - port: metrics + namespaceSelector: + matchNames: + - $(NAMESPACE) + selector: + matchLabels: + sumologic.com/app: fluentd-logs + sumologic.com/scrape: "true" +sumologic: + metrics: + serviceMonitors: [] +`, + ExpectedNames: []string{ + "collection-sumologic-fluentd-logs-test", + }, + TemplatePaths: allTemplatePaths, + }, + { + Name: "default", + ValuesYaml: "", + ExpectedNames: []string{ + "collection-sumologic-fluentd-logs", + "collection-sumologic-otelcol-logs", + "collection-sumologic-fluentd-metrics", + "collection-sumologic-otelcol-metrics", + "collection-sumologic-fluentd-events", + "collection-fluent-bit", + "collection-sumologic-otelcol-logs-collector", + "collection-sumologic-otelcol-traces", + "collection-sumologic-prometheus", + }, + TemplatePaths: allTemplatePaths, + }, + } + + for _, tt := range testCases { + t.Run(tt.Name, func(t *testing.T) { + names := []string{} + for _, templatePath := range tt.TemplatePaths { + servicemonitors := GetServiceMonitors(t, tt.ValuesYaml, templatePath) + for _, sm := range servicemonitors { + names = append(names, sm.Name) + } + } + + assert.Equal(t, tt.ExpectedNames, names) + }) + } + +} diff --git a/tests/helm/utils.go b/tests/helm/utils.go index 64b8ef53a6..22be0349ee 100644 --- a/tests/helm/utils.go +++ b/tests/helm/utils.go @@ -14,6 +14,7 @@ import ( "github.com/gruntwork-io/go-commons/files" "github.com/gruntwork-io/terratest/modules/helm" "github.com/gruntwork-io/terratest/modules/logger" + prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" @@ -57,6 +58,19 @@ func GetOtelConfigYaml(t *testing.T, valuesYaml string, templatePath string) str return otelConfigYaml } +// GetServiceMonitors returns serviceMonitors list from the given templatePath +// In case of error it returns empty list +func GetServiceMonitors(t *testing.T, valuesYaml string, templatePath string) []*prometheus.ServiceMonitor { + renderedYamlString, err := RenderTemplateFromValuesStringE(t, valuesYaml, templatePath) + if err != nil { + return []*prometheus.ServiceMonitor{} + } + + var list prometheus.ServiceMonitorList + helm.UnmarshalK8SYaml(t, renderedYamlString, &list) + return list.Items +} + // UnmarshalMultipleFromYaml can unmarshal multiple objects of the same type from a yaml string // containing multiple documents, separated by --- func UnmarshalMultipleFromYaml[T any](t *testing.T, yamlDocs string) []T { @@ -97,13 +111,22 @@ func SplitYaml(yamlDocs string) ([]string, error) { } // RenderTemplateFromValuesString renders a template based on its path and a values string -// it uses package defaults for other parameters +// it uses package defaults for other parameters. This function will fail +// the test if there is an error rendering the template. func RenderTemplateFromValuesString(t *testing.T, valuesYaml string, templatePath string) string { + renderedYamlString, err := RenderTemplateFromValuesStringE(t, valuesYaml, templatePath) + require.NoError(t, err) + return renderedYamlString +} + +// RenderTemplateFromValuesStringE renders a template based on its path and a values string +// it uses package defaults for other parameters. +func RenderTemplateFromValuesStringE(t *testing.T, valuesYaml string, templatePath string) (string, error) { valuesFile, err := os.CreateTemp(t.TempDir(), "values.yaml") require.NoError(t, err) _, err = valuesFile.WriteString(valuesYaml) require.NoError(t, err) - renderedYamlString := RenderTemplate( + return RenderTemplateE( t, &helm.Options{ ValuesFiles: []string{valuesFile.Name()}, @@ -120,7 +143,6 @@ func RenderTemplateFromValuesString(t *testing.T, valuesYaml string, templatePat "--namespace", defaultNamespace, ) - return renderedYamlString } // The functions below are copied from terratest for the sole reason of being able to skip the helm dependency update