opslevel_k8s_controller

package module
v2024.4.26 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 26 Imported by: 0

README

License Made With Go Release Issues Contributors Activity CodeCov Dependabot Go Reference

Overall

opslevel-k8s-controller

A utility library for easily making and running k8s controllers

Installation

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

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 = "v2024.4.26"

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(ctx context.Context, wg *sync.WaitGroup)

Start starts the informer factory and sends events in the queue to the main loop. If a wait group is passed, Start will decrement it once it processes all events in the queue after one loop. If a wait group is not passed, Start will run continuously until the passed context is interrupted.

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) MatchesFilter added in v2024.4.26

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

func (*K8SFilter) MatchesNamespace added in v2024.4.26

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

type K8SResource added in v2024.4.26

type K8SResource struct {
	Metadata v1.ObjectMeta `json:"metadata"`
}

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