Skip to content

Commit

Permalink
Sync the MaxItems constraints for singleton lists from the Go schema
Browse files Browse the repository at this point in the history
to the JSON schema.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed May 30, 2024
1 parent 37bee7e commit 8d6b923
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
36 changes: 31 additions & 5 deletions config/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ package config

import (
"context"
// Note(ezgidemirel): we are importing this to embed provider schema document
_ "embed"
"fmt"
"regexp"
"strconv"

// Note(ezgidemirel): we are importing this to embed provider schema document
_ "embed"

"github.com/crossplane/upjet/pkg/config"
"github.com/crossplane/upjet/pkg/config/conversion"
"github.com/crossplane/upjet/pkg/registry/reference"
Expand Down Expand Up @@ -93,7 +92,35 @@ func getProviderSchema(s string) (*schema.Provider, error) {
// configuration is being read for the code generation pipelines.
// In that case, we will only use the JSON schema for generating
// the CRDs.
// GetProvider returns provider configuration
func GetProvider(ctx context.Context, generationProvider bool) (*config.Provider, error) {
pc, err := getProvider(ctx, generationProvider)
if err != nil {
return nil, err
}
// if not the generation time provider configuration, we are already using
// the Go schema, so no need for further processing.
if !generationProvider {
return pc, nil
}

// if we are preparing the provider configuration for code generation, then
// there can potentially be inconsistencies in the MaxItems constraints
// between the JSON schema and the Go schema. We need to sync the MaxItems
// constraints from the Go schema (source of truth) to the JSON schema.
pGo, err := getProvider(ctx, false) // to access the Go schema
if err != nil {
return nil, errors.Wrap(err, "failed to get the runtime Provider configuration")
}
// sync the MaxItems constraints for the singleton lists from the Go schema
// to the JSON schema using the maxItemsSync visitor.
if err := pGo.TraverseTFSchemas(config.NewListEmbedderWithMaxItemsSync(pc.Resources)); err != nil {
return nil, errors.Wrap(err, "failed to sync the MaxItems constraints for singleton lists in the Go schema to the JSON schema")
}
return pc, errors.Wrap(bumpVersionsWithEmbeddedLists(pc), "failed to bump API versions with converted singleton lists")
}

func getProvider(ctx context.Context, generationProvider bool) (*config.Provider, error) {
var p *schema.Provider
var fwProvider fwprovider.Provider
var err error
Expand Down Expand Up @@ -121,7 +148,6 @@ func GetProvider(ctx context.Context, generationProvider bool) (*config.Provider
config.WithMainTemplate(hack.MainTemplate),
config.WithTerraformProvider(p),
config.WithTerraformPluginFrameworkProvider(fwProvider),
config.WithSchemaTraversers(&config.SingletonListEmbedder{}),
config.WithDefaultResourceOptions(
GroupKindOverrides(),
KindOverrides(),
Expand All @@ -143,7 +169,7 @@ func GetProvider(ctx context.Context, generationProvider bool) (*config.Provider
}

pc.ConfigureResources()
return pc, bumpVersionsWithEmbeddedLists(pc)
return pc, nil
}

func bumpVersionsWithEmbeddedLists(pc *config.Provider) error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,4 @@ replace github.com/hashicorp/terraform-plugin-log => github.com/gdavison/terrafo

replace github.com/hashicorp/terraform-provider-aws => github.com/upbound/terraform-provider-aws v0.0.0-20240523140457-101595b8576e

replace github.com/crossplane/upjet => github.com/ulucinar/upbound-upjet v0.0.0-20240529194811-fc6948e3c67c
replace github.com/crossplane/upjet => github.com/ulucinar/upbound-upjet v0.0.0-20240530181334-ad820b341ab0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ=
github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w=
github.com/ulucinar/upbound-upjet v0.0.0-20240529194811-fc6948e3c67c h1:IVb1OMy4jR4WrxZ+mrGtqiKAPpgQvqfykL6VsTXrMl8=
github.com/ulucinar/upbound-upjet v0.0.0-20240529194811-fc6948e3c67c/go.mod h1:3pDVtCgyBc5f2Zx4K5HEPxxhjndmOc5CHCJNpIivK/g=
github.com/ulucinar/upbound-upjet v0.0.0-20240530181334-ad820b341ab0 h1:ldFJ2H/D1oSuJZnoJ/xQEUp+WEHTumBAUGXe4VCtARE=
github.com/ulucinar/upbound-upjet v0.0.0-20240530181334-ad820b341ab0/go.mod h1:3pDVtCgyBc5f2Zx4K5HEPxxhjndmOc5CHCJNpIivK/g=
github.com/upbound/terraform-provider-aws v0.0.0-20240523140457-101595b8576e h1:Oq2PA7E9F52eMoc8vAqKMffek7gqymBSiXd5kiitHbE=
github.com/upbound/terraform-provider-aws v0.0.0-20240523140457-101595b8576e/go.mod h1:yWeBcsPCtmEKOurig3GUxpnbQnw8AbEsHFVnakEsHUU=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down

0 comments on commit 8d6b923

Please sign in to comment.