kubectl - get names of pods with specific label - kubernetes

I am trying to get the podname from the pod json using the command which is returning the error
kgp -o jsonpath="{.items[*].metadata[?(#.labels.module=='ddvv-script')].name}"
Error
is not array or slice and cannot be filtered. Printing more information for debugging the template:
template was:
{.items[*].metadata[?(#.labels.module=='ddvv-script')].name}
object given to jsonpath engine was:
Sample file
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": "2020-09-18T17:42:50Z",
"generateName": "ddvv-script-6b784db6bd-",
"labels": {
"app": "my-configs",
"lf.module": "ddvv-script",
"module": "ddvv-script",
"pod-template-hash": "6b784db6bd",
"release": "config"
},
"name": "ddvv-script-6b784db6bd-rjtgh",
What is wrong with this command

You can use below command. It gets podname of pods which has label module=ddvv-script
kubectl get pods --selector=module=ddvv-script --output=jsonpath={.items..metadata.name}

kgp -o jsonpath="{.items[*].metadata[?(#.labels.module=='ddvv-script')].name}"
should be
kgp -o jsonpath="{.items[?(#.metadata.labels.module=='ddvv-script')].metadata.name}"

Related

How to obtain the specified part of the content from the output content in k8s?

When using kubectl get -o yaml/json to obtain resource information, the output content is too detailed, how to obtain the specified part of the content?
[root#ops-harbor ~]# kubectl get -n monitoring prometheus-prome-prometheus-operator-prometheus-0 -ojson
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": "2021-11-02T08:58:33Z",
"generateName": "prometheus-prome-prometheus-operator-prometheus-",
"labels": {
"app": "prometheus",
"controller-revision-hash": "prometheus-prome-prometheus-operator-prometheus-c56894959",
"prometheus": "prome-prometheus-operator-prometheus",
"statefulset.kubernetes.io/pod-name": "prometheus-prome-prometheus-operator-prometheus-0"
},
"name": "prometheus-prome-prometheus-operator-prometheus-0",
"namespace": "monitoring",
"ownerReferences": [
{
"apiVersion": "apps/v1",
"blockOwnerDeletion": true,
"controller": true,
"kind": "StatefulSet",
"name": "prometheus-prome-prometheus-operator-prometheus",
"uid": "3c02e78b-610c-4e9c-9171-cc47b00274a3"
}
],
"resourceVersion": "2640925",
"selfLink": "/api/v1/namespaces/monitoring/pods/prometheus-prome-prometheus-operator-prometheus-0",
"uid": "e728914c-2a3c-4d6a-8a18-5ebec0e0cebd"
},
# ...long long content
For example, I only want to get the following 2 sections of information.
"apiVersion": "v1",
"kind": "Pod",
"ownerReferences": [
{
"apiVersion": "apps/v1",
"blockOwnerDeletion": true,
"controller": true,
"kind": "StatefulSet",
"name": "prometheus-prome-prometheus-operator-prometheus",
"uid": "3c02e78b-610c-4e9c-9171-cc47b00274a3"
}
kubectl get pod -n monitoring prometheus-prome-prometheus-operator-prometheus-0 -o json | jq .metadata.ownerReferences
or
kubectl get pod -n monitoring prometheus-prome-prometheus-operator-prometheus-0 -o jsonpath={.metadata.ownerReferences} | jq

kubectl jsonpath query and output multiple object values

Below is my output of kubectl get deploy --all-namespaces:
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"annotations": {
"downscaler/uptime": "Mon-Fri 07:00-23:59 Australia/Sydney",
"name": "actiontest-v2.0.9",
"namespace": "actiontest",
},
"spec": {
......
......
},
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"annotations": {
"downscaler/uptime": "Mon-Fri 07:00-21:00 Australia/Sydney",
"name": "anotherapp-v0.1.10",
"namespace": "anotherapp",
},
"spec": {
......
......
}
}
I need to find the name of the deployment and its namespace if the annotation "downscaler/uptime" matches the value "Mon-Fri 07:00-21:00 Australia/Sydney". I am expecting an output like below:
deployment_name,namespace
If I am running below query against a single deployment, I get the required output.
#kubectl get deploy -n anotherapp -o jsonpath='{range .[*]}{.items[?(#.metadata.annotations.downscaler/uptime=="Mon-Fri 07:00-21:00 Australia/Sydney")].metadata.name}{","}{.items[?(#.metadata.annotations.downscaler/uptime=="Mon-Fri 07:00-21:00 Australia/Sydney")].metadata.namespace}{"\n"}'
anotherapp-v0.1.10,anotherapp
But when I run it against all namespaces, I am getting an output like below:
#kubectl get deploy --all-namespaces -o jsonpath='{range .[*]}{.items[?(#.metadata.annotations.downscaler/uptime=="Mon-Fri 07:00-21:00 Australia/Sydney")].metadata.name}{","}{.items[?(#.metadata.annotations.downscaler/uptime=="Mon-Fri 07:00-21:00 Australia/Sydney")].metadata.namespace}{"\n"}'
actiontest-v2.0.9 anotherapp-v0.1.10, actiontest anotherapp
This is quite short answer, however you can use this option:
kubectl get deploy --all-namespaces -o jsonpath='{range .items[?(.metadata.annotations.downscaler/uptime=="Mon-Fri 07:00-21:00 Australia/Sydney")]}{.metadata.name}{"\t"}{.metadata.namespace}{"\n"}'
What I changed is logic how to work with data:
First thing what happens is getting into range list of elements we need to work on, not everything. I used filter expression - see Jsonpath notation - syntax elements.
And once we have already filtered entities in the list, we can easily retrieve other fields we need.

how to get objects's metadata name in kubernetes

I can get all the things of a list of objects, such as Secrets and ConfigMaps.
{
"kind": "SecretList",
"apiVersion": "v1",
"metadata": {
"selfLink": "/api/v1/namespaces/kube-system/secrets",
"resourceVersion": "499638"
},
"items": [{
"metadata": {
"name": "aaa",
"namespace": "kube-system",
"selfLink": "/api/v1/namespaces/kube-system/secrets/aaa",
"uid": "96b0fbee-f14c-423d-9734-53fed20ae9f9",
"resourceVersion": "1354",
"creationTimestamp": "2020-02-24T11:20:23Z"
},
"data": "aaa"
}]
}
but I only want the name list, for this example :"aaa". Is there any way?
Yes, you can achieve it by using jsonpath output. Note that the specification you posted will look quite differently once applied. It will create one Secret object in your kube-system namespace and when you run:
$ kubectl get secret -n kube-system aaa -o json
the output will look similar to the following:
{
"apiVersion": "v1",
"kind": "Secret",
"metadata": {
"creationTimestamp": "2020-02-25T11:08:21Z",
"name": "aaa",
"namespace": "kube-system",
"resourceVersion": "34488887",
"selfLink": "/api/v1/namespaces/kube-system/secrets/aaa",
"uid": "229edeb3-57bf-11ea-b366-42010a9c0093"
},
"type": "Opaque"
}
To get only the name of your Secret you need to run:
kubectl get secret aaa -n kube-system -o jsonpath='{.metadata.name}'
i think this should work.
kubectl get secretlist -o=jsonpath="{.items[*].metadata.name]}" | grep -v HEAD | head -n1
check link in the below for more info.
https://kubernetes.io/docs/reference/kubectl/jsonpath/

How can I filter events for the cluster autoscaler in kubernetes?

I see the following event from kubectl get events:
{
"apiVersion": "v1",
"count": 1,
"eventTime": null,
"firstTimestamp": "2019-12-04T19:52:51Z",
"involvedObject": {
"apiVersion": "v1",
"kind": "Pod",
"name": "example-deployment-55f789d54c-tlwnz",
"namespace": "default",
"resourceVersion": "82663",
"uid": "2fdbd034-16cf-11ea-bc4a-42010a800186"
},
"kind": "Event",
"lastTimestamp": "2019-12-04T19:52:51Z",
"message": "Unable to mount volumes for pod \"example-deployment-55f789d54c-tlwnz_default(2fdbd034-16cf-11ea-bc4a-42010a800186)\": timeout expired waiting for volumes to attach or mount for pod \"default\"/\"example-deployment-55f789d54c-tlwnz\". list of unmounted volumes=[nfs-volume]. list of unattached volumes=[nfs-volume default-token-kc7ks]",
"metadata": {
"creationTimestamp": "2019-12-04T19:52:51Z",
"name": "example-deployment-55f789d54c-tlwnz.15dd430deb31e8fd",
"namespace": "default",
"resourceVersion": "1529",
"selfLink": "/api/v1/namespaces/default/events/example-deployment-55f789d54c-tlwnz.15dd430deb31e8fd",
"uid": "a7c80266-16cf-11ea-bc4a-42010a800186"
},
"reason": "FailedMount",
"reportingComponent": "",
"reportingInstance": "",
"source": {
"component": "kubelet",
"host": "gke-test-a2e50ea5b9f1dd9-my-node-pool-5a20b1ac-vk9q"
},
"type": "Warning"
}
....
I've tried filtering by: kubectl get events --all-namespaces -o json --field-selector source.component=cluster-autoscaler but that errors with:
{
"apiVersion": "v1",
"items": [],
"kind": "List",
"metadata": {
"resourceVersion": "",
"selfLink": ""
}
}
Error from server (BadRequest): Unable to find "/v1, Resource=events" that match label selector "", field selector "source.component=cluster-autoscaler": field label not supported: source.component
How can I filter this?
Can be done using jq (though it does not return a JSON array - but individual JSON objects seperated by newlines):
kubectl get events --all-namespaces -o json | jq '.items[]|select(.source.component=="cluster-autoscaler")'

Kubeedge on Kataocoda - no matches for kind "Node" in version "v1"

I am following Kubeedge v1.0.0 deployment on Katacoda and on executing the following command.
kubectl apply -f $GOPATH/src/github.com/kubeedge/kubeedge/build/node.json -s <kubedge-node-ip-address>:8080
It gives me an error
error: unable to recognize "/root/kubeedge/src/github.com/kubeedge/kubeedge/build/node.json": no matches for kind "Node" in version "v1"
Tried searching for this error but found no relevant answers. Anyone has idea on to get through this?
Below is the content of my node.json file
{
"kind": "Node",
"apiVersion": "v1",
"metadata": {
"name": "edge-node-1",
"labels": {
"name": "edge-node",
"node-role.kubernetes.io/edge": ""
}
}
}
I have reproduced it in Katakoda and in my case it works perfectly. I recommend you to go through the tutorial once again and take each step carefully.
You need to pay attention for step 7. Change metadata.name to the name of the edge node:
vim $GOPATH/src/github.com/kubeedge/kubeedge/build/node.json
{
"kind": "Node",
"apiVersion": "v1",
"metadata": {
"name": "edge-node",
"labels": {
"name": "edge-node",
"node-role.kubernetes.io/edge": ""
}
}
}
Then, execute following command, where you need to change IP address:
kubectl apply -f $GOPATH/src/github.com/kubeedge/kubeedge/build/node.json -s <kubedge-node-ip-address>:8080
Another command to check if a correct API version was used, is:
kubectl explain node -s <kubedge-node-ip-address>:8080
After successful creation of node you should see:
node/edge-node created