mirror of
https://github.com/rancher/norman.git
synced 2025-10-21 08:59:56 +00:00
Generator and build cleanup
This commit is contained in:
19
build.go
19
build.go
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/rancher/norman/api"
|
||||
"github.com/rancher/norman/controller"
|
||||
"github.com/rancher/norman/leader"
|
||||
"github.com/rancher/norman/pkg/kwrapper/k3s"
|
||||
"github.com/rancher/norman/pkg/kwrapper/k8s"
|
||||
"github.com/rancher/norman/pkg/remotedialer"
|
||||
"github.com/rancher/norman/store/crd"
|
||||
@@ -26,14 +25,20 @@ func GetServer(ctx context.Context) *Server {
|
||||
return ctx.Value(serverContextKey{}).(*Server)
|
||||
}
|
||||
|
||||
func (c *Config) Build(ctx context.Context, opts Options) (context.Context, *Server, error) {
|
||||
func (c *Config) Build(ctx context.Context, opts *Options) (context.Context, *Server, error) {
|
||||
var (
|
||||
err error
|
||||
starters []controller.Starter
|
||||
)
|
||||
|
||||
if c.Name == "" {
|
||||
return ctx, nil, errors.New("Name must be set on norman.Config")
|
||||
return ctx, nil, errors.New("name must be set on norman.Config")
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &Options{
|
||||
K8sMode: "external",
|
||||
}
|
||||
}
|
||||
|
||||
r := &Runtime{
|
||||
@@ -47,7 +52,7 @@ func (c *Config) Build(ctx context.Context, opts Options) (context.Context, *Ser
|
||||
|
||||
ctx = context.WithValue(ctx, serverContextKey{}, server)
|
||||
|
||||
ctx, err = c.defaults(ctx, r, opts)
|
||||
ctx, err = c.defaults(ctx, r, *opts)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
@@ -75,7 +80,7 @@ func (c *Config) Build(ctx context.Context, opts Options) (context.Context, *Ser
|
||||
}
|
||||
|
||||
if !opts.DisableControllers {
|
||||
go c.masterControllers(ctx, r, opts)
|
||||
go c.masterControllers(ctx, r)
|
||||
}
|
||||
|
||||
if !c.DisableAPI {
|
||||
@@ -124,7 +129,7 @@ func (c *Config) registerControllers(ctx context.Context, controllers []Controll
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) masterControllers(ctx context.Context, r *Runtime, opts Options) {
|
||||
func (c *Config) masterControllers(ctx context.Context, r *Runtime) {
|
||||
leader.RunOrDie(ctx, c.Name, c.K8sClient, func(ctx context.Context) {
|
||||
var (
|
||||
err error
|
||||
@@ -177,7 +182,7 @@ func (c *Config) defaults(ctx context.Context, r *Runtime, opts Options) (contex
|
||||
}
|
||||
|
||||
if c.K3s.DataDir != "" && c.K3s.RemoteDialerAuthorizer != nil {
|
||||
ctx, r.K3sServerConfig, r.K3sTunnelServer, err = k3s.NewConfig(ctx, c.K3s.DataDir, c.K3s.RemoteDialerAuthorizer)
|
||||
ctx, r.K3sServerConfig, r.K3sTunnelServer, err = k8s.NewK3sConfig(ctx, c.K3s.DataDir, c.K3s.RemoteDialerAuthorizer)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
|
31
generator/cleanup/cleanup.go
Normal file
31
generator/cleanup/cleanup.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cleanup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Cleanup(path string) error {
|
||||
return filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
||||
fmt.Println(path)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.Contains(path, "vendor") {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
if strings.HasPrefix(info.Name(), "zz_generated") {
|
||||
fmt.Println("Removing", path)
|
||||
if err := os.Remove(path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
46
generator/default.go
Normal file
46
generator/default.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/norman/types"
|
||||
)
|
||||
|
||||
var (
|
||||
baseCattle = "client"
|
||||
baseK8s = "apis"
|
||||
)
|
||||
|
||||
func DefaultGenerate(schemas *types.Schemas, pkgPath string, publicAPI bool, backendTypes map[string]bool) error {
|
||||
version := getVersion(schemas)
|
||||
group := strings.Split(version.Group, ".")[0]
|
||||
|
||||
cattleOutputPackage := path.Join(pkgPath, baseCattle, group, version.Version)
|
||||
if !publicAPI {
|
||||
cattleOutputPackage = ""
|
||||
}
|
||||
k8sOutputPackage := path.Join(pkgPath, baseK8s, version.Group, version.Version)
|
||||
|
||||
if err := Generate(schemas, backendTypes, cattleOutputPackage, k8sOutputPackage); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getVersion(schemas *types.Schemas) *types.APIVersion {
|
||||
var version types.APIVersion
|
||||
for _, schema := range schemas.Schemas() {
|
||||
if version.Group == "" {
|
||||
version = schema.Version
|
||||
continue
|
||||
}
|
||||
if version.Group != schema.Version.Group ||
|
||||
version.Version != schema.Version.Version {
|
||||
panic("schema set contains two APIVersions")
|
||||
}
|
||||
}
|
||||
|
||||
return &version
|
||||
}
|
@@ -366,13 +366,17 @@ func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutput
|
||||
cattleDir := path.Join(baseDir, cattleOutputPackage)
|
||||
k8sDir := path.Join(baseDir, k8sOutputPackage)
|
||||
|
||||
if cattleOutputPackage == "" {
|
||||
cattleDir = ""
|
||||
}
|
||||
|
||||
if err := prepareDirs(cattleDir, k8sDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
controllers := []*types.Schema{}
|
||||
var controllers []*types.Schema
|
||||
|
||||
cattleClientTypes := []*types.Schema{}
|
||||
var cattleClientTypes []*types.Schema
|
||||
for _, schema := range schemas.Schemas() {
|
||||
if blackListTypes[schema.ID] {
|
||||
continue
|
||||
@@ -380,8 +384,10 @@ func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutput
|
||||
|
||||
_, backendType := backendTypes[schema.ID]
|
||||
|
||||
if err := generateType(cattleDir, schema, schemas); err != nil {
|
||||
return err
|
||||
if cattleDir != "" {
|
||||
if err := generateType(cattleDir, schema, schemas); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if backendType ||
|
||||
@@ -402,8 +408,10 @@ func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutput
|
||||
}
|
||||
}
|
||||
|
||||
if err := generateClient(cattleDir, cattleClientTypes); err != nil {
|
||||
return err
|
||||
if cattleDir != "" {
|
||||
if err := generateClient(cattleDir, cattleClientTypes); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(controllers) > 0 {
|
||||
@@ -424,11 +432,19 @@ func Generate(schemas *types.Schemas, backendTypes map[string]bool, cattleOutput
|
||||
return err
|
||||
}
|
||||
|
||||
return gofmt(baseDir, cattleOutputPackage)
|
||||
if cattleOutputPackage != "" {
|
||||
return gofmt(baseDir, cattleOutputPackage)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareDirs(dirs ...string) error {
|
||||
for _, dir := range dirs {
|
||||
if dir == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user