Skip to content

KubeConfig.loadFromDefault currentContext is inconsistent with kubectl with multiple kubeconfigs #579

@adrianord

Description

@adrianord

When using multiple kubeconfigs through the KUBECONFIG environment variable, kubectl always uses the first file in the KUBECONFIG environment variable to grab the current context.

One example found here
https://github.com/kubernetes/kubectl/blob/952f50e545b14e0a2a0781ef47c342cdba5a8c54/pkg/cmd/config/current_context.go#L62-L76

However, because loadFromDefault uses mergeConfig, it always overwrites the currentContext with next kubeconfig it merges, causing it to use the last kubeconfig's context.

javascript/src/config.ts

Lines 234 to 245 in a8d4f1c

public mergeConfig(config: KubeConfig): void {
this.currentContext = config.currentContext;
config.clusters.forEach((cluster: Cluster) => {
this.addCluster(cluster);
});
config.users.forEach((user: User) => {
this.addUser(user);
});
config.contexts.forEach((ctx: Context) => {
this.addContext(ctx);
});
}

This causes using kubectl to change the context to not affect loadFromDefault when making CLI tools where the users sets the KUBECONFIG environment variable with multiple kubeconfigs since kubectl use-context sets the context in the first kubeconfig in the environment variable.
https://github.com/kubernetes/kubectl/blob/952f50e545b14e0a2a0781ef47c342cdba5a8c54/pkg/cmd/config/use_context.go#L65-L79

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions