Labels
This section shows how to use the Template Driver for managing labels on Kubernetes objects.
While it is also possible to set labels via Score , the approach shown here shifts the management of labels down to the Platform, ensuring consistency and relieving developers of the task to repeat common labels for each Workload in the Score extension file.
config-labels.yaml
: Resource Definition of typeconfig
which defines the value for a sample label at a central place.custom-workload-with-dynamic-labels.yaml
: Add dynamic labels to your Workload. This format is for use with the Humanitec CLI .custom-namespace-with-dynamic-labels.yaml
: Add dynamic labels to your Namespace. This format is for use with the Humanitec CLI .
Resource Definitions
config-labels.yaml
(
view on GitHub
)
:
# This "config" type Resource Definition provides the value for the sample label
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
id: app-config
entity:
name: app-config
type: config
driver_type: humanitec/template
driver_inputs:
values:
templates:
# Returns a sample output named "cost_center_id" to be used as a label
outputs: |
cost_center_id: my-example-id
# Match the resource ID "app-config" so that it can be requested via that ID
criteria:
- res_id: app-config
custom-namespace-with-dynamic-labels.yaml
(
view on GitHub
)
:
# This Resource Definition references the "config" resource to use its output as a label
# and adds another label taken from the Deployment context
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
id: custom-namespace-with-label
entity:
name: custom-namespace-with-label
type: k8s-namespace
driver_type: humanitec/template
driver_inputs:
values:
templates:
init: |
name: ${context.app.id}-${context.env.id}
manifests: |
namespace.yaml:
location: cluster
data:
apiVersion: v1
kind: Namespace
metadata:
labels:
env_id: ${context.env.id}
cost_center_id: ${resources['config.default#app-config'].outputs.cost_center_id}
name: {{ .init.name }}
outputs: |
namespace: {{ .init.name }}
# Set matching criteria as required
criteria:
- {}
custom-workload-with-dynamic-labels.yaml
(
view on GitHub
)
:
# This Resource Definition references the "config" resource to use its output as a label
# and adds another label taken from the Deployment context
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
id: custom-workload-with-label
entity:
name: custom-workload-with-label
type: workload
driver_type: humanitec/template
driver_inputs:
values:
templates:
outputs: |
update:
- op: add
path: /spec/labels
value:
{{- range $key, $val := .resource.spec.labels }}
{{ $key }}: {{ $val | quote }}
{{- end }}
env_id: ${context.env.id}
cost_center_id: ${resources['config.default#app-config'].outputs.cost_center_id}
# If the Score file also defines a service, add labels to the service object
{{- if .resource.spec.service }}
- op: add
path: /spec/service/labels
value:
{{- range $key, $val := .resource.spec.service.labels }}
{{ $key }}: {{ $val | quote }}
{{- end }}
env_id: ${context.env.id}
cost_center_id: ${resources['config.default#app-config'].outputs.cost_center_id}
{{- end }}
# Set matching criteria as required
criteria:
- {}
config-labels.tf
(
view on GitHub
)
:
resource "humanitec_resource_definition" "app-config" {
driver_type = "humanitec/template"
id = "app-config"
name = "app-config"
type = "config"
driver_inputs = {
values_string = jsonencode({
"templates" = {
"outputs" = "cost_center_id: my-example-id\n"
}
})
}
}
resource "humanitec_resource_definition_criteria" "app-config_criteria_0" {
resource_definition_id = resource.humanitec_resource_definition.app-config.id
res_id = "app-config"
}
custom-namespace-with-dynamic-labels.tf
(
view on GitHub
)
:
resource "humanitec_resource_definition" "custom-namespace-with-label" {
driver_type = "humanitec/template"
id = "custom-namespace-with-label"
name = "custom-namespace-with-label"
type = "k8s-namespace"
driver_inputs = {
values_string = jsonencode({
"templates" = {
"init" = "name: $${context.app.id}-$${context.env.id}\n"
"manifests" = <<END_OF_TEXT
namespace.yaml:
location: cluster
data:
apiVersion: v1
kind: Namespace
metadata:
labels:
env_id: $${context.env.id}
cost_center_id: $${resources['config.default#app-config'].outputs.cost_center_id}
name: {{ .init.name }}
END_OF_TEXT
"outputs" = "namespace: {{ .init.name }}\n"
}
})
}
}
resource "humanitec_resource_definition_criteria" "custom-namespace-with-label_criteria_0" {
resource_definition_id = resource.humanitec_resource_definition.custom-namespace-with-label.id
}
custom-workload-with-dynamic-labels.tf
(
view on GitHub
)
:
resource "humanitec_resource_definition" "custom-workload-with-label" {
driver_type = "humanitec/template"
id = "custom-workload-with-label"
name = "custom-workload-with-label"
type = "workload"
driver_inputs = {
values_string = jsonencode({
"templates" = {
"outputs" = <<END_OF_TEXT
update:
- op: add
path: /spec/labels
value:
{{- range $key, $val := .resource.spec.labels }}
{{ $key }}: {{ $val | quote }}
{{- end }}
env_id: $${context.env.id}
cost_center_id: $${resources['config.default#app-config'].outputs.cost_center_id}
# If the Score file also defines a service, add labels to the service object
{{- if .resource.spec.service }}
- op: add
path: /spec/service/labels
value:
{{- range $key, $val := .resource.spec.service.labels }}
{{ $key }}: {{ $val | quote }}
{{- end }}
env_id: $${context.env.id}
cost_center_id: $${resources['config.default#app-config'].outputs.cost_center_id}
{{- end }}
END_OF_TEXT
}
})
}
}
resource "humanitec_resource_definition_criteria" "custom-workload-with-label_criteria_0" {
resource_definition_id = resource.humanitec_resource_definition.custom-workload-with-label.id
}