Add custom annotations to Kubernetes YAMLs

Move2Kube generates Kubernetes YAMLs based on the needs of the application, but there might be situations that might require specific fields to be different in the output. This example illustrates how to add an annotation to the Ingress YAML specifying an ingress class.

  1. Create an empty workspace directory named workspace and make it the current working directory. Assume all commands are executed within this directory.
$ mkdir workspace && cd workspace
  1. Use the enterprise-app as the input for this flow.
$ curl | bash -s -- -d samples/enterprise-app/src -r move2kube-demos
$ ls src		config-utils		customers	docs			frontend		gateway			orders
  1. Run Move2Kube without any customization and the output ingress does not have any annotation. Once done, delete the myproject directory.
$ move2kube transform -s src/ --qa-skip && cat myproject/deploy/yamls/myproject-ingress.yaml && rm -rf myproject
  kind: Ingress
    creationTimestamp: null
    labels: myproject
    name: myproject
  1. Get the Starlark based custom transformer here and copy it into the customizations sub-directory.
$ curl | bash -s -- -d add-custom-kubernetes-annotation -r move2kube-transformers -o customizations
  1. Transform using this customization and specify it using the -c flag.
$ move2kube transform -s src/ -c customizations/ --qa-skip

Once the output is generated, note in the snippet of the ingress file (myproject/deploy/yamls/myproject-ingress.yaml) that there is an annotation for the ingress class added ( haproxy):

$ cat myproject/deploy/yamls/myproject-ingress.yaml
  kind: Ingress
    annotations: haproxy
    creationTimestamp: null
    labels: myproject
    name: myproject

Anatomy of ingress annotator transformer

This custom transformer uses a configuration YAML (ingress-annotator.yaml) and a Starlark script ( to add an annotation to the ingress YAML. The contents of custom transformer are shown below:

$ ls customizations/add-custom-kubernetes-annotation  ingress-annotator.yaml

The configuration YAML specifies that the custom transformer consumes and produces a Kubernetes YAML artifact type as shown in the consumes and produces section.

$ cat customizations/add-custom-kubernetes-annotation/ingress-annotator.yaml
  kind: Transformer
    name: IngressAnnotator
    labels: false
    class: "Starlark"
        merge: false
        # Ensures a artifact of this type gets immediately intercepted by this transformer
        mode: "MandatoryPassThrough"
        disabled: false
      starFile: ""

The code of the Starlark script is shown below. At a high-level, the code requires only the transform() function as it acts upon any Kubernetes YAML generated within Move2Kube. The transform() function loops through every YAML generated for every detected service, checks whether it is an ingress YAML, and if so adds the annotation. The path mappings are meant to persist these changes.

$ cat customizations/add-custom-kubernetes-annotation/
  {% raw %}def transform(new_artifacts, old_artifacts):
      pathMappings = []
      artifacts = []

      for a in new_artifacts:
          yamlsPath = a["paths"]["KubernetesYamls"][0]
          serviceName = a["name"]

          fileList = fs.readdir(yamlsPath)
          yamlsBasePath = yamlsPath.split("/")[-1]
          # Create a custom path template for the service, whose values gets filled and can be used in other pathmappings
          pathTemplateName = serviceName.replace("-", "") + yamlsBasePath
          tplPathData = {'PathTemplateName': pathTemplateName}
          pathMappings.append({'type': 'PathTemplate', \
                              'sourcePath': "{{ OutputRel \"" + yamlsPath + "\" }}", \
                              'templateConfig': tplPathData})
          for f in fileList:
              filePath = fs.pathjoin(yamlsPath, f)
              s =
              yamlData = yaml.loads(s)
              if yamlData['kind'] != 'Ingress':
              if 'annotations' not in yamlData['metadata']:
                  yamlData['metadata']['annotations'] = {'': 'haproxy'}
                  yamlData['metadata']['annotations'][''] = 'haproxy'
              s = yaml.dumps(yamlData)
              fs.write(filePath, s)
              pathMappings.append({'type': 'Default', \
                      'sourcePath': yamlsPath, \
                      'destinationPath': "{{ ." + pathTemplateName + " }}"})

      return {'pathMappings': pathMappings, 'artifacts': artifacts}{% endraw %}

This tutorial can be replicated in the UI by uploading the zip file of the custom transformer as a customization. Obtain the zip of the source and customization by adding a -z to the end of the commands used in steps 2 and 4.

The next step is parameterizing custom fields in Helm Chart, Kustomize, and OC Templates.