Resource Definitions

Driver

Capability

Flavor

Resource Type

Annotations

This section shows how to use the Template Driver for managing annotations on Kubernetes objects.

While it is also possible to set annotations via Score , the approach shown here shifts the management of annotations down to the Platform, ensuring consistency and relieving developers of the task to repeat common annotations for each Workload in the Score extension file.

The example illustrates an annotation on the Kubernetes Service object (specific to Google Kubernetes Engine in this case). But if you want to see a more generic approach with annotations on Workloads too, you can follow the same approach as described in the example with labels .

Resource Definitions


annotations.yaml ( view on GitHub ) :

# This Resource Definition shows how to add annotations to the Kubernetes service object using the Template Driver
apiVersion: entity.humanitec.io/v1b1

kind: Definition
metadata:
  id: annotations
entity:
  name: annotations
  type: workload
  driver_type: humanitec/template
  driver_inputs:
    values:
      templates:
        outputs: |
          update:
            - op: add
              path: /spec/deployment/annotations
              value:
                {{- range $key, $val := .resource.spec.deployment.annotations }}
                {{ $key }}: {{ $val | quote }}
                {{- end }}
            - op: add
              path: /spec/pod/annotations
              value:
                {{- range $key, $val := .resource.spec.pod.annotations }}
                {{ $key }}: {{ $val | quote }}
                {{- end }}
            # If the Score file also defines a service, add annotations to the service object
            {{- if .resource.spec.service }}
            - op: add
              path: /spec/service/annotations
              value:
                {{- range $key, $val := .resource.spec.service.annotations }}
                {{ $key }}: {{ $val | quote }}
                {{- end }}
            {{- $port := values .resource.spec.service.ports | first }}

            - op: add
              path: /spec/service/annotations/cloud.google.com~1neg
              value: '{"ingress":true,"exposed_ports":{ {{- $port.service_port | quote -}} :{}}}'
            {{- end }}
  criteria:
    - {}


annotations.tf ( view on GitHub ) :

resource "humanitec_resource_definition" "annotations" {
  driver_type = "humanitec/template"
  id          = "annotations"
  name        = "annotations"
  type        = "workload"
  driver_inputs = {
    values_string = jsonencode({
      "templates" = {
        "outputs" = <<END_OF_TEXT
update:
  - op: add
    path: /spec/deployment/annotations
    value:
      {{- range $key, $val := .resource.spec.deployment.annotations }}
      {{ $key }}: {{ $val | quote }}
      {{- end }}
  - op: add
    path: /spec/pod/annotations
    value:
      {{- range $key, $val := .resource.spec.pod.annotations }}
      {{ $key }}: {{ $val | quote }}
      {{- end }}
  # If the Score file also defines a service, add annotations to the service object
  {{- if .resource.spec.service }}
  - op: add
    path: /spec/service/annotations
    value:
      {{- range $key, $val := .resource.spec.service.annotations }}
      {{ $key }}: {{ $val | quote }}
      {{- end }}
  {{- $port := values .resource.spec.service.ports | first }}

  - op: add
    path: /spec/service/annotations/cloud.google.com~1neg
    value: '{"ingress":true,"exposed_ports":{ {{- $port.service_port | quote -}} :{}}}'
  {{- end }}
END_OF_TEXT
      }
    })
  }
}

resource "humanitec_resource_definition_criteria" "annotations_criteria_0" {
  resource_definition_id = resource.humanitec_resource_definition.annotations.id

}

Top