1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
package k8s
import (
"io/ioutil"
"github.com/scaleway/scaleway-sdk-go/internal/errors"
"github.com/scaleway/scaleway-sdk-go/scw"
"gopkg.in/yaml.v2"
)
// Kubeconfig represents a kubernetes kubeconfig file
type Kubeconfig struct {
raw []byte
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
CurrentContext string `yaml:"current-context"`
Clusters []*KubeconfigClusterWithName `yaml:"clusters"`
Contexts []*KubeconfigContextWithName `yaml:"contexts"`
Users []*KubeconfigUserWithName `yaml:"users"`
}
// KubeconfigUserWithName represents a named cluster in the kubeconfig file
type KubeconfigClusterWithName struct {
Name string `yaml:"name"`
Cluster KubeconfigCluster `yaml:"cluster"`
}
// KubeconfigCluster represents a cluster in the kubeconfig file
type KubeconfigCluster struct {
Server string `yaml:"server,omitempty"`
CertificateAuthorityData string `yaml:"certificate-authority-data,omitempty"`
}
// KubeconfigContextWithName represents a named context in the kubeconfig file
type KubeconfigContextWithName struct {
Name string `yaml:"name"`
Context KubeconfigContext `yaml:"context"`
}
// KubeconfigContext represents a context in the kubeconfig file
type KubeconfigContext struct {
Cluster string `yaml:"cluster"`
Namespace string `yaml:"namespace,omitempty"`
User string `yaml:"user"`
}
// KubeconfigUserWithName represents a named user in the kubeconfig file
type KubeconfigUserWithName struct {
Name string `yaml:"name"`
User KubeconfigUser `yaml:"user"`
}
// KubeconfigUser represents a user in the kubeconfig file
type KubeconfigUser struct {
ClientCertificateData string `yaml:"client-certificate-data,omitempty"`
ClientKeyData string `yaml:"client-key-data,omitempty"`
Password string `yaml:"password,omitempty"`
Username string `yaml:"username,omitempty"`
Token string `yaml:"token,omitempty"`
}
// GetRaw returns the raw bytes of the kubeconfig
func (k *Kubeconfig) GetRaw() []byte {
return k.raw
}
// GetServer returns the server URL of the cluster in the kubeconfig
func (k *Kubeconfig) GetServer() (string, error) {
if len(k.Clusters) != 1 {
return "", errors.New("kubeconfig should have only one cluster")
}
return k.Clusters[0].Cluster.Server, nil
}
// GetCertificateAuthorityData returns the server certificate authority data of the cluster in the kubeconfig
func (k *Kubeconfig) GetCertificateAuthorityData() (string, error) {
if len(k.Clusters) != 1 {
return "", errors.New("kubeconfig should have only one cluster")
}
return k.Clusters[0].Cluster.CertificateAuthorityData, nil
}
// GetToken returns the token for the cluster in the kubeconfig
func (k *Kubeconfig) GetToken() (string, error) {
if len(k.Users) != 1 {
return "", errors.New("kubeconfig should have only one user")
}
return k.Users[0].User.Token, nil
}
// GetClusterKubeConfig downloads the kubeconfig for the given cluster
func (s *API) GetClusterKubeConfig(req *GetClusterKubeConfigRequest, opts ...scw.RequestOption) (*Kubeconfig, error) {
kubeconfigFile, err := s.getClusterKubeConfig(&GetClusterKubeConfigRequest{
Region: req.Region,
ClusterID: req.ClusterID,
})
if err != nil {
return nil, errors.Wrap(err, "error getting cluster kubeconfig")
}
kubeconfigContent, err := ioutil.ReadAll(kubeconfigFile.Content)
if err != nil {
return nil, errors.Wrap(err, "error reading kubeconfig content")
}
var kubeconfig Kubeconfig
err = yaml.Unmarshal(kubeconfigContent, &kubeconfig)
if err != nil {
return nil, errors.Wrap(err, "error unmarshaling kubeconfig")
}
kubeconfig.raw = kubeconfigContent
return &kubeconfig, nil
}
|