Arun A
Arun A

Reputation: 655

Is there a REST api to read image names of all deployments in a given namespace in a GKE cluster

Is there a GKE (Google Kubernetes Engine) REST API that gives the same results as the output of the command

kubectl get deployment -o=json --namespace dev --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.template.spec.containers[*].image"

Also if I want to read the data from a java program where do I start?

Upvotes: 0

Views: 132

Answers (1)

Fcmam5
Fcmam5

Reputation: 6832

According to Kubernetes REST API documentation, there are no query parameters do filter for fields: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#deployment-v1-apps

However, you can still use the endpoint /apis/apps/v1/namespaces/{namespace}/deployments

And then use jq to filter for the images in your deployments, or if you are consuming this REST API from an application, I expect that you can read the response and then only filter for the fields you need.

I tried the first approach using jq from my machine for:

k get deployments -n mhajeb -o wide

NAME      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES    SELECTOR
busybox   0/1     1            0           18m   busybox      busybox   app=busybox
nginx     1/1     1            1           18m   nginx        nginx     app=nginx

With this call + jq filters

curl -s http://127.0.0.1:53350/apis/apps/v1/namespaces/mhajeb/deployments | jq -r '.items[].spec.template.spec.containers[].image'

And I got

busybox
nginx

The full response from k8s API (without applying jq projection) was:

{
  "kind": "DeploymentList",
  "apiVersion": "apps/v1",
  "metadata": {
    "resourceVersion": "1026"
  },
  "items": [
    {
      "metadata": {
        "name": "busybox",
        "namespace": "mhajeb",
        "uid": "647a2ea5-e631-4ec7-b77b-c79757acd1d3",
        "resourceVersion": "730",
        "generation": 1,
        "creationTimestamp": "2023-01-11T01:22:22Z",
        "labels": {
          "app": "busybox"
        },
        "annotations": {
          "deployment.kubernetes.io/revision": "1"
        },
        "managedFields": [
          {
            "manager": "kube-controller-manager",
            "operation": "Update",
            "apiVersion": "apps/v1",
            "time": "2023-01-11T01:22:22Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:annotations": {
                  ".": {},
                  "f:deployment.kubernetes.io/revision": {}
                }
              },
              "f:status": {
                "f:conditions": {
                  ".": {},
                  "k:{\"type\":\"Available\"}": {
                    ".": {},
                    "f:lastTransitionTime": {},
                    "f:lastUpdateTime": {},
                    "f:message": {},
                    "f:reason": {},
                    "f:status": {},
                    "f:type": {}
                  },
                  "k:{\"type\":\"Progressing\"}": {
                    ".": {},
                    "f:lastTransitionTime": {},
                    "f:lastUpdateTime": {},
                    "f:message": {},
                    "f:reason": {},
                    "f:status": {},
                    "f:type": {}
                  }
                },
                "f:observedGeneration": {},
                "f:replicas": {},
                "f:unavailableReplicas": {},
                "f:updatedReplicas": {}
              }
            },
            "subresource": "status"
          },
          {
            "manager": "kubectl-create",
            "operation": "Update",
            "apiVersion": "apps/v1",
            "time": "2023-01-11T01:22:22Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:labels": {
                  ".": {},
                  "f:app": {}
                }
              },
              "f:spec": {
                "f:progressDeadlineSeconds": {},
                "f:replicas": {},
                "f:revisionHistoryLimit": {},
                "f:selector": {},
                "f:strategy": {
                  "f:rollingUpdate": {
                    ".": {},
                    "f:maxSurge": {},
                    "f:maxUnavailable": {}
                  },
                  "f:type": {}
                },
                "f:template": {
                  "f:metadata": {
                    "f:labels": {
                      ".": {},
                      "f:app": {}
                    }
                  },
                  "f:spec": {
                    "f:containers": {
                      "k:{\"name\":\"busybox\"}": {
                        ".": {},
                        "f:image": {},
                        "f:imagePullPolicy": {},
                        "f:name": {},
                        "f:resources": {},
                        "f:terminationMessagePath": {},
                        "f:terminationMessagePolicy": {}
                      }
                    },
                    "f:dnsPolicy": {},
                    "f:restartPolicy": {},
                    "f:schedulerName": {},
                    "f:securityContext": {},
                    "f:terminationGracePeriodSeconds": {}
                  }
                }
              }
            }
          }
        ]
      },
      "spec": {
        "replicas": 1,
        "selector": {
          "matchLabels": {
            "app": "busybox"
          }
        },
        "template": {
          "metadata": {
            "creationTimestamp": null,
            "labels": {
              "app": "busybox"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "busybox",
                "image": "busybox",
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "terminationMessagePolicy": "File",
                "imagePullPolicy": "Always"
              }
            ],
            "restartPolicy": "Always",
            "terminationGracePeriodSeconds": 30,
            "dnsPolicy": "ClusterFirst",
            "securityContext": {},
            "schedulerName": "default-scheduler"
          }
        },
        "strategy": {
          "type": "RollingUpdate",
          "rollingUpdate": {
            "maxUnavailable": "25%",
            "maxSurge": "25%"
          }
        },
        "revisionHistoryLimit": 10,
        "progressDeadlineSeconds": 600
      },
      "status": {
        "observedGeneration": 1,
        "replicas": 1,
        "updatedReplicas": 1,
        "unavailableReplicas": 1,
        "conditions": [
          {
            "type": "Available",
            "status": "False",
            "lastUpdateTime": "2023-01-11T01:22:22Z",
            "lastTransitionTime": "2023-01-11T01:22:22Z",
            "reason": "MinimumReplicasUnavailable",
            "message": "Deployment does not have minimum availability."
          },
          {
            "type": "Progressing",
            "status": "True",
            "lastUpdateTime": "2023-01-11T01:22:22Z",
            "lastTransitionTime": "2023-01-11T01:22:22Z",
            "reason": "ReplicaSetUpdated",
            "message": "ReplicaSet \"busybox-65c78966cc\" is progressing."
          }
        ]
      }
    },
    {
      "metadata": {
        "name": "nginx",
        "namespace": "mhajeb",
        "uid": "d0bfb22d-2d54-42ce-8d8c-ba2fe3a87ebb",
        "resourceVersion": "781",
        "generation": 1,
        "creationTimestamp": "2023-01-11T01:22:40Z",
        "labels": {
          "app": "nginx"
        },
        "annotations": {
          "deployment.kubernetes.io/revision": "1"
        },
        "managedFields": [
          {
            "manager": "kubectl-create",
            "operation": "Update",
            "apiVersion": "apps/v1",
            "time": "2023-01-11T01:22:40Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:labels": {
                  ".": {},
                  "f:app": {}
                }
              },
              "f:spec": {
                "f:progressDeadlineSeconds": {},
                "f:replicas": {},
                "f:revisionHistoryLimit": {},
                "f:selector": {},
                "f:strategy": {
                  "f:rollingUpdate": {
                    ".": {},
                    "f:maxSurge": {},
                    "f:maxUnavailable": {}
                  },
                  "f:type": {}
                },
                "f:template": {
                  "f:metadata": {
                    "f:labels": {
                      ".": {},
                      "f:app": {}
                    }
                  },
                  "f:spec": {
                    "f:containers": {
                      "k:{\"name\":\"nginx\"}": {
                        ".": {},
                        "f:image": {},
                        "f:imagePullPolicy": {},
                        "f:name": {},
                        "f:resources": {},
                        "f:terminationMessagePath": {},
                        "f:terminationMessagePolicy": {}
                      }
                    },
                    "f:dnsPolicy": {},
                    "f:restartPolicy": {},
                    "f:schedulerName": {},
                    "f:securityContext": {},
                    "f:terminationGracePeriodSeconds": {}
                  }
                }
              }
            }
          },
          {
            "manager": "kube-controller-manager",
            "operation": "Update",
            "apiVersion": "apps/v1",
            "time": "2023-01-11T01:22:44Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:annotations": {
                  ".": {},
                  "f:deployment.kubernetes.io/revision": {}
                }
              },
              "f:status": {
                "f:availableReplicas": {},
                "f:conditions": {
                  ".": {},
                  "k:{\"type\":\"Available\"}": {
                    ".": {},
                    "f:lastTransitionTime": {},
                    "f:lastUpdateTime": {},
                    "f:message": {},
                    "f:reason": {},
                    "f:status": {},
                    "f:type": {}
                  },
                  "k:{\"type\":\"Progressing\"}": {
                    ".": {},
                    "f:lastTransitionTime": {},
                    "f:lastUpdateTime": {},
                    "f:message": {},
                    "f:reason": {},
                    "f:status": {},
                    "f:type": {}
                  }
                },
                "f:observedGeneration": {},
                "f:readyReplicas": {},
                "f:replicas": {},
                "f:updatedReplicas": {}
              }
            },
            "subresource": "status"
          }
        ]
      },
      "spec": {
        "replicas": 1,
        "selector": {
          "matchLabels": {
            "app": "nginx"
          }
        },
        "template": {
          "metadata": {
            "creationTimestamp": null,
            "labels": {
              "app": "nginx"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "nginx",
                "image": "nginx",
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "terminationMessagePolicy": "File",
                "imagePullPolicy": "Always"
              }
            ],
            "restartPolicy": "Always",
            "terminationGracePeriodSeconds": 30,
            "dnsPolicy": "ClusterFirst",
            "securityContext": {},
            "schedulerName": "default-scheduler"
          }
        },
        "strategy": {
          "type": "RollingUpdate",
          "rollingUpdate": {
            "maxUnavailable": "25%",
            "maxSurge": "25%"
          }
        },
        "revisionHistoryLimit": 10,
        "progressDeadlineSeconds": 600
      },
      "status": {
        "observedGeneration": 1,
        "replicas": 1,
        "updatedReplicas": 1,
        "readyReplicas": 1,
        "availableReplicas": 1,
        "conditions": [
          {
            "type": "Available",
            "status": "True",
            "lastUpdateTime": "2023-01-11T01:22:44Z",
            "lastTransitionTime": "2023-01-11T01:22:44Z",
            "reason": "MinimumReplicasAvailable",
            "message": "Deployment has minimum availability."
          },
          {
            "type": "Progressing",
            "status": "True",
            "lastUpdateTime": "2023-01-11T01:22:44Z",
            "lastTransitionTime": "2023-01-11T01:22:40Z",
            "reason": "NewReplicaSetAvailable",
            "message": "ReplicaSet \"nginx-76d6c9b8c\" has successfully progressed."
          }
        ]
      }
    }
  ]
}

For your second question: It's not clear, what do you mean by "read data from a java program"

Upvotes: 1

Related Questions