Skip to content

Commit

Permalink
Resource replacer that injects resource information into strings
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <[email protected]>
  • Loading branch information
Adirio committed Feb 26, 2020
1 parent e79701e commit ec5c4ad
Show file tree
Hide file tree
Showing 34 changed files with 102 additions and 146 deletions.
18 changes: 11 additions & 7 deletions pkg/model/resource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ func (opts *Options) safeImport(unsafe string) string {
func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource {
res := opts.newResource()

replacer := res.Replacer()

// NOTE: while directories can have "-" and ".", v1 needs that directory to be a Go package
// and that is the reason why we remove them for the directory
pkg := path.Join(c.Repo, "pkg", "apis", res.GroupPackageName, opts.Version)
pkg := replacer.Replace(path.Join(c.Repo, "pkg", "apis", "%[group-package-name]", "%[version]"))
domain := c.Domain

// pkg and domain may need to be changed in case we are referring to a builtin core resource:
Expand All @@ -180,11 +182,11 @@ func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource
// - In any other case, default to => project resource
// TODO: need to support '--resource-pkg-path' flag for specifying resourcePath
if !doResource {
file := filepath.Join("pkg", "apis", res.GroupPackageName, opts.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(opts.Kind)))
file := replacer.Replace(filepath.Join(
"pkg", "apis", "%[group-package-name]", "%[version]", "%[kind]_types.go"))
if _, err := os.Stat(file); os.IsNotExist(err) {
if coreDomain, found := coreGroups[opts.Group]; found {
pkg = path.Join("k8s.io", "api", opts.Group, opts.Version)
pkg = replacer.Replace(path.Join("k8s.io", "api", "%[group]", "%[version]"))
domain = coreDomain
}
}
Expand All @@ -205,9 +207,11 @@ func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource
func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource {
res := opts.newResource()

pkg := path.Join(c.Repo, "api", opts.Version)
replacer := res.Replacer()

pkg := replacer.Replace(path.Join(c.Repo, "api", "%[version]"))
if c.MultiGroup {
pkg = path.Join(c.Repo, "apis", res.Group, opts.Version)
pkg = replacer.Replace(path.Join(c.Repo, "apis", "%[group]", "%[version]"))
}
domain := c.Domain

Expand All @@ -220,7 +224,7 @@ func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource {
if !doResource {
if !c.HasResource(opts.GVK()) {
if coreDomain, found := coreGroups[opts.Group]; found {
pkg = path.Join("k8s.io", "api", opts.Group, opts.Version)
pkg = replacer.Replace(path.Join("k8s.io", "api", "%[group]", "%[version]"))
domain = coreDomain
}
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/model/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package resource

import (
"fmt"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/config"
)

Expand Down Expand Up @@ -62,3 +65,19 @@ func (r *Resource) GVK() config.GVK {
Kind: r.Kind,
}
}

func wrapKey(key string) string {
return fmt.Sprintf("%%[%s]", key)
}

func (r Resource) Replacer() *strings.Replacer {
var replacements []string

replacements = append(replacements, wrapKey("group"), r.Group)
replacements = append(replacements, wrapKey("group-package-name"), r.GroupPackageName)
replacements = append(replacements, wrapKey("version"), r.Version)
replacements = append(replacements, wrapKey("kind"), strings.ToLower(r.Kind))
replacements = append(replacements, wrapKey("plural"), strings.ToLower(r.Plural))

return strings.NewReplacer(replacements...)
}
28 changes: 0 additions & 28 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package scaffold

import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/model"
Expand Down Expand Up @@ -90,11 +88,6 @@ func (s *apiScaffolder) newUniverse() *model.Universe {

func (s *apiScaffolder) scaffoldV1() error {
if s.doResource {
fmt.Println(filepath.Join("pkg", "apis", s.resource.GroupPackageName, s.resource.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
fmt.Println(filepath.Join("pkg", "apis", s.resource.GroupPackageName, s.resource.Version,
fmt.Sprintf("%s_types_test.go", strings.ToLower(s.resource.Kind))))

if err := machinery.NewScaffold().Execute(
s.newUniverse(),
&crdv1.Register{},
Expand All @@ -117,11 +110,6 @@ func (s *apiScaffolder) scaffoldV1() error {
}

if s.doController {
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(s.resource.Kind),
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(s.resource.Kind),
fmt.Sprintf("%s_controller_test.go", strings.ToLower(s.resource.Kind))))

if err := machinery.NewScaffold().Execute(
s.newUniverse(),
&controllerv1.Controller{},
Expand All @@ -145,14 +133,6 @@ func (s *apiScaffolder) scaffoldV2() error {
}
}

if s.config.MultiGroup {
fmt.Println(filepath.Join("apis", s.resource.Group, s.resource.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
} else {
fmt.Println(filepath.Join("api", s.resource.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
}

if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&templatesv2.Types{},
Expand Down Expand Up @@ -183,14 +163,6 @@ func (s *apiScaffolder) scaffoldV2() error {
}

if s.doController {
if s.config.MultiGroup {
fmt.Println(filepath.Join("controllers", s.resource.Group,
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
} else {
fmt.Println(filepath.Join("controllers",
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
}

if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&controllerv2.SuiteTest{},
Expand Down
6 changes: 2 additions & 4 deletions pkg/scaffold/internal/templates/v1/controller/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package controller

import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -37,9 +35,9 @@ type AddController struct {
// SetTemplateDefaults implements input.Template
func (f *AddController) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "controller", fmt.Sprintf(
"add_%s.go", strings.ToLower(f.Resource.Kind)))
f.Path = filepath.Join("pkg", "controller", "add_%[kind].go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = addControllerTemplate

Expand Down
8 changes: 4 additions & 4 deletions pkg/scaffold/internal/templates/v1/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package controller

import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -35,10 +35,10 @@ type Controller struct {
// SetTemplateDefaults implements input.Template
func (f *Controller) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "controller",
strings.ToLower(f.Resource.Kind),
strings.ToLower(f.Resource.Kind)+"_controller.go")
f.Path = filepath.Join("pkg", "controller", "%[kind]", "%[kind]_controller.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)
fmt.Println(f.Path)

f.TemplateBody = controllerTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controller

import (
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -36,9 +35,9 @@ type SuiteTest struct {
// SetTemplateDefaults implements input.Template
func (f *SuiteTest) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "controller",
strings.ToLower(f.Resource.Kind), strings.ToLower(f.Resource.Kind)+"_controller_suite_test.go")
f.Path = filepath.Join("pkg", "controller", "%[kind]", "%[kind]_controller_suite_test.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = controllerSuiteTestTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package controller

import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -35,9 +35,10 @@ type Test struct {
// SetTemplateDefaults implements input.Template
func (f *Test) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "controller",
strings.ToLower(f.Resource.Kind), strings.ToLower(f.Resource.Kind)+"_controller_test.go")
f.Path = filepath.Join("pkg", "controller", "%[kind]", "%[kind]_controller_test.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)
fmt.Println(f.Path)

f.TemplateBody = controllerTestTemplate

Expand Down
5 changes: 2 additions & 3 deletions pkg/scaffold/internal/templates/v1/crd/addtoscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package crd

import (
"fmt"
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/model/file"
Expand All @@ -35,9 +34,9 @@ type AddToScheme struct {
// SetTemplateDefaults implements input.Template
func (f *AddToScheme) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", fmt.Sprintf(
"addtoscheme_%s_%s.go", f.Resource.GroupPackageName, f.Resource.Version))
f.Path = filepath.Join("pkg", "apis", "addtoscheme_%[group-package-name]_%[version].go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = addResourceTemplate

Expand Down
6 changes: 2 additions & 4 deletions pkg/scaffold/internal/templates/v1/crd/crd_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package crd

import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -36,9 +34,9 @@ type CRDSample struct {
// SetTemplateDefaults implements input.Template
func (f *CRDSample) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "samples", fmt.Sprintf(
"%s_%s_%s.yaml", f.Resource.GroupPackageName, f.Resource.Version, strings.ToLower(f.Resource.Kind)))
f.Path = filepath.Join("config", "samples", "%[group-package-name]_%[version]_%[kind].yaml")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = crdSampleTemplate

Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/internal/templates/v1/crd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type Doc struct {
// SetTemplateDefaults implements input.Template
func (f *Doc) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, f.Resource.Version, "doc.go")
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "%[version]", "doc.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = docGoTemplate

Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/internal/templates/v1/crd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type Group struct {
// SetTemplateDefaults implements input.Template
func (f *Group) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, "group.go")
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "group.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = groupTemplate

Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/internal/templates/v1/crd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type Register struct {
// SetTemplateDefaults implements input.Template
func (f *Register) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, f.Resource.Version, "register.go")
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "%[version]", "register.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = registerTemplate

Expand Down
6 changes: 3 additions & 3 deletions pkg/scaffold/internal/templates/v1/crd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package crd
import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -36,9 +35,10 @@ type Types struct {
// SetTemplateDefaults implements input.Template
func (f *Types) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, f.Resource.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(f.Resource.Kind)))
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "%[version]", "%[kind]_types.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)
fmt.Println(f.Path)

f.TemplateBody = typesTemplate

Expand Down
6 changes: 3 additions & 3 deletions pkg/scaffold/internal/templates/v1/crd/typestest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package crd
import (
"fmt"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -36,9 +35,10 @@ type TypesTest struct {
// SetTemplateDefaults implements input.Template
func (f *TypesTest) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, f.Resource.Version,
fmt.Sprintf("%s_types_test.go", strings.ToLower(f.Resource.Kind)))
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "%[version]", "%[kind]_types_test.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)
fmt.Println(f.Path)

f.TemplateBody = typesTestTemplate

Expand Down
5 changes: 2 additions & 3 deletions pkg/scaffold/internal/templates/v1/crd/version_suitetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package crd

import (
"fmt"
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/model/file"
Expand All @@ -35,9 +34,9 @@ type VersionSuiteTest struct {
// SetTemplateDefaults implements input.Template
func (f *VersionSuiteTest) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, f.Resource.Version,
fmt.Sprintf("%s_suite_test.go", f.Resource.Version))
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "%[version]", "%[version]_suite_test.go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = versionSuiteTestTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package webhook

import (
"fmt"
"path/filepath"
"strings"

Expand All @@ -39,10 +38,9 @@ type AddAdmissionWebhookBuilderHandler struct {
// SetTemplateDefaults implements input.Template
func (f *AddAdmissionWebhookBuilderHandler) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("pkg", "webhook",
fmt.Sprintf("%s_server", f.Server),
fmt.Sprintf("add_%s_%s.go", f.Type, strings.ToLower(f.Resource.Kind)))
f.Path = filepath.Join("pkg", "webhook", f.Server+"_server", "add_"+f.Type+"_%[kind].go")
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = addAdmissionWebhookBuilderHandlerTemplate

Expand Down
Loading

0 comments on commit ec5c4ad

Please sign in to comment.