Skip to content

Commit

Permalink
Merge pull request #195 from ulucinar/controller-map
Browse files Browse the repository at this point in the history
Deprecate config.BasePackages.Controller in favor of ControllerMap
  • Loading branch information
ulucinar authored May 2, 2023
2 parents 1d0b7b0 + 56c3caa commit 645d726
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
18 changes: 18 additions & 0 deletions pkg/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import (
tjname "github.com/upbound/upjet/pkg/types/name"
)

const (
// PackageNameConfig is the name of the provider subpackage that contains
// the base resources (e.g., ProviderConfig, ProviderConfigUsage,
// StoreConfig. etc.).
// TODO: we should be careful that there may also exist short groups with
// these names. We can consider making these configurable by the provider
// maintainer.
PackageNameConfig = "config"
// PackageNameMonolith is the name of the backwards-compatible
// provider subpackage that contains the all the resources.
PackageNameMonolith = "monolith"
)

// Commonly used resource configurations.
var (
DefaultBasePackages = BasePackages{
Expand All @@ -21,10 +34,15 @@ var (
"apis/v1alpha1",
"apis/v1beta1",
},
//nolint:staticcheck
Controller: []string{
// Default package for ProviderConfig controllers
"internal/controller/providerconfig",
},
ControllerMap: map[string]string{
// Default package for ProviderConfig controllers
"internal/controller/providerconfig": PackageNameConfig,
},
}

// NopSensitive does nothing.
Expand Down
8 changes: 6 additions & 2 deletions pkg/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ func (cc ResourceConfiguratorChain) Configure(r *Resource) {
}
}

// BasePackages keeps lists of base packages that needs to be registered as API
// BasePackages keeps lists of packages that needs to be registered as API
// and controllers. Typically, we expect to see ProviderConfig packages here.
// These APIs and controllers belong to non-generated (manually maintained)
// resources.
type BasePackages struct {
APIVersion []string
Controller []string
// Deprecated: Use ControllerMap instead.
Controller []string
ControllerMap map[string]string
}

// Provider holds configuration for a provider to be generated with Upjet.
Expand Down
39 changes: 28 additions & 11 deletions pkg/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ type terraformedInput struct {
ParametersTypeName string
}

const (
// TODO: we should be careful that there may also exist short groups with
// these names. We can consider making these configurable by the provider
// maintainer.
configPackageName = "config"
monolithPackageName = "monolith"
)

// Run runs the Upjet code generation pipelines.
func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo
// Note(turkenh): nolint reasoning - this is the main function of the code
Expand Down Expand Up @@ -65,10 +57,35 @@ func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo
}
// Add ProviderConfig controller package to the list of controller packages.
controllerPkgMap := make(map[string][]string)
// new API takes precedence
for p, g := range pc.BasePackages.ControllerMap {
path := filepath.Join(pc.ModulePath, p)
controllerPkgMap[g] = append(controllerPkgMap[g], path)
controllerPkgMap[config.PackageNameMonolith] = append(controllerPkgMap[config.PackageNameMonolith], path)
}
//nolint:staticcheck
for _, p := range pc.BasePackages.Controller {
path := filepath.Join(pc.ModulePath, p)
controllerPkgMap[configPackageName] = append(controllerPkgMap[configPackageName], path)
controllerPkgMap[monolithPackageName] = append(controllerPkgMap[monolithPackageName], path)
found := false
for _, p := range controllerPkgMap[config.PackageNameConfig] {
if path == p {
found = true
break
}
}
if !found {
controllerPkgMap[config.PackageNameConfig] = append(controllerPkgMap[config.PackageNameConfig], path)
}
found = false
for _, p := range controllerPkgMap[config.PackageNameMonolith] {
if path == p {
found = true
break
}
}
if !found {
controllerPkgMap[config.PackageNameMonolith] = append(controllerPkgMap[config.PackageNameMonolith], path)
}
}
count := 0
for group, versions := range resourcesGroups {
Expand Down Expand Up @@ -99,7 +116,7 @@ func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo
}
sGroup := strings.Split(group, ".")[0]
controllerPkgMap[sGroup] = append(controllerPkgMap[sGroup], ctrlPkgPath)
controllerPkgMap[monolithPackageName] = append(controllerPkgMap[monolithPackageName], ctrlPkgPath)
controllerPkgMap[config.PackageNameMonolith] = append(controllerPkgMap[config.PackageNameMonolith], ctrlPkgPath)
if err := exampleGen.Generate(group, version, resources[name]); err != nil {
panic(errors.Wrapf(err, "cannot generate example manifest for resource %s", name))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/pipeline/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/muvaf/typewriter/pkg/wrapper"
"github.com/pkg/errors"

"github.com/upbound/upjet/pkg/config"
"github.com/upbound/upjet/pkg/pipeline/templates"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func (sg *ProviderGenerator) Generate(versionPkgMap map[string][]string, mainTem
t = tmpl
}
if t == nil {
return errors.Wrap(sg.generate("", versionPkgMap[monolithPackageName]), "failed to generate the controller setup file")
return errors.Wrap(sg.generate("", versionPkgMap[config.PackageNameMonolith]), "failed to generate the controller setup file")
}
for g, versionPkgList := range versionPkgMap {
if err := sg.generate(g, versionPkgList); err != nil {
Expand Down

0 comments on commit 645d726

Please sign in to comment.