I have a demo golang program to list Pods without a particular label. I want to modify it so it also can add a label to each pod.
(I'm using the AWS hosted Kubernetes service, EKS so there's some boilerplate code specific to EKS )
package main
import (
"fmt"
eksauth "github.com/chankh/eksutil/pkg/auth"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main() {
cfg := &eksauth.ClusterConfig{ClusterName: "my_cluster_name"}
clientset, _ := eksauth.NewAuthClient(cfg)
api := clientset.CoreV1()
// Get all pods from all namespaces without the "sent_alert_emailed" label.
pods, _ := api.Pods("").List(metav1.ListOptions{LabelSelector: "!sent_alert_emailed"})
for i, pod := range pods.Items {
fmt.Println(fmt.Sprintf("[%2d] %s, Phase: %s, Created: %s, HostIP: %s", i, pod.GetName(), string(pod.Status.Phase), pod.GetCreationTimestamp(), string(pod.Status.HostIP)))
// Here I want to add a label to this pod
// e.g. something like:
// pod.addLabel("sent_alert_emailed=true")
}
}
I know kubectl can be used to add labels, e.g.
kubectl label pod my-pod new-label=awesome # Add a Label
kubectl label pod my-pod new-label=awesomer --overwrite # Change a existing label
I was hoping there would be an equivalent method via the go-client?