Skip to content

Commit

Permalink
sandwich-contexts-kubernetes: add withImageLoadRetry
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Aug 2, 2024
1 parent 908ff28 commit 53c9aea
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module Test.Sandwich.Contexts.Kubernetes.Images (
, loadImage
, loadImage'

, withImageLoadRetry
, withImageLoadRetry'

, introduceImages

, findAllImages
Expand All @@ -25,8 +28,10 @@ module Test.Sandwich.Contexts.Kubernetes.Images (
, ImagePullPolicy(..)
) where

import Control.Monad.Catch (Handler(..), MonadMask)
import Control.Monad.IO.Unlift
import Control.Monad.Logger
import Control.Retry
import Data.String.Interpolate
import Data.Text as T
import Relude
Expand Down Expand Up @@ -169,6 +174,20 @@ loadImage' (KubernetesClusterContext {kubernetesClusterType, kubernetesClusterNa
-- loadedImages `shouldContain` [image']
-- return image'

-- | Same as 'withImageLoadRetry'', but with a reasonable default retry policy.
withImageLoadRetry :: (MonadLoggerIO m, MonadMask m) => ImageLoadSpec -> m a -> m a
withImageLoadRetry = withImageLoadRetry' (exponentialBackoff 50000 <> limitRetries 5)

-- | A combinator to wrap around your 'loadImage' or 'loadImageIfNecessary' calls to retry
-- on failure. Image loads sometimes fail on Minikube (version 1.33.0 at time of writing).
withImageLoadRetry' :: (MonadLoggerIO m, MonadMask m) => RetryPolicyM m -> ImageLoadSpec -> m a -> m a
withImageLoadRetry' policy ils action =
recovering policy [\_status -> Handler (\(e :: FailureReason) -> do
warn [i|#{ils}: retrying load due to exception: #{e}|]
return True)] $ \_ ->
action


-- | Helper to introduce a list of images into a Kubernetes cluster.
-- Stores the list of transformed image names under the "kubernetesClusterImages" label.
introduceImages :: (
Expand Down

0 comments on commit 53c9aea

Please sign in to comment.