Skip to content

Commit 97e629e

Browse files
committed
Add EXISTING_KCP_CONTEXT to select a specific context from existing kubeconfig
On-behalf-of: SAP <[email protected]> Signed-off-by: Marvin Beckers <[email protected]>
1 parent 9c0f035 commit 97e629e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

envtest/server.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var log = ctrllog.Log.WithName("envtest")
4242
/*
4343
It's possible to override some defaults, by setting the following environment variables:
4444
* USE_EXISTING_KCP (boolean): if set to true, envtest will use an existing kcp.
45+
* EXISTING_KCP_CONTEXT (string): if USE_EXISTING_KCP is set, this is the context loaded from the kubeconfig for connecting to the existing kcp.
4546
* TEST_ASSET_KCP (string): path to the kcp binary to use
4647
* TEST_KCP_ASSETS (string): directory containing the binaries to use (kcp). Defaults to /usr/local/kcp/bin.
4748
* TEST_KCP_START_TIMEOUT (string supported by time.ParseDuration): timeout for test kcp to start. Defaults to 1m.
@@ -50,6 +51,7 @@ It's possible to override some defaults, by setting the following environment va
5051
*/
5152
const (
5253
envUseExistingCluster = "USE_EXISTING_KCP"
54+
envExistingKcpContext = "EXISTING_KCP_CONTEXT"
5355
envAttachOutput = "TEST_ATTACH_KCP_OUTPUT"
5456
envStartTimeout = "TEST_KCP_START_TIMEOUT"
5557
envStopTimeout = "TEST_KCP_STOP_TIMEOUT"
@@ -127,6 +129,12 @@ type Environment struct {
127129
// It defaults to the USE_EXISTING_KCP environment variable if unspecified.
128130
UseExistingKcp *bool
129131

132+
// ExistingKcpContext indicates that when UseExistingKcp is set to true,
133+
// a specific context should be loaded from a kubeconfig instead of the
134+
// current one. It defaults to the EXISTING_KCP_CONTEXT environment variable
135+
// if unspecified.
136+
ExistingKcpContext string
137+
130138
// KcpStartTimeout is the maximum duration each kcp component
131139
// may take to start. It defaults to the TEST_KCP_START_TIMEOUT
132140
// environment variable or 20 seconds if unspecified.
@@ -163,8 +171,15 @@ func (te *Environment) Start() (*rest.Config, error) {
163171
// only load a config if it hasn't already been set.
164172
log.V(1).Info("automatically acquiring client configuration")
165173

174+
kubeContext := te.ExistingKcpContext
175+
if kubeContext == "" {
176+
// if the env variable doesn't exist this is empty string, which is fine,
177+
// because that is the currently active context.
178+
kubeContext = os.Getenv(envExistingKcpContext)
179+
}
180+
166181
var err error
167-
te.Config, err = config.GetConfig()
182+
te.Config, err = config.GetConfigWithContext(kubeContext)
168183
if err != nil {
169184
return nil, fmt.Errorf("unable to get configuration for existing cluster: %w", err)
170185
}

0 commit comments

Comments
 (0)