-
Notifications
You must be signed in to change notification settings - Fork 216
/
impersonate.go
82 lines (71 loc) · 2.3 KB
/
impersonate.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
package helmdeployer
import (
"fmt"
"github.com/rancher/wrangler/pkg/ratelimit"
apierror "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
func (h *Helm) getServiceAccount(name string) (string, string, error) {
currentName := name
if currentName == "" {
currentName = DefaultServiceAccount
}
_, err := h.serviceAccountCache.Get(h.agentNamespace, currentName)
if apierror.IsNotFound(err) && name == "" {
// if we can't find the service account, but none was asked for, don't use any
return "", "", nil
} else if err != nil {
return "", "", fmt.Errorf("looking up service account %s/%s: %w", h.agentNamespace, currentName, err)
}
return h.agentNamespace, currentName, nil
}
type impersonatingGetter struct {
genericclioptions.RESTClientGetter
config clientcmd.ClientConfig
restConfig *rest.Config
}
func newImpersonatingGetter(namespace, name string, getter genericclioptions.RESTClientGetter) (genericclioptions.RESTClientGetter, error) {
config := clientcmd.NewDefaultClientConfig(impersonationConfig(namespace, name), &clientcmd.ConfigOverrides{})
restConfig, err := config.ClientConfig()
if err != nil {
return nil, err
}
restConfig.RateLimiter = ratelimit.None
return &impersonatingGetter{
RESTClientGetter: getter,
config: config,
restConfig: restConfig,
}, nil
}
func (i *impersonatingGetter) ToRESTConfig() (*rest.Config, error) {
return i.restConfig, nil
}
func (i *impersonatingGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
return i.config
}
func impersonationConfig(namespace, name string) clientcmdapi.Config {
return clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
"cluster": {
Server: "https://kubernetes.default",
CertificateAuthority: "/run/secrets/kubernetes.io/serviceaccount/ca.crt",
},
},
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"user": {
TokenFile: "/run/secrets/kubernetes.io/serviceaccount/token",
Impersonate: fmt.Sprintf("system:serviceaccount:%s:%s", namespace, name),
},
},
Contexts: map[string]*clientcmdapi.Context{
"default": {
Cluster: "cluster",
AuthInfo: "user",
},
},
CurrentContext: "default",
}
}