Customize generated Dockerfile and built-in transformer behavior

In this tutorial, Move2Kube will add a custom Dockerfile, and a custom file.

  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 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.
  • If the Dockerfile is generated for the frontend app, it uses as the base image.
  • There are no scripts named in the frontend service directory.
  • The Kubernetes YAMLs are generated in myproject/deploy/yamls directory.
$ move2kube transform -s src/ --qa-skip && ls myproject/source/frontend && cat myproject/source/frontend/Dockerfile && ls myproject/deploy && rm -rf myproject
Dockerfile		dr-surge.js		manifest.yml		package-lock.json	server.js		stories			test-setup.js		webpack.common.js LICENSE		__mocks__		jest.config.js		nodemon.json		package.json		src			stylePaths.js		tsconfig.json

COPY . .
RUN npm install
RUN npm run build
CMD npm run start

cicd			compose			knative			knative-parameterized	yamls			yamls-parameterized

The next steps will:

  • The base image of the Dockerfile generated for Node.js from to
  • Add a new script named in the Node.js app directories along with the Dockerfile in the frontend directory.
  • Change the location of Kubernetes YAMLs from myproject/deploy/yamls to myproject/yamls-elsewhere.
  1. Use a custom configured version of the Node.js built-in transformer and the Kubernetes built-in transformer to achieve this. Copy it into the customizations sub-directory.
$ curl | bash -s -- -d custom-dockerfile-change-built-in-behavior -r move2kube-transformers -o customizations
  1. Transform using this customization and specify the customization using the -c flag.
$ move2kube transform -s src/ -c customizations/ --qa-skip

Once the output is generated, note the following:

  • The Dockerfile generated for the frontend app contains the custom base image.
  • A new file named was generated in the frontend directory.
  • The Kubernetes YAMLs are now generated in myproject/yamls-elsewhere directory and the parameterized YAMLs are also in myproject/yamls-elsewhere-parameterized directory.
$ ls myproject/source/frontend
Dockerfile		dr-surge.js		manifest.yml		package-lock.json	server.js		stylePaths.js		tsconfig.json LICENSE		__mocks__		jest.config.js		nodemon.json		package.json		src			stories			test-setup.js		webpack.common.js
$ cat myproject/source/frontend/Dockerfile
COPY . .
RUN npm install
RUN npm run build
CMD sh
$ ls myproject			deploy				scripts				source				yamls-elsewhere			yamls-elsewhere-parameterized

Anatomy of transformers in custom-dockerfile-change-built-in-behavior

The two customized transformers in the directory are nodejs and kubernetes. The contents of custom-dockerfile-custom-files are shown below:

  $ tree customizations
  └── custom-dockerfile-change-built-in-behavior
      ├── kubernetes
      │   └── kubernetes.yaml
      └── nodejs
          ├── nodejs.yaml
          └── templates
              ├── Dockerfile

To custom configure a built-in transformer, copy the built-in transformer’s configuration directory from the move2kube source, change the configurations, use it as a customization, and make it override the built-in transformer using the override config in the yaml.

In this case, change the Dockerfile template, add a script, and change the transformer configuration YAML.

  1. To change the template, the custom template is placed in customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/Dockerfile. The template is the same as the one used in the built-in transformer, except it is a custom base image and a custom CMD.
{% raw %}$ cat customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/Dockerfile
COPY . .
RUN npm install
{{- if .Build }}
RUN npm run build
{{- end}}
EXPOSE {{ .Port }}
CMD sh{% endraw %}
  1. Add customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/
$ ls customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/
  1. The transformer.yaml is the transformer configuration with two changes compared to the built-in transformer:
  • The custom transformer name is Nodejs-CustomFiles (see name field in the metadata section).
  • Specify an override section which is asking Move2Kube to disable the transformer named Nodejs-Dockerfile if it is present.
$ cat customizations/custom-dockerfile-change-built-in-behavior/nodejs/nodejs.yaml
  kind: Transformer
    name: Nodejs-CustomFiles
    labels: containerization true
    class: "NodejsDockerfileGenerator"
      levels: -1
        merge: false
        disabled: false
        disabled: false
      matchLabels: Nodejs-Dockerfile
      defaultNodejsVersion: "12"
  1. In the kubernetes transformer, change the name and override the config. Also change the default behavior of the transformer, which is to put the Kubernetes yamls in deploy/yamls directory by changing the spec.config.outputPath to yamls-elsewhere.
$ cat customizations/custom-dockerfile-change-built-in-behavior/kubernetes/kubernetes.yaml
  {% raw %}apiVersion:
  kind: Transformer
    name: KubernetesWithCustomOutputDirectory
    labels: true
    class: "Kubernetes"
      levels: 0
        merge: true
        disabled: false
      matchLabels: Kubernetes
      matchLabels: "true"
      outputPath: "yamls-elsewhere"
      ingressName: "{{ .ProjectName }}"{% endraw %}

The next step is adding custom annotations to Kubernetes YAMLs.