Development Guide

We gathered a set of best practices here to aid development.

Build from sources

In order to build the operator you need to clone the git repository.

  1. git clone https://github.com/apache/flink-kubernetes-operator.git

To build from the command line, it is necessary to have Maven 3 and a Java Development Kit (JDK) installed. Please note that Flink Kubernetes Operator requires Java 11.

To build the project, you can use the following command:

  1. mvn clean install

To speed up the build you can:

  • skip the tests by using ’ -DskipTests’
  • use Maven’s parallel build feature, e.g., ‘mvn package -T 1C’ will attempt to build 1 module for each CPU core in parallel
  1. mvn clean install -DskipTests -T 1C

Local environment setup

We recommend you install Docker Desktop, minikube and helm on your local machine. For the setup please refer to our quickstart.

Building docker images

You can build your own flavor of image as follows via specifying your <repo>:

  1. docker build . -t <repo>/flink-kubernetes-operator:latest
  2. docker push <repo>/flink-kubernetes-operator:latest

If you are using minikube you might want to load the image directly instead of pushing it to a registry:

  1. minikube image load <repo>/flink-kubernetes-operator:latest

You can cut a corner via using the docker daemon of your minikube installation directly as follows:

  1. eval $(minikube docker-env)
  2. DOCKER_BUILDKIT=1 docker build . -t <repo>/flink-kubernetes-operator:latest

When you want to reset your environment to the defaults you can do the following:

  1. eval $(minikube docker-env --unset)

The most useful insight when it comes to minikube that it is just a docker container on your local machine and you can ssh to it with the following command in case you needed to hack something there (like adding a hostpath mount or modifying docker images).

  1. minikube ssh
  2. Last login: Wed Mar 9 10:01:21 2022 from 192.168.49.1
  3. docker@minikube:~$ docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. flink-kubernetes-operator latest cf7856d9ef59 23 hours ago 578MB
  6. docker@minikube:~$ exit

Installing the operator locally

  1. helm install flink-kubernetes-operator helm/flink-kubernetes-operator --set image.repository=<repo>/flink-kubernetes-operator --set image.tag=latest

To uninstall you can simply call:

  1. helm uninstall flink-kubernetes-operator

Running the operator from the IDE

You can run or debug the FlinkOperator from your preferred IDE. The operator itself is accessing the deployed Flink clusters through the REST interface. When running locally the rest.port, rest.address and kubernetes.rest-service.exposed.type Flink configuration parameters must be modified.

When using minikube tunnel the rest service is exposed on localhost:8081

  1. > minikube tunnel
  2. > kubectl get services
  3. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  4. basic-session-example ClusterIP None <none> 6123/TCP,6124/TCP 14h
  5. basic-session-example-rest LoadBalancer 10.96.36.250 127.0.0.1 8081:30572/TCP 14h

The operator picks up the default log and flink configurations from /opt/flink/conf. You can put the rest configuration parameters here:

  1. cat /opt/flink/conf/flink-conf.yaml
  2. rest.port: 8081
  3. rest.address: localhost
  4. kubernetes.rest-service.exposed.type: LoadBalancer

Due to fabric8 conflicts between core Flink and the operator, the flink-kubernetes-operator module depends on the shaded flink-kubernetes-standalone jar which contains the relocated version of the old fabric8 Kubernetes client. Unfortunately IntelliJ is not great at handling dependencies with classifiers so you might get the following error when trying to run FlinkOperator#main:

  1. java.lang.NoSuchMethodError: 'java.lang.Object io.fabric8.kubernetes.client.dsl.ServiceResource.fromServer()'

There are two solutions to this problem, both requires you to first build the project.

First:

  1. mvn clean install

Then either:

  1. Import the flink-kubernetes-operator submodule as a separate IntelliJ project. This will resolve the classifier dependency correctly from your local maven cache.
  2. If you want to keep a single multi-module project, you need to add the flink-kubernetes-standalone-XX-shaded.jar on the classpath manually when running the main method: Edit Run Configuration/Modify Options/Modify Classpath

Generating and Upgrading the CRD

By default, the CRD is generated by the Fabric8 CRDGenerator, when building from source. When installing flink-kubernetes-operator for the first time, the CRD will be applied to the kubernetes cluster automatically. But it will not be removed or upgraded when re-installing the flink-kubernetes-operator, as described in the relevant helm documentation. So if the CRD is changed, you have to delete the CRD resource manually, and re-install the flink-kubernetes-operator.

  1. kubectl delete crd flinkdeployments.flink.apache.org

Mounts

The operator supports to specify the volume mounts. The default mounts to hostPath can be activated by the following command. You can change the default mounts in the helm/flink-kubernetes-operator/values.yaml

  1. helm install flink-operator helm/flink-operator --set operatorVolumeMounts.create=true --set operatorVolumes.create=true

CI/CD

We use GitHub Actions to help you automate your software development workflows in the same place you store code and collaborate on pull requests and issues. You can write individual tasks, called actions, and combine them to create a custom workflow. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.

Considering the cost of running the builds, the stability, and the maintainability, flink-kubernetes-operator chose GitHub Actions and build the whole CI/CD solution on it. All the unit tests, integration tests, and the end-to-end tests will be triggered for each PR.

Note: Please make sure the CI passed before merging.