Setting custom tags on dynamically provisioned Volumes

2 min read | by Jordi Prats

Since the version 1.6.0 of the EBS CSI driver it is now possible to define a set of custom tags to add the the volumes.

To do so just need to specify as many tags as we need to set under StorageClass.parameters. We'll need to configure each tag as a key with the prefix tagSpecification with it's value being the tag's key and value with an = sign to separate them. So, for example, the following StorageClass:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: ebs-demo
provisioner: ebs.csi.aws.com
parameters:
  tagSpecification_1: "ExampleTag=ItsValue"
  tagSpecification_2: "AnotherTag=WithAnotherVolume"

It would create volumes with the following tags:

  • Example: ItsValue
  • AnotherTag: WithAnotherVolume

If we enable the --extra-create-metadata flag on the external-provisioner sidecar we can use certain variables for the tags:

  • PVCNamespace: Name of the namespace where the PVC sits
  • PVCName: Name of the PVC object
  • PVName: Name of the PV object

For example, assuming we have the following StorageClass configured:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: ebs-demo
provisioner: ebs.csi.aws.com
parameters:
  tagSpecification_1: "pvc_namespace={{ .PVCNamespace }}"
  tagSpecification_2: "pvc_name={{ .PVCName }}"
  tagSpecification_3: "pv_name={{ .PVName }}"

If we create a PVC named datadir on the pet2cattle namespace the tags on the final EBS volume would be similar to this:

  • pvc_namespace: pet2cattle
  • pvc_name: datadir
  • pv_name: pvc-04bbf306-0939-4cb6-9b09-4e66bafd27b3

We can even step up your game by using functions such as field, substring, toUpper, toLower and contains. For example, we can use the contains functions to set a tag that be set to true or false depending on the name of the namespace, for example:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: ebs-demo
provisioner: ebs.csi.aws.com
parameters:
  tagSpecification_1: 'backup={{ .PVCNamespace | contains "prod" }}'

This would create set the tag backup=true only if the name of the namespace contains the string prod.


Posted on 06/07/2022