fix(k8s): memory leak during k8s stack deployment [BE-12281] (#1264)

release/2.33
Oscar Zhou 2025-09-30 18:00:12 +13:00 committed by GitHub
parent 7450501b7a
commit 74b1dd04d1
2 changed files with 58 additions and 1 deletions

View File

@ -46,7 +46,8 @@ func generateConfigFlags(token, server, namespace, kubeconfigPath string, insecu
return nil, errors.New("must provide either a kubeconfig path or a server")
}
configFlags := genericclioptions.NewConfigFlags(true)
// Pass 'false' to usePersistentConfig to prevent memory leaks.
configFlags := genericclioptions.NewConfigFlags(false)
if namespace != "" {
configFlags.Namespace = &namespace
}

View File

@ -0,0 +1,56 @@
package libkubectl
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGenerateConfigFlags(t *testing.T) {
config, err := generateConfigFlags("test-token", "https://api.example.com", "", "", false)
require.NoError(t, err)
require.NotNil(t, config)
_, err = generateConfigFlags("test-token", "", "", "", false)
require.Error(t, err)
}
func TestNewClient(t *testing.T) {
// Test with server and token
client, err := NewClient(&ClientAccess{
Token: "test-token",
ServerUrl: "https://api.example.com",
}, "", "", false)
require.NoError(t, err)
require.NotNil(t, client)
// Verify the client has the expected structure for a Kubernetes client
require.NotNil(t, client.factory, "Expected factory to be set")
require.NotNil(t, client.streams, "Expected streams to be set")
require.NotNil(t, client.out, "Expected output buffer to be set")
}
func TestNewClientWithKubeconfig(t *testing.T) {
// Test with kubeconfig path
client, err := NewClient(&ClientAccess{
Token: "",
ServerUrl: "",
}, "test-namespace", "/path/to/kubeconfig", true)
require.NoError(t, err)
require.NotNil(t, client)
// Verify the client has the expected structure for a Kubernetes client
require.NotNil(t, client.factory, "Expected factory to be set")
require.NotNil(t, client.streams, "Expected streams to be set")
require.NotNil(t, client.out, "Expected output buffer to be set")
}
func TestNewClientError(t *testing.T) {
// Test error case when both server and kubeconfig are empty
client, err := NewClient(&ClientAccess{
Token: "",
ServerUrl: "",
}, "", "", false)
require.Error(t, err)
require.Nil(t, client)
}