From 03ab04b6386131642e227c41b43d03aa434186c4 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 30 Oct 2018 22:57:59 -0700 Subject: [PATCH] Generator and build cleanup --- build.go | 19 +++++++++------ generator/cleanup/cleanup.go | 31 ++++++++++++++++++++++++ generator/default.go | 46 ++++++++++++++++++++++++++++++++++++ generator/generator.go | 30 +++++++++++++++++------ 4 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 generator/cleanup/cleanup.go create mode 100644 generator/default.go diff --git a/build.go b/build.go index 281e5b44..cea8300a 100644 --- a/build.go +++ b/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 } diff --git a/generator/cleanup/cleanup.go b/generator/cleanup/cleanup.go new file mode 100644 index 00000000..edb2615f --- /dev/null +++ b/generator/cleanup/cleanup.go @@ -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 + }) +} diff --git a/generator/default.go b/generator/default.go new file mode 100644 index 00000000..eb7f63d3 --- /dev/null +++ b/generator/default.go @@ -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 +} diff --git a/generator/generator.go b/generator/generator.go index 9e510f71..e65199bb 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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 }