Skip to content

Resource Query Cheat Sheet

Joel Baxter edited this page Jun 27, 2021 · 2 revisions

This page contains examples of CLI queries that are useful when working with KubeDirector-related resources.

Tools

You'll need to have kubectl installed and be familiar with using it. The "kubectl cheat sheet" will be helpful.

The jq utility is also quite handy for processing and formatting JSON output, and it will be used in the more complex examples below. If you're not familiar with jq yet, there are a bunch of cheat sheets for jq out there, plus the examples below should give you a good idea of how you are likely going to use it in this context.

Custom Resource Definitions

The schemas of the custom resources are defined here on the wiki:

When looking at those pages, be aware that some resource properties are valid only for certain versions of KubeDirector. In a table of fields, properties supported by later KD versions will be in lower table rows. Divider lines that say "properties supported by KD <some verson number>+ below" indicate where these transitions occur.

If you have the necessary privileges on your K8s cluster -- and these will usually require administrative privileges, not "user confined to some namespaces" privileges -- you can also examine the custom resource definitions currently installed for the cluster:

kubectl get crd | grep kubedirector

You should see the following three CRD names: kubedirectorapps.kubedirector.hpe.com, kubedirectorclusters.kubedirector.hpe.com, and kubedirectorconfigs.kubedirector.hpe.com.

To see the spec supported for a particular custom resource, you can use this command, with the appropriate CRD name replacing <crd-name>:

kubectl get crd <crd-name> -o json | jq '.spec.versions[0].schema.openAPIV3Schema.properties.spec.properties'

Custom Resources - Basic

The following command will list all KubeDirectorApp resources in the current working namespace (select some other namespace with the kubectl -n option if necessary):

kubectl get kdapp

And the following command will list all KubeDirectorCluster resources in the current working namespace:

kubectl get kdcluster

As with any "kubectl get" command, you can also specify a resource name to identify one particular resource, and add a kubectl output flag like -o yaml or -o json to print resource content. For example to dump a kdcluster named <kdcluster-name> in YAML format, use:

kubectl get kdcluster <kdcluster-name> -o yaml

Custom Resources - Advanced

The jq utility comes in handy when you are only interested in some of the resource content. For example, you can print only the status stanza of a kdcluster (in JSON format) with this command:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status'

You can also print only some portion of the status stanza, with commands like these:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status.state'
kubectl get kdcluster <kdcluster-name> -o json | jq '.status.memberStateRollup'
kubectl get kdcluster <kdcluster-name> -o json | jq '.status.roles[]'

The select operation in jq allows you to pick out portions of the output based on property values. Some useful examples below.

Print the member statuses only for members in role <role-id>:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status.roles[] |
    select(.id=="<role-id>")'

(It's not necessary to have the select on a new line; we're just doing that here for display purposes.)

Print the member statuses only for members that are not in "configured" state:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status.roles[].members[] |
    select(.state!="configured")'

Print only the state and pod name for members that are not in "configured" state:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status.roles[].members[] |
    select(.state!="configured") |
    .state + ": " + .pod'

(Note that when choosing "state" values for the select, the possible values are: "create pending", "creating", "delete pending", "deleting", "config error", or "configured".)

Print the member statuses only for members where the container is not in "running" state:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status.roles[].members[] |
    select(.stateDetail.lastKnownContainerState!="running")'

Print only the container state and pod name for members where the container is not in "running" state:

kubectl get kdcluster <kdcluster-name> -o json | jq '.status.roles[].members[] |
    select(.stateDetail.lastKnownContainerState!="running") |
    .stateDetail.lastKnownContainerState + ": " + .pod'

(Note that when choosing "lastKnownContainerState" values for the select, the possible values are: "running", "unresponsive", "initializing", "waiting", "terminated", "absent", or unknown".)

Native Resources

If you want to find resources that are being used to implement a particular kdcluster named <kdcluster-name>, you can do a query that selects resources based on their labels.

kubectl get <resource-type> --selector='kubedirector.hpe.com/kdcluster=<kdcluster-name>'

<resource-type> here may be pod, svc, statefulset, or pvc.

You can further limit your query to show only the resources involved in the role <role-id>:

kubectl get <resource-type> \
    --selector='kubedirector.hpe.com/kdcluster=<kdcluster-name>,kubedirector.hpe.com/role=<role-id>'

If for some reason you'd rather find resources in any kdcluster that is an instance of the kdapp named <kdapp-name>, that's possible too:

kubectl get <resource-type> --selector='kubedirector.hpe.com/kdapp=<kdapp-name>'