Labeling and tagging resources

Tags and labels are a way to organize and categorize your resources. This page explains how to add tags and labels to the Konnect entities that Kong Operator manages. Each entity supports tags, labels, or both.

Labeling

Labels are key-value pairs you can attach to Konnect control planes and data plane nodes. You can set labels in the following ways:

Labels must meet the Konnect label requirements. Control plane labels have the following additional restrictions:

  • Keys can’t start with k8s.
  • Kong Operator only checks the length of values, so make sure values also follow the Konnect requirements.

To add labels to a KonnectGatewayControlPlane object, set the labels field in the spec.createControlPlaneRequest section.

echo '
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha2
metadata:
  name: gateway-control-plane
  namespace: default
spec:
  createControlPlaneRequest:
    labels: # Arbitrary key-value pairs
      environment: production
      team: devops
    name: gateway-control-plane
  konnect:
    authRef:
      name: konnect-api-auth # Reference to the KonnectAPIAuthConfiguration object
  ' | kubectl apply -f -

You can verify the KonnectGatewayControlPlane was reconciled successfully by checking its Programmed condition.

kubectl get -n kong konnectgatewaycontrolplane gateway-control-plane \
  -o=jsonpath='{.status.conditions[?(@.type=="Programmed")]}' | jq

The output should look similar to this:

{
  "observedGeneration": 1,
  "reason": "Programmed",
  "status": "True",
  "type": "Programmed"
}

At this point, labels should be visible in the Konnect UI.

Labels on hybrid Gateways

When Kong Operator provisions a hybrid Gateway, you can set Konnect labels with the konghq.com/konnect-cp-labels and konghq.com/konnect-dp-labels annotations on the Gateway or GatewayClass resource. For an example, see Set Konnect labels on control planes and data planes.

Kong Operator writes the labels to the resources that it generates for the Gateway:

  • konghq.com/konnect-cp-labels: spec.createControlPlaneRequest.labels of the KonnectGatewayControlPlane
  • konghq.com/konnect-dp-labels: spec.konnect.dataPlane.labels of the KonnectExtension

Kong Operator merges the labels on a Gateway with the labels from its GatewayClass. When both set the same key, the Gateway value takes precedence.

Labels set through these annotations have the following limitations:

  • Each annotation can contain at most 5 labels on the Gateway and at most 5 labels on the GatewayClass. The merged result for each annotation can’t exceed 5 labels either. For example, if the GatewayClass sets 3 control plane labels and the Gateway sets 3 other control plane labels, the merged result has 6 labels, so Kong Operator rejects it.
  • Each label must contain exactly one =, and the key can’t be empty. Because , separates labels and = separates the key from the value, keys and values can’t contain these characters.
  • Kong Operator removes spaces around keys and values.
  • The konghq.com/konnect-cp-labels annotation only applies to control planes that Kong Operator creates. Kong Operator ignores it when the GatewayConfiguration mirrors an existing control plane.

Control plane labels can’t use the managed-by key, because Kong Operator sets it on the control plane that it creates for the Gateway. For the other label requirements, see Labeling.

If an annotation is invalid, Kong Operator doesn’t apply any labels from that annotation and sets the Programmed condition of the Gateway to False, with the reason InvalidKonnectLabels.

Tagging

Tags are values that you can attach to objects. All the Kong Konnect entities that can be attached to a KonnectGatewayControlPlane object support tagging. You can add tags to those entities by specifying the tags field in their spec section.

For example, to add tags to a KongService object, you can apply the following YAML manifest:

echo '
kind: KongService
apiVersion: configuration.konghq.com/v1alpha1
metadata:
  name: service
  namespace: default
spec:
  tags: # Arbitrary list of strings
    - production
    - devops
  name: service
  host: example.com
  controlPlaneRef:
    type: konnectNamespacedRef
    konnectNamespacedRef:
      name: gateway-control-plane # Reference to the KonnectGatewayControlPlane object
  ' | kubectl apply -f -

You can verify the KongService was reconciled successfully by checking its Programmed condition.

kubectl get -n kong kongservice service \
  -o=jsonpath='{.status.conditions[?(@.type=="Programmed")]}' | jq

The output should look similar to this:

{
  "observedGeneration": 1,
  "reason": "Programmed",
  "status": "True",
  "type": "Programmed"
}

At this point, tags should be visible in the Konnect UI.

konghq.com/tags annotation

Alternatively you can use the konghq.com/tags annotation to add tags to any Kong Konnect entity that supports tagging.

The value of this annotation is treated as comma-separated list of tags.

For example, to add tags to a KongService object using the konghq.com/tags annotation, you can apply the following YAML manifest:

echo '
kind: KongService
apiVersion: configuration.konghq.com/v1alpha1
metadata:
  name: service
  namespace: default
  annotations:
    konghq.com/tags: "production,devops" # Arbitrary list of strings as comma-separated values
spec:
  name: service
  host: example.com
  controlPlaneRef:
    type: konnectNamespacedRef
    konnectNamespacedRef:
      name: gateway-control-plane # Reference to the KonnectGatewayControlPlane object
  ' | kubectl apply -f -

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!