-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: dump the kubeconfig for the testenv server
This is going to be useful to inspect the state of the cluster after a test fails. Set the env variable SKIP_CLEANUP to "true" or "1" to skip the cleanup of the environment to make use of the kubeconfig. The kubeconfig is dumped in the testbin directory. After debugging you will have to kill the testEnv. The two binaries that are used by the testEnv are etcd and kube-apiserver. Check for the running process with a command like ps aux | grep -e kube-apiserver -e etcd Signed-off-by: Raghavendra Talur <raghavendra.talur@gmail.com> (cherry picked from commit 07a61b4)
- Loading branch information
1 parent
4664e7e
commit 9157b6a
Showing
3 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package controllers | ||
|
||
import ( | ||
"os" | ||
|
||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/client-go/tools/clientcmd/api" | ||
) | ||
|
||
// WriteKubeConfigToFile writes the kubeconfig content to a specified file path. | ||
func WriteKubeConfigToFile(kubeConfigContent, filePath string) error { | ||
return os.WriteFile(filePath, []byte(kubeConfigContent), 0o600) | ||
} | ||
|
||
// ConvertRestConfigToKubeConfig converts a rest.Config to a kubeconfig string. | ||
func ConvertRestConfigToKubeConfig(restConfig *rest.Config) (string, error) { | ||
kubeConfig := api.NewConfig() | ||
|
||
cluster := api.NewCluster() | ||
cluster.Server = restConfig.Host | ||
cluster.CertificateAuthorityData = restConfig.CAData | ||
|
||
user := api.NewAuthInfo() | ||
user.ClientCertificateData = restConfig.CertData | ||
user.ClientKeyData = restConfig.KeyData | ||
user.Token = restConfig.BearerToken | ||
|
||
context := api.NewContext() | ||
context.Cluster = "cluster" | ||
context.AuthInfo = "user" | ||
|
||
kubeConfig.Clusters["cluster"] = cluster | ||
kubeConfig.AuthInfos["user"] = user | ||
kubeConfig.Contexts["test"] = context | ||
kubeConfig.CurrentContext = "test" | ||
|
||
kubeConfigContent, err := clientcmd.Write(*kubeConfig) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(kubeConfigContent), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters