Skip to content

Commit 51ac8dc

Browse files
committed
fixup: Cache kubeconfig
1 parent 29737e4 commit 51ac8dc

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/fixture/tmpnet/kube_runtime.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ func (c *KubeRuntimeConfig) ensureDefaults(ctx context.Context, log logging.Logg
160160

161161
type KubeRuntime struct {
162162
node *Node
163+
164+
kubeConfig *restclient.Config
163165
}
164166

165167
// readState reads the URI and staking address for the node if the node is running.
@@ -818,13 +820,23 @@ func (p *KubeRuntime) runtimeConfig() *KubeRuntimeConfig {
818820
return p.node.getRuntimeConfig().Kube
819821
}
820822

823+
// getKubeconfig retrieves the kubeconfig for the target cluster. It
824+
// will be cached after the first call to avoid unnecessary logging
825+
// when running in-cluster.
821826
func (p *KubeRuntime) getKubeconfig() (*restclient.Config, error) {
822-
runtimeConfig := p.runtimeConfig()
823-
return GetClientConfig(
824-
p.node.network.log,
825-
runtimeConfig.ConfigPath,
826-
runtimeConfig.ConfigContext,
827-
)
827+
if p.kubeConfig == nil {
828+
runtimeConfig := p.runtimeConfig()
829+
config, err := GetClientConfig(
830+
p.node.network.log,
831+
runtimeConfig.ConfigPath,
832+
runtimeConfig.ConfigContext,
833+
)
834+
if err != nil {
835+
return nil, fmt.Errorf("failed to get kubeconfig: %w", err)
836+
}
837+
p.kubeConfig = config
838+
}
839+
return p.kubeConfig, nil
828840
}
829841

830842
func (p *KubeRuntime) getClientset() (*kubernetes.Clientset, error) {

0 commit comments

Comments
 (0)