Categorygithub.com/ericchiang/k8s
modulepackage
0.4.0
Repository: https://github.com/ericchiang/k8s.git
Documentation: pkg.go.dev

# README

A simple Go client for Kubernetes

GoDoc

A slimmed down Go client generated using Kubernetes' new protocol buffer support. This package behaves similarly to official Kubernetes' Go client, but only imports two external dependencies.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ericchiang/k8s"
)

func main() {
    client, err := k8s.NewInClusterClient()
    if err != nil {
        log.Fatal(err)
    }

    nodes, err := client.CoreV1().ListNodes(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    for _, node := range nodes.Items {
        fmt.Printf("name=%q schedulable=%t\n", *node.Metadata.Name, !*node.Spec.Unschedulable)
    }
}

Requirements

Versioned supported

This client supports every API group version present since 1.3.

Usage

Namespace

When performing a list or watch operation, the namespace to list or watch in is provided as an argument.

pods, err := core.ListPods(ctx, "custom-namespace") // Pods from the "custom-namespace"

A special value AllNamespaces indicates that the list or watch should be performed on all cluster resources.

pods, err := core.ListPods(ctx, k8s.AllNamespaces) // Pods in all namespaces.

Both in-cluster and out-of-cluster clients are initialized with a primary namespace. This is the recommended value to use when listing or watching.

client, err := k8s.NewInClusterClient()
if err != nil {
    // handle error
}

// List pods in the namespace the client is running in.
pods, err := client.CoreV1().ListPods(ctx, client.Namespace)

Label selectors

Label selectors can be provided to any list operation.

l := new(k8s.LabelSelector)
l.Eq("tier", "production")
l.In("app", "database", "frontend")

pods, err := client.CoreV1().ListPods(ctx, client.Namespace, l.Selector())

Working with resources

Use the generated API types directly to create and modify resources.

import (
    "context"

    "github.com/ericchiang/k8s"
    "github.com/ericchiang/k8s/api/v1"
    metav1 "github.com/ericchiang/k8s/apis/meta/v1"
)

func createConfigMap(client *k8s.Client, name string, values map[string]string) error {
    cm := &v1.ConfigMap{
        Metadata: &metav1.ObjectMeta{
            Name:      &name,
            Namespace: &client.Namespace,
        },
        Data: values,
    }
    // Will return the created configmap as well.
    _, err := client.CoreV1().CreateConfigMap(context.TODO(), cm)
    return err
}

API structs use pointers to int, bool, and string types to differentiate between the zero value and an unsupplied one. This package provides convenience methods for creating pointers to literals of basic types.

Creating out-of-cluster clients

Out-of-cluster clients can be constructed by either creating an http.Client manually or parsing a Config object. The following is an example of creating a client from a kubeconfig:

import (
    "io/ioutil"

    "github.com/ericchiang/k8s"

    "github.com/ghodss/yaml"
)

// loadClient parses a kubeconfig from a file and returns a Kubernetes
// client. It does not support extensions or client auth providers.
func loadClient(kubeconfigPath string) (*k8s.Client, error) {
    data, err := ioutil.ReadFile(kubeconfigPath)
    if err != nil {
        return nil, fmt.Errorf("read kubeconfig: %v", err)
    }

    // Unmarshal YAML into a Kubernetes config object.
    var config k8s.Config
    if err := yaml.Unmarshal(data, &config); err != nil {
        return nil, fmt.Errorf("unmarshal kubeconfig: %v", err)
    }
    return k8s.NewClient(&config)
}

Errors

Errors returned by the Kubernetes API are formatted as unversioned.Status objects and surfaced by clients as *k8s.APIErrors. Programs that need to inspect error codes or failure details can use a type cast to access this information.

// createConfigMap creates a configmap in the client's default namespace
// but does not return an error if a configmap of the same name already
// exists.
func createConfigMap(client *k8s.Client, name string, values map[string]string) error {
    cm := &v1.ConfigMap{
        Metadata: &metav1.ObjectMeta{
            Name:      &name,
            Namespace: &client.Namespace,
        },
        Data: values,
    }

    _, err := client.CoreV1().CreateConfigMap(context.TODO(), cm)

    // If an HTTP error was returned by the API server, it will be of type
    // *k8s.APIError. This can be used to inspect the status code.
    if apiErr, ok := err.(*k8s.APIError); ok {
        // Resource already exists. Carry on.
        if apiErr.Code == http.StatusConflict {
            return nil
        }
    }
    return fmt.Errorf("create configmap: %v", err)
}

# Packages

No description provided by the author
No description provided by the author
Package runtime is a generated protocol buffer package.
No description provided by the author
No description provided by the author

# Functions

Bool is a convinence for converting a bool literal to a pointer to a bool.
Int is a convinence for converting an int literal to a pointer to an int.
NewClient initializes a client from a client config.
NewInClusterClient returns a client that uses the service account bearer token mounted into Kubernetes pods.
ResourceVersion causes watch operations to only show changes since a particular version of a resource.
String returns a pointer to a string.
Timeout declares the timeout for list and watch operations.

# Constants

AllNamespaces is given to list and watch operations to signify that the code should list or watch resources in all namespaces.
Types for watch events.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

APIError is an error from a unexpected status code.
AppsV1Alpha1 is a client for interacting with the apps/v1alpha1 API group.
No description provided by the author
AppsV1Beta1 is a client for interacting with the apps/v1beta1 API group.
No description provided by the author
No description provided by the author
AuthenticationV1 is a client for interacting with the authentication.k8s.io/v1 API group.
AuthenticationV1Beta1 is a client for interacting with the authentication.k8s.io/v1beta1 API group.
AuthInfo contains information that describes identity information.
AuthorizationV1 is a client for interacting with the authorization.k8s.io/v1 API group.
AuthorizationV1Beta1 is a client for interacting with the authorization.k8s.io/v1beta1 API group.
AuthProviderConfig holds the configuration for a specified auth provider.
AutoscalingV1 is a client for interacting with the autoscaling/v1 API group.
No description provided by the author
AutoscalingV2Alpha1 is a client for interacting with the autoscaling/v2alpha1 API group.
No description provided by the author
BatchV1 is a client for interacting with the batch/v1 API group.
No description provided by the author
BatchV2Alpha1 is a client for interacting with the batch/v2alpha1 API group.
No description provided by the author
CertificatesV1Alpha1 is a client for interacting with the certificates.k8s.io/v1alpha1 API group.
No description provided by the author
CertificatesV1Beta1 is a client for interacting with the certificates.k8s.io/v1beta1 API group.
No description provided by the author
Client is a Kuberntes client.
Cluster contains information about how to communicate with a kubernetes cluster.
Config holds the information needed to build connect to remote kubernetes clusters as a given user.
Context is a tuple of references to a cluster (how do I communicate with a kubernetes cluster), a user (how do I identify myself), and a namespace (what subset of resources do I want to work with).
CoreV1 is a client for interacting with the /v1 API group.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Discovery is a client used to determine the API version and supported resources of the server.
ExtensionsV1Beta1 is a client for interacting with the extensions/v1beta1 API group.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ImagepolicyV1Alpha1 is a client for interacting with the imagepolicy/v1alpha1 API group.
LabelSelector represents a Kubernetes label selector.
NamedAuthInfo relates nicknames to auth information.
NamedCluster relates nicknames to cluster information.
NamedContext relates nicknames to context information.
NamedExtension relates nicknames to extension information.
PolicyV1Alpha1 is a client for interacting with the policy/v1alpha1 API group.
No description provided by the author
PolicyV1Beta1 is a client for interacting with the policy/v1beta1 API group.
No description provided by the author
No description provided by the author
RBACV1Alpha1 is a client for interacting with the rbac.authorization.k8s.io/v1alpha1 API group.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RBACV1Beta1 is a client for interacting with the rbac.authorization.k8s.io/v1beta1 API group.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SettingsV1Alpha1 is a client for interacting with the settings/v1alpha1 API group.
No description provided by the author
StorageV1 is a client for interacting with the storage.k8s.io/v1 API group.
StorageV1Beta1 is a client for interacting with the storage.k8s.io/v1beta1 API group.
No description provided by the author
No description provided by the author
ThirdPartyResources is a client used for interacting with user defined API groups.
No description provided by the author

# Interfaces

Option represents optional call parameters, such as label selectors.