Skip to content

Commit

Permalink
⚠️ Remove api deprecations: The methods NewBundle, GetShortName, IsLe…
Browse files Browse the repository at this point in the history
…gacyLayout are no longer available (#3929)

* ⚠️ remove deprecated api NewBundle in favor of NewBundleWithOptions
* ⚠️ remove deprecated apis no longer used GetShortName and IsLegacyLayout
* cleanup: remove unused usage of IsLegacyLayout
  • Loading branch information
camilamacedo86 committed May 20, 2024
1 parent afa5b3c commit 98c187e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 115 deletions.
38 changes: 1 addition & 37 deletions pkg/plugin/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,42 +58,6 @@ func WithDeprecationMessage(msg string) BundleOption {

}

// NewBundle creates a new Bundle with the provided name and version, and that wraps the provided plugins.
// The list of supported project versions is computed from the provided plugins.
//
// Deprecated: Use the NewBundle informing the options from now one. Replace its use for as the
// following example. Example:
//
// mylanguagev1Bundle, _ := plugin.NewBundle(plugin.WithName(language.DefaultNameQualifier),
// plugin.WithVersion(plugin.Version{Number: 1}),
// plugin.WithPlugins(kustomizecommonv1.Plugin{}, mylanguagev1.Plugin{}),
func NewBundle(name string, version Version, deprecateWarning string, plugins ...Plugin) (Bundle, error) {
supportedProjectVersions := CommonSupportedProjectVersions(plugins...)
if len(supportedProjectVersions) == 0 {
return nil, fmt.Errorf("in order to bundle plugins, they must all support at least one common project version")
}

// Plugins may be bundles themselves, so unbundle here
// NOTE(Adirio): unbundling here ensures that Bundle.Plugin always returns a flat list of Plugins instead of also
// including Bundles, and therefore we don't have to use a recursive algorithm when resolving.
allPlugins := make([]Plugin, 0, len(plugins))
for _, plugin := range plugins {
if pluginBundle, isBundle := plugin.(Bundle); isBundle {
allPlugins = append(allPlugins, pluginBundle.Plugins()...)
} else {
allPlugins = append(allPlugins, plugin)
}
}

return bundle{
name: name,
version: version,
plugins: allPlugins,
supportedProjectVersions: supportedProjectVersions,
deprecateWarning: deprecateWarning,
}, nil
}

// NewBundleWithOptions creates a new Bundle with the provided BundleOptions.
// The list of supported project versions is computed from the provided plugins in options.
func NewBundleWithOptions(opts ...BundleOption) (Bundle, error) {
Expand Down Expand Up @@ -149,7 +113,7 @@ func (b bundle) Plugins() []Plugin {
return b.plugins
}

// Plugins implements Bundle
// DeprecationWarning return the warning message
func (b bundle) DeprecationWarning() string {
return b.deprecateWarning
}
18 changes: 14 additions & 4 deletions pkg/plugin/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ var _ = Describe("Bundle", func() {
{p1, p2, p3},
{p1, p3, p4},
} {
b, err := NewBundle(name, version, "", plugins...)

b, err := NewBundleWithOptions(WithName(name),
WithVersion(version),
WithPlugins(plugins...))
Expect(err).NotTo(HaveOccurred())
Expect(b.Name()).To(Equal(name))
Expect(b.Version().Compare(version)).To(Equal(0))
Expand All @@ -88,9 +91,13 @@ var _ = Describe("Bundle", func() {
var a, b Bundle
var err error
plugins := []Plugin{p1, p2, p3}
a, err = NewBundle("a", version, "", p1, p2)
a, err = NewBundleWithOptions(WithName("a"),
WithVersion(version),
WithPlugins(p1, p2))
Expect(err).NotTo(HaveOccurred())
b, err = NewBundle("b", version, "", a, p3)
b, err = NewBundleWithOptions(WithName("b"),
WithVersion(version),
WithPlugins(a, p3))
Expect(err).NotTo(HaveOccurred())
versions := b.SupportedProjectVersions()
sort.Slice(versions, func(i int, j int) bool {
Expand All @@ -113,7 +120,10 @@ var _ = Describe("Bundle", func() {

{p1, p2, p3, p4},
} {
_, err := NewBundle(name, version, "", plugins...)
_, err := NewBundleWithOptions(WithName(name),
WithVersion(version),
WithPlugins(plugins...))

Expect(err).To(HaveOccurred())
}
})
Expand Down
20 changes: 0 additions & 20 deletions pkg/plugin/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,6 @@ func SplitKey(key string) (string, string) {
return keyParts[0], keyParts[1]
}

// GetShortName returns plugin's short name (name before domain) if name
// is fully qualified (has a domain suffix), otherwise GetShortName returns name.
// Deprecated
func GetShortName(name string) string {
return strings.SplitN(name, ".", 2)[0]
}

// Deprecated: it was added to ensure backwards compatibility and should
// be removed when we remove the go/v3 plugin
// IsLegacyLayout returns true when is possible to identify that the project
// was scaffolded with the previous layout
func IsLegacyLayout(config config.Config) bool {
for _, pluginKey := range config.GetPluginChain() {
if strings.Contains(pluginKey, "go.kubebuilder.io/v3") || strings.Contains(pluginKey, "go.kubebuilder.io/v2") {
return true
}
}
return false
}

// Validate ensures a Plugin is valid.
func Validate(p Plugin) error {
if err := validateName(p.Name()); err != nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/plugin/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ var _ = Describe("SplitKey", func() {
})
})

var _ = Describe("GetShortName", func() {
It("should extract base names from domains", func() {
Expect(GetShortName(name)).To(Equal(short))
})
})

var _ = Describe("Validate", func() {
It("should succeed for valid plugins", func() {
plugin := mockPlugin{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,15 @@ type Types struct {

// Port if informed we will create the scaffold with this spec
Port string

IsLegacyLayout bool
}

// SetTemplateDefaults implements file.Template
func (f *Types) SetTemplateDefaults() error {
if f.Path == "" {

if f.IsLegacyLayout {
if f.MultiGroup {
if f.Resource.Group != "" {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "%[kind]_types.go")
} else {
f.Path = filepath.Join("apis", "%[version]", "%[kind]_types.go")
}
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_types.go")
}
if f.MultiGroup && f.Resource.Group != "" {
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_types.go")
} else {
if f.MultiGroup && f.Resource.Group != "" {
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_types.go")
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_types.go")
}
f.Path = filepath.Join("api", "%[version]", "%[kind]_types.go")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,23 @@ type ControllerTest struct {
machinery.BoilerplateMixin
machinery.ResourceMixin

Port string
IsLegacyLayout bool
PackageName string
Port string
PackageName string
}

// SetTemplateDefaults implements file.Template
func (f *ControllerTest) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup && f.Resource.Group != "" {
if f.IsLegacyLayout {
f.Path = filepath.Join("controllers", "%[group]", "%[kind]_controller_test.go")
} else {
f.Path = filepath.Join("internal", "controller", "%[group]", "%[kind]_controller_test.go")
}
f.Path = filepath.Join("internal", "controller", "%[group]", "%[kind]_controller_test.go")
} else {
if f.IsLegacyLayout {
f.Path = filepath.Join("controllers", "%[kind]_controller_test.go")
} else {
f.Path = filepath.Join("internal", "controller", "%[kind]_controller_test.go")
}
f.Path = filepath.Join("internal", "controller", "%[kind]_controller_test.go")
}
}
f.Path = f.Resource.Replacer().Replace(f.Path)
log.Println(f.Path)

f.PackageName = "controller"
if f.IsLegacyLayout {
f.PackageName = "controllers"
}

f.IfExistsAction = machinery.OverwriteFile

log.Println("creating import for %", f.Resource.Path)
Expand Down
16 changes: 3 additions & 13 deletions pkg/plugins/golang/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

var (
Expand Down Expand Up @@ -76,11 +75,7 @@ func (opts Options) UpdateResource(res *resource.Resource, c config.Config) {

if opts.DoAPI {
//nolint:staticcheck
if plugin.IsLegacyLayout(c) {
res.Path = resource.APIPackagePathLegacy(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
} else {
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
}
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())

res.API = &resource.API{
CRDVersion: "v1",
Expand All @@ -94,14 +89,9 @@ func (opts Options) UpdateResource(res *resource.Resource, c config.Config) {
}

if opts.DoDefaulting || opts.DoValidation || opts.DoConversion {
// IsLegacyLayout is added to ensure backwards compatibility and should
// be removed when we remove the go/v3 plugin
//nolint:staticcheck
if plugin.IsLegacyLayout(c) {
res.Path = resource.APIPackagePathLegacy(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
} else {
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
}
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())

res.Webhooks.WebhookVersion = "v1"
if opts.DoDefaulting {
res.Webhooks.Defaulting = true
Expand Down

0 comments on commit 98c187e

Please sign in to comment.