2017-11-13 19:50:25 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
var k8sClientTemplate = `package {{.version.Version}}
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2017-11-28 22:08:31 +00:00
|
|
|
"context"
|
2017-11-13 19:50:25 +00:00
|
|
|
|
2018-04-03 19:49:20 +00:00
|
|
|
"github.com/rancher/norman/objectclient"
|
2018-10-10 02:27:02 +00:00
|
|
|
"github.com/rancher/norman/objectclient/dynamic"
|
2017-11-28 21:28:25 +00:00
|
|
|
"github.com/rancher/norman/controller"
|
2018-04-02 22:45:10 +00:00
|
|
|
"github.com/rancher/norman/restwatch"
|
2017-11-13 19:50:25 +00:00
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
2018-10-22 17:52:03 +00:00
|
|
|
type contextKeyType struct{}
|
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
type Interface interface {
|
|
|
|
RESTClient() rest.Interface
|
2017-11-28 21:28:25 +00:00
|
|
|
controller.Starter
|
2017-11-13 19:50:25 +00:00
|
|
|
{{range .schemas}}
|
|
|
|
{{.CodeNamePlural}}Getter{{end}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
sync.Mutex
|
|
|
|
restClient rest.Interface
|
2017-11-28 21:28:25 +00:00
|
|
|
starters []controller.Starter
|
2017-11-13 19:50:25 +00:00
|
|
|
{{range .schemas}}
|
|
|
|
{{.ID}}Controllers map[string]{{.CodeName}}Controller{{end}}
|
|
|
|
}
|
|
|
|
|
2018-10-22 17:52:03 +00:00
|
|
|
func Factory(ctx context.Context, config rest.Config) (context.Context, controller.Starter, error) {
|
|
|
|
c, err := NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.WithValue(ctx, contextKeyType{}, c), c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func From(ctx context.Context) Interface {
|
|
|
|
return ctx.Value(contextKeyType{}).(Interface)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
func NewForConfig(config rest.Config) (Interface, error) {
|
|
|
|
if config.NegotiatedSerializer == nil {
|
2018-10-10 02:27:02 +00:00
|
|
|
config.NegotiatedSerializer = dynamic.NegotiatedSerializer
|
2017-11-13 19:50:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 22:45:10 +00:00
|
|
|
restClient, err := restwatch.UnversionedRESTClientFor(&config)
|
2017-11-13 19:50:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
restClient: restClient,
|
|
|
|
{{range .schemas}}
|
|
|
|
{{.ID}}Controllers: map[string]{{.CodeName}}Controller{},{{end}}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RESTClient() rest.Interface {
|
|
|
|
return c.restClient
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
func (c *Client) Sync(ctx context.Context) error {
|
|
|
|
return controller.Sync(ctx, c.starters...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Start(ctx context.Context, threadiness int) error {
|
|
|
|
return controller.Start(ctx, threadiness, c.starters...)
|
|
|
|
}
|
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
{{range .schemas}}
|
|
|
|
type {{.CodeNamePlural}}Getter interface {
|
|
|
|
{{.CodeNamePlural}}(namespace string) {{.CodeName}}Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) {{.CodeNamePlural}}(namespace string) {{.CodeName}}Interface {
|
2018-04-03 19:49:20 +00:00
|
|
|
objectClient := objectclient.NewObjectClient(namespace, c.restClient, &{{.CodeName}}Resource, {{.CodeName}}GroupVersionKind, {{.ID}}Factory{})
|
2017-11-13 19:50:25 +00:00
|
|
|
return &{{.ID}}Client{
|
|
|
|
ns: namespace,
|
|
|
|
client: c,
|
|
|
|
objectClient: objectClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
`
|