@@ -42,6 +42,7 @@ var log = ctrllog.Log.WithName("envtest")
4242/*
4343It'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*/
5152const (
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"
@@ -124,21 +126,28 @@ type Environment struct {
124126
125127 // UseExistingCluster indicates that this environments should use an
126128 // existing kubeconfig, instead of trying to stand up a new kcp.
129+ // It defaults to the USE_EXISTING_KCP environment variable if unspecified.
127130 UseExistingKcp * bool
128131
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+
129138 // KcpStartTimeout is the maximum duration each kcp component
130139 // may take to start. It defaults to the TEST_KCP_START_TIMEOUT
131- // environment variable or 20 seconds if unspecified
140+ // environment variable or 20 seconds if unspecified.
132141 KcpStartTimeout time.Duration
133142
134143 // KcpStopTimeout is the maximum duration each kcp component
135144 // may take to stop. It defaults to the TEST_KCP_STOP_TIMEOUT
136- // environment variable or 20 seconds if unspecified
145+ // environment variable or 20 seconds if unspecified.
137146 KcpStopTimeout time.Duration
138147
139148 // AttachKcpOutput indicates if kcp output will be attached to os.Stdout and os.Stderr.
140149 // Enable this to get more visibility of the testing kcp.
141- // It respect TEST_ATTACH_KCP_OUTPUT environment variable.
150+ // It respects the the TEST_ATTACH_KCP_OUTPUT environment variable.
142151 AttachKcpOutput bool
143152}
144153
@@ -162,11 +171,22 @@ func (te *Environment) Start() (*rest.Config, error) {
162171 // only load a config if it hasn't already been set.
163172 log .V (1 ).Info ("automatically acquiring client configuration" )
164173
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+
165181 var err error
166- te .Config , err = config .GetConfig ( )
182+ te .Config , err = config .GetConfigWithContext ( kubeContext )
167183 if err != nil {
168184 return nil , fmt .Errorf ("unable to get configuration for existing cluster: %w" , err )
169185 }
186+
187+ if strings .Contains (te .Config .Host , "/clusters/" ) {
188+ return nil , fmt .Errorf ("'%s' contains /clusters/ but should point to base context" , te .Config .Host )
189+ }
170190 }
171191 } else {
172192 shard := te .Kcp .GetRootShard ()
0 commit comments