janitor

package
v0.0.0-...-4f31ccc Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package janitor will clean up expired sessions

Purpose

Generally speaking, this package will remove presumably expired session resources from a kubernetes cluster. It is intended to compliment the github.com/venezia/k8s-gorilla-session-store/gorilla package. This package could be run by the same service that is creating the sessions, or simply be a cron-job or supporting service that is just cleaning up the detritus

Intended Use

The package is meant to be used like so:

    ctx, ctxCancel := context.WithCancel(context.Background())
	janitor, err := janitor.New(ctx, config)
    go func() { janitor.Clean() }()
    go func() {
        time.Sleep(120 * time.Second)
        ctxCancel()
    }()

Where the janitor would run for 120 seconds. Obviously one may want it to run forever, etc.

Kubernetes Considerations

Errors

While an error can be generated if a Janitor is misconfigured, errors are not going to be returned to the caller. Perhaps at a later time how to handle errors for a background job could be fleshed out. Instead when something that would typically result in an error being returned happens, a log entry is provided.

Logging Considerations

This package supports log/slog.Logger as a logger provided to it through the configuration of a new store.

Index

Constants

View Source
const (
	// GenericErrorType is the generic error for the demo package.
	GenericErrorType ErrorType = "generic"

	// TransientErrorType are errors when a failure happened, but we think it is a transient error
	//
	// Example causes include:
	//  - network connectivity disrupted
	//
	// It is reasonable to think that attempting the operation later may result in a happier outcome
	TransientErrorType ErrorType = "transient"

	// ExecutionErrorType is the error type that can return if something was attempted, but we cannot fulfill the request
	//
	// Repeated attempts are unlikely to result in a better outcome, as best as we can tell, the input was valid
	//
	// Examples would include:
	//  - misconfiguration of an upstream service (not disconnection, misconfiguration)
	ExecutionErrorType ErrorType = "execution"

	// InputErrorType is the error type that can return if something was attempted, but we cannot fulfill the request
	//
	// Repeated attempts are unlikely to result in a better outcome, as best as we can tell, the input was invalid
	//
	InputErrorType ErrorType = "input"

	// NotImplementedErrorType are errors when what asked simply is not implemented
	//
	// It is reasonable to expect that this change will never change
	NotImplementedErrorType ErrorType = "not-implemented"

	// GenericError is a coverall - hopefully never used
	GenericError ErrorMessage = "an error occurred"

	FeatureNotImplementedError ErrorMessage = "this feature of the store is not implemented for this provider"
)
View Source
const (
	MinimumSleepDuration      = 1 * time.Second
	SessionListPaginationSize = 50
)
View Source
const (
	// Main Kubernetes Driver Messages
	LogCannotReadKubeconfigFile        = "cannot read in kubeconfig file as specified"
	LogCannotConvertKubeconfigContents = "could not convert kubeconfig contents to a valid configuration"
	LogCannotUseInClusterConfig        = "seemingly not in a kubernetes cluster, cannot use in cluster configuration"
	LogCannotConvertInClusterConfig    = "seemingly in a kubernetes cluster but cannot create a valid cluster configuration"

	LogSleepDurationTooShort = "sleep duration is set too short"

	LogK8SClientAlreadyExists                     = "kubernetes client already set, not going to recreate"
	LogCannotCreateKubernetesClient               = "error occurred while trying to create a kubernetes client"
	LogK8SConfigCreationThroughKubeconfigLocation = "attempting to create a kubernetes configuration via a kubeconfig file location setting"
	LogK8SConfigCreationThroughKubeconfig         = "attempting to create a kubernetes configuration via a kubeconfig contents provided directly in configuration"
	LogK8SConfigCreationThroughInCluster          = "attempting to create a kubernetes configuration through the in-cluster option"

	LogLeavingCleanContextClosed = "clean function ending as the parent context has closed"

	LogGettingListOfSessions                          = "getting list of sessions"
	LogCannotGetListOfSessions                        = "cannot get list of sessions"
	LogFoundSessionToDelete                           = "found session to delete because of age"
	LogCannotDeleteNonExistentSessionFromKubernetes   = "could not delete session from kubernetes - already removed"
	LogErrorTryingToDeleteSessionResourceInKubernetes = "error occurred while trying to delete a session resource in kubernetes"

	LogSessionIDKey          = "sessionID"
	LogKubeconfigLocationKey = "kubeconfigLocation"
	LogKubeconfigSizeKey     = "kubeconfigSize"
	LogErrorKey              = "error"
	LogContinueKey           = "continue"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	K8S           K8SConfig
	SleepDuration time.Duration
	Logger        *slog.Logger
}

type Error

type Error struct {
	Type    ErrorType
	Details interface{}
	Message ErrorMessage
	Err     error
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ErrorMessage

type ErrorMessage string

type ErrorType

type ErrorType string

type Janitor

type Janitor struct {
	Ctx    context.Context
	Config *Config
}

func New

func New(ctx context.Context, config *Config) (*Janitor, error)

New returns a new Janitor based on configuration

func (*Janitor) Clean

func (j *Janitor) Clean()

func (*Janitor) EnsureK8SClient

func (j *Janitor) EnsureK8SClient() error

EnsureK8SClient will try to create a kubernetes client if it isn't already set

func (*Janitor) GetK8SConfig

func (j *Janitor) GetK8SConfig() (*rest.Config, error)

GetK8SConfig will try to get a working kubernetes client configuration returning an error if it cannot

func (*Janitor) K8SCleanExpiredSessions

func (j *Janitor) K8SCleanExpiredSessions()

K8SCleanExpiredSessions will fetch all sessions, look to see if any of them have expired, and then remove them. The list is paginated - currently set through constant SessionListPaginationSize - but it will iterate across all sessions

type K8SConfig

type K8SConfig struct {
	KubeconfigLocation string // Where a kubeconfig file is located on the system
	Kubeconfig         []byte // The contents of a kubeconfig
	Namespace          string // The namespace that will be used to look for resources
	Client             clientset.Interface
}

Jump to

Keyboard shortcuts

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