forked from crossplane/upjet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
285 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package conversion | ||
|
||
import ( | ||
"github.com/crossplane/crossplane-runtime/pkg/fieldpath" | ||
"github.com/crossplane/crossplane-runtime/pkg/resource" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
AllVersions = "*" | ||
) | ||
|
||
const ( | ||
pathObjectMeta = "ObjectMeta" | ||
) | ||
|
||
type Conversion interface { | ||
GetSourceVersion() string | ||
GetTargetVersion() string | ||
} | ||
|
||
type PavedConversion interface { | ||
Conversion | ||
ConvertPaved(src, target fieldpath.Paved) error | ||
} | ||
|
||
type ManagedConversion interface { | ||
Conversion | ||
ConvertTerraformed(src, target resource.Managed) | ||
} | ||
|
||
type baseConversion struct { | ||
sourceVersion string | ||
targetVersion string | ||
} | ||
|
||
func newBaseConversion(sourceVersion, targetVersion string) baseConversion { | ||
return baseConversion{ | ||
sourceVersion: sourceVersion, | ||
targetVersion: targetVersion, | ||
} | ||
} | ||
|
||
func (c *baseConversion) GetSourceVersion() string { | ||
return c.sourceVersion | ||
} | ||
|
||
func (c *baseConversion) GetTargetVersion() string { | ||
return c.targetVersion | ||
} | ||
|
||
type fieldCopy struct { | ||
baseConversion | ||
sourceField string | ||
targetField string | ||
} | ||
|
||
func (f *fieldCopy) ConvertPaved(src, target fieldpath.Paved) error { | ||
v, err := src.GetValue(f.sourceField) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to get the field %q from the conversion source object", f.sourceField) | ||
} | ||
return errors.Wrapf(target.SetValue(f.targetField, v), "failed to set the field %q of the conversion target object", f.targetField) | ||
} | ||
|
||
func NewObjectMetaConversion() Conversion { | ||
return &fieldCopy{ | ||
baseConversion: newBaseConversion(AllVersions, AllVersions), | ||
sourceField: pathObjectMeta, | ||
targetField: pathObjectMeta, | ||
} | ||
} | ||
|
||
func NewFieldRenameConversion(sourceVersion, sourceField, targetVersion, targetField string) Conversion { | ||
return &fieldCopy{ | ||
baseConversion: newBaseConversion(sourceVersion, targetVersion), | ||
sourceField: sourceField, | ||
targetField: targetField, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pipeline | ||
|
||
import ( | ||
"go/types" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/muvaf/typewriter/pkg/wrapper" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/crossplane/upjet/pkg/pipeline/templates" | ||
) | ||
|
||
// NewConversionConvertibleGenerator returns a new ConversionConvertibleGenerator. | ||
func NewConversionConvertibleGenerator(pkg *types.Package, rootDir, group, version string) *ConversionConvertibleGenerator { | ||
return &ConversionConvertibleGenerator{ | ||
LocalDirectoryPath: filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0])), | ||
LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), | ||
pkg: pkg, | ||
version: version, | ||
} | ||
} | ||
|
||
// ConversionConvertibleGenerator generates conversion methods implementing the | ||
// conversion.Convertible interface on the CRD structs. | ||
type ConversionConvertibleGenerator struct { | ||
LocalDirectoryPath string | ||
LicenseHeaderPath string | ||
|
||
pkg *types.Package | ||
version string | ||
} | ||
|
||
// Generate writes generated conversion.Convertible interface functions | ||
func (cg *ConversionConvertibleGenerator) Generate(cfgs []*terraformedInput, apiVersion string) error { | ||
entries, err := os.ReadDir(cg.LocalDirectoryPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot list the directory entries for the source folder %s while generating the conversion.Convertible interface functions", cg.LocalDirectoryPath) | ||
} | ||
|
||
for _, e := range entries { | ||
if !e.IsDir() || e.Name() == cg.version { | ||
// we skip spoke generation for the current version as the assumption is | ||
// the current CRD version is the hub version. | ||
continue | ||
} | ||
trFile := wrapper.NewFile(cg.pkg.Path(), cg.pkg.Name(), templates.ConversionConvertibleTemplate, | ||
wrapper.WithGenStatement(GenStatement), | ||
wrapper.WithHeaderPath(cg.LicenseHeaderPath), | ||
) | ||
filePath := filepath.Join(cg.LocalDirectoryPath, "zz_generated.conversion.go") | ||
vars := map[string]any{ | ||
"APIVersion": apiVersion, | ||
} | ||
resources := make([]map[string]any, len(cfgs)) | ||
index := 0 | ||
for _, cfg := range cfgs { | ||
resources[index] = map[string]any{ | ||
"CRD": map[string]string{ | ||
"Kind": cfg.Kind, | ||
}, | ||
} | ||
index++ | ||
} | ||
vars["Resources"] = resources | ||
if err := trFile.Write(filePath, vars, os.ModePerm); err != nil { | ||
return errors.Wrapf(err, "cannot write the generated conversion Hub functions file %s", filePath) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pipeline | ||
|
||
import ( | ||
"go/types" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/muvaf/typewriter/pkg/wrapper" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/crossplane/upjet/pkg/pipeline/templates" | ||
) | ||
|
||
// NewConversionHubGenerator returns a new ConversionHubGenerator. | ||
func NewConversionHubGenerator(pkg *types.Package, rootDir, group, version string) *ConversionHubGenerator { | ||
return &ConversionHubGenerator{ | ||
LocalDirectoryPath: filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0]), version), | ||
LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), | ||
pkg: pkg, | ||
} | ||
} | ||
|
||
// ConversionHubGenerator generates conversion methods implementing the | ||
// conversion.Hub interface on the CRD structs. | ||
type ConversionHubGenerator struct { | ||
LocalDirectoryPath string | ||
LicenseHeaderPath string | ||
|
||
pkg *types.Package | ||
} | ||
|
||
// Generate writes generated conversion.Hub interface functions | ||
func (cg *ConversionHubGenerator) Generate(cfgs []*terraformedInput, apiVersion string) error { | ||
trFile := wrapper.NewFile(cg.pkg.Path(), cg.pkg.Name(), templates.ConversionHubTemplate, | ||
wrapper.WithGenStatement(GenStatement), | ||
wrapper.WithHeaderPath(cg.LicenseHeaderPath), | ||
) | ||
filePath := filepath.Join(cg.LocalDirectoryPath, "zz_generated.conversion.go") | ||
vars := map[string]any{ | ||
"APIVersion": apiVersion, | ||
} | ||
resources := make([]map[string]any, len(cfgs)) | ||
index := 0 | ||
for _, cfg := range cfgs { | ||
resources[index] = map[string]any{ | ||
"CRD": map[string]string{ | ||
"Kind": cfg.Kind, | ||
}, | ||
} | ||
index++ | ||
} | ||
vars["Resources"] = resources | ||
return errors.Wrapf( | ||
trFile.Write(filePath, vars, os.ModePerm), | ||
"cannot write the generated conversion Hub functions file %s", filePath, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
{{ .Header }} | ||
|
||
{{ .GenStatement }} | ||
|
||
package {{ .APIVersion }} | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/conversion" | ||
) | ||
|
||
{{ range .Resources }} | ||
// ConvertTo converts this {{ .CRD.Kind }} to the hub type. | ||
func (tr *{{ .CRD.Kind }}) ConvertTo(dstRaw conversion.Hub) error { | ||
return nil | ||
} | ||
|
||
// ConvertFrom converts from the hub type to the {{ .CRD.Kind }} type. | ||
func (tr *{{ .CRD.Kind }}) ConvertFrom(srcRaw conversion.Hub) error { | ||
return nil | ||
} | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
{{ .Header }} | ||
|
||
{{ .GenStatement }} | ||
|
||
package {{ .APIVersion }} | ||
|
||
{{ range .Resources }} | ||
// Hub marks this type as a conversion hub. | ||
func (tr *{{ .CRD.Kind }}) Hub() {} | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters