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 ¶
- This package uses a generated client github.com/venezia/k8s-gorilla-session-store/k8s/client/clientset/versioned.Interface to interact with kubernetes.
- There is a custom resource definition file provided in the k8s/crd folder. This needs to be applied to the kubernetes cluster
- If RBAC is enabled, RBAC permissions will probably be needed
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 ¶
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" )
const ( MinimumSleepDuration = 1 * time.Second SessionListPaginationSize = 50 )
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 Error ¶
type Error struct { Type ErrorType Details interface{} Message ErrorMessage Err error }
type ErrorMessage ¶
type ErrorMessage string
type Janitor ¶
func (*Janitor) EnsureK8SClient ¶
EnsureK8SClient will try to create a kubernetes client if it isn't already set
func (*Janitor) GetK8SConfig ¶
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