Resource Definitions
Driver
Capability
Flavor
Resource Type
Servicemonitor
This section contains an example Resource Definition using the Template Driver for provisioning Prometheus ServiceMonitor for your Workloads.
The solution consists of one workload
Resource Definitions injecting its associated ServiceMonitor
manifest if a service
is defined in the Score file.
Note that the port on the generated ServiceMonitor
will be taken by the first port
of the service
’s ports
. Default port value to 8080
. You can adapt these parts to meet with your own requirements.
Resource Definitions #
servicemonitor-workload-def.yaml
(
view on GitHub
)
:
# This Resource Definition adds a Prometheus ServiceMonitor to a Workload
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
id: servicemonitor-workload
entity:
driver_type: humanitec/template
name: servicemonitor-workload
type: workload
driver_inputs:
values:
namespace: ${resources['k8s-namespace#k8s-namespace'].outputs.namespace}
templates:
init: |
workload: {{ .resource.id }}
{{- if gt (.resource.spec | dig "service" "ports" (list) | len) 0 }}
servicePort: {{ get (.resource.spec.service.ports | values | first ) "service_port" }}
{{- else }}
servicePort: 8080
{{- end }}
manifests: |
{{- if .resource.spec.service }}
service-monitor.yaml:
location: namespace
data:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ .init.workload }}-svc-monitor
spec:
endpoints:
- interval: 30s
targetPort: {{ .init.servicePort }}
path: /metrics
namespaceSelector:
matchNames:
- {{ .driver.values.namespace }}
selector:
matchLabels:
humanitec.io/workload: {{ .init.workload }}
{{- end }}
servicemonitor-workload-def.tf
(
view on GitHub
)
:
resource "humanitec_resource_definition" "servicemonitor-workload" {
driver_type = "humanitec/template"
id = "servicemonitor-workload"
name = "servicemonitor-workload"
type = "workload"
driver_inputs = {
values_string = jsonencode({
"namespace" = "$${resources['k8s-namespace#k8s-namespace'].outputs.namespace}"
"templates" = {
"init" = <<END_OF_TEXT
workload: {{ .resource.id }}
{{- if gt (.resource.spec | dig "service" "ports" (list) | len) 0 }}
servicePort: {{ get (.resource.spec.service.ports | values | first ) "service_port" }}
{{- else }}
servicePort: 8080
{{- end }}
END_OF_TEXT
"manifests" = <<END_OF_TEXT
{{- if .resource.spec.service }}
service-monitor.yaml:
location: namespace
data:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ .init.workload }}-svc-monitor
spec:
endpoints:
- interval: 30s
targetPort: {{ .init.servicePort }}
path: /metrics
namespaceSelector:
matchNames:
- {{ .driver.values.namespace }}
selector:
matchLabels:
humanitec.io/workload: {{ .init.workload }}
{{- end }}
END_OF_TEXT
}
})
}
}