1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-30 17:22:44 +00:00
norman/generator/k8s_client_template.go

74 lines
1.7 KiB
Go
Raw Normal View History

2017-11-13 19:50:25 +00:00
package generator
var k8sClientTemplate = `package {{.version.Version}}
import (
"sync"
"github.com/rancher/norman/clientbase"
2017-11-28 21:28:25 +00:00
"github.com/rancher/norman/controller"
2017-11-13 19:50:25 +00:00
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
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}}
}
func NewForConfig(config rest.Config) (Interface, error) {
if config.NegotiatedSerializer == nil {
configConfig := dynamic.ContentConfig()
config.NegotiatedSerializer = configConfig.NegotiatedSerializer
}
restClient, err := rest.UnversionedRESTClientFor(&config)
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 {
objectClient := clientbase.NewObjectClient(namespace, c.restClient, &{{.CodeName}}Resource, {{.CodeName}}GroupVersionKind, {{.ID}}Factory{})
return &{{.ID}}Client{
ns: namespace,
client: c,
objectClient: objectClient,
}
}
{{end}}
`