// Module included in the following assemblies:
//
// assembly-resource-status-access.adoc

[id='con-custom-resources-info-{context}']
= Performing `kubectl` operations on custom resources

[role="_abstract"]
You can use `kubectl` commands to retrieve information and perform other operations on Strimzi custom resources.
Use `kubectl` commands, such as `get`, `describe`, `edit`, or `delete`, to perform operations on resource types.
For example, `kubectl get kafkatopics` retrieves a list of all Kafka topics and `kubectl get kafkas` retrieves all deployed Kafka clusters.

When referencing resource types, you can use both singular and plural names:
`kubectl get kafkas` gets the same results as `kubectl get kafka`.

You can also use the _short name_ of the resource.
Learning short names can save you time when managing Strimzi.
The short name for `Kafka` is `k`, so you can also run `kubectl get k` to list all Kafka clusters.

.Listing Kafka clusters
[source,shell]
----
kubectl get k

NAME         READY   METADATA STATE   WARNINGS
my-cluster   True    KRaft
----

.Long and short names for each Strimzi resource
[cols="3*",options="header",stripes="none"]
|===

m|Strimzi resource      |Long name          |Short name

| Kafka                 | kafka             | k
| Kafka Node Pool       | kafkanodepool     | knp
| Kafka Topic           | kafkatopic        | kt
| Kafka User            | kafkauser         | ku
| Kafka Connect         | kafkaconnect      | kc
| Kafka Connector       | kafkaconnector    | kctr
| Kafka MirrorMaker 2   | kafkamirrormaker2 | kmm2
| Kafka Bridge          | kafkabridge       | kb
| Kafka Rebalance       | kafkarebalance    | kr
| Strimzi Pod Set       | strimzipodset     | sps

|===

== Resource categories

Categories of custom resources can also be used in `kubectl` commands.

All Strimzi custom resources belong to the category `strimzi`, so you can use `strimzi` to get all the Strimzi resources with one command.

For example, running `kubectl get strimzi` lists all Strimzi custom resources in a given namespace.

.Listing all custom resources
[source,shell]
----
kubectl get strimzi

NAME                                                   PODS   READY PODS   CURRENT PODS   AGE
strimzipodset.core.strimzi.io/my-cluster-brokers       3      3            3              6h11m
strimzipodset.core.strimzi.io/my-cluster-controllers   3      3            3              6h11m

NAME                                         DESIRED REPLICAS   ROLES            NODEIDS
kafkanodepool.kafka.strimzi.io/brokers       3                  ["broker"]       [3,4,5]
kafkanodepool.kafka.strimzi.io/controllers   3                  ["controller"]   [0,1,2]

NAME                                READY   METADATA STATE   WARNINGS
kafka.kafka.strimzi.io/my-cluster   True    KRaft

NAME                                   PARTITIONS REPLICATION FACTOR
kafkatopic.kafka.strimzi.io/kafka-apps 3          3

NAME                                   AUTHENTICATION AUTHORIZATION
kafkauser.kafka.strimzi.io/my-user     tls            simple
----

The `kubectl get strimzi -o name` command returns all resource types and resource names.
The `-o name` option fetches the output in the _type/name_ format

.Listing all resource types and names
[source,shell]
----
kubectl get strimzi -o name

strimzipodset.core.strimzi.io/my-cluster-brokers
strimzipodset.core.strimzi.io/my-cluster-controllers
kafkanodepool.kafka.strimzi.io/brokers
kafkanodepool.kafka.strimzi.io/controllers
kafka.kafka.strimzi.io/my-cluster
kafkatopic.kafka.strimzi.io/kafka-apps
kafkauser.kafka.strimzi.io/my-user
----

You can combine this `strimzi` command with other commands.
For example, you can pass it into a `kubectl delete` command to delete all resources in a single command.

.Deleting all custom resources
[source,shell]
----
kubectl delete $(kubectl get strimzi -o name)

strimzipodset.core.strimzi.io "my-cluster-brokers" deleted
strimzipodset.core.strimzi.io "my-cluster-controllers" deleted
kafkanodepool.kafka.strimzi.io "brokers" deleted
kafkanodepool.kafka.strimzi.io "controllers" deleted
kafka.kafka.strimzi.io "my-cluster" deleted
kafkatopic.kafka.strimzi.io "kafka-apps" deleted
kafkauser.kafka.strimzi.io "my-user" deleted
----

Deleting all resources in a single operation might be useful, for example,
when you are testing new Strimzi features.

== Querying the status of sub-resources

There are other values you can pass to the `-o` option.
For example, by using `-o yaml` you get the output in YAML format.
Using `-o json` will return it as JSON.

You can see all the options in `kubectl get --help`.

One of the most useful options is the {K8SJsonPath}, which allows you to pass JSONPath expressions to query the Kubernetes API.
A JSONPath expression can extract or navigate specific parts of any resource.

For example, you can use the JSONPath expression `{.status.listeners[?(@.name=="tls")].bootstrapServers}`
to get the bootstrap address from the status of the Kafka custom resource and use it in your Kafka clients.

Here, the command retrieves the `bootstrapServers` value of the listener named `tls`:

.Retrieving the bootstrap address
[source,shell]
----
kubectl get kafka my-cluster -o=jsonpath='{.status.listeners[?(@.name=="tls")].bootstrapServers}{"\n"}'

my-cluster-kafka-bootstrap.myproject.svc:9093
----

By changing the name condition you can also get the address of the other Kafka listeners.

You can use `jsonpath` to extract any other property or group of properties from any custom resource.
