{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Test.Sandwich.Contexts.Kubernetes.Namespace (
withKubernetesNamespace
, withKubernetesNamespace'
, withKubernetesNamespace''
, withKubernetesNamespace'''
, createKubernetesNamespace
, createKubernetesNamespace'
, createKubernetesNamespace''
, destroyKubernetesNamespace
, destroyKubernetesNamespace'
, destroyKubernetesNamespace''
) where
import Control.Monad
import Data.String.Interpolate
import Relude hiding (force)
import System.Exit
import Test.Sandwich
import Test.Sandwich.Contexts.Files
import Test.Sandwich.Contexts.Kubernetes.Kubectl
import Test.Sandwich.Contexts.Kubernetes.Types
import UnliftIO.Exception
import UnliftIO.Process
withKubernetesNamespace :: (
KubectlBasicWithoutReader context m
)
=> Text
-> SpecFree context m ()
-> SpecFree context m ()
withKubernetesNamespace :: forall context (m :: * -> *).
KubectlBasicWithoutReader context m =>
Text -> SpecFree context m () -> SpecFree context m ()
withKubernetesNamespace Text
namespace = String
-> (ExampleT context m [Result] -> ExampleT context m ())
-> SpecFree context m ()
-> SpecFree context m ()
forall context (m :: * -> *).
HasCallStack =>
String
-> (ExampleT context m [Result] -> ExampleT context m ())
-> SpecFree context m ()
-> SpecFree context m ()
around [i|Create the '#{namespace}' kubernetes namespace|]
(ExampleT context m [Result] -> ExampleT context m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ExampleT context m [Result] -> ExampleT context m ())
-> (ExampleT context m [Result] -> ExampleT context m [Result])
-> ExampleT context m [Result]
-> ExampleT context m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExampleT context m ()
-> ExampleT context m ()
-> ExampleT context m [Result]
-> ExampleT context m [Result]
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_ (Text -> ExampleT context m ()
forall context (m :: * -> *).
KubectlBasic context m =>
Text -> m ()
createKubernetesNamespace Text
namespace) (Bool -> Text -> ExampleT context m ()
forall context (m :: * -> *).
KubectlBasic context m =>
Bool -> Text -> m ()
destroyKubernetesNamespace Bool
False Text
namespace))
withKubernetesNamespace' :: (
KubectlBasic context m
)
=> Text
-> m a
-> m a
withKubernetesNamespace' :: forall context (m :: * -> *) a.
KubectlBasic context m =>
Text -> m a -> m a
withKubernetesNamespace' Text
namespace =
m () -> m () -> m a -> m a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_ (Text -> m ()
forall context (m :: * -> *).
KubectlBasic context m =>
Text -> m ()
createKubernetesNamespace Text
namespace) (Bool -> Text -> m ()
forall context (m :: * -> *).
KubectlBasic context m =>
Bool -> Text -> m ()
destroyKubernetesNamespace Bool
False Text
namespace)
withKubernetesNamespace'' :: (
KubernetesClusterBasic context m
)
=> FilePath
-> Text
-> m a
-> m a
withKubernetesNamespace'' :: forall context (m :: * -> *) a.
KubernetesClusterBasic context m =>
String -> Text -> m a -> m a
withKubernetesNamespace'' String
kubectl Text
namespace =
m () -> m () -> m a -> m a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_ (String -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
String -> Text -> m ()
createKubernetesNamespace' String
kubectl Text
namespace) (String -> Bool -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
String -> Bool -> Text -> m ()
destroyKubernetesNamespace' String
kubectl Bool
False Text
namespace)
withKubernetesNamespace''' :: (
KubernetesClusterBasic context m
)
=> KubernetesClusterContext
-> FilePath
-> Text
-> m a
-> m a
withKubernetesNamespace''' :: forall context (m :: * -> *) a.
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Text -> m a -> m a
withKubernetesNamespace''' KubernetesClusterContext
kcc String
kubectl Text
namespace =
m () -> m () -> m a -> m a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_ (KubernetesClusterContext -> String -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Text -> m ()
createKubernetesNamespace'' KubernetesClusterContext
kcc String
kubectl Text
namespace) (KubernetesClusterContext -> String -> Bool -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Bool -> Text -> m ()
destroyKubernetesNamespace'' KubernetesClusterContext
kcc String
kubectl Bool
False Text
namespace)
createKubernetesNamespace :: (
KubectlBasic context m
)
=> Text
-> m ()
createKubernetesNamespace :: forall context (m :: * -> *).
KubectlBasic context m =>
Text -> m ()
createKubernetesNamespace Text
namespace =
forall (a :: Symbol) context (m :: * -> *).
(MonadReader context m, HasFile context a) =>
m String
askFile @"kubectl" m String -> (String -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (String -> Text -> m ()) -> Text -> String -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
String -> Text -> m ()
createKubernetesNamespace' Text
namespace
createKubernetesNamespace' :: (
KubernetesClusterBasic context m
)
=> FilePath
-> Text
-> m ()
createKubernetesNamespace' :: forall context (m :: * -> *).
KubernetesClusterBasic context m =>
String -> Text -> m ()
createKubernetesNamespace' String
kubectl Text
namespace =
Label "kubernetesCluster" KubernetesClusterContext
-> m KubernetesClusterContext
forall context (l :: Symbol) a (m :: * -> *).
(HasLabel context l a, MonadReader context m) =>
Label l a -> m a
getContext Label "kubernetesCluster" KubernetesClusterContext
kubernetesCluster m KubernetesClusterContext
-> (KubernetesClusterContext -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\KubernetesClusterContext
kcc -> KubernetesClusterContext -> String -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Text -> m ()
createKubernetesNamespace'' KubernetesClusterContext
kcc String
kubectl Text
namespace)
destroyKubernetesNamespace :: (
KubectlBasic context m
)
=> Bool
-> Text
-> m ()
destroyKubernetesNamespace :: forall context (m :: * -> *).
KubectlBasic context m =>
Bool -> Text -> m ()
destroyKubernetesNamespace Bool
force Text
namespace =
forall (a :: Symbol) context (m :: * -> *).
(MonadReader context m, HasFile context a) =>
m String
askFile @"kubectl" m String -> (String -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\String
x -> String -> Bool -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
String -> Bool -> Text -> m ()
destroyKubernetesNamespace' String
x Bool
force Text
namespace)
destroyKubernetesNamespace' :: (
KubernetesClusterBasic context m
)
=> FilePath
-> Bool
-> Text
-> m ()
destroyKubernetesNamespace' :: forall context (m :: * -> *).
KubernetesClusterBasic context m =>
String -> Bool -> Text -> m ()
destroyKubernetesNamespace' String
kubectl Bool
force Text
namespace = do
Label "kubernetesCluster" KubernetesClusterContext
-> m KubernetesClusterContext
forall context (l :: Symbol) a (m :: * -> *).
(HasLabel context l a, MonadReader context m) =>
Label l a -> m a
getContext Label "kubernetesCluster" KubernetesClusterContext
kubernetesCluster m KubernetesClusterContext
-> (KubernetesClusterContext -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\KubernetesClusterContext
kcc -> KubernetesClusterContext -> String -> Bool -> Text -> m ()
forall context (m :: * -> *).
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Bool -> Text -> m ()
destroyKubernetesNamespace'' KubernetesClusterContext
kcc String
kubectl Bool
force Text
namespace)
createKubernetesNamespace'' :: (
KubernetesClusterBasic context m
)
=> KubernetesClusterContext
-> FilePath
-> Text
-> m ()
createKubernetesNamespace'' :: forall context (m :: * -> *).
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Text -> m ()
createKubernetesNamespace'' KubernetesClusterContext
kcc String
kubectl Text
namespace = do
let args :: [String]
args = [String
"create", String
"namespace", Text -> String
forall a. ToString a => a -> String
toString Text
namespace]
env <- KubernetesClusterContext -> m [(String, String)]
forall (m :: * -> *).
MonadLoggerIO m =>
KubernetesClusterContext -> m [(String, String)]
getKubectlEnvironment KubernetesClusterContext
kcc
createProcessWithLogging ((proc kubectl args) { env = Just env, delegate_ctlc = True })
>>= waitForProcess >>= (`shouldBe` ExitSuccess)
destroyKubernetesNamespace'' :: (
KubernetesClusterBasic context m
)
=> KubernetesClusterContext
-> FilePath
-> Bool
-> Text
-> m ()
destroyKubernetesNamespace'' :: forall context (m :: * -> *).
KubernetesClusterBasic context m =>
KubernetesClusterContext -> String -> Bool -> Text -> m ()
destroyKubernetesNamespace'' KubernetesClusterContext
kcc String
kubectl Bool
force Text
namespace = do
let args :: [String]
args = [String
"delete", String
"namespace", Text -> String
forall a. ToString a => a -> String
toString Text
namespace]
[String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> if Bool
force then [String
"--force"] else []
env <- KubernetesClusterContext -> m [(String, String)]
forall (m :: * -> *).
MonadLoggerIO m =>
KubernetesClusterContext -> m [(String, String)]
getKubectlEnvironment KubernetesClusterContext
kcc
createProcessWithLogging ((proc kubectl args) { env = Just env, delegate_ctlc = True })
>>= waitForProcess >>= (`shouldBe` ExitSuccess)