Skip to content

FairwindsOps/controller-utils

Repository files navigation

controller-utils

FairwindsOps Apache 2.0 license

This is a library of Go functions to assist in building Kubernetes Controllers.

The pkg/controller package contains the main functionality. pkg/log contains helpers around logging. You can pass in a logr object to control the logs of this library.

Basic Usage

package main

import (
	"context"
	"fmt"

	"k8s.io/apimachinery/pkg/api/meta"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/restmapper"
	ctrl "sigs.k8s.io/controller-runtime"

	"github.com/fairwindsops/controller-utils/pkg/controller"
)

func main() {
	dynamic, restMapper, err := getKubeClient()
	if err != nil {
		panic(err)
	}
	client := controller.Client{
		Context:    context.TODO(),
		Dynamic:    dynamic,
		RESTMapper: restMapper,
	}
	workloads, err := client.GetAllTopControllersSummary("")
	if err != nil {
		panic(err)
	}
	for _, workload := range workloads {
		ctrl := workload.TopController
		fmt.Println()
		fmt.Printf("Workload: %s/%s/%s\n", ctrl.GetKind(), ctrl.GetNamespace(), ctrl.GetName())
		fmt.Printf("  num pods: %d\n", workload.PodCount)
		fmt.Printf("  running: %d\n", workload.RunningPodCount)
		fmt.Printf("  podSpec: %#v\n", workload.PodSpec)
	}
}

func getKubeClient() (dynamic.Interface, meta.RESTMapper, error) {
	var restMapper meta.RESTMapper
	var dynamicClient dynamic.Interface
	kubeConf, configError := ctrl.GetConfig()
	if configError != nil {
		return dynamicClient, restMapper, configError
	}

	api, err := kubernetes.NewForConfig(kubeConf)
	if err != nil {
		return dynamicClient, restMapper, err
	}

	dynamicClient, err = dynamic.NewForConfig(kubeConf)
	if err != nil {
		return dynamicClient, restMapper, err
	}

	resources, err := restmapper.GetAPIGroupResources(api.Discovery())
	if err != nil {
		return dynamicClient, restMapper, err
	}
	restMapper = restmapper.NewDiscoveryRESTMapper(resources)
	return dynamicClient, restMapper, nil
}

Join the Fairwinds Open Source Community

The goal of the Fairwinds Community is to exchange ideas, influence the open source roadmap, and network with fellow Kubernetes users. Chat with us on Slack join the user group to get involved!

Love Fairwinds Open Source? Share your business email and job title and we'll send you a free Fairwinds t-shirt!

Other Projects from Fairwinds

Enjoying controller-utils? Check out some of our other projects:

  • Polaris - Audit, enforce, and build policies for Kubernetes resources, including over 20 built-in checks for best practices
  • Goldilocks - Right-size your Kubernetes Deployments by compare your memory and CPU settings against actual usage
  • Pluto - Detect Kubernetes resources that have been deprecated or removed in future versions
  • Nova - Check to see if any of your Helm charts have updates available
  • rbac-manager - Simplify the management of RBAC in your Kubernetes clusters