mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
Split clientaccess token/kubeconfig code
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
parent
45dd4afe50
commit
cdfc6cfa1a
48
pkg/clientaccess/kubeconfig.go
Normal file
48
pkg/clientaccess/kubeconfig.go
Normal file
@ -0,0 +1,48 @@
|
||||
package clientaccess
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
// WriteClientKubeConfig generates a kubeconfig at destFile that can be used to connect to a server at url with the given certs and keys
|
||||
func WriteClientKubeConfig(destFile string, url string, serverCAFile string, clientCertFile string, clientKeyFile string) error {
|
||||
serverCA, err := ioutil.ReadFile(serverCAFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read %s", serverCAFile)
|
||||
}
|
||||
|
||||
clientCert, err := ioutil.ReadFile(clientCertFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read %s", clientCertFile)
|
||||
}
|
||||
|
||||
clientKey, err := ioutil.ReadFile(clientKeyFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read %s", clientKeyFile)
|
||||
}
|
||||
|
||||
config := clientcmdapi.NewConfig()
|
||||
|
||||
cluster := clientcmdapi.NewCluster()
|
||||
cluster.CertificateAuthorityData = serverCA
|
||||
cluster.Server = url
|
||||
|
||||
authInfo := clientcmdapi.NewAuthInfo()
|
||||
authInfo.ClientCertificateData = clientCert
|
||||
authInfo.ClientKeyData = clientKey
|
||||
|
||||
context := clientcmdapi.NewContext()
|
||||
context.AuthInfo = "default"
|
||||
context.Cluster = "default"
|
||||
|
||||
config.Clusters["default"] = cluster
|
||||
config.AuthInfos["default"] = authInfo
|
||||
config.Contexts["default"] = context
|
||||
config.CurrentContext = "default"
|
||||
|
||||
return clientcmd.WriteToFile(*config, destFile)
|
||||
}
|
@ -12,8 +12,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -33,45 +31,6 @@ const (
|
||||
|
||||
type OverrideURLCallback func(config []byte) (*url.URL, error)
|
||||
|
||||
// WriteClientKubeConfig generates a kubeconfig at destFile that can be used to connect to a server at url with the given certs and keys
|
||||
func WriteClientKubeConfig(destFile string, url string, serverCAFile string, clientCertFile string, clientKeyFile string) error {
|
||||
serverCA, err := ioutil.ReadFile(serverCAFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read %s", serverCAFile)
|
||||
}
|
||||
|
||||
clientCert, err := ioutil.ReadFile(clientCertFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read %s", clientCertFile)
|
||||
}
|
||||
|
||||
clientKey, err := ioutil.ReadFile(clientKeyFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read %s", clientKeyFile)
|
||||
}
|
||||
|
||||
config := clientcmdapi.NewConfig()
|
||||
|
||||
cluster := clientcmdapi.NewCluster()
|
||||
cluster.CertificateAuthorityData = serverCA
|
||||
cluster.Server = url
|
||||
|
||||
authInfo := clientcmdapi.NewAuthInfo()
|
||||
authInfo.ClientCertificateData = clientCert
|
||||
authInfo.ClientKeyData = clientKey
|
||||
|
||||
context := clientcmdapi.NewContext()
|
||||
context.AuthInfo = "default"
|
||||
context.Cluster = "default"
|
||||
|
||||
config.Clusters["default"] = cluster
|
||||
config.AuthInfos["default"] = authInfo
|
||||
config.Contexts["default"] = context
|
||||
config.CurrentContext = "default"
|
||||
|
||||
return clientcmd.WriteToFile(*config, destFile)
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
CACerts []byte `json:"cacerts,omitempty"`
|
||||
BaseURL string `json:"baseurl,omitempty"`
|
Loading…
Reference in New Issue
Block a user