-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/kube/kubetest: Add
kubetest
package
This commit adds the `kubetest` subpackage to the `kube` package. It contains a small set of helpers for setuping up kubernetes' `envtest.Environment`.
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package kubetest | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/redpanda-data/helm-charts/pkg/kube" | ||
"github.com/stretchr/testify/require" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
) | ||
|
||
const controlPlaneVersion = "1.30.x" | ||
|
||
// NewEnv starts a local kubernetes control plane via [envtest.Environment] and | ||
// returns a [kube.Ctl] to access it. The provided [testing.T] will be used to | ||
// shutdown the control plane at the end of the test. | ||
func NewEnv(t *testing.T) *kube.Ctl { | ||
// TODO: Would be nice to instead just import setup-envtest but the package | ||
// isn't exactly friendly to be used as a library. Alternatively, we could | ||
// use nix to provide the etcd and kubeapi-server binaries as that's all | ||
// setup-envtest does. | ||
if _, err := exec.LookPath("setup-envtest"); err != nil { | ||
t.Fatalf("setup-envtest not found in $PATH. Did you forget nix develop?") | ||
} | ||
|
||
stdout, err := exec.Command("setup-envtest", "use", controlPlaneVersion, "-p", "path").CombinedOutput() | ||
require.NoError(t, err) | ||
|
||
env := envtest.Environment{ | ||
BinaryAssetsDirectory: string(stdout), | ||
ControlPlaneStartTimeout: 30 * time.Second, | ||
ControlPlaneStopTimeout: 30 * time.Second, | ||
} | ||
|
||
cfg, err := env.Start() | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
require.NoError(t, env.Stop()) | ||
}) | ||
|
||
ctl, err := kube.FromRESTConfig(cfg) | ||
require.NoError(t, err) | ||
|
||
return ctl | ||
} |