Janshair Khan
Janshair Khan

Reputation: 2687

Kubernetes `client-go` - How to get container status in a pod

After following this and this, how do I watch containers status (If a container crashed, completed etc) in a Pod and trigger events when a container status changes in a Pod?

Let's say I have a Pod with 2 containers:

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  containers:
  - image: busybox
    name: busybox5
    command:
      - sleep
      - "5"
    imagePullPolicy: IfNotPresent
  - image: busybox
    name: busybox50
    command:
      - sleep
      - "50"
    imagePullPolicy: IfNotPresent
  restartPolicy: Never

I want to get notified when the busybox5 container finishes executions not about busybox50. I have done something like below using informers:

        UpdateFunc: func(oldObj, obj interface{}) {
            mObj := obj.(v1.Object)
            log.Printf("%s: Updated", mObj.GetName())
        },

This is simple. But how it works in a multi container Pod? What if I want to handle events about busybox5 container only in the Pod. How can I achieve this in Go?

Upvotes: 1

Views: 1502

Answers (1)

kergeodeta
kergeodeta

Reputation: 106

I think you need the client-go informers. Here's a good tutorial about them: https://firehydrant.io/blog/stay-informed-with-kubernetes-informers/

You can create an asynchronous event listener for the Pod in which your containers are running and then when one of a container status is change then the pod status is change also (updated, so you should listen to update events). So you got the update event from your pod, after all you need get the Pod cointainers.

I hope you looking for this :)

Upvotes: 1

Related Questions