opslevel_k8s_controller

package module
v2023.12.22 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: MIT Imports: 26 Imported by: 0

README

opslevel-k8s-controller

A utility library for easily making and running k8s controllers

Installation

go get github.com/opslevel/opslevel-k8s-controller/v2023

Then to create a k8s controller you can simply do

selector := opslevel_k8s_controller.K8SSelector{
    ApiVersion: "apps/v1",
    Kind: "Deployment",
    Excludes: []string{`.metadata.namespace == "kube-system"`}
}
resync := time.Hour*24
batch := 500
runOnce := false
controller, err := opslevel_k8s_controller.NewK8SController(selector, resync, batch, runOnce)
if err != nil {
    //... Handle error ...
}
callback := func(items []interface{}) {
    for _, item := range items {
        // ... Process K8S Resource ...
    }
}
controller.OnAdd = callback
controller.OnUpdate = callback
controller.Start()

Because of the way the selector works you can easily target any k8s resource in your cluster and you have the power of JQ to exclude resources that might match the expression.

Documentation

Index

Constants

View Source
const ReleaseVersion = "v2023.12.22"

Variables

This section is empty.

Functions

This section is empty.

Types

type K8SClient

type K8SClient struct {
	Client  kubernetes.Interface
	Dynamic dynamic.Interface
	Mapper  *restmapper.DeferredDiscoveryRESTMapper
}

func NewK8SClient

func NewK8SClient() (*K8SClient, error)

NewK8SClient This creates a wrapper which gives you an initialized and connected kubernetes client It then has a number of helper functions

func (*K8SClient) GetAllNamespaces

func (c *K8SClient) GetAllNamespaces() ([]string, error)

func (*K8SClient) GetGVR

func (c *K8SClient) GetGVR(selector K8SSelector) (*schema.GroupVersionResource, error)

func (*K8SClient) GetInformerFactory

func (c *K8SClient) GetInformerFactory(resync time.Duration) dynamicinformer.DynamicSharedInformerFactory

func (*K8SClient) GetMapping

func (c *K8SClient) GetMapping(selector K8SSelector) (*meta.RESTMapping, error)

func (*K8SClient) GetNamespaces

func (c *K8SClient) GetNamespaces(selector K8SSelector) ([]string, error)

func (*K8SClient) Query

func (c *K8SClient) Query(selector K8SSelector) (output []unstructured.Unstructured, err error)

type K8SController

type K8SController struct {
	OnAdd    K8SControllerHandler
	OnUpdate K8SControllerHandler
	OnDelete K8SControllerHandler
	// contains filtered or unexported fields
}

func NewK8SController

func NewK8SController(selector K8SSelector, resyncInterval time.Duration) (*K8SController, error)

func (*K8SController) Start

func (c *K8SController) Start(wg *sync.WaitGroup)

Start - starts the informer faktory and sync's the data. The wait group passed in is used to track when the informer has gone through 1 full loop and syncronized all the k8s data 1 time

type K8SControllerEventType

type K8SControllerEventType string
const (
	ControllerEventTypeCreate K8SControllerEventType = "create"
	ControllerEventTypeUpdate K8SControllerEventType = "update"
	ControllerEventTypeDelete K8SControllerEventType = "delete"
)

type K8SControllerHandler

type K8SControllerHandler func(interface{})

type K8SEvent

type K8SEvent struct {
	Key  string
	Type K8SControllerEventType
}

type K8SFilter

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

func NewK8SFilter

func NewK8SFilter(selector K8SSelector) *K8SFilter

func (*K8SFilter) Matches

func (f *K8SFilter) Matches(data any) bool

type K8SSelector

type K8SSelector struct {
	ApiVersion string   `json:"apiVersion" yaml:"apiVersion"`
	Kind       string   `json:"kind" yaml:"kind"`
	Namespaces []string `json:"namespaces,omitempty" yaml:"namespaces,omitempty"`
	Labels     []string `json:"labels,omitempty" yaml:"labels,omitempty"`
	Excludes   []string `json:"excludes,omitempty" yaml:"excludes,omitempty"`
}

func (*K8SSelector) GetListOptions

func (selector *K8SSelector) GetListOptions() metav1.ListOptions

func (*K8SSelector) LabelSelector

func (selector *K8SSelector) LabelSelector() string

Jump to

Keyboard shortcuts

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