How to generate the bundle for non-operator-sdk scalfolded operators
One key way to release the Kubernetes Operator to publish it on OperatorHub.io. To publish the operator on OperatorHub.io, you need to generate the bundle artifacts for your operator. If you develop your Kubernetes Operator with the Operator SDK, you can follow the instruction here to achieve your goal. However, if you develop your Kubernetes Operator, not based on the Operator SDK, how can you generate the bundle artifacts required by OperatorHub.io? This blog will use the project Knative Operator as a reference, to walk you though all the necessary steps.
The ultimate goal is to generate the bundle artifacts as the outputs, including:
- a manifests directory (
bundle/manifests) containing a CSV and all CRDs from config/crds
- a metadata directory (
bundle/metadata), and bundle.Dockerfile have been created in the Operator project.
The main idea is to create a similar directory structure for your project and run the same commands as operator-sdk requires to generate the bundle, so firstly you need to install kustomize and operator-sdk, regardless of how you build your Kubernetes Operator.
Then, manage the YAML files for your operator by using kustomize. Knative Operator saves all the YAML files under the directory /config. In order NOT to disrupt the current way of building the operator, but at the same time supporting the bundle generation by kustomize and operator-sdk. As a reconciliation, we can create and arrange the YAML files based on kustomize, and use the symbolic links to create the files needed for building purposes.
As the directory structure shown as below, the YAML files have been grouped into several categories, like crd file, rbac files, webhook files, etc. All the yaml files directly under /config are files connecting to the back files using the symbolic links.
Finally, you probably would like to automate the bundle generation by using a shell script with all the necessary commands inside. As in Knative Operator, we created a file named generate-bundle.sh to generate all the bundle artifacts. It contains all the commands with the parameters. Each time when we generate the bundle artifacts for a certain release, we just need to run this script with the correct version number, and all the bundle artifacts will be generated. You can add any command in the script to tailor the functionalities based on your own needs, but you cannot skip two basic commands to directly generate the bundle artifacts under the directory of /bundle:
To generate the bundle:
kustomize build config/manifests | operator-sdk generate bundle -q --overwrite --version $VERSION
To validate the bundle
operator-sdk bundle validate ./bundle
Based on what has been described above, the bundle artifacts can be generated for Knative Operator by following this instruction. In order to make it happen, you need to install kustomize and operator-sdk, manage the YAML files with kustomize and reference them by using symbolic links for other purposes, and automate the generation by using a script or other automation tools.
Don’t want to derail? Follow Vincent!