2017-11-13 12:50:25 -07:00
|
|
|
package generator
|
|
|
|
|
|
|
|
var k8sClientTemplate = `package {{.version.Version}}
|
|
|
|
|
|
|
|
import (
|
2020-05-15 17:38:28 -07:00
|
|
|
"github.com/rancher/lasso/pkg/client"
|
|
|
|
"github.com/rancher/lasso/pkg/controller"
|
2018-04-03 12:49:20 -07:00
|
|
|
"github.com/rancher/norman/objectclient"
|
2022-04-15 16:22:46 -07:00
|
|
|
"github.com/rancher/norman/generator"
|
2020-05-15 17:38:28 -07:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-11-13 12:50:25 -07:00
|
|
|
"k8s.io/client-go/rest"
|
2020-07-14 12:25:37 -07:00
|
|
|
{{.importPackage}}
|
2017-11-13 12:50:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Interface interface {
|
|
|
|
{{range .schemas}}
|
|
|
|
{{.CodeNamePlural}}Getter{{end}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2020-05-15 17:38:28 -07:00
|
|
|
controllerFactory controller.SharedControllerFactory
|
|
|
|
clientFactory client.SharedClientFactory
|
2017-11-13 12:50:25 -07:00
|
|
|
}
|
|
|
|
|
2020-05-15 17:38:28 -07:00
|
|
|
func NewForConfig(cfg rest.Config) (Interface, error) {
|
|
|
|
scheme := runtime.NewScheme()
|
2020-07-14 12:25:37 -07:00
|
|
|
if err := {{.prefix}}AddToScheme(scheme); err != nil {
|
2020-05-15 17:38:28 -07:00
|
|
|
return nil, err
|
2017-11-13 12:50:25 -07:00
|
|
|
}
|
2022-04-15 16:22:46 -07:00
|
|
|
sharedOpts := &controller.SharedControllerFactoryOptions{
|
|
|
|
SyncOnlyChangedObjects: generator.SyncOnlyChangedObjects(),
|
|
|
|
}
|
|
|
|
controllerFactory, err := controller.NewSharedControllerFactoryFromConfigWithOptions(&cfg, scheme, sharedOpts)
|
2017-11-13 12:50:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-08 17:40:16 -07:00
|
|
|
return NewFromControllerFactory(controllerFactory), nil
|
2020-05-15 17:38:28 -07:00
|
|
|
}
|
2017-11-13 12:50:25 -07:00
|
|
|
|
2022-12-08 17:40:16 -07:00
|
|
|
func NewFromControllerFactory(factory controller.SharedControllerFactory) Interface {
|
2017-11-13 12:50:25 -07:00
|
|
|
return &Client{
|
2020-05-15 17:38:28 -07:00
|
|
|
controllerFactory: factory,
|
|
|
|
clientFactory: factory.SharedCacheFactory().SharedClientFactory(),
|
2022-12-08 17:40:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFromControllerFactoryWithAgent(userAgent string, factory controller.SharedControllerFactory) Interface {
|
|
|
|
return &Client{
|
|
|
|
controllerFactory: factory,
|
|
|
|
clientFactory: client.NewSharedClientFactoryWithAgent(userAgent, factory.SharedCacheFactory().SharedClientFactory()),
|
|
|
|
}
|
2017-11-13 12:50:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{{range .schemas}}
|
|
|
|
type {{.CodeNamePlural}}Getter interface {
|
|
|
|
{{.CodeNamePlural}}(namespace string) {{.CodeName}}Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) {{.CodeNamePlural}}(namespace string) {{.CodeName}}Interface {
|
2020-05-15 17:38:28 -07:00
|
|
|
sharedClient := c.clientFactory.ForResourceKind({{.CodeName}}GroupVersionResource, {{.CodeName}}GroupVersionKind.Kind, {{ . | namespaced }})
|
|
|
|
objectClient := objectclient.NewObjectClient(namespace, sharedClient, &{{.CodeName}}Resource, {{.CodeName}}GroupVersionKind, {{.ID}}Factory{})
|
2017-11-13 12:50:25 -07:00
|
|
|
return &{{.ID}}Client{
|
|
|
|
ns: namespace,
|
|
|
|
client: c,
|
|
|
|
objectClient: objectClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
`
|