Categorygithub.com/yue9944882/kubernetes-client-lambda

# README

Kubernetes Client Lambda

Build Status codecov Go Doc

What is Kubernetes Client Lambda?

logo

Kubernetes Client Lambda (KCL) is a wrapper library over kubernetes/client-go which provides light-weight lambda-styled streamized kubernetes resource manipulation interface. This project is basically inspired by Groovy style lambda, and is aiming at reducing the coding-overhead when referencing too many struct / interface provided by kubernetes/client-go. The only dependency of this project is kubernetes/client-go, so this project can be pure and light-weight for you. Currently KCL only provides support for those common-used resources like Pod / Service.. Click to see all the supported resources in KCL.

Note that: KCL can be compatible with any kubernetes version, because KCL is a wrapping over client-go and client-go will provide it

Also, KCL defines more useful primitive operation type beyond those provided, e.g. Create-If-Not-Exist, Update-Or-Create, Delete-If-Exist.., which can help you make your code more clean. These primitive operation is always at the end of streaming and consumes all the remaining element and apply operation on every element.

Basically, KCL is implemented by golang reflect and golang native facilities. KCL defines various resources provided by kubernetes as enumeratoin which hides some unneccessary details like API-version. Sometimes, managing these details can be painful for kubernetes developers. Meanwhile, I find it costs time when mocking kubernetes resources in static unit test, so KCL provides a very simple mocking for kubernetes resources which is implemented via map.

detail

With KCL, you can operate kubernetes resources like this example:

// See doc for more info about lambda functions Grep / Map..
import kubernetes "github.com/yue9944882/kubernetes-client-lambda"

// In-Cluster example
kubernetes.InCluster().Type(kubernetes.ReplicaSet).InNamespace("test").NamePrefix("foo-").Map(func(rs *api_ext_v1.ReplicaSet) rs*api_ext_v1.ReplicaSet {
    // Edit in-place or clone a new one
    rs.Meta.Labels["foo-label1"] = "test" 
    return rs
}).Update()


// Out-Of-Cluster example
kubernetes.Pod.OutOfClusterDefault().InNamespace("devops").NameEqual("test-pod").Each(
    func(pod *api_v1.Pod) {
        count++
    },
)

As the following example is shown, Calling Mock() on Kubernetes Type Enumeration will create the expected mocking resources for you:

import kubernetes "github.com/yue9944882/kubernetes-client-lambda"

kubernetes.Mock().Type(kubernetes.ReplicaSet).InNamespace("test").Add(
    // An anonymous function simply returns a pointer to kubernetes resource 
    // Returned objects will be added to stream
    func(){
        rs.Name = "foo"
        rs.Namespace = "test"
        return &rs
    },
).Create()

// For non-namespace resources, leave namespace to "" in InNamespace call
kubernetes.Mock().Type(kubernetes.Namespace).InNamespace("").Add(
    // An anonymous function simply returns a pointer to kubernetes resource 
    // Returned objects will be added to stream
    func(){
        ns.Name = "devops"
        return &ns
    },
).Create()

Also watching the changes can be easier via KCL's wrapping of client-go's informer. You don't have to directly access events from client-go, instead, registering / unregistering closure to KCL's API:

import kubernetes "github.com/yue9944882/kubernetes-client-lambda"
import "k8s.io/apimachinery/pkg/watch"

var kcl KubernetesClientLambda = kubernetes.InCluster()
// kcl = kubernetes.Mock()

kcl.Type(kubernetes.ReplicaSet).WatchNamespace("default").Register(watch.Added, func(rs *api_v1.ReplicaSet){
    fmt.Println(rs.Name)
})

How to Get it?

go get github.com/yue9944882/kubernetes-client-lambda

Why Kubernetes Client Lambda is better?

  • Manipulating kubernetes resources in one line
  • Lambda-styled kubernetes resource processing.
  • Watch resource event by registering a "Function" lambda
  • Pipelined and streamlized.
  • Light-weight and only depends on kubernetes/client-go
  • User-friendly mocking kubernetes static interface

Checkout more examples under example folder.

Supported Lambda Function Type

First we have following types of lambda function:

(KR denotes Kubernetes Resources, a pointer to resouce, e.g. *api_v1.Pod, *api_ext_v1.ReplicaSet..)

Primitive Lambda Type

NameParameter TypeReturn Type
FunctionKR-
ConsumerKRKR
PredicateKRbool
Producer-KR
Kubernetes Resource Lambda Snippet
NamePipelinableDescription
NameEqualyesFilter out resources if its name mismatches
NamePrefixyesFilter out resources if its name doesn't have the prefix
NameRegexyesFilter out resources if its name doesn't match the regular expression
HasAnnotationyesFilter out resources if it doesn't have the annotation
HasAnnotationKeyyesFilter out resources if it doesn't have the annotation key
HasLabelyesFilter out resources if it doesn't have the label
HasLabelKeyyesFilter out resources if it doesn't have the label key

And these lambda can be consumed by following function:

Primitive Pipeline Type
NamePipelinableLambda TypeDescription
Collectyes-Deep copies the elements and put them into collection
AddyesProducerAdd the element returned by lambda into collection
MapyesConsumerAdd all the elements returned by lambda to a new collection
GrepyesPredicateRemove the element from collection if applied lambda returned a false
FirstyesPredicateTake only the first element when applied lambda returned a true
IternoFunctionApply the lambda to every elements in the collection

Primitive methods like CreateIfNotExist, DeleteIfExist have no parameter and just consumes all elements at the end of the pipelining. Here are supported primitive kubernetes operation functions below:

Basic Operation
OperationParamReturn1Return2
EachFunctionlambda error-
AnyPredicateboollambda error
EveryPredicateboollambda error
NotEmpty-boollambda error
Kubernetes Operation
OperationParamReturn1Return2
Create-bool(sucess)lambda error
CreateIfNotExists-bool(success)lambda error
Delete-bool(sucess)lambda error
DeleteIfExists-bool(success)lambda error
Update-bool(sucess)lambda error
UpdateIfExists-bool(success)lambda error
UpdateOrCreate-bool(success)lambda error

# Packages

No description provided by the author

# Functions

InCluster establishes connection with kube-apiserver if the program is running in a kubernetes cluster.
Mock return a mock interface of lambda KubernetesClient the mock KubernetesClient is statusful and if you want to reset its status then use MockReset.
OutOfCluster establishe connection witha kube-apiserver by loading specific kube-config.
OutOfClusterDefault loads configuration from ~/.kube/config.
OutOfClusterInContext is used to switch context of multi-cluster kubernetes.

# Constants

rbac.
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
autoscaling.
extensions.
batch.
No description provided by the author
No description provided by the author
network.
No description provided by the author
No description provided by the author
No description provided by the author
core.
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
apps.
storage.

# Structs

ErrMultiLambdaFailure contains one or more error occured from lambda invocation chain.
No description provided by the author
No description provided by the author
Lambda is a basic and core type of KCL.
No description provided by the author

# Interfaces

Consumer is a function has one parameter and returns one value.
Function is a function has one parameter and has no return value.
KubernetesClientLambda provides manipulation interface for resources.
KubernetesLambda provides access entry function for kubernetes.
KubernetesWatch provides watch registry for kubernetes.
No description provided by the author
Predicate is a function has only one parameter and return boolean.
Producer is a function takes no parameter and returns a value.

# Type aliases

Resource is kubernetes resource enumeration hiding api version.