Skip to content

Commit

Permalink
Merge pull request #402 from ulucinar/available-versions
Browse files Browse the repository at this point in the history
Add config.Resource.PreviousVersions to specify the previous versions of an MR API
  • Loading branch information
ulucinar authored May 15, 2024
2 parents 942508c + b5622a1 commit 92d1af8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ type Resource struct {
// Version is the API version being generated for the corresponding CRD.
Version string

// PreviousVersions is the list of API versions previously generated for this
// resource for multi-versioned managed resources. upjet will attempt to load
// the type definitions from these previous versions if configured.
PreviousVersions []string

// ControllerReconcileVersion is the CRD API version the associated
// controller will watch & reconcile. If left unspecified,
// defaults to the value of Version. This configuration parameter
Expand Down Expand Up @@ -536,6 +541,9 @@ type Resource struct {
// conflict. By convention, also used in upjet, the field name is preceded by
// the value of the generated Kind, for example:
// "TagParameters": "ClusterTagParameters"
// Deprecated: OverrideFieldNames has been deprecated in favor of loading
// the already existing type names from the older versions of the MR APIS
// via the PreviousVersions API.
OverrideFieldNames map[string]string

// requiredFields are the fields that will be marked as required in the
Expand Down
4 changes: 4 additions & 0 deletions pkg/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func Run(pc *config.Provider, rootDir string) { //nolint:gocyclo
tfGen := NewTerraformedGenerator(versionGen.Package(), rootDir, group, version)
ctrlGen := NewControllerGenerator(rootDir, pc.ModulePath, group)

if err := versionGen.InsertPreviousObjects(versions); err != nil {
panic(errors.Wrapf(err, "cannot insert type definitions from the previous versions into the package scope for group %q", group))
}

for _, name := range sortedResources(resources) {
paramTypeName, err := crdGen.Generate(resources[name])
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions pkg/pipeline/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
package pipeline

import (
"fmt"
"go/types"
"os"
"path/filepath"
"strings"

"github.com/muvaf/typewriter/pkg/wrapper"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"

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

Expand Down Expand Up @@ -60,3 +63,36 @@ func (vg *VersionGenerator) Generate() error {
func (vg *VersionGenerator) Package() *types.Package {
return vg.pkg
}

// InsertPreviousObjects inserts into this VersionGenerator's package scope all
// the type definitions from the previous versions of the managed resource APIs
// found in the Go package.
func (vg *VersionGenerator) InsertPreviousObjects(versions map[string]map[string]*config.Resource) error {
for _, resources := range versions {
for _, r := range resources {
for _, v := range r.PreviousVersions {
if vg.Version != v {
// if this previous version v is not the version we are currently
// processing
continue
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedTypes,
Dir: vg.DirectoryPath,
}, fmt.Sprintf("zz_%s_types.go", strings.ToLower(r.Kind)))
if err != nil {
return errors.Wrapf(err, "cannot load the previous versions of %q from path %s", r.Name, vg.pkg.Path())
}
for _, p := range pkgs {
if p.Types == nil || p.Types.Scope() == nil {
continue
}
for _, n := range p.Types.Scope().Names() {
vg.pkg.Scope().Insert(p.Types.Scope().Lookup(n))
}
}
}
}
}
return nil
}

0 comments on commit 92d1af8

Please sign in to comment.