mirror of
https://github.com/rancher/norman.git
synced 2025-08-09 11:17:48 +00:00
Switch handler return type to runtime.Object
This commit is contained in:
parent
aea43f15fd
commit
fa4bdda91b
@ -41,7 +41,7 @@ type {{.schema.CodeName}}List struct {
|
|||||||
Items []{{.prefix}}{{.schema.CodeName}}
|
Items []{{.prefix}}{{.schema.CodeName}}
|
||||||
}
|
}
|
||||||
|
|
||||||
type {{.schema.CodeName}}HandlerFunc func(key string, obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
type {{.schema.CodeName}}HandlerFunc func(key string, obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
|
||||||
|
|
||||||
type {{.schema.CodeName}}Lister interface {
|
type {{.schema.CodeName}}Lister interface {
|
||||||
List(namespace string, selector labels.Selector) (ret []*{{.prefix}}{{.schema.CodeName}}, err error)
|
List(namespace string, selector labels.Selector) (ret []*{{.prefix}}{{.schema.CodeName}}, err error)
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package generator
|
package generator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -12,7 +14,7 @@ var (
|
|||||||
baseK8s = "apis"
|
baseK8s = "apis"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DefaultGenerate(schemas *types.Schemas, pkgPath string, publicAPI bool, backendTypes map[string]bool) error {
|
func DefaultGenerate(schemas *types.Schemas, pkgPath string, publicAPI bool, foreignTypes map[string]bool) error {
|
||||||
version := getVersion(schemas)
|
version := getVersion(schemas)
|
||||||
group := strings.Split(version.Group, ".")[0]
|
group := strings.Split(version.Group, ".")[0]
|
||||||
|
|
||||||
@ -22,13 +24,31 @@ func DefaultGenerate(schemas *types.Schemas, pkgPath string, publicAPI bool, bac
|
|||||||
}
|
}
|
||||||
k8sOutputPackage := path.Join(pkgPath, baseK8s, version.Group, version.Version)
|
k8sOutputPackage := path.Join(pkgPath, baseK8s, version.Group, version.Version)
|
||||||
|
|
||||||
if err := Generate(schemas, backendTypes, cattleOutputPackage, k8sOutputPackage); err != nil {
|
if err := Generate(schemas, foreignTypes, cattleOutputPackage, k8sOutputPackage); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ControllersForForeignTypes(baseOutputPackage string, gv schema.GroupVersion, nsObjs []interface{}, objs []interface{}) error {
|
||||||
|
version := gv.Version
|
||||||
|
group := gv.Group
|
||||||
|
groupPath := group
|
||||||
|
|
||||||
|
if groupPath == "" {
|
||||||
|
groupPath = "core"
|
||||||
|
}
|
||||||
|
|
||||||
|
k8sOutputPackage := path.Join(baseOutputPackage, baseK8s, groupPath, version)
|
||||||
|
|
||||||
|
return GenerateControllerForTypes(&types.APIVersion{
|
||||||
|
Version: version,
|
||||||
|
Group: group,
|
||||||
|
Path: fmt.Sprintf("/k8s/%s-%s", groupPath, version),
|
||||||
|
}, k8sOutputPackage, nsObjs, objs)
|
||||||
|
}
|
||||||
|
|
||||||
func getVersion(schemas *types.Schemas) *types.APIVersion {
|
func getVersion(schemas *types.Schemas) *types.APIVersion {
|
||||||
var version types.APIVersion
|
var version types.APIVersion
|
||||||
for _, schema := range schemas.Schemas() {
|
for _, schema := range schemas.Schemas() {
|
||||||
|
@ -361,7 +361,7 @@ func GenerateControllerForTypes(version *types.APIVersion, k8sOutputPackage stri
|
|||||||
return gofmt(baseDir, k8sOutputPackage)
|
return gofmt(baseDir, k8sOutputPackage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutputPackage, k8sOutputPackage string) error {
|
func Generate(schemas *types.Schemas, foreignTypes map[string]bool, cattleOutputPackage, k8sOutputPackage string) error {
|
||||||
baseDir := args.DefaultSourceTree()
|
baseDir := args.DefaultSourceTree()
|
||||||
cattleDir := path.Join(baseDir, cattleOutputPackage)
|
cattleDir := path.Join(baseDir, cattleOutputPackage)
|
||||||
k8sDir := path.Join(baseDir, k8sOutputPackage)
|
k8sDir := path.Join(baseDir, k8sOutputPackage)
|
||||||
@ -382,7 +382,7 @@ func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutput
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
_, backendType := backendTypes[schema.ID]
|
_, backendType := foreignTypes[schema.ID]
|
||||||
|
|
||||||
if cattleDir != "" {
|
if cattleDir != "" {
|
||||||
if err := generateType(cattleDir, schema, schemas); err != nil {
|
if err := generateType(cattleDir, schema, schemas); err != nil {
|
||||||
|
@ -10,9 +10,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type {{.schema.CodeName}}Lifecycle interface {
|
type {{.schema.CodeName}}Lifecycle interface {
|
||||||
Create(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
Create(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
|
||||||
Remove(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
Remove(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
|
||||||
Updated(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
Updated(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type {{.schema.ID}}LifecycleAdapter struct {
|
type {{.schema.ID}}LifecycleAdapter struct {
|
||||||
@ -46,9 +46,9 @@ func (w *{{.schema.ID}}LifecycleAdapter) Updated(obj runtime.Object) (runtime.Ob
|
|||||||
func New{{.schema.CodeName}}LifecycleAdapter(name string, clusterScoped bool, client {{.schema.CodeName}}Interface, l {{.schema.CodeName}}Lifecycle) {{.schema.CodeName}}HandlerFunc {
|
func New{{.schema.CodeName}}LifecycleAdapter(name string, clusterScoped bool, client {{.schema.CodeName}}Interface, l {{.schema.CodeName}}Lifecycle) {{.schema.CodeName}}HandlerFunc {
|
||||||
adapter := &{{.schema.ID}}LifecycleAdapter{lifecycle: l}
|
adapter := &{{.schema.ID}}LifecycleAdapter{lifecycle: l}
|
||||||
syncFn := lifecycle.NewObjectLifecycleAdapter(name, clusterScoped, adapter, client.ObjectClient())
|
syncFn := lifecycle.NewObjectLifecycleAdapter(name, clusterScoped, adapter, client.ObjectClient())
|
||||||
return func(key string, obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
return func(key string, obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error) {
|
||||||
newObj, err := syncFn(key, obj)
|
newObj, err := syncFn(key, obj)
|
||||||
if o, ok := newObj.(*{{.prefix}}{{.schema.CodeName}}); ok {
|
if o, ok := newObj.(runtime.Object); ok {
|
||||||
return o, err
|
return o, err
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user