k8s.io/client-go: add OverridingClientConfig overriding RawConfig

Kubernetes-commit: 740b4c456d731922196f8231df9ab585198696d6
This commit is contained in:
Ivo Gosemann
2023-07-18 13:40:12 +02:00
committed by Kubernetes Publisher
parent 17b5405ddb
commit 89528c43be
2 changed files with 58 additions and 68 deletions

View File

@@ -1015,70 +1015,66 @@ func TestCleanANSIEscapeCodes(t *testing.T) {
}
func TestMergeRawConfigDoOverride(t *testing.T) {
cfg := createValidTestConfig()
const (
server = "https://anything.com:8080"
token = "the-token"
modifiedServer = "http://localhost:8081"
modifiedToken = "modified-token"
)
config := createValidTestConfig()
// add another context which to modify with overrides
config.Clusters["modify"] = &clientcmdapi.Cluster{
Server: server,
}
config.AuthInfos["modify"] = &clientcmdapi.AuthInfo{
Token: token,
}
config.Contexts["modify"] = &clientcmdapi.Context{
Cluster: "modify",
AuthInfo: "modify",
Namespace: "modify",
}
// create overrides for the modify context
overrides := &ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: "http://localhost:8081",
Server: modifiedServer,
},
Context: clientcmdapi.Context{
Namespace: "foobar",
Cluster: "clean",
AuthInfo: "clean",
Cluster: "modify",
AuthInfo: "modify",
},
AuthInfo: clientcmdapi.AuthInfo{
Token: "modified-token",
Token: modifiedToken,
},
CurrentContext: "clean",
CurrentContext: "modify",
}
cut, err := NewClientConfigWithMergedRawConfig(*cfg, overrides)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
act, err := cut.RawConfig()
cut := NewDefaultClientConfig(*config, overrides)
act, err := cut.MergedRawConfig()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if act.Clusters["clean"].Server != "http://localhost:8081" {
t.Errorf("Expected server %v, got %v", "http://localhost:8081", act.Clusters["clean"].Server)
// ensure overrides were applied to "modify"
actContext := act.CurrentContext
if actContext != "modify" {
t.Errorf("Expected context %v, got %v", "modify", actContext)
}
if act.Clusters[actContext].Server != "http://localhost:8081" {
t.Errorf("Expected server %v, got %v", "http://localhost:8081", act.Clusters[actContext].Server)
}
if act.Contexts[actContext].Namespace != "foobar" {
t.Errorf("Expected namespace %v, got %v", "foobar", act.Contexts[actContext].Namespace)
}
if act.Contexts["clean"].Namespace != "foobar" {
t.Errorf("Expected namespace %v, got %v", "foobar", act.Contexts["clean"].Namespace)
}
}
func TestMergeRawConfigDoNotOverride(t *testing.T) {
cfg := createValidTestConfig()
overrides := &ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: "http://localhost:8081",
},
Context: clientcmdapi.Context{
Namespace: "foobar",
Cluster: "clean",
AuthInfo: "clean",
},
AuthInfo: clientcmdapi.AuthInfo{
Token: "modified-token",
},
CurrentContext: "clean",
}
cut := NewDefaultClientConfig(*cfg, overrides)
act, err := cut.RawConfig()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if act.Clusters["clean"].Server != cfg.Clusters["clean"].Server {
t.Errorf("Expected server %v, got %v", cfg.Clusters["clean"].Server, act.Clusters["clean"].Server)
}
if act.Contexts["clean"].Namespace != cfg.Contexts["clean"].Namespace {
t.Errorf("Expected namespace %v, got %v", cfg.Contexts["clean"].Namespace, act.Contexts["clean"].Namespace)
// ensure context "clean" was not touched
if act.Clusters["clean"].Server != config.Clusters["clean"].Server {
t.Errorf("Expected server %v, got %v", config.Clusters["clean"].Server, act.Clusters["clean"].Server)
}
if act.Contexts["clean"].Namespace != config.Contexts["clean"].Namespace {
t.Errorf("Expected namespace %v, got %v", config.Contexts["clean"].Namespace, act.Contexts["clean"].Namespace)
}
}