From e72aa6bbb1ad81ac3024328e61759fa29949aa4e Mon Sep 17 00:00:00 2001 From: Muvaffak Onus Date: Tue, 16 Nov 2021 17:42:28 +0300 Subject: [PATCH] config: call the groupsuffix rootgroup because it is not just a string suffix anymore Signed-off-by: Muvaffak Onus --- pkg/config/provider.go | 17 +++++++++++------ pkg/pipeline/run.go | 34 +++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/pkg/config/provider.go b/pkg/config/provider.go index cd8dd6e7..66ab9205 100644 --- a/pkg/config/provider.go +++ b/pkg/config/provider.go @@ -52,13 +52,17 @@ type Provider struct { // for "aws_rds_cluster", we drop "aws_" prefix and its group ("rds") to set // Kind of the resource as "Cluster". TerraformResourcePrefix string - // GroupSuffix is the suffix to append to resource groups, e.g. - // ".aws.tf.crossplane.io". Defaults to "..tf.crossplane.io". - GroupSuffix string + + // RootGroup is the root group that all CRDs groups in the provider are based + // on, e.g. "aws.tf.crossplane.io". + // Defaults to ".tf.crossplane.io". + RootGroup string + // ShortName is the short name of the provider. Typically, added as a CRD // category, e.g. "tfaws". Default to "tf". For more details on CRD // categories, see: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#categories ShortName string + // ModulePath is the go module path for the tf based provider repo, e.g. // "github.com/crossplane-contrib/provider-tf-aws" ModulePath string @@ -77,6 +81,7 @@ type Provider struct { // can add "aws_shield_protection_group$". To skip whole aws waf group, one // can add "aws_waf.*" to the list. SkipList []string + // IncludeList is a list of regex for the Terraform resources to be // skipped. For example, to include "aws_shield_protection_group" into // the generated resources, one can add "aws_shield_protection_group$". @@ -96,10 +101,10 @@ type Provider struct { // A ProviderOption configures a Provider. type ProviderOption func(*Provider) -// WithGroupSuffix configures GroupSuffix for resources of this Provider. +// WithGroupSuffix configures RootGroup for resources of this Provider. func WithGroupSuffix(s string) ProviderOption { return func(p *Provider) { - p.GroupSuffix = s + p.RootGroup = s } } @@ -143,7 +148,7 @@ func NewProvider(resourceMap map[string]*schema.Resource, prefix string, moduleP p := &Provider{ ModulePath: modulePath, TerraformResourcePrefix: fmt.Sprintf("%s_", prefix), - GroupSuffix: fmt.Sprintf(".%s.tf.crossplane.io", prefix), + RootGroup: fmt.Sprintf("%s.tf.crossplane.io", prefix), ShortName: fmt.Sprintf("tf%s", prefix), BasePackages: DefaultBasePackages, DefaultResourceFn: DefaultResource, diff --git a/pkg/pipeline/run.go b/pkg/pipeline/run.go index de7094f4..fda0dc5e 100644 --- a/pkg/pipeline/run.go +++ b/pkg/pipeline/run.go @@ -35,15 +35,21 @@ func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo // for better readability considering the straightforward logic here. // Group resources based on their Group and API Versions. + // An example entry in the tree would be: + // ec2.tfaws.crossplane.io -> v1alpha1 -> aws_vpc resourcesGroups := map[string]map[string]map[string]*config.Resource{} for name, resource := range pc.Resources { - if len(resourcesGroups[resource.ShortGroup]) == 0 { - resourcesGroups[resource.ShortGroup] = map[string]map[string]*config.Resource{} + group := pc.RootGroup + if resource.ShortGroup != "" { + group = strings.ToLower(resource.ShortGroup) + "." + pc.RootGroup } - if len(resourcesGroups[resource.ShortGroup][resource.Version]) == 0 { - resourcesGroups[resource.ShortGroup][resource.Version] = map[string]*config.Resource{} + if len(resourcesGroups[group]) == 0 { + resourcesGroups[group] = map[string]map[string]*config.Resource{} } - resourcesGroups[resource.ShortGroup][resource.Version][name] = resource + if len(resourcesGroups[group][resource.Version]) == 0 { + resourcesGroups[group][resource.Version] = map[string]*config.Resource{} + } + resourcesGroups[group][resource.Version][name] = resource } // Add ProviderConfig API package to the list of API version packages. @@ -57,11 +63,7 @@ func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo controllerPkgList = append(controllerPkgList, filepath.Join(pc.ModulePath, p)) } count := 0 - for shortGroup, versions := range resourcesGroups { - group := pc.GroupSuffix - if shortGroup != "" { - group = strings.ToLower(shortGroup) + "." + pc.GroupSuffix - } + for group, versions := range resourcesGroups { for version, resources := range versions { versionGen := NewVersionGenerator(rootDir, pc.ModulePath, group, version) crdGen := NewCRDGenerator(versionGen.Package(), rootDir, pc.ShortName, group, version) @@ -108,13 +110,15 @@ func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo // NOTE(muvaf): gosec linter requires that the whole command is hard-coded. // So, we set the directory of the command instead of passing in the directory // as an argument to "find". - goimportsCmd := exec.Command("bash", "-c", "goimports -w $(find . -iname 'zz_*')") - goimportsCmd.Dir = filepath.Clean(filepath.Join(rootDir, "apis")) - if out, err := goimportsCmd.CombinedOutput(); err != nil { + apisCmd := exec.Command("bash", "-c", "goimports -w $(find . -iname 'zz_*')") + apisCmd.Dir = filepath.Clean(filepath.Join(rootDir, "apis")) + if out, err := apisCmd.CombinedOutput(); err != nil { panic(errors.Wrap(err, "cannot run goimports for apis folder: "+string(out))) } - goimportsCmd.Dir = filepath.Clean(filepath.Join(rootDir, "internal")) - if out, err := goimportsCmd.CombinedOutput(); err != nil { + + internalCmd := exec.Command("bash", "-c", "goimports -w $(find . -iname 'zz_*')") + internalCmd.Dir = filepath.Clean(filepath.Join(rootDir, "internal")) + if out, err := internalCmd.CombinedOutput(); err != nil { panic(errors.Wrap(err, "cannot run goimports for internal folder: "+string(out))) }