Skip to content

Commit

Permalink
Merge pull request #219 from ibuildthecloud/master
Browse files Browse the repository at this point in the history
Switch handler return type to runtime.Object
  • Loading branch information
ibuildthecloud authored Nov 6, 2018
2 parents 0dbd456 + fa4bdda commit 5bafd7b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion generator/controller_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type {{.schema.CodeName}}List struct {
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 {
List(namespace string, selector labels.Selector) (ret []*{{.prefix}}{{.schema.CodeName}}, err error)
Expand Down
24 changes: 22 additions & 2 deletions generator/default.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package generator

import (
"fmt"
"path"
"strings"

"github.com/rancher/norman/types"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var (
baseCattle = "client"
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)
group := strings.Split(version.Group, ".")[0]

Expand All @@ -22,13 +24,31 @@ func DefaultGenerate(schemas *types.Schemas, pkgPath string, publicAPI bool, bac
}
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 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 {
var version types.APIVersion
for _, schema := range schemas.Schemas() {
Expand Down
4 changes: 2 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func GenerateControllerForTypes(version *types.APIVersion, k8sOutputPackage stri
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()
cattleDir := path.Join(baseDir, cattleOutputPackage)
k8sDir := path.Join(baseDir, k8sOutputPackage)
Expand All @@ -382,7 +382,7 @@ func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutput
continue
}

_, backendType := backendTypes[schema.ID]
_, backendType := foreignTypes[schema.ID]

if cattleDir != "" {
if err := generateType(cattleDir, schema, schemas); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions generator/lifecycle_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)
type {{.schema.CodeName}}Lifecycle interface {
Create(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
Remove(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
Updated(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
Create(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
Remove(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
Updated(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
}
type {{.schema.ID}}LifecycleAdapter struct {
Expand Down Expand Up @@ -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 {
adapter := &{{.schema.ID}}LifecycleAdapter{lifecycle: l}
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)
if o, ok := newObj.(*{{.prefix}}{{.schema.CodeName}}); ok {
if o, ok := newObj.(runtime.Object); ok {
return o, err
}
return nil, err
Expand Down

0 comments on commit 5bafd7b

Please sign in to comment.