generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 77
/
kubeconfig.go
40 lines (33 loc) · 859 Bytes
/
kubeconfig.go
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
package kubernetes
import (
"os"
"path/filepath"
"github.com/layer5io/meshkit/models"
"github.com/layer5io/meshkit/utils"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
func (c *Client) GetKubeConfig() (*models.Kubeconfig, error) {
// Look for kubeconfig from the path mentioned in $KUBECONFIG
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = filepath.Join(utils.GetHome(), ".kube", "config")
}
var config *models.Kubeconfig
file, err := os.ReadFile(kubeconfig)
if err != nil {
err = errors.Wrap(err, "could not read kubeconfig:")
return nil, err
}
if err := yaml.Unmarshal(file, &config); err != nil {
return nil, err
}
return config, nil
}
func (c *Client) GetCurrentContext() (string, error) {
config, err := c.GetKubeConfig()
if err != nil {
return "", err
}
return config.CurrentContext, nil
}