1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-30 04:03:46 +00:00
steve/pkg/client/factory.go

195 lines
6.4 KiB
Go
Raw Permalink Normal View History

2019-08-04 17:41:32 +00:00
package client
import (
2020-02-03 21:28:25 +00:00
"fmt"
2020-02-08 20:04:20 +00:00
"net/http"
2020-02-02 05:13:32 +00:00
"time"
"github.com/rancher/apiserver/pkg/types"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/attributes"
2020-02-03 21:28:25 +00:00
"k8s.io/apiserver/pkg/endpoints/request"
2019-08-04 17:41:32 +00:00
"k8s.io/client-go/dynamic"
2020-06-01 22:59:38 +00:00
"k8s.io/client-go/kubernetes"
2020-02-08 20:04:20 +00:00
"k8s.io/client-go/metadata"
2019-08-04 17:41:32 +00:00
"k8s.io/client-go/rest"
)
type Factory struct {
2020-02-27 17:34:51 +00:00
impersonate bool
tableClientCfg *rest.Config
tableWatchClientCfg *rest.Config
clientCfg *rest.Config
watchClientCfg *rest.Config
metadata metadata.Interface
2020-03-13 02:14:40 +00:00
dynamic dynamic.Interface
2020-02-27 17:34:51 +00:00
Config *rest.Config
2019-08-04 17:41:32 +00:00
}
2020-02-08 20:04:20 +00:00
type addQuery struct {
values map[string]string
next http.RoundTripper
}
func (a *addQuery) RoundTrip(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
for k, v := range a.values {
q.Set(k, v)
}
req.Header.Set("Accept", "application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io")
2020-02-08 20:04:20 +00:00
req.URL.RawQuery = q.Encode()
return a.next.RoundTrip(req)
}
2020-02-03 21:28:25 +00:00
func NewFactory(cfg *rest.Config, impersonate bool) (*Factory, error) {
clientCfg := rest.CopyConfig(cfg)
clientCfg.QPS = 10000
clientCfg.Burst = 100
2020-02-27 17:34:51 +00:00
watchClientCfg := rest.CopyConfig(clientCfg)
watchClientCfg.Timeout = 30 * time.Minute
setTable := func(rt http.RoundTripper) http.RoundTripper {
2020-02-08 20:04:20 +00:00
return &addQuery{
values: map[string]string{
"includeObject": "Object",
},
next: rt,
}
2020-02-27 17:34:51 +00:00
}
2020-02-03 21:28:25 +00:00
2020-02-27 17:34:51 +00:00
tableClientCfg := rest.CopyConfig(clientCfg)
tableClientCfg.Wrap(setTable)
tableClientCfg.AcceptContentTypes = "application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io"
2020-02-27 17:34:51 +00:00
tableWatchClientCfg := rest.CopyConfig(watchClientCfg)
tableWatchClientCfg.Wrap(setTable)
tableWatchClientCfg.AcceptContentTypes = "application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io"
2020-02-02 05:13:32 +00:00
2020-02-08 20:04:20 +00:00
md, err := metadata.NewForConfig(cfg)
2020-02-02 05:13:32 +00:00
if err != nil {
return nil, err
}
2020-02-03 21:28:25 +00:00
2020-03-13 02:14:40 +00:00
d, err := dynamic.NewForConfig(cfg)
if err != nil {
return nil, err
}
2019-08-04 17:41:32 +00:00
return &Factory{
2020-03-13 02:14:40 +00:00
dynamic: d,
2020-02-27 17:34:51 +00:00
metadata: md,
impersonate: impersonate,
tableClientCfg: tableClientCfg,
tableWatchClientCfg: tableWatchClientCfg,
clientCfg: clientCfg,
watchClientCfg: watchClientCfg,
Config: watchClientCfg,
2019-08-04 17:41:32 +00:00
}, nil
}
2020-02-08 20:04:20 +00:00
func (p *Factory) MetadataClient() metadata.Interface {
return p.metadata
2019-09-09 21:28:55 +00:00
}
2020-10-30 23:08:28 +00:00
func (p *Factory) AdminDynamicClient() dynamic.Interface {
2020-03-13 02:14:40 +00:00
return p.dynamic
}
2020-06-01 22:59:38 +00:00
func (p *Factory) IsImpersonating() bool {
return p.impersonate
}
func (p *Factory) K8sInterface(ctx *types.APIRequest) (kubernetes.Interface, error) {
cfg, err := setupConfig(ctx, p.clientCfg, p.impersonate)
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(cfg)
}
2020-06-02 15:51:42 +00:00
func (p *Factory) AdminK8sInterface() (kubernetes.Interface, error) {
return kubernetes.NewForConfig(p.clientCfg)
2020-06-01 22:59:38 +00:00
}
func (p *Factory) DynamicClient(ctx *types.APIRequest, warningHandler rest.WarningHandler) (dynamic.Interface, error) {
return newDynamicClient(ctx, p.clientCfg, p.impersonate, warningHandler)
2020-10-30 23:08:28 +00:00
}
func (p *Factory) Client(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
return newClient(ctx, p.clientCfg, s, namespace, p.impersonate, warningHandler)
2020-02-10 17:18:20 +00:00
}
func (p *Factory) AdminClient(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
return newClient(ctx, p.clientCfg, s, namespace, false, warningHandler)
2019-08-04 17:41:32 +00:00
}
2020-02-02 05:13:32 +00:00
func (p *Factory) ClientForWatch(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
return newClient(ctx, p.watchClientCfg, s, namespace, p.impersonate, warningHandler)
}
func (p *Factory) AdminClientForWatch(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
return newClient(ctx, p.watchClientCfg, s, namespace, false, warningHandler)
}
func (p *Factory) TableClient(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
if attributes.Table(s) {
return newClient(ctx, p.tableClientCfg, s, namespace, p.impersonate, warningHandler)
}
return p.Client(ctx, s, namespace, warningHandler)
2020-02-27 17:34:51 +00:00
}
func (p *Factory) TableAdminClient(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
if attributes.Table(s) {
return newClient(ctx, p.tableClientCfg, s, namespace, false, warningHandler)
}
return p.AdminClient(ctx, s, namespace, warningHandler)
2020-02-27 17:34:51 +00:00
}
func (p *Factory) TableClientForWatch(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
if attributes.Table(s) {
return newClient(ctx, p.tableWatchClientCfg, s, namespace, p.impersonate, warningHandler)
}
return p.ClientForWatch(ctx, s, namespace, warningHandler)
2020-02-10 17:18:20 +00:00
}
func (p *Factory) TableAdminClientForWatch(ctx *types.APIRequest, s *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
if attributes.Table(s) {
return newClient(ctx, p.tableWatchClientCfg, s, namespace, false, warningHandler)
}
return p.AdminClientForWatch(ctx, s, namespace, warningHandler)
2020-02-03 21:28:25 +00:00
}
2020-06-01 22:59:38 +00:00
func setupConfig(ctx *types.APIRequest, cfg *rest.Config, impersonate bool) (*rest.Config, error) {
2020-02-10 17:18:20 +00:00
if impersonate {
2020-02-03 21:28:25 +00:00
user, ok := request.UserFrom(ctx.Context())
if !ok {
return nil, fmt.Errorf("user not found for impersonation")
}
cfg = rest.CopyConfig(cfg)
cfg.Impersonate.UserName = user.GetName()
cfg.Impersonate.Groups = user.GetGroups()
cfg.Impersonate.Extra = user.GetExtra()
}
2020-06-01 22:59:38 +00:00
return cfg, nil
}
func newDynamicClient(ctx *types.APIRequest, cfg *rest.Config, impersonate bool, warningHandler rest.WarningHandler) (dynamic.Interface, error) {
2020-06-01 22:59:38 +00:00
cfg, err := setupConfig(ctx, cfg, impersonate)
cfg.WarningHandler = warningHandler
2020-06-01 22:59:38 +00:00
if err != nil {
return nil, err
}
2020-02-03 21:28:25 +00:00
2020-10-30 23:08:28 +00:00
return dynamic.NewForConfig(cfg)
}
func newClient(ctx *types.APIRequest, cfg *rest.Config, s *types.APISchema, namespace string, impersonate bool, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
client, err := newDynamicClient(ctx, cfg, impersonate, warningHandler)
2020-02-03 21:28:25 +00:00
if err != nil {
return nil, err
}
2020-02-02 05:13:32 +00:00
gvr := attributes.GVR(s)
2020-02-03 21:28:25 +00:00
return client.Resource(gvr).Namespace(namespace), nil
2020-02-02 05:13:32 +00:00
}