k8s

package
v2.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package k8s is a facade over (super-terrible, very difficult to understand) client-go to provide a higher-level interface to Kubernetes, with support for simple, high-level APIs for watching resources (including from stable, long-running processes) and implementing basic controllers.

It is intended to offer the same API for (nearly) every Kubernetes resource, including easy CRD access without codegen.

Index

Constants

View Source
const (
	// NamespaceAll is the argument to specify on a context when you want to list or filter
	// resources across all namespaces.
	NamespaceAll = metav1.NamespaceAll
	// NamespaceNone is the argument for a context when there is no namespace.
	NamespaceNone = metav1.NamespaceNone
)

Variables

This section is empty.

Functions

func QKind added in v2.1.0

func QKind(gv, k string) string

Given a "group/version" string (i.g. from ".apiVersion") and a "kind" string, return a qualified "kind.version.group" string.

Types

type Client

type Client struct {
	Namespace string
	// contains filtered or unexported fields
}

Client is the top-level handle to the Kubernetes cluster.

func NewClient

func NewClient(info *KubeInfo) (*Client, error)

NewClient constructs a k8s.Client, optionally using a previously-constructed KubeInfo.

func (*Client) List

func (c *Client) List(ctx context.Context, resource string) ([]Resource, error)

List calls ListNamespace(...) with NamespaceAll.

func (*Client) ListNamespace

func (c *Client) ListNamespace(ctx context.Context, namespace, resource string) ([]Resource, error)

ListNamespace returns a slice of Resources. If the resource is not namespaced, the namespace must be NamespaceNone. If the resource is namespaced, NamespaceAll lists across all namespaces.

func (*Client) ListQuery

func (c *Client) ListQuery(ctx context.Context, query Query) ([]Resource, error)

ListQuery returns all the Kubernetes resources that satisfy the supplied query.

func (*Client) ResolveResourceType

func (c *Client) ResolveResourceType(resource string) (ResourceType, error)

ResolveResourceType takes the name of a resource type (TYPE[[.VERSION].GROUP], where TYPE may be singular, plural, or an abbreviation; like you might pass to `kubectl get`) and returns cluster-specific canonical information about that resource type.

For example, with Kubernetes v1.10.5:

"pod"        -> {Group: "",           Version: "v1",      Name: "pods",        Kind: "Pod",        Namespaced: true}
"deployment" -> {Group: "extensions", Version: "v1beta1", Name: "deployments", Kind: "Deployment", Namespaced: true}
"svc.v1."    -> {Group: "",           Version: "v1",      Name: "services",    Kind: "Service",    Namespaced: true}

Newer versions of Kubernetes might instead put "pod" in the "core" group, or put "deployment" in apps/v1 instead of extensions/v1beta1.

func (*Client) SelectiveList

func (c *Client) SelectiveList(ctx context.Context, namespace, resource, fieldSelector, labelSelector string) ([]Resource, error)

func (*Client) Watcher

func (c *Client) Watcher() *Watcher

Watcher returns a Kubernetes Watcher for the specified client.

type KubeInfo

type KubeInfo struct {
	// contains filtered or unexported fields
}

KubeInfo holds the data required to talk to a cluster

func NewKubeInfo

func NewKubeInfo(configfile, context, namespace string) *KubeInfo

NewKubeInfo returns a useable KubeInfo, handling optional kubeconfig, context, and namespace.

func NewKubeInfoFromFlags

func NewKubeInfoFromFlags(flags *pflag.FlagSet) *KubeInfo

NewKubeInfoFromFlags adds the generic kubeconfig flags to the provided FlagSet, and returns a *KubeInfo that configures itself based on those flags.

func (*KubeInfo) GetConfigFlags

func (info *KubeInfo) GetConfigFlags() *genericclioptions.ConfigFlags

GetConfigFlags returns the genericclioptions.ConfigFlags from inside the KubeInfo

func (*KubeInfo) GetKubectl

func (info *KubeInfo) GetKubectl(args string) (string, error)

GetKubectl returns the arguments for a runnable kubectl command that talks to the same cluster as the associated ClientConfig.

func (*KubeInfo) GetKubectlArray

func (info *KubeInfo) GetKubectlArray(args ...string) ([]string, error)

GetKubectlArray does what GetKubectl does but returns the result as a []string.

func (*KubeInfo) GetRestConfig

func (info *KubeInfo) GetRestConfig() (*rest.Config, error)

GetRestConfig returns a REST config

func (*KubeInfo) Namespace

func (info *KubeInfo) Namespace() (string, error)

Namespace returns the namespace for a KubeInfo.

type Map

type Map map[string]interface{}

Map is a YAML/JSON-ish map with some convenience methods on it.

func (Map) GetBool

func (m Map) GetBool(key string) bool

GetBool returns m[key], type-asserted to be a bool. If m[key] is not set, or it is not a bool, then false is returned.

That is: it always safely returns a usable bool.

func (Map) GetInt64

func (m Map) GetInt64(key string) int64

GetInt64 returns m[key], type-asserted to be an int64. If m[key] is not set, or it is not an int64, then 0 is returned.

That is: it always safely returns a usable int64.

func (Map) GetMap

func (m Map) GetMap(name string) map[string]interface{}

GetMap returns m[name], type-asserted to be a map. If m[name] is not set, or is not a map, then an non-nil empty map is returned.

That is: it always safely returns a usable map.

func (Map) GetMaps

func (m Map) GetMaps(name string) []map[string]interface{}

GetMaps returns m[name], type-asserted to be a list of maps. If m[name] is not set, or is not a list of maps, then a nil array is returned.

That is: it always safely returns a usable slice of maps.

func (Map) GetString

func (m Map) GetString(key string) string

GetString returns m[key], type-asserted to be a string. If m[key] is not set, or it is not a string, then an empty string is returned.

That is: it always safely returns a usable string.

type Metadata

type Metadata map[string]interface{}

func (Metadata) Annotations

func (m Metadata) Annotations() map[string]interface{}

func (Metadata) Name

func (m Metadata) Name() string

Name returns the metadata "name".

func (Metadata) Namespace

func (m Metadata) Namespace() string

Namespace returns the metadata "namespace".

func (Metadata) QName

func (m Metadata) QName() string

func (Metadata) ResourceVersion

func (m Metadata) ResourceVersion() string

ResourceVersion returns the metadata "resourceVersion".

type Query

type Query struct {
	// The Kind of a query may use any of the short names or abbreviations permitted by kubectl.
	Kind string

	// The Namespace field specifies the namespace to query.  Use NamespaceAll to query all
	// namespaces.  If the resource type is not namespaced, this field must be NamespaceNone.
	Namespace string

	// The FieldSelector and LabelSelector fields contain field and label selectors as
	// documented by kubectl.
	FieldSelector string
	LabelSelector string
	// contains filtered or unexported fields
}

Query describes a query for a set of Kubernetes resources.

type Resource

type Resource map[string]interface{}

Resource is map from strings to any with some convenience methods for accessing typical Kubernetes resource fields.

func NewResourceFromYaml

func NewResourceFromYaml(yaml map[interface{}]interface{}) Resource

NewResourceFromYaml takes a (already-parsed) untyped YAML structure, and fixes it up to be JSON-compatible, and returns it as a Resource.

func ParseResources

func ParseResources(name, input string) (result []Resource, err error)

func (Resource) Data

func (r Resource) Data() Map

func (Resource) Empty

func (r Resource) Empty() bool

func (Resource) Kind

func (r Resource) Kind() string

func (Resource) Metadata

func (r Resource) Metadata() Metadata

func (Resource) Name

func (r Resource) Name() string

func (Resource) Namespace

func (r Resource) Namespace() string

func (Resource) QKind

func (r Resource) QKind() string

QKind returns a fully qualified resource kind with the following format: <kind>.<version>.<group>

func (Resource) QName

func (r Resource) QName() string

func (Resource) ResourceVersion

func (r Resource) ResourceVersion() string

func (Resource) Spec

func (r Resource) Spec() Map

func (Resource) Status

func (r Resource) Status() Map

type ResourceType

type ResourceType struct {
	Group      string
	Version    string
	Name       string // lowercase plural, called Resource in Kubernetes code
	Kind       string // uppercase singular
	Namespaced bool
}

ResourceType describes a Kubernetes resource type in a particular cluster. See ResolveResourceType() for more information.

It is equivalent to a k8s.io/apimachinery/pkg/api/meta.RESTMapping

func (ResourceType) String

func (r ResourceType) String() string

type Watcher

type Watcher struct {
	Client *Client
	// contains filtered or unexported fields
}

Watcher is a kubernetes watcher that can watch multiple queries simultaneously

func NewWatcher

func NewWatcher(info *KubeInfo) (*Watcher, error)

NewWatcher returns a Kubernetes watcher for the specified cluster.

func (*Watcher) Exists

func (w *Watcher) Exists(kind, qname string) (bool, error)

Exists returns true if the `qname` resource (of kind `kind`) exists

func (*Watcher) Get

func (w *Watcher) Get(kind, qname string) (Resource, error)

Get gets the `qname` resource (of kind `kind`)

func (*Watcher) List

func (w *Watcher) List(kind string) ([]Resource, error)

List lists all the resources with kind `kind`

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context) error

Start starts the watcher

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop stops a watch. It is safe to call Stop from multiple goroutines and call it multiple times. This is useful, e.g. for implementing a timed wait pattern. You can have your watch callback test for a condition and invoke Stop() when that condition is met, while simultaneously havin a background goroutine call Stop() when a timeout is exceeded and not worry about these two things racing each other (at least with respect to invoking Stop()).

func (*Watcher) UpdateStatus

func (w *Watcher) UpdateStatus(ctx context.Context, resource Resource) (Resource, error)

UpdateStatus updates the status of the `resource` provided

func (*Watcher) Wait

func (w *Watcher) Wait(ctx context.Context) error

func (*Watcher) WatchQuery

func (w *Watcher) WatchQuery(query Query, listener func(*Watcher) error) error

WatchQuery watches the set of resources identified by the supplied query and invokes the supplied listener whenever they change.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL