Merge pull request #2497 from crazy-max/fix-k8s-kubeconfig

k8s: fix concurrent kubeconfig access when loading nodes
This commit is contained in:
CrazyMax 2024-06-04 12:10:44 +02:00 committed by GitHub
commit 747b75a217
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 7 deletions

View File

@ -9,8 +9,8 @@ contexts:
cluster: test-cluster cluster: test-cluster
user: test-user user: test-user
namespace: zoinx namespace: zoinx
name: test name: k3s
current-context: test current-context: k3s
kind: Config kind: Config
preferences: {} preferences: {}
users: users:

View File

@ -167,11 +167,12 @@ func NewKubernetesConfig(configPath string) clientcmd.ClientConfig {
// ConfigFromEndpoint loads kubernetes config from endpoint // ConfigFromEndpoint loads kubernetes config from endpoint
func ConfigFromEndpoint(endpointName string, s store.Reader) (clientcmd.ClientConfig, error) { func ConfigFromEndpoint(endpointName string, s store.Reader) (clientcmd.ClientConfig, error) {
if strings.HasPrefix(endpointName, "kubernetes://") { if strings.HasPrefix(endpointName, "kubernetes://") {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
u, _ := url.Parse(endpointName) u, _ := url.Parse(endpointName)
if kubeconfig := u.Query().Get("kubeconfig"); kubeconfig != "" { if kubeconfig := u.Query().Get("kubeconfig"); kubeconfig != "" {
_ = os.Setenv(clientcmd.RecommendedConfigPathEnvVar, kubeconfig) rules.Precedence = append(rules.Precedence, kubeconfig)
rules.ExplicitPath = kubeconfig
} }
rules := clientcmd.NewDefaultClientConfigLoadingRules()
apiConfig, err := rules.Load() apiConfig, err := rules.Load()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,20 +1,35 @@
package context package context
import ( import (
"os"
"testing" "testing"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/context/store"
cliflags "github.com/docker/cli/cli/flags" cliflags "github.com/docker/cli/cli/flags"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestDefaultContextInitializer(t *testing.T) { func TestDefaultContextInitializer(t *testing.T) {
os.Setenv("KUBECONFIG", "./fixtures/test-kubeconfig") t.Setenv("KUBECONFIG", "./fixtures/test-kubeconfig")
defer os.Unsetenv("KUBECONFIG")
ctx, err := command.ResolveDefaultContext(&cliflags.ClientOptions{}, command.DefaultContextStoreConfig()) ctx, err := command.ResolveDefaultContext(&cliflags.ClientOptions{}, command.DefaultContextStoreConfig())
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "default", ctx.Meta.Name) assert.Equal(t, "default", ctx.Meta.Name)
assert.Equal(t, "zoinx", ctx.Meta.Endpoints[KubernetesEndpoint].(EndpointMeta).DefaultNamespace) assert.Equal(t, "zoinx", ctx.Meta.Endpoints[KubernetesEndpoint].(EndpointMeta).DefaultNamespace)
} }
func TestConfigFromEndpoint(t *testing.T) {
t.Setenv("KUBECONFIG", "./fixtures/test-kubeconfig")
cfg, err := ConfigFromEndpoint(
"kubernetes:///buildx-test-4c972a3f9d369614b40f28a281790c7e?deployment=buildkit-4c2ed3ed-970f-4f3d-a6df-a4fcbab4d5cf-d9d73&kubeconfig=.%2Ffixtures%2Fk3s-kubeconfig",
store.New(config.ContextStoreDir(), command.DefaultContextStoreConfig()),
)
require.NoError(t, err)
rawcfg, err := cfg.RawConfig()
require.NoError(t, err)
ctxcfg := "k3s"
if _, ok := rawcfg.Contexts[ctxcfg]; !ok {
t.Errorf("Context config %q not found", ctxcfg)
}
}