Customizing generated Dockerfile and behavior of built-in transformer
Big picture
In this example, we look at how to make Move2Kube add custom Dockerfile and a custom file.
- Let us start by creating an empty workspace directory say
workspace
and make it the current working directory. We will assume all commands are executed within this directory.$ mkdir workspace && cd workspace
- Lets use the enterprise-app as input for this flow.
$ curl https://move2kube.konveyor.io/scripts/download.sh | bash -s -- -d samples/enterprise-app/src -r move2kube-demos $ ls src README.md config-utils customers docs frontend gateway orders
- Let’s first run Move2Kube without any customization.
- If we notice the
Dockerfile
generated for thefrontend
app, it usesregistry.access.redhat.com/ubi8/nodejs-14
as base image - There are no scripts named
start-nodejs.sh
in thefrontend
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 __mocks__ jest.config.js package-lock.json src test-setup.js webpack.dev.js LICENSE dist manifest.yml package.json stories tsconfig.json webpack.prod.js README.md dr-surge.js nodemon.json server.js stylePaths.js webpack.common.js FROM registry.access.redhat.com/ubi8/nodejs-14 COPY . . RUN npm install RUN npm run build USER root RUN chown -R 1001:0 /opt/app-root/src/.npm RUN chmod -R 775 /opt/app-root/src/.npm USER 1001 EXPOSE 8080 CMD npm run start cicd compose knative knative-parameterized yamls yamls-parameterized
- If we notice the
Let’s say, we want to change
- The base image of the Dockerfile generated for nodejs from
registry.access.redhat.com/ubi8/nodejs-14
toregistry.access.redhat.com/ubi8/nodejs-14-minimal
- Add a new script named
start-nodejs.sh
in the nodejs app directories along with the Dockerfile, in our casefrontend
directory - Change the location of Kubernetes yamls from
myproject/deploy/yamls
tomyproject/yamls-elsewhere
- We will use a custom configured version of the nodejs built-in transformer and kubernetes built-in transformer to achieve this. We copy it into the
customizations
sub-directory.$ curl https://move2kube.konveyor.io/scripts/download.sh | bash -s -- -d custom-dockerfile-change-built-in-behavior -r move2kube-transformers -o customizations
- Now lets transform with this customization. Specify the customization using the
-c
flag.$ move2kube transform -s src/ -c customizations/ --qa-skip
Once the output is generated, we can observe
- The Dockerfile generated for the
frontend
app contains the custom base image - A new file named
start-nodejs.sh
is generated in thefrontend
directory - The kubernetes yamls are now generated in
myproject/yamls-elsewhere
directory, and hence the parameterized yamls are also inmyproject/yamls-elsewhere-parameterized
directory.$ ls myproject/source/frontend Dockerfile __mocks__ jest.config.js package-lock.json src stylePaths.js webpack.common.js LICENSE dist manifest.yml package.json start-nodejs.sh test-setup.js webpack.dev.js README.md dr-surge.js nodemon.json server.js stories tsconfig.json webpack.prod.js $ cat myproject/source/frontend/Dockerfile FROM registry.access.redhat.com/ubi8/nodejs-14-minimal COPY . . RUN npm install RUN npm run build EXPOSE 8080 CMD sh start-nodejs.sh $ ls myproject Readme.md 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 as shown below:
$ tree customizations
customizations
└── custom-dockerfile-change-built-in-behavior
├── kubernetes
│ └── transformer.yaml
└── nodejs
├── mappings
│ └── nodeversions.yaml
├── templates
│ ├── Dockerfile
│ └── start-nodejs.sh
└── transformer.yaml
To custom configure an built-in transformer, you can copy the built-in transformer’s configuration directory from move2kube
source, change the configurations and use it as a customization. You can make it override the built-in transformer using the override
config in the yaml.
In this case, we change the Dockerfile template, add a script and change the transformer configuration yaml.
- To change the template, we have added our custom template 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 that we are using a custom base image and a customCMD
here.$ cat customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/Dockerfile FROM registry.access.redhat.com/ubi8/nodejs-14-minimal COPY . . RUN npm install {{- if .Build }} RUN npm run build {{- end}} EXPOSE {{ .Port }} CMD sh start-nodejs.sh
- Add
customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/start-nodejs.sh
.$ ls customizations/custom-dockerfile-change-built-in-behavior/nodejs/templates/ Dockerfile start-nodejs.sh
- The
transformer.yaml
is the transformer configuration. We have two changes here compared to the built-in transformer:- The name of our custom transformer is
Nodejs-CustomFiles
(seename
field in themetadata
section). - We are also specifying an
override
section which is asking Move2Kube to disable the transformer namedNodejs-Dockerfile
if this transformer is present.$ cat customizations/custom-dockerfile-change-built-in-behavior/nodejs/transformer.yaml
apiVersion: move2kube.konveyor.io/v1alpha1 kind: Transformer metadata: name: Nodejs-CustomFiles labels: move2kube.konveyor.io/task: containerization move2kube.konveyor.io/built-in: false spec: class: "NodejsDockerfileGenerator" directoryDetect: levels: -1 consumes: Service: merge: false produces: Dockerfile: disabled: false DockerfileForService: disabled: false override: matchLabels: move2kube.konveyor.io/name: Nodejs-Dockerfile
- The name of our custom transformer is
- In the
kubernetes
transformer, we change the name and override config too. But in addition, we change the default behavior of the transformer, which is to put the Kubernetes yamls indeploy/yamls
directory by changing thespec.config.outputPath
toyamls-elsewhere
.$ cat customizations/custom-dockerfile-change-built-in-behavior/kubernetes/transformer.yaml
apiVersion: move2kube.konveyor.io/v1alpha1 kind: Transformer metadata: name: KubernetesWithCustomOutputDirectory labels: move2kube.konveyor.io/built-in: false spec: class: "Kubernetes" directoryDetect: levels: 0 consumes: IR: merge: true produces: KubernetesYamls: disabled: false override: matchLabels: move2kube.konveyor.io/name: Kubernetes dependency: matchLabels: move2kube.konveyor.io/kubernetesclusterselector: "true" config: outputPath: "yamls-elsewhere" ingressName: "{{ .ProjectName }}"
Next step Adding custom annotations to Kubernetes YAMLs