How to check the K8s / K3s pod status programmatically with containerd runtime?

8/6/2021

I am wondering whether there is a provision to check the pod status in k8s environment with containerd as runtime programmatically.

I tried looking at https://github.com/containerd/containerd and https://github.com/kubernetes-sigs/cri-tools but both don't seem to have any means to get the precise status of each pod.

Cri-tools / crictl can give whether the pod is ready or not ready ( but that doesn't consider whether the readiness checks are passing behind the scenes are not ).

Below is the code that checks health status of docker container in docker world

Question

  • Is it possible to know the status of each pod running in k8s/k3s in GOlang client libraries like shown below when containerd is runtime?

Any inputs are much appreciated. Thank you.

// Create a docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	filteredContainers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
	if err != nil {
		return false, err
	}
if len(filteredContainers) < 1 {
		return false, errors.New("No other docker containers are up")
}

// inspect each container ..
inspectResult, err := cli.ContainerInspect(ctx, containerID)
	if err != nil {
		return false, err
	} else {
		if inspectResult.State.Health != nil {
			// Check Health Status in available container
			if inspectResult.State.Health.Status == "healthy" {
				return true, nil
			}
		} 
	}
	return false, nil
-- Mozhi
containerd
docker
go
k3s
kubernetes

0 Answers