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
- func QKind(gv, k string) string
- type Client
- func (c *Client) List(ctx context.Context, resource string) ([]Resource, error)
- func (c *Client) ListNamespace(ctx context.Context, namespace, resource string) ([]Resource, error)
- func (c *Client) ListQuery(ctx context.Context, query Query) ([]Resource, error)
- func (c *Client) ResolveResourceType(resource string) (ResourceType, error)
- func (c *Client) SelectiveList(ctx context.Context, namespace, resource, fieldSelector, labelSelector string) ([]Resource, error)
- func (c *Client) Watcher() *Watcher
- type KubeInfo
- func (info *KubeInfo) GetConfigFlags() *genericclioptions.ConfigFlags
- func (info *KubeInfo) GetKubectl(args string) (string, error)
- func (info *KubeInfo) GetKubectlArray(args ...string) ([]string, error)
- func (info *KubeInfo) GetRestConfig() (*rest.Config, error)
- func (info *KubeInfo) Namespace() (string, error)
- type Map
- type Metadata
- type Query
- type Resource
- func (r Resource) Data() Map
- func (r Resource) Empty() bool
- func (r Resource) Kind() string
- func (r Resource) Metadata() Metadata
- func (r Resource) Name() string
- func (r Resource) Namespace() string
- func (r Resource) QKind() string
- func (r Resource) QName() string
- func (r Resource) ResourceVersion() string
- func (r Resource) Spec() Map
- func (r Resource) Status() Map
- type ResourceType
- type Watcher
- func (w *Watcher) Exists(kind, qname string) (bool, error)
- func (w *Watcher) Get(kind, qname string) (Resource, error)
- func (w *Watcher) List(kind string) ([]Resource, error)
- func (w *Watcher) Start(ctx context.Context) error
- func (w *Watcher) Stop()
- func (w *Watcher) UpdateStatus(ctx context.Context, resource Resource) (Resource, error)
- func (w *Watcher) Wait(ctx context.Context) error
- func (w *Watcher) WatchQuery(query Query, listener func(*Watcher) error) error
Constants ¶
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 ¶
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 ¶
NewClient constructs a k8s.Client, optionally using a previously-constructed KubeInfo.
func (*Client) ListNamespace ¶
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 ¶
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 ¶
type KubeInfo ¶
type KubeInfo struct {
// contains filtered or unexported fields
}
KubeInfo holds the data required to talk to a cluster
func NewKubeInfo ¶
NewKubeInfo returns a useable KubeInfo, handling optional kubeconfig, context, and namespace.
func NewKubeInfoFromFlags ¶
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 ¶
GetKubectl returns the arguments for a runnable kubectl command that talks to the same cluster as the associated ClientConfig.
func (*KubeInfo) GetKubectlArray ¶
GetKubectlArray does what GetKubectl does but returns the result as a []string.
func (*KubeInfo) GetRestConfig ¶
GetRestConfig returns a REST config
type Map ¶
type Map map[string]interface{}
Map is a YAML/JSON-ish map with some convenience methods on it.
func (Map) GetBool ¶
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 ¶
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 ¶
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.
type Metadata ¶
type Metadata map[string]interface{}
func (Metadata) Annotations ¶
func (Metadata) ResourceVersion ¶
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 (Resource) QKind ¶
QKind returns a fully qualified resource kind with the following format: <kind>.<version>.<group>
func (Resource) ResourceVersion ¶
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 ¶
NewWatcher returns a Kubernetes watcher for the specified cluster.
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 ¶
UpdateStatus updates the status of the `resource` provided