# Running tests for Kafka interaction

The source code for the operator wraps the kafka client that we use (github.com/segmentio/kafka-go) with one abstraction layer that can be found in the package github.com/ecida/runtime-controller/pkg/kafka. This package handles all interaction with the Kafka cluster. Since it is pretty difficult to test whether this connection works while running from within the operator, there are tests that use this abstraction layer separately.

# Tests in pkg/kafka

The tests in the pkg/kafka_test package connect to Kubernetes, query the services, and find the IP address of the service that is a load balancer, and has a name that matches *-bootstrap within the kafka namespace. This is likely the kafka cluster entrypoint that the operator will connect to.

To get this to work on your own development machine you will need to have a Kafka cluster that epxoses its brokers on a kubernetes load balancer. This also means that you need to have load balancers available on your Kubernetes cluster. On minikube, these loadbalancers come built in and can be activated by running:

minikube tunnel

This command may require root privilege. It essentially creates routes in your routing table so that you can connect to an IP range that minikube uses for load balancers. After running this command, you will see that any services that require a load balancer will obtain an "external IP". While this command is running you can connect to these IP addresses locally.

To create a kafka cluster that exposes its brokers on a load balancer, you will need to kubectl apply -n kafka -f kafkaresources/kafka-persistent-single.yaml in the operator repository. The relevant section of this CR is the following:

    listeners:
      - name: plain
        port: 9092
        type: loadbalancer
        tls: false

Applying that resource will create a kafka cluster that is reachable on a loadbalancer.

# Running the tests

Code in this package can be run by executing the following command from the root of the repository:

go test ./pkg/kafka

Note that output will be omitted in case a test succeeds, and only output of failing testcases will be printed.

# The cmd/kafkatest package

This package is meant as scratch paper, and just used for running functions against the kafka cluster. The code is extremely dirty and not meant for general use.