Skip to content

golang utilities for interacting with the kubernetes API

License

trivago/go-kubernetes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-kubernetes

This library provides a simplified interface on top of the official kubernetes client libraries.

The design focus of this module is usability, not performance or type-safety. We wanted to create a module that is closer to the use of kubectl based scripts.

We use this library for tools that interact with the Kubernetes API, especially when the inspected objects are of varying types.
There's also support for building simple admission webhooks using the Gin framework. This provides an alternative solutions to writing admission controllers that is more lightweight than the official SDK.

Maintenance and PRs

This repository is in active development but is not our main focus.
PRs are welcome, but will take some time to be reviewed.

License

All files in the repository are subject to the Apache 2.0 License

Builds and Releases

All commits to the main branch need to use conventional commits.
Releases will be generated automatically from these commits using Release Please.

Required tools

All required tools can be installed locally via nix and are loaded on demand via direnv.
On MacOS you can install nix via the installer from determinate systems.

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

We provided a justfile to generate the required .envrc file. Run just init-nix to get started, or run the script directly.

Running unit-tests

After you have set up your environment, run unittests via just test or

go test ./...

Running commit checks

We use pre-commit hooks to ensure code quality. These hooks are run on PR creation.
If you encounter issues reported during PR creation, please run the tests locally until the issues are resolved.

You can use just lint to run the pre-commit hooks.
Please note that this command requires [RequiredTools] to be installed

Examples

We provide example code in the cmd directory. This code is not part of the official library.

List all namespaces

func ListAllNamespaces() {
  // Get the kubeconfig file path
  kubeConfigPath := os.Getenv("KUBECONFIG")
  if kubeConfigPath == "" {
      kubeConfigPath = os.ExpandEnv("$HOME/.kube/config")
  }

  // Create a new client
  client, err := kubernetes.NewClient(kubeConfigPath)
  if err != nil {
    log.Fatal(err)
  }

  // List all objects of type "namespace"
  namespaces, err := client.ListAllObjects(kubernetes.ResourceNamespace, "", "")
  if err != nil {
    log.Fatal(err)
  }

  // Print the names
  for _, namespace := range namespaces {
    fmt.Println(namespace.GetName())
  }
}

Contributors 3

  •  
  •  
  •  

Languages