====
    Copyright Kroxylicious Authors.

    Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
====

This script was used to produce https://asciinema.org/a/kEG5mTwegb8yf9efwAKKT6iRW.
I used the "Paste Special" feature of iTerm to inject the below text into a terminal session which was being recorded
with asciinema. The "Paste Special" chunking options were tune to give an appearance of a human typing.

# This demo will look at the Kroxylicious Multi-tenant Feature. It is an incubating feature, so it is not
# ready for production use. But please try it and send us some feedback.

# let us imagine that a team needs each developer to have their own Kafka Cluster for development work.
# the team would like to avoid the costs associated with running a cluster per developer.

# we will use Strimzi to deploy a Kafka Cluster to Kubernetes, then use Kroxylicious to expose that single
# Kafka Cluster as if it were many clusters, with each tenant isolated from one and other.

# the kafka cluster and kroxylicious will be deployed to Kubernetes.
# on the client side, kaf (https://github.com/birdayz/kaf) will be used to demonstrate the isolation. It will run
# off-cluster. it will connect to the kroxylicious via a nodeport kubernetes service.

# we'll use a script to install strimzi, create a kafka cluster, and deployment kroxylicious.
# let's do that now

QUAY_ORG=k_wall ./scripts/run-with-strimzi.sh kubernetes-examples/multitenant

# let's look at what we have running in kube.  It's all in the kubernetes namespace kafka.

kubectl config set-context --current --namespace=kafka

# let's start with kafka cluster.  this is the cluster we'll expose using kroxylicious.
kubectl get kafka my-cluster

# notice it's got three brokers (replicas)

kubectl get kafka my-cluster -o yaml

# notice also the bootstrap address is my-cluster-kafka-bootstrap.kafka.svc:9092.
# We'll see the bootstrap address wired in kroxylicious in its configuration.

# Let's see the kroxylicious configuration now.

 kubectl get cm kroxylicious-config -o yaml

# there are a couple of important bits:
# - the filters with the MultiTenant - this enables the MultiTenant feature
# - the two 'virtual' cluster definitions, devenv1 and devenv2
#   - these provide the isolated kafka cluster environment to our developers.
#   - each virtual cluster has its own bootstrap.
# notice that both virtual cluster point to (target) our kafka cluster.

# finally there is a nodeport kubernetes service that is exposing kroxylicious to the host

kubectl get service kroxylicious-service

# let's connect kafka clients up to the virtual clusters and do some
# work to illustrate that the two environments are indeed separate from one and other.
# later, we'll take a peep directly at the kafka cluster so you'll appreciate how naming is used to
# achieve the isolation.


# we are going to use kaf to create a topic called billingapp, and then publish and consume a message to the topic.

# let's configure kaf to know about devenv1.
# notice we are using the bootstrap of the first virtual cluster

kaf config add-cluster devenv1 -b minikube:30192
kaf config select-cluster

# look at the broker topology
kaf nodes
# notice these are brokers presented by the virtual cluster, not the real cluster

# now let's create that topic
kaf topic create billingapp

# confirm the topic exists
kaf topics

# publish a message to it
echo 'hello, world' | kaf produce billingapp

# consume it using a group, and commit the offsets
kaf consume billingapp --group billinggroup --commit

# finally, let's show the group exists
kaf groups

# Now let's switch to devenv2 and demonstrate devenv2 is independent of devenv1

kaf config add-cluster devenv2 -b minikube:30292
kaf config select-cluster

# let's look at the broker topology
kaf nodes
# notice these are brokers presented by the virtual cluster too, again not the real cluster.
# notice too, the port numbers are different from those used by devenv1

# let's check the topics and groups. we expect this environment to have none.
kaf topics
kaf groups

# now let's create a billingapp topic in this environment too.  like before
# we'll produce and consume a message to it, but we'll use a different message.
# like before we'll use the same group name.

# the resources will be independent of those in devenv1 even though they have the
# same names.

# let's create that topic
kaf topic create billingapp

# confirm the topic exists
kaf topics

# publish a message to it, with a message in spanish rather than english
echo 'hola, mundo' | kaf produce billingapp

# consume it using a group
kaf consume billingapp --group billinggroup --commit

# okay, time to look beneath the hood at the kafka cluster.  how's the multi-tenancy
# feature actually wired things up?  Let's look at the topics and groups present on the
# cluster itself.

# the kafka cluster actually isn't available off cluster, so we'll use an ephemeral pod
# to query it using the Apache Kafka tooling.

# first the topics
kubectl -n kafka run listtopics -ti --image=quay.io/strimzi/kafka:0.38.0-kafka-3.6.0 --rm=true --restart=Never -- bin/kafka-topics.sh --bootstrap-server my-cluster-kafka-bootstrap:9092 --list
# notice how the topics names are prefixed with the virtual cluster's name.

# now, the groups
kubectl -n kafka run listgroups -ti --image=quay.io/strimzi/kafka:0.38.0-kafka-3.6.0 --rm=true --restart=Never -- bin/kafka-consumer-groups.sh --bootstrap-server my-cluster-kafka-bootstrap:9092 --list
# notice that the same prefixing has been done.

# let's wrap up.
# what have we seen?  we've seen a kafka cluster of three brokers presented to the world as if it
# were two independent kafka clusters.  The MultiTenant filter dynamically rewrites the resource names to
# create isolation between the tenants.  This will takes place transparently: no extra software on the client or broker
# and no extra configuration, beyond bootstrap.

# Hope you've found the demo interesting.  If you have questions get in touch via https://kroxylicious.slack.com/






