sandwich-contexts-kubernetes
Safe HaskellNone
LanguageHaskell2010

Test.Sandwich.Contexts.Kubernetes

Description

Create Kubernetes clusters with either kind or Minikube, obtaining the relevant binary from either the current PATH or from Nix.

Also contains functions for waiting for pods and services to exist, running commands with kubectl, logging, service forwarding, and port forwarding.

Synopsis

Kind clusters

introduceKindClusterViaNix Source #

Arguments

:: forall context (m :: Type -> Type). (HasBaseContext context, MonadUnliftIO m, HasNixContext context) 
=> KindClusterOptions

Options

-> SpecFree (KindContext context) m ()

Child spec

-> SpecFree context m ()

Parent spec

Introduce a Kubernetes cluster using kind, deriving the kind and kubectl binaries from the Nix context.

introduceKindClusterViaEnvironment Source #

Arguments

:: forall context (m :: Type -> Type). (HasBaseContext context, MonadUnliftIO m) 
=> KindClusterOptions

Options

-> SpecFree (KindContext context) m () 
-> SpecFree context m () 

Introduce a Kubernetes cluster using kind, deriving the kind and kubectl binaries from the PATH.

introduceKindCluster' Source #

Arguments

:: forall context (m :: Type -> Type). (HasBaseContext context, MonadUnliftIO m) 
=> FilePath

Path to kind binary

-> FilePath

Path to kubectl binary

-> KindClusterOptions 
-> SpecFree (KindContext context) m () 
-> SpecFree context m () 

Introduce a Kubernetes cluster using kind, passing in the kind and kubectl binaries.

data KindClusterOptions Source #

Constructors

KindClusterOptions 

Fields

Minikube clusters

introduceMinikubeClusterViaNix Source #

Arguments

:: forall context (m :: Type -> Type). (HasBaseContext context, MonadUnliftIO m, HasNixContext context) 
=> MinikubeClusterOptions

Options

-> SpecFree (MinikubeClusterContext context) m ()

Child spec

-> SpecFree context m ()

Parent spec

Introduce a Minikube cluster, deriving the minikube binary from the Nix context.

introduceMinikubeClusterViaEnvironment Source #

Arguments

:: forall context (m :: Type -> Type). (HasBaseContext context, MonadUnliftIO m) 
=> MinikubeClusterOptions

Options

-> SpecFree (MinikubeClusterContext context) m () 
-> SpecFree context m () 

Introduce a Minikube cluster, deriving the minikube binary from the PATH.

introduceMinikubeCluster' Source #

Arguments

:: forall context (m :: Type -> Type). (HasBaseContext context, MonadUnliftIO m) 
=> FilePath

Path to minikube binary

-> MinikubeClusterOptions 
-> SpecFree (MinikubeClusterContext context) m () 
-> SpecFree context m () 

Introduce a Minikube cluster, passing in the minikube binary path.

Wait for pods/services

waitForPodsToExist Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m, MonadReader context m, HasKubernetesClusterContext context) 
=> Text

Namespace

-> Map Text Text

Pod labels

-> Double

Time in seconds to wait

-> Maybe Int

Optional desired pod count to wait for

-> m () 

Wait for a set of pods to exist, specified by a set of labels.

waitForPodsToBeReady Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m, MonadReader context m, HasKubernetesClusterContext context, HasFile context "kubectl") 
=> Text

Namespace

-> Map Text Text

Pod labels

-> Double

Time in seconds to wait

-> m () 

Wait for a set of pods to be in the Ready condition, specified by a set of labels.

waitForServiceEndpointsToExist Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m, MonadReader context m, HasKubernetesClusterContext context) 
=> Text

Namespace

-> Text

Service name

-> Double

Time in seconds to wait

-> m () 

Wait for a service to have its set of endpoints ready, i.e.:

  • They each have at least one IP address
  • They each have an empty set of "not ready addresses"

Run commands with kubectl

askKubectlArgs Source #

Arguments

:: KubectlBasic context m 
=> m (FilePath, [(String, String)])

Returns the kubectl binary and environment variables.

Retrieve the kubectl binary path and the set of environment variables to use when invoking it. Derives these from a HasFile context and the KubernetesClusterContext respectively.

Useful for running Kubectl commands with createProcess etc.

askKubectlEnvironment Source #

Arguments

:: KubernetesClusterBasic context m 
=> m [(String, String)]

Returns the kubectl environment variables.

Same as askKubectlArgs, but only returns the environment variables.

getKubectlEnvironment Source #

Arguments

:: MonadLoggerIO m 
=> KubernetesClusterContext

Kubernetes cluster context

-> m [(String, String)]

Returns the kubectl environment variables.

Same as askKubectlArgs, but only returns the environment variables.

Forward services

withForwardKubernetesService Source #

Arguments

:: KubectlBasic context m 
=> Text

Namespace

-> Text

Service name

-> (URI -> m a)

Callback receiving the service URL.

-> m a 

Forward a Kubernetes service, so that it can be reached at a local URI.

withForwardKubernetesService' Source #

Arguments

:: (MonadLoggerIO m, MonadUnliftIO m, HasBaseContextMonad context m) 
=> KubernetesClusterContext

Kubernetes cluster context

-> FilePath

Binary path for kubectl

-> Text

Namespace

-> Text

Service name

-> (URI -> m a)

Callback receiving the service URL.

-> m a 

Same as withForwardKubernetesService, but allows you to pass in the KubernetesClusterContext and kubectl binary.

Logs

withKubectlLogs Source #

Arguments

:: (MonadLogger m, MonadFail m, MonadUnliftIO m, HasBaseContextMonad ctx m, HasFile ctx "kubectl") 
=> FilePath

Kubeconfig file

-> Text

Namespace

-> Text

Log target (pod, service, etc.)

-> Maybe Text

Specific container to get logs from

-> Bool

Whether to interrupt the process to shut it down while cleaning up

-> (KubectlLogsContext -> m a)

Callback receiving the KubectlLogsContext

-> m a 

Run a kubectl logs process, placing the logs in a file in the current test node directory.

Note that this will stop working if the pod you're talking to goes away (even if you do it against a service). If this happens, a rerun of the command is needed to resume log forwarding.

Port forwarding

withKubectlPortForward Source #

Arguments

:: (HasCallStack, KubectlBasic context m) 
=> FilePath

Path to kubeconfig file

-> Text

Namespace

-> Text

Target name (pod, service, etc.)

-> PortNumber

Target port number

-> (KubectlPortForwardContext -> m a) 
-> m a 

Run a kubectl port-forward process, making the port available in the KubectlPortForwardContext.

Note that this will stop working if the pod you're talking to goes away (even if you do it against a service). If this happens, a rerun of the command is needed to resume port forwarding.

withKubectlPortForward' Source #

Arguments

:: (HasCallStack, KubernetesBasic context m) 
=> FilePath 
-> FilePath

Path to kubeconfig file

-> Text

Namespace

-> (PortNumber -> Bool)

Callback to check if the proposed local port is acceptable

-> Maybe PortNumber 
-> Text

Target name (pod, service, etc.)

-> PortNumber

Target port number

-> (KubectlPortForwardContext -> m a) 
-> m a 

Same as withKubectlPortForward, but allows you to pass in the kubectl binary path.

Types

type HasKubernetesClusterContext context = HasLabel context "kubernetesCluster" KubernetesClusterContext Source #

Constraint aliases

type KubernetesBasic context (m :: Type -> Type) = (MonadLoggerIO m, MonadUnliftIO m, HasBaseContextMonad context m) Source #

type KubectlBasic context (m :: Type -> Type) = (KubernetesClusterBasic context m, HasFile context "kubectl") Source #

type NixContextBasic context (m :: Type -> Type) = (MonadLoggerIO m, MonadUnliftIO m, HasBaseContextMonad context m, HasNixContext context) Source #

type KubectlBasicWithoutReader context (m :: Type -> Type) = (MonadUnliftIO m, HasBaseContext context, HasKubernetesClusterContext context, HasFile context "kubectl") Source #