Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Using the Kubernetes Client for Go

November 25, 2016
Mike Dame
Related topics:
ContainersDeveloper toolsDevOpsGoKubernetes

    The Kubernetes client package for Go provides developers with a vast range of functions to access data and resources in a cluster. Taking advantage of its capabilities can allow the opportunity to build powerful controllers, monitoring and managing your cluster, beyond the scope of what is offered by stock OpenShift or Kubernetes setups.

    For example, the PodInterface allows you to list, update, delete, or get specific pods either by namespace or across all namespaces. This interface is complemented by similar implementations for many other cluster resource types such as ReplicationControllers and ResourceQuotas.

    // These are the imports used throughout this article
    import (
       "log"
       "time"
    
       "github.com/openshift/origin/pkg/client/cache"
       "github.com/openshift/origin/pkg/cmd/util/clientcmd"
    
       "github.com/spf13/pflag"
       kapi "k8s.io/kubernetes/pkg/api"
       kcache "k8s.io/kubernetes/pkg/client/cache"
       kclient "k8s.io/kubernetes/pkg/client/unversioned"
       ctlresource "k8s.io/kubernetes/pkg/kubectl/resource"
       "k8s.io/kubernetes/pkg/runtime"
       "k8s.io/kubernetes/pkg/watch"
    )
    
    func main() {
       var kubeClient kclient.Interface
       config, err := clientcmd.DefaultClientConfig(pflag.NewFlagSet("empty", pflag.ContinueOnError)).ClientConfig()
       if err != nil {
          log.Printf("Error creating cluster config: %s", err)
       }
    
       kubeClient, err = kclient.New(config)
       podInterface := kubeClient.Pods(namespace)
       podList, err := podInterface.List(kapi.ListOptions{})
    
       if err != nil {
          return err
       }
    }

    And that PodInterface can be used to directly operate on resources in the cluster, such as deleting pods:

    for _, pod := range podList.Items {
       err = podInterface.Delete(pod.Name, &kapi.DeleteOptions{})
    
       if err != nil {
          log.Printf(“Error: %s”, err)
       }
    }

    When combined with a ListWatch from Kubernetes’ cache package, you can easily monitor and handle incoming events in the cluster related to that type of object. To store and process these events at your leisure, Kubernetes provides a DeltaFIFO struct, while OpenShift’s cache package provides an EventQueue struct, which just expands on the use cases of DeltaFIFO when processing object change events.

    podQueue := cache.NewEventQueue(kcache.MetaNamespaceKeyFunc)
    
    podLW := &kcache.ListWatch{
       ListFunc: func(options kapi.ListOptions) (runtime.Object, error) {
          return s.kubeClient.Pods(kapi.NamespaceAll).List(options)
       },
       WatchFunc: func(options kapi.ListOptions) (watch.Interface, error) {
          return s.kubeClient.Pods(kapi.NamespaceAll).Watch(options)
       },
    }
    kcache.NewReflector(podLW, &kapi.Pod{}, podQueue, 0).Run()
    
    go func() {
       for {
          event, pod, err := podQueue.Pop()
          err = handlePod(event, pod.(*kapi.Pod))
          if err != nil {
             log.Fatalf("Error capturing pod event: %s", err)
          }
       }
    }()

    These different event types also let you handle events that create, modify, or delete resources differently:

    func handlePod(eventType watch.EventType, pod *kapi.Pod) {
       switch eventType {
       case watch.Added:
          log.Printf(“Pod %s added!”, pod.Name)
       case watch.Modified:
          log.Printf(“Pod %s modified!”, pod.Name)
       case watch.Deleted:
          log.Printf(“Pod %s deleted!”, pod.Name)
       }
    }

    Putting it all together in an example, say you wanted to restrict a certain namespace from creating new pods during specific hours. The full code could look like this:

    import (
       "log"
       "time"
    
       "github.com/openshift/origin/pkg/client/cache"
       "github.com/openshift/origin/pkg/cmd/util/clientcmd"
    
       "github.com/spf13/pflag"
       kapi "k8s.io/kubernetes/pkg/api"
       kcache "k8s.io/kubernetes/pkg/client/cache"
       kclient "k8s.io/kubernetes/pkg/client/unversioned"
       ctlresource "k8s.io/kubernetes/pkg/kubectl/resource"
       "k8s.io/kubernetes/pkg/runtime"
       "k8s.io/kubernetes/pkg/watch"
    )
    
    func main() {
       var kubeClient kclient.Interface
       config, err := clientcmd.DefaultClientConfig(pflag.NewFlagSet("empty", pflag.ContinueOnError)).ClientConfig()
       if err != nil {
          log.Printf("Error creating cluster config: %s", err)
       }
    
       kubeClient, err = kclient.New(config)
       podQueue := cache.NewEventQueue(kcache.MetaNamespaceKeyFunc)
    
       podLW := &kcache.ListWatch{
          ListFunc: func(options kapi.ListOptions) (runtime.Object, error) {
             return kubeClient.Pods(kapi.NamespaceAll).List(options)
          },
          WatchFunc: func(options kapi.ListOptions) (watch.Interface, error) {
             return kubeClient.Pods(kapi.NamespaceAll).Watch(options)
          },
       }
       kcache.NewReflector(podLW, &kapi.Pod{}, podQueue, 0).Run()
    
       go func() {
          for {
             event, pod, err := podQueue.Pop()
             err = handlePod(event, pod.(*kapi.Pod), kubeClient)
             if err != nil {
                log.Fatalf("Error capturing pod event: %s", err)
             }
          }
       }()
    }
    
    func handlePod(eventType watch.EventType, pod *kapi.Pod, kubeClient kclient.Interface) {
       switch eventType {
       case watch.Added:
          log.Printf(“Pod %s added!”, pod.Name)
          if pod.Namespace == “namespaceWeWantToRestrict” {
             hour := time.Now().Hour()
             if hour >= 5 && hour <= 10 {
                err := kubeClient.Pods(pod.Namespace).Delete(pod.Name, &kapi.DeleteOptions{})
                if err != nil {
                   log.Printf(“Error deleting pod %s: %s”, pod.Name, err)
                }
             }
          }
       case watch.Modified:
          log.Printf(“Pod %s modified!”, pod.Name)
       case watch.Deleted:
          log.Printf(“Pod %s deleted!”, pod.Name)
       }
    }

    Of course, if this project is using a ReplicationController the pod we deleted will just be recreated causing a loop for the next 5 hours, which is undesirable. In that case, you might also want to use a ReplicationControllerInterface to also scale down any RCs in this project:

    if pod.Namespace == “namespaceWeWantToRestrict” {
       hour := time.Now().Hour()
       if hour >= 5 && hour <= 10 {
          rcList, err := kubeClient.ReplicationControllers(pod.Namespace).List(kapi.ListOptions{})
          if err != nil {
             log.Printf(“Error getting RCs in namespace %s: %s”, pod.Namespace, err)
          }
    
          for _, rc := range rcList.Items {
             rc.Spec.Replicas = 0
             _, err := kubeClient.ReplicationControllers(pod.Namespace).Update(rc)
             if err != nil {
                log.Printf(“Error scaling RC %s: %s”, rc.Name, err)
             }
          }
    
          err = kubeClient.Pods(pod.Namespace).Delete(pod.Name, &kapi.DeleteOptions{})
          if err != nil {
             log.Printf(“Error deleting pod %s: %s”, pod.Name, err)
          }
       }
    }

    You may find more practical uses for the Kubernetes client than this, but it’s a good showcase of just how easy it is to interact with your cluster in a small (or maybe large) controller you own. Using a ListWatch allows you to react more dynamically to incoming events, rather than having to try to predict situations you need to control.

    Last updated: September 3, 2019

    Recent Posts

    • Learn to optimize, deploy, and benchmark LLMs with vLLM: A New Free Course

    • Build modular AI pipelines with OpenShift AI and reusable components

    • Kafka Monthly Digest: May 2026

    • UBI 9 and 10 builders on Paketo Buildpacks with multi-arch support

    • Deploy Hermes Agent on OpenShift AI with vLLM model serving

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.