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