-
Notifications
You must be signed in to change notification settings - Fork 567
/
kubeconfig.go
127 lines (117 loc) · 4 KB
/
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
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
119
120
121
122
123
124
125
126
127
package kindenv
import (
"context"
"io"
"net/http"
"os"
"os/exec"
"strings"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
"github.com/pachyderm/pachyderm/v2/src/internal/promutil"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// Kubeconfig is the path to a kubeconfig file. It has methods on it for working on the cluster
// that is configured by that file.
type Kubeconfig string
// testSentinel is used by the tests to indicate that the in-memory Kube client declared below
// should be used.
const testSentinel = Kubeconfig("\x00test-sentinel\x00")
// testKubeClient is an in-memory Kube client for tests of this package.
var testKubeClient kubernetes.Interface
// GetKubeconfig returns a Kubeconfig object for the cluster. Call `Close()` when you're done to
// avoid filling up /tmp with junk.
func (c *Cluster) GetKubeconfig(ctx context.Context) (Kubeconfig, error) {
var internal bool
if ci := os.Getenv("CI"); ci == "true" {
internal = true
}
if c.kubeconfig != "" {
return c.kubeconfig, nil
}
f, err := os.CreateTemp("", "pachdev-kubeconfig-*")
if err != nil {
return "", errors.Wrap(err, "create tmpfile for config")
}
cfg, err := c.provider.KubeConfig(c.name, internal)
if err != nil {
return "", errors.Wrap(err, "get kubeconfig")
}
log.Debug(ctx, "kubeconfig dump", zap.String("kubeconfig", cfg))
if _, err := io.Copy(f, strings.NewReader(cfg)); err != nil {
return "", errors.Wrap(err, "write kubeconfig")
}
if err := f.Close(); err != nil {
return "", errors.Wrap(err, "close kubeconfig")
}
k := Kubeconfig(f.Name())
c.kubeconfig = k
return k, nil
}
// Close removes the kubeconfig file.
func (k *Kubeconfig) Close() error {
if k == nil || *k == "" || *k == testSentinel {
return nil
}
if err := os.Remove(string(*k)); err != nil {
return errors.Wrap(err, "remove kubeconfig")
}
*k = ""
return nil
}
// KubectlCommand returns an exec.Cmd that will run kubectl.
func (k Kubeconfig) KubectlCommand(ctx context.Context, args ...string) *exec.Cmd {
path, ok := bazel.FindBinary("//tools/kubectl", "_kubectl")
if !ok {
log.Error(ctx, "binary not built with bazel; falling back to host kubectl")
path = "kubectl"
}
cmd := exec.CommandContext(ctx, path, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "KUBECONFIG="+string(k))
cmd.Args[0] = "kubectl"
cmd.Stdout = log.WriterAt(pctx.Child(ctx, "kubectl.stdout"), log.InfoLevel)
cmd.Stderr = log.WriterAt(pctx.Child(ctx, "kubectl.stderr"), log.InfoLevel)
return cmd
}
// HelmCommand returns an exec.Cmd that will run helm.
func (k Kubeconfig) HelmCommand(ctx context.Context, args ...string) *exec.Cmd {
path, ok := bazel.FindBinary("//tools/helm", "_helm")
if !ok {
log.Error(ctx, "binary not built with bazel; falling back to host helm")
path = "helm"
}
cmd := exec.CommandContext(ctx, path, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "KUBECONFIG="+string(k))
cmd.Args[0] = "helm"
cmd.Stdout = log.WriterAt(pctx.Child(ctx, "helm.stdout"), log.InfoLevel)
cmd.Stderr = log.WriterAt(pctx.Child(ctx, "helm.stderr"), log.InfoLevel)
return cmd
}
// Client returns a kubernetes.Interface connected to the cluster referenced by k.
func (k Kubeconfig) Client() (kubernetes.Interface, error) {
if k == testSentinel {
return testKubeClient, nil
}
loadingRules := &clientcmd.ClientConfigLoadingRules{
ExplicitPath: string(k),
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
config, err := kubeConfig.ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "load kubeconfig")
}
config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return promutil.InstrumentRoundTripper("kubernetes", rt)
}
st, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "new k8s clientset")
}
return st, nil
}