Skip to content

Commit

Permalink
Add scaffold to controllers with the api cor external resources if we…
Browse files Browse the repository at this point in the history
… do know the path

Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Feb 10, 2021
1 parent 979f7a6 commit 2af85b9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
12 changes: 9 additions & 3 deletions pkg/model/file/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ import (
// DefaultFuncMap returns the default template.FuncMap for rendering the template.
func DefaultFuncMap() template.FuncMap {
return template.FuncMap{
"title": strings.Title,
"lower": strings.ToLower,
"hashFNV": hashFNV,
"title": strings.Title,
"lower": strings.ToLower,
"isEmptyStr": isEmptyString,
"hashFNV": hashFNV,
}
}

// isEmptyString returns whether the string is empty
func isEmptyString(s string) bool {
return s == ""
}

// hashFNV will generate a random string useful for generating a unique string
func hashFNV(s string) (string, error) {
hasher := fnv.New32a()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
{{- end }}
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(req ctrl.Request) (ctrl.Resul
// SetupWithManager sets up the controller with the Manager.
func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}).
{{- else -}}
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func (f *SuiteTest) GetCodeFragments() file.CodeFragmentsMap {

// Generate import code fragments
imports := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
imports = append(imports, fmt.Sprintf(apiImportCodeFragment, f.Resource.ImportAlias(), f.Resource.Path))
}

// Generate add scheme code fragments
addScheme := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
addScheme = append(addScheme, fmt.Sprintf(addschemeCodeFragment, f.Resource.ImportAlias()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
{{- end }}
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl
// SetupWithManager sets up the controller with the Manager.
func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}).
{{- else -}}
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func (f *SuiteTest) GetCodeFragments() file.CodeFragmentsMap {

// Generate import code fragments
imports := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
imports = append(imports, fmt.Sprintf(apiImportCodeFragment, f.Resource.ImportAlias(), f.Resource.Path))
}

// Generate add scheme code fragments
addScheme := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
addScheme = append(addScheme, fmt.Sprintf(addschemeCodeFragment, f.Resource.ImportAlias()))
}

Expand Down

0 comments on commit 2af85b9

Please sign in to comment.