028 Project 28: Kubernetes Rollout Checker
028 Build a Kubernetes Rollout Checker
Check if a Deployment is fully rolled out.
Full main.go
package main
import (
"context"
"flag"
"fmt"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
name := flag.String("name", "", "deployment name")
ns := flag.String("n", "default", "namespace")
flag.Parse()
if *name == "" {
fmt.Println("need -name")
os.Exit(2)
}
cfg, err := clientcmd.BuildConfigFromFlags("", os.Getenv("HOME")+"/.kube/config")
if err != nil {
panic(err)
}
cli, err := kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
}
d, err := cli.AppsV1().Deployments(*ns).Get(context.Background(), *name, metav1.GetOptions{})
if err != nil {
panic(err)
}
desired := int32(1)
if d.Spec.Replicas != nil {
desired = *d.Spec.Replicas
}
ready := d.Status.ReadyReplicas
updated := d.Status.UpdatedReplicas
fmt.Printf("deployment=%s/%s desired=%d updated=%d ready=%d\n", *ns, *name, desired, updated, ready)
if ready == desired && updated == desired {
fmt.Println("rollout: healthy")
os.Exit(0)
}
fmt.Println("rollout: pending")
os.Exit(1)
}Step-by-Step Explanation
- Build Kubernetes client config from kubeconfig.
- Scope operations by namespace or resource type.
- Query or watch API objects.
- Extract actionable status fields.
- Return output and exit codes suitable for scripts and CI.
Code Anatomy
- Setup phase creates client and context.
- Query/watch phase pulls cluster state.
- Presentation phase prints concise operational results.
Learning Goals
- Use
client-gosafely and predictably. - Convert API objects into operator-facing insights.
- Build automation-friendly cluster checks.