sandwich-contexts-kubernetes: Get a couple more kubectl from context

This commit is contained in:
thomasjm 2024-05-27 02:32:57 -07:00
parent 0928961b23
commit 73191ed135
2 changed files with 19 additions and 9 deletions

View File

@ -1,3 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
module Test.Sandwich.Contexts.Kubernetes.KubectlLogs (
KubectlLogsContext (..)
@ -12,6 +14,7 @@ import qualified Data.Text as T
import Relude hiding (withFile)
import System.FilePath
import Test.Sandwich
import Test.Sandwich.Contexts.Files
import Test.Sandwich.Util.Process (gracefullyStopProcess)
import UnliftIO.Exception
import UnliftIO.IO (withFile)
@ -27,9 +30,12 @@ data KubectlLogsContext = KubectlLogsContext
-- | 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 forwarding
withKubectlLogs :: (
HasBaseContextMonad ctx m, MonadLogger m, MonadFail m, MonadUnliftIO m
MonadLogger m, MonadFail m, MonadUnliftIO m
, HasBaseContextMonad ctx m, HasFile ctx "kubectl"
) => FilePath -> Text -> Text -> Maybe Text -> Bool -> (KubectlLogsContext -> m a) -> m a
withKubectlLogs kubeConfigFile namespace target maybeContainer interruptWhenDone action = do
kubectlBinary <- askFile @"kubectl"
let args = ["logs", toString target
, "--namespace", toString namespace
, "--kubeconfig", kubeConfigFile]
@ -43,10 +49,11 @@ withKubectlLogs kubeConfigFile namespace target maybeContainer interruptWhenDone
withFile logPath WriteMode $ \h -> do
hSetBuffering h LineBuffering
bracket (createProcess ((proc "kubectl" args) { std_out = UseHandle h
, std_err = UseHandle h
, create_group = True
}))
bracket (createProcess ((proc kubectlBinary args) {
std_out = UseHandle h
, std_err = UseHandle h
, create_group = True
}))
(\(_, _, _, ps) -> if
| interruptWhenDone -> void $ gracefullyStopProcess ps 30_000_000
| otherwise -> void $ waitForProcess ps

View File

@ -1,12 +1,11 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Test.Sandwich.Contexts.Kubernetes.Waits where
import Test.Sandwich.Contexts.Kubernetes.Run
import Test.Sandwich.Contexts.Kubernetes.Types
import Control.Monad.Catch (MonadMask)
import Control.Monad.IO.Unlift
import Control.Monad.Logger
@ -21,6 +20,9 @@ import Kubernetes.OpenAPI.Model as Kubernetes
import Relude
import System.Exit
import Test.Sandwich
import Test.Sandwich.Contexts.Files
import Test.Sandwich.Contexts.Kubernetes.Run
import Test.Sandwich.Contexts.Kubernetes.Types
import Test.Sandwich.Contexts.Waits
import UnliftIO.Process
@ -82,13 +84,14 @@ listPods namespace labels =
waitForPodsToBeReady :: (
MonadUnliftIO m, MonadLogger m
, MonadReader context m, HasKubernetesClusterContext context
, MonadReader context m, HasKubernetesClusterContext context, HasFile context "kubectl"
) => Text -> Map Text Text -> Double -> m ()
waitForPodsToBeReady namespace labels timeInSeconds = do
kubectlBinary <- askFile @"kubectl"
kubeConfigFile <- kubernetesClusterKubeConfigPath <$> getContext kubernetesCluster
let labelArgs = [[i|-l #{k}=#{v}|] | (k, v) <- M.toList labels]
p <- createProcessWithLogging (proc "kubectl" (
p <- createProcessWithLogging (proc kubectlBinary (
["wait", "pods"
, "--kubeconfig", kubeConfigFile
, "-n", toString namespace