k8s

package module
v0.11.7 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

Introduction

The library implements various handlers to more easy manipulate k8s resources such as pods, deployments, etc, inside or outside k8s cluster. A program that uses the library and runs in a k8s pod meant to be inside k8s cluster. If you simply run examples in your pc/mac or server, it meant outside k8s cluster. Both of inside and outside k8s cluster is supported by the library.

There are three kind handler:

  • k8s handler. Its a universal handler that simply invoke dynamic handler to create/update/patch/delete k8s resources and get/list k8s resources from listers instead of accessing the API server directly.
  • dynamic handler. Its a universal handler that create/update/delete/patch/get/list k8s resources by the underlying dynamic client.
  • typed handler such as deployment/pod handler. Its a typed handler that use typed client(clientset) to create/update/patch/delete/get/list typed resources(such as deployments, pods, etc.).

To create a handler for outside or inside cluster just call deployment.New(ctx, "", namespace). The New() function will find the kubeconfig file or the file pointed to by the variable KUBECONFIG. If neither is found, it will use the default kubeconfig filepath $HOME/.kube/config. New() will create a deployment handler for the outside cluster if kubeconfig is found . If no kubeconfig file is found, New() will create an in-cluster rest.Config to create the deployment handler.

The kubeconfig precedence is:

  • kubeconfig variable passed.
  • KUBECONFIG environment variable pointing at a file.
  • $HOME/.kube/config if exists.
  • In-cluster config if running in cluster.

The variable namespace is used to limit the scope of the handler. If namespace=test, the handler is only allowed to create/update/delete deployments in namespace/test. Of course, handler.WithNamespace(newNamespace) returns a new temporary handler that allowed to create/update/delete deployments in the new namespace, for examples:

namespace := "test"
newNamespace := "test-new"
// Inside cluster. the program run within k8s pod.
handler, _ := deployment.New(ctx, "", namespace)
// handler is only allowed to create/update/delete deployment in namespace/test.
handler.Create(filename)
// handler is only allowed to create/update/delete deployment in namespace/test-new.
handler.WithNamespace(newNamespace).Create(filename)
// handler is only allowed to create/update/delete deployment in namespace/test (not namespace/test-new).
handler.Create(filename)
handler.ResetNamespace(newNamespace)
// handler is only allowed to create/update/delete deployment in namespace/test-new (not namespace/test).
handler.Create(filename)

The namespace precedence is:

  • namespace defined in yaml file or json file.

  • namespace specified by WithNamespace() or MultiNamespace() method.

  • namespace specified in New() or NewOrDie() funciton.

  • namespace will be ignored if k8s resource is cluster scope.

  • if namespace is empty, default to "default" namespace.

The library is used by another open source project that used to backup pv/pvc data attached by deployments/statefulsets/daemosnets/pods running in k8s cluster.

For furthermore examples of how to use this library, see examples.

Installation

go get github.com/forbearing/k8s@v0.11.7

Documents

k8s handler examples:

Its a universal handler that simply invoke dynamic handler to create/update/apply/patch/delete/watch k8s resources and get/list k8s resources from listers instead of accessing the API server directly.

Dynamic handler examples:

Its a universal handler that create/update/apply/patch/delete/get/list/watch k8s resources by the underlying dynamic client.

Deployment handler examples:

Its a typed handler that use typed client(clientset) to create/update/apply/patch/delete/get/list/watch typed resources(such as deployments, pods, etc.).

Pod handler examples:

More examples:

TODO

  • https://github.com/kubernetes/kubectl/tree/master/pkg
  • Simplify the use of client-go informer, lister
  • create/delete/update/delete/get ... all kinds of k8s resources by dyanmic client.
  • Support crate/update/delete/get... Event resources
  • Support crate/update/delete/get... Endpoint resources
  • Support crate/update/delete/get... EndpointSlice resources
  • Support crate/update/delete/get... LimitRange resources
  • Support crate/update/delete/get... PriorityClass resources
  • Support crate/update/delete/get... ResourceQuota resources
  • Support crate/update/delete/get... Lease resources
  • Add function: GVK(), GVR(), Kind(), Group(), Version(), Resource(), KindToResource(), ResourceToKind()
  • signal handler
  • Finalizers
  • controller and owner
  • UpdateStatus: update Deployment/StatefulSet... status
  • UpdateScale: scale Deployment/StatefulSet...
  • DeleteCollection
  • Leader Election
  • Recoder
  • Replace interface{} -> any
  • fack client
  • EnvTest
  • healthz
  • metrics
  • port-forward for pod, deployment and service
  • proxy
  • all handler support Patch() method to patch k8s resource.
  • operators refer to https://sdk.operatorframework.io/docs/building-operators/golang/references/client/
  • Has/Get/GetAll/Set/Remove/RemoveAll Labels and Annotations
  • Add MultiNamespace() method to create/update/delete k8s resource in multi namespaces at once.
  • k8s handler create/update/delete/apply k8s resource by the underling dynamic but get/list from listers.
  • create/update/delete/apply muitlple k8s resource at once.
  • Admission Webhook: validating admission webhook, mutating admission webhook.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Handler  dynamic.Handler
	New      = dynamic.New
	NewOrDie = dynamic.NewOrDie
)
View Source
var Clientset = client.Clientset

Clientset creates a *kubernetes.Clientset for the given kubeconfig.

View Source
var ClientsetOrDie = client.ClientsetOrDie

ClientsetOrDie creates a *kubernetes.Clientset for the given kubeconfig. panic if there is any error occurs.

View Source
var DiscoveryClient = client.DiscoveryClient

DiscoveryClient creates a *discovery.DiscoveryClient for the given kubeconfig.

View Source
var DiscoveryClientOrDie = client.DiscoveryClientOrDie

DiscoveryClientOrDie creates a *discovery.DiscoveryClient for the given kubeconfig. panic if there is any error occurs.

View Source
var DynamicClient = client.DynamicClient

DynamicClient creates a dynamic.Interface for the given kubeconfig.

View Source
var DynamicClientOrDie = client.DynamicClientOrDie

DynamicClient creates a dynamic.Interface for the given kubeconfig. panic if there is any error occurs.

View Source
var RESTClient = client.RESTClient

RESTClient creates a *rest.RESTClient for the given kubeconfig.

View Source
var RESTClientOrDie = client.RESTClientOrDie

RESTClientOrDie creates a *rest.RESTClient for the given kubeconfig. panic if there is any error occurs.

View Source
var RESTConfig = client.RESTConfig

RESTConfig creates a *rest.Config for the given kubeconfig. create rest config, and config precedence. * kubeconfig variable passed. * KUBECONFIG environment variable pointing at a file * $HOME/.kube/config if exists. * In-cluster config if running in cluster

View Source
var RESTConfigOrDie = client.RESTConfigOrDie

RESTConfigOrDie creates a *rest.Config for the given kubeconfig. panic if there is any error occurs.

View Source
var RawConfig = client.RawConfig

RawConfig holds the information needed to build connect to remote kubernetes clusters as a given user

ref: https://stackoverflow.com/questions/70885022/how-to-get-current-k8s-context-name-using-client-go

Functions

func ApplyF

func ApplyF(ctx context.Context, kubeconfig, filename string, namespace string, opts ...Options) error

ApplyF work like "kubectl apply -f filename.yaml -n test", The namespace defined in yaml have higher precedence than namespace specified here.

func DeleteF

func DeleteF(ctx context.Context, kubeconfig, filename string, namespace string, opts ...Options) error

DeleteF work like "kubectl delete -f filename.yaml -n test", The namespace defined in yaml have higher precedence than namespace specified here.

Types

type Options added in v0.11.0

type Options int
const (
	IgnoreAlreadyExists Options = iota
	IgnoreNotFound
	IgnoreInvalid
	IgnoreTimeout
)

Directories

Path Synopsis
examples
_controller
Package controller contains code for controllers (like the replication controller).
Package controller contains code for controllers (like the replication controller).
_controller/bootstrap
Package bootstrap provides automatic processes necessary for bootstraping.
Package bootstrap provides automatic processes necessary for bootstraping.
_controller/certificates
Package certificates implements an abstract controller that is useful for building controllers that manage CSRs
Package certificates implements an abstract controller that is useful for building controllers that manage CSRs
_controller/certificates/approver
Package approver implements an automated approver for kubelet certificates.
Package approver implements an automated approver for kubelet certificates.
_controller/certificates/cleaner
Package cleaner implements an automated cleaner that does garbage collection on CSRs that meet specific criteria.
Package cleaner implements an automated cleaner that does garbage collection on CSRs that meet specific criteria.
_controller/certificates/signer
Package signer implements a CA signer that uses keys stored on local disk.
Package signer implements a CA signer that uses keys stored on local disk.
_controller/cronjob
Package cronjob contains the controller for CronJob objects.
Package cronjob contains the controller for CronJob objects.
_controller/daemon
Package daemon contains logic for watching and synchronizing daemons.
Package daemon contains logic for watching and synchronizing daemons.
_controller/deployment
Package deployment contains all the logic for handling Kubernetes Deployments.
Package deployment contains all the logic for handling Kubernetes Deployments.
_controller/endpoint
Package endpoint provides EndpointController implementation to manage and sync service endpoints.
Package endpoint provides EndpointController implementation to manage and sync service endpoints.
_controller/job
Package job contains logic for watching and synchronizing jobs.
Package job contains logic for watching and synchronizing jobs.
_controller/namespace
Package namespace contains a controller that handles namespace lifecycle
Package namespace contains a controller that handles namespace lifecycle
_controller/nodeipam
Package nodeipam contains code for syncing cloud instances with node registry
Package nodeipam contains code for syncing cloud instances with node registry
_controller/nodeipam/ipam
Package ipam provides different allocators for assigning IP ranges to nodes.
Package ipam provides different allocators for assigning IP ranges to nodes.
_controller/podautoscaler
Package podautoscaler contains logic for autoscaling number of pods based on metrics observed.
Package podautoscaler contains logic for autoscaling number of pods based on metrics observed.
_controller/podgc
Package podgc contains a very simple pod "garbage collector" implementation, PodGCController, that runs in the controller manager.
Package podgc contains a very simple pod "garbage collector" implementation, PodGCController, that runs in the controller manager.
_controller/replicaset
Package replicaset contains logic for watching and synchronizing ReplicaSets.
Package replicaset contains logic for watching and synchronizing ReplicaSets.
_controller/replication
Package replication contains logic for watching and synchronizing replication controllers.
Package replication contains logic for watching and synchronizing replication controllers.
_controller/resourcequota
Package resourcequota contains a controller that makes resource quota usage observations
Package resourcequota contains a controller that makes resource quota usage observations
_controller/serviceaccount
Package serviceaccount provides implementations to manage service accounts and service account tokens
Package serviceaccount provides implementations to manage service accounts and service account tokens
_controller/volume/attachdetach
Package attachdetach implements a controller to manage volume attach and detach operations.
Package attachdetach implements a controller to manage volume attach and detach operations.
_controller/volume/attachdetach/cache
Package cache implements data structures used by the attach/detach controller to keep track of volumes, the nodes they are attached to, and the pods that reference them.
Package cache implements data structures used by the attach/detach controller to keep track of volumes, the nodes they are attached to, and the pods that reference them.
_controller/volume/attachdetach/populator
Package populator implements interfaces that monitor and keep the states of the desired_state_of_word in sync with the "ground truth" from informer.
Package populator implements interfaces that monitor and keep the states of the desired_state_of_word in sync with the "ground truth" from informer.
_controller/volume/attachdetach/reconciler
Package reconciler implements interfaces that attempt to reconcile the desired state of the with the actual state of the world by triggering actions.
Package reconciler implements interfaces that attempt to reconcile the desired state of the with the actual state of the world by triggering actions.
_controller/volume/attachdetach/statusupdater
Package statusupdater implements interfaces that enable updating the status of API objects.
Package statusupdater implements interfaces that enable updating the status of API objects.
_controller/volume/ephemeral
Package ephemeral implements the controller part of https://github.com/kubernetes/enhancements/tree/master/keps/sig-storage/1698-generic-ephemeral-volumes
Package ephemeral implements the controller part of https://github.com/kubernetes/enhancements/tree/master/keps/sig-storage/1698-generic-ephemeral-volumes
job
k8s
pod
util
signals
signals is a package for handling signals to gratefully shutdown app in combination with kubernetes pod graceful termination policy.
signals is a package for handling signals to gratefully shutdown app in combination with kubernetes pod graceful termination policy.

Jump to

Keyboard shortcuts

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