Skip to content

Commit 03b3853

Browse files
committed
feat(k8s): introduce several retry functions to deal w/ flaky apis
Signed-off-by: Mitch Hulscher <[email protected]>
1 parent e63181e commit 03b3853

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

modules/k8s/kubectl.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"io/ioutil"
55
"net/url"
66
"os"
7+
"time"
78

89
"github.com/stretchr/testify/require"
910

11+
"github.com/gruntwork-io/terratest/modules/retry"
1012
"github.com/gruntwork-io/terratest/modules/shell"
1113
"github.com/gruntwork-io/terratest/modules/testing"
1214
)
@@ -22,6 +24,26 @@ func RunKubectlE(t testing.TestingT, options *KubectlOptions, args ...string) er
2224
return err
2325
}
2426

27+
// RunKubectlWithRetry will call kubectl using the provided options and args, failing the test after errors have been retried.
28+
func RunKubectlWithRetry(t testing.TestingT, options *KubectlOptions, retries int, sleepBetweenRetries time.Duration, args ...string) {
29+
err := RunKubectlWithRetryE(t, options, retries, sleepBetweenRetries, args...)
30+
require.NoError(t, err)
31+
}
32+
33+
// RunKubectlWithRetryE will call kubectl using the provided options and args, reytring on error.
34+
func RunKubectlWithRetryE(t testing.TestingT, options *KubectlOptions, retries int, sleepBetweenRetries time.Duration, args ...string) error {
35+
_, err := retry.DoWithRetryE(
36+
t,
37+
"",
38+
retries,
39+
sleepBetweenRetries,
40+
func() (string, error) {
41+
return "", RunKubectlE(t, options, args...)
42+
},
43+
)
44+
return err
45+
}
46+
2547
// RunKubectlAndGetOutputE will call kubectl using the provided options and args, returning the output of stdout and
2648
// stderr.
2749
func RunKubectlAndGetOutputE(t testing.TestingT, options *KubectlOptions, args ...string) (string, error) {
@@ -44,6 +66,29 @@ func RunKubectlAndGetOutputE(t testing.TestingT, options *KubectlOptions, args .
4466
return shell.RunCommandAndGetOutputE(t, command)
4567
}
4668

69+
// RunKubectlAndGetOutputWithRetry will call kubectl using the provided options and args, returning the output of stdout and
70+
// stderr, failing the test after errors have been retried.
71+
func RunKubectlAndGetOutputWithRetry(t testing.TestingT, options *KubectlOptions, retries int, sleepBetweenRetries time.Duration, args ...string) string {
72+
output, err := RunKubectlAndGetOutputWithRetryE(t, options, retries, sleepBetweenRetries, args...)
73+
require.NoError(t, err)
74+
return output
75+
}
76+
77+
// RunKubectlAndGetOutputWithRetryE will call kubectl using the provided options and args, returning the output of stdout and
78+
// stderr, reytring on error.
79+
func RunKubectlAndGetOutputWithRetryE(t testing.TestingT, options *KubectlOptions, retries int, sleepBetweenRetries time.Duration, args ...string) (string, error) {
80+
output, err := retry.DoWithRetryE(
81+
t,
82+
"",
83+
retries,
84+
sleepBetweenRetries,
85+
func() (string, error) {
86+
return RunKubectlAndGetOutputE(t, options, args...)
87+
},
88+
)
89+
return output, err
90+
}
91+
4792
// KubectlDelete will take in a file path and delete it from the cluster targeted by KubectlOptions. If there are any
4893
// errors, fail the test immediately.
4994
func KubectlDelete(t testing.TestingT, options *KubectlOptions, configPath string) {
@@ -122,6 +167,28 @@ func KubectlApplyFromStringE(t testing.TestingT, options *KubectlOptions, config
122167
return KubectlApplyE(t, options, tmpfile)
123168
}
124169

170+
// KubectlApplyFromStringWithRetry will take in a kubernetes resource config as a string and apply it on the cluster specified
171+
// by the provided kubectl options. The test fails after errors have been retried.
172+
func KubectlApplyFromStringWithRetry(t testing.TestingT, options *KubectlOptions, retries int, sleepBetweenRetries time.Duration, configData string) {
173+
err := KubectlApplyFromStringWithRetryE(t, options, retries, sleepBetweenRetries, configData)
174+
require.NoError(t, err)
175+
}
176+
177+
// KubectlApplyFromStringWithRetryE will take in a kubernetes resource config as a string and apply it on the cluster specified
178+
// by the provided kubectl options. Errors are retried.
179+
func KubectlApplyFromStringWithRetryE(t testing.TestingT, options *KubectlOptions, retries int, sleepBetweenRetries time.Duration, configData string) error {
180+
_, err := retry.DoWithRetryE(
181+
t,
182+
"",
183+
retries,
184+
sleepBetweenRetries,
185+
func() (string, error) {
186+
return "", KubectlApplyFromStringE(t, options, configData)
187+
},
188+
)
189+
return err
190+
}
191+
125192
// StoreConfigToTempFile will store the provided config data to a temporary file created on the os and return the
126193
// filename.
127194
func StoreConfigToTempFile(t testing.TestingT, configData string) string {

0 commit comments

Comments
 (0)