-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I create kube config file from rest.Config object? #711
Comments
I think I will need to convert my |
Following. Have similar use case, to convert rest config to kubeconfig file |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Ultimately, it's about converting https://pkg.go.dev/k8s.io/client-go/rest?tab=doc#Config into https://pkg.go.dev/k8s.io/client-go/tools/clientcmd/api?tab=doc#Config |
Stale issues rot after 30d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Rotten issues close after 30d of inactivity. Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
@fejta-bot: Closing this issue. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
This works c, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
os.Exit(1)
}
clientConfig := clientcmd.NewDefaultClientConfig(*c, nil)
config, err = clientConfig.ClientConfig() |
In my case, I had a func (kc *KubeController) GenerateKubeConfiguration(namespace string) (string, error) {
// Get secret list for namespace
secretList, err := kc.clientset.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return "", err
}
// Get secret containing authentication data
secret := secretList.Items[1]
clusters := make(map[string]*clientcmdapi.Cluster)
clusters["default-cluster"] = &clientcmdapi.Cluster{
Server: kc.restconfig.Host,
CertificateAuthorityData: secret.Data["ca.crt"],
}
contexts := make(map[string]*clientcmdapi.Context)
contexts["default-context"] = &clientcmdapi.Context{
Cluster: "default-cluster",
Namespace: namespace,
AuthInfo: namespace,
}
authinfos := make(map[string]*clientcmdapi.AuthInfo)
authinfos[namespace] = &clientcmdapi.AuthInfo{
Token: string(secret.Data["token"]),
}
clientConfig := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: clusters,
Contexts: contexts,
CurrentContext: "default-context",
AuthInfos: authinfos,
}
clientcmd.WriteToFile(clientConfig, "/var/tmp/" + namespace + ".kubeconfig")
} |
very useful!!! |
I found a new way. Test ok~
|
just an easier way to create a kubeconfig file from the rest.Config coming from import (
"os"
"k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
func CreateKubeconfigFileForRestConfig(restConfig rest.Config) string {
clusters := make(map[string]*clientcmdapi.Cluster)
clusters["default-cluster"] = &clientcmdapi.Cluster{
Server: restConfig.Host,
CertificateAuthorityData: restConfig.CAData,
}
contexts := make(map[string]*clientcmdapi.Context)
contexts["default-context"] = &clientcmdapi.Context{
Cluster: "default-cluster",
AuthInfo: "default-user",
}
authinfos := make(map[string]*clientcmdapi.AuthInfo)
authinfos["default-user"] = &clientcmdapi.AuthInfo{
ClientCertificateData: restConfig.CertData,
ClientKeyData: restConfig.KeyData,
}
clientConfig := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: clusters,
Contexts: contexts,
CurrentContext: "default-context",
AuthInfos: authinfos,
}
kubeConfigFile, _ := os.CreateTemp("", "kubeconfig")
_ = clientcmd.WriteToFile(clientConfig, kubeConfigFile.Name())
return kubeConfigFile.Name()
} |
This will only work on a rest.Config object having a cert and key data. The other way to do it by @Jordan396 above works on any rest.Config object (it just uses the API server host). |
I have
rest.Config
(https://godoc.org/k8s.io/client-go/rest#Config) object in my code. I want to write it to a kube config file to use it later. How would I go about writing it to a file?The text was updated successfully, but these errors were encountered: