diff --git a/generator/controller_template.go b/generator/controller_template.go index 26d52f49..c913dfec 100644 --- a/generator/controller_template.go +++ b/generator/controller_template.go @@ -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) diff --git a/generator/default.go b/generator/default.go index eb7f63d3..6da459a2 100644 --- a/generator/default.go +++ b/generator/default.go @@ -1,10 +1,12 @@ package generator import ( + "fmt" "path" "strings" "github.com/rancher/norman/types" + "k8s.io/apimachinery/pkg/runtime/schema" ) var ( @@ -12,7 +14,7 @@ var ( 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] @@ -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() { diff --git a/generator/generator.go b/generator/generator.go index e65199bb..8d5c3f1c 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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) @@ -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 { diff --git a/generator/lifecycle_template.go b/generator/lifecycle_template.go index a9f14abd..5594e446 100644 --- a/generator/lifecycle_template.go +++ b/generator/lifecycle_template.go @@ -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 { @@ -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