The library implements various handlers to more easier create/update/apply/patch/delete/get/list/watch k8s resources such as pods, deployments, etc. inside or outside k8s cluster.
The library implements various handlers to more easier create/update/apply/patch/delete/get/list/watch 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 are supported by the library.
There are three kind handler:
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:
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.
go get github.com/forbearing/[email protected]
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.
Its a universal handler that create/update/apply/patch/delete/get/list/watch k8s resources by the underlying dynamic client.
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.).
Event
resourcesEndpoint
resourcesEndpointSlice
resourcesLimitRange
resourcesPriorityClass
resourcesResourceQuota
resourcesLease
resources