Skip to content

Commit 1e78a6f

Browse files
authored
Merge pull request #1911 from Adirio/update-models
⚠ Update models
2 parents 79ecd51 + ea75c59 commit 1e78a6f

File tree

102 files changed

+5729
-2732
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+5729
-2732
lines changed

cmd/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
"log"
2121

2222
"sigs.k8s.io/kubebuilder/v3/pkg/cli"
23-
"sigs.k8s.io/kubebuilder/v3/pkg/model/config"
23+
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
24+
cfgv3alpha "sigs.k8s.io/kubebuilder/v3/pkg/config/v3alpha"
2425
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
2526
pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
2627
)
@@ -29,13 +30,13 @@ func main() {
2930
c, err := cli.New(
3031
cli.WithCommandName("kubebuilder"),
3132
cli.WithVersion(versionString()),
32-
cli.WithDefaultProjectVersion(config.Version3Alpha),
33+
cli.WithDefaultProjectVersion(cfgv3alpha.Version),
3334
cli.WithPlugins(
3435
&pluginv2.Plugin{},
3536
&pluginv3.Plugin{},
3637
),
37-
cli.WithDefaultPlugins(config.Version2, &pluginv2.Plugin{}),
38-
cli.WithDefaultPlugins(config.Version3Alpha, &pluginv3.Plugin{}),
38+
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
39+
cli.WithDefaultPlugins(cfgv3alpha.Version, &pluginv3.Plugin{}),
3940
cli.WithCompletion,
4041
)
4142
if err != nil {

pkg/cli/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (c cli) bindCreateAPI(ctx plugin.Context, cmd *cobra.Command) {
8383
}
8484

8585
subcommand := createAPIPlugin.GetCreateAPISubcommand()
86-
subcommand.InjectConfig(&cfg.Config)
86+
subcommand.InjectConfig(cfg.Config)
8787
subcommand.BindFlags(cmd.Flags())
8888
subcommand.UpdateContext(&ctx)
8989
cmd.Long = ctx.Description

pkg/cli/cli.go

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"github.com/spf13/pflag"
2626

2727
internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
28-
"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
29-
"sigs.k8s.io/kubebuilder/v3/pkg/model/config"
28+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
29+
cfgv3alpha "sigs.k8s.io/kubebuilder/v3/pkg/config/v3alpha"
3030
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
3131
)
3232

@@ -74,9 +74,9 @@ type cli struct { //nolint:maligned
7474
// CLI version string.
7575
version string
7676
// Default project version in case none is provided and a config file can't be found.
77-
defaultProjectVersion string
77+
defaultProjectVersion config.Version
7878
// Default plugins in case none is provided and a config file can't be found.
79-
defaultPlugins map[string][]string
79+
defaultPlugins map[config.Version][]string
8080
// Plugins registered in the cli.
8181
plugins map[string]plugin.Plugin
8282
// Commands injected by options.
@@ -87,7 +87,7 @@ type cli struct { //nolint:maligned
8787
/* Internal fields */
8888

8989
// Project version to scaffold.
90-
projectVersion string
90+
projectVersion config.Version
9191
// Plugin keys to scaffold with.
9292
pluginKeys []string
9393

@@ -131,8 +131,8 @@ func newCLI(opts ...Option) (*cli, error) {
131131
// Default cli options.
132132
c := &cli{
133133
commandName: "kubebuilder",
134-
defaultProjectVersion: internalconfig.DefaultVersion,
135-
defaultPlugins: make(map[string][]string),
134+
defaultProjectVersion: cfgv3alpha.Version,
135+
defaultPlugins: make(map[config.Version][]string),
136136
plugins: make(map[string]plugin.Plugin),
137137
}
138138

@@ -191,90 +191,98 @@ func (c *cli) getInfoFromFlags() (string, []string, error) {
191191
}
192192

193193
// getInfoFromConfigFile obtains the project version and plugin keys from the project config file.
194-
func getInfoFromConfigFile() (string, []string, error) {
194+
func getInfoFromConfigFile() (config.Version, []string, error) {
195195
// Read the project configuration file
196196
projectConfig, err := internalconfig.Read()
197197
switch {
198198
case err == nil:
199199
case os.IsNotExist(err):
200-
return "", nil, nil
200+
return config.Version{}, nil, nil
201201
default:
202-
return "", nil, err
202+
return config.Version{}, nil, err
203203
}
204204

205205
return getInfoFromConfig(projectConfig)
206206
}
207207

208208
// getInfoFromConfig obtains the project version and plugin keys from the project config.
209209
// It is extracted from getInfoFromConfigFile for testing purposes.
210-
func getInfoFromConfig(projectConfig *config.Config) (string, []string, error) {
210+
func getInfoFromConfig(projectConfig config.Config) (config.Version, []string, error) {
211211
// Split the comma-separated plugins
212212
var pluginSet []string
213-
if projectConfig.Layout != "" {
214-
for _, p := range strings.Split(projectConfig.Layout, ",") {
213+
if projectConfig.GetLayout() != "" {
214+
for _, p := range strings.Split(projectConfig.GetLayout(), ",") {
215215
pluginSet = append(pluginSet, strings.TrimSpace(p))
216216
}
217217
}
218218

219-
return projectConfig.Version, pluginSet, nil
219+
return projectConfig.GetVersion(), pluginSet, nil
220220
}
221221

222222
// resolveFlagsAndConfigFileConflicts checks if the provided combined input from flags and
223223
// the config file is valid and uses default values in case some info was not provided.
224224
func (c cli) resolveFlagsAndConfigFileConflicts(
225-
flagProjectVersion, cfgProjectVersion string,
225+
flagProjectVersionString string,
226+
cfgProjectVersion config.Version,
226227
flagPlugins, cfgPlugins []string,
227-
) (string, []string, error) {
228+
) (config.Version, []string, error) {
229+
// Parse project configuration version from flags
230+
var flagProjectVersion config.Version
231+
if flagProjectVersionString != "" {
232+
if err := flagProjectVersion.Parse(flagProjectVersionString); err != nil {
233+
return config.Version{}, nil, fmt.Errorf("unable to parse project version flag: %w", err)
234+
}
235+
}
236+
228237
// Resolve project version
229-
var projectVersion string
238+
var projectVersion config.Version
239+
isFlagProjectVersionInvalid := flagProjectVersion.Validate() != nil
240+
isCfgProjectVersionInvalid := cfgProjectVersion.Validate() != nil
230241
switch {
231-
// If they are both blank, use the default
232-
case flagProjectVersion == "" && cfgProjectVersion == "":
242+
// If they are both invalid (empty is invalid), use the default
243+
case isFlagProjectVersionInvalid && isCfgProjectVersionInvalid:
233244
projectVersion = c.defaultProjectVersion
234-
// If they are equal doesn't matter which we choose
235-
case flagProjectVersion == cfgProjectVersion:
245+
// If any is invalid (empty is invalid), choose the other
246+
case isCfgProjectVersionInvalid:
236247
projectVersion = flagProjectVersion
237-
// If any is blank, choose the other
238-
case cfgProjectVersion == "":
239-
projectVersion = flagProjectVersion
240-
case flagProjectVersion == "":
248+
case isFlagProjectVersionInvalid:
241249
projectVersion = cfgProjectVersion
242-
// If none is blank and they are different error out
250+
// If they are equal doesn't matter which we choose
251+
case flagProjectVersion.Compare(cfgProjectVersion) == 0:
252+
projectVersion = flagProjectVersion
253+
// If both are valid (empty is invalid) and they are different error out
243254
default:
244-
return "", nil, fmt.Errorf("project version conflict between command line args (%s) "+
245-
"and project configuration file (%s)", flagProjectVersion, cfgProjectVersion)
246-
}
247-
// It still may be empty if default, flag and config project versions are empty
248-
if projectVersion != "" {
249-
// Validate the project version
250-
if err := validation.ValidateProjectVersion(projectVersion); err != nil {
251-
return "", nil, err
252-
}
255+
return config.Version{}, nil, fmt.Errorf("project version conflict between command line args (%s) "+
256+
"and project configuration file (%s)", flagProjectVersionString, cfgProjectVersion)
253257
}
254258

255259
// Resolve plugins
256260
var plugins []string
261+
isFlagPluginsEmpty := len(flagPlugins) == 0
262+
isCfgPluginsEmpty := len(cfgPlugins) == 0
257263
switch {
258-
// If they are both blank, use the default
259-
case len(flagPlugins) == 0 && len(cfgPlugins) == 0:
260-
plugins = c.defaultPlugins[projectVersion]
264+
// If they are both empty, use the default
265+
case isFlagPluginsEmpty && isCfgPluginsEmpty:
266+
if defaults, hasDefaults := c.defaultPlugins[projectVersion]; hasDefaults {
267+
plugins = defaults
268+
}
269+
// If any is empty, choose the other
270+
case isCfgPluginsEmpty:
271+
plugins = flagPlugins
272+
case isFlagPluginsEmpty:
273+
plugins = cfgPlugins
261274
// If they are equal doesn't matter which we choose
262275
case equalStringSlice(flagPlugins, cfgPlugins):
263276
plugins = flagPlugins
264-
// If any is blank, choose the other
265-
case len(cfgPlugins) == 0:
266-
plugins = flagPlugins
267-
case len(flagPlugins) == 0:
268-
plugins = cfgPlugins
269-
// If none is blank and they are different error out
277+
// If none is empty and they are different error out
270278
default:
271-
return "", nil, fmt.Errorf("plugins conflict between command line args (%v) "+
279+
return config.Version{}, nil, fmt.Errorf("plugins conflict between command line args (%v) "+
272280
"and project configuration file (%v)", flagPlugins, cfgPlugins)
273281
}
274282
// Validate the plugins
275283
for _, p := range plugins {
276284
if err := plugin.ValidateKey(p); err != nil {
277-
return "", nil, err
285+
return config.Version{}, nil, err
278286
}
279287
}
280288

@@ -315,8 +323,8 @@ func (c *cli) resolve() error {
315323
// under no support contract. However users should be notified _why_ their plugin cannot be found.
316324
var extraErrMsg string
317325
if version != "" {
318-
ver, err := plugin.ParseVersion(version)
319-
if err != nil {
326+
var ver plugin.Version
327+
if err := ver.Parse(version); err != nil {
320328
return fmt.Errorf("error parsing input plugin version from key %q: %v", pluginKey, err)
321329
}
322330
if !ver.IsStable() {

pkg/cli/cli_suite_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
2424

25+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2526
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2627
)
2728

@@ -39,27 +40,27 @@ var (
3940
type mockPlugin struct { //nolint:maligned
4041
name string
4142
version plugin.Version
42-
projectVersions []string
43+
projectVersions []config.Version
4344
}
4445

45-
func newMockPlugin(name, version string, projVers ...string) plugin.Plugin {
46-
v, err := plugin.ParseVersion(version)
47-
if err != nil {
46+
func newMockPlugin(name, version string, projVers ...config.Version) plugin.Plugin {
47+
var v plugin.Version
48+
if err := v.Parse(version); err != nil {
4849
panic(err)
4950
}
5051
return mockPlugin{name, v, projVers}
5152
}
5253

53-
func (p mockPlugin) Name() string { return p.name }
54-
func (p mockPlugin) Version() plugin.Version { return p.version }
55-
func (p mockPlugin) SupportedProjectVersions() []string { return p.projectVersions }
54+
func (p mockPlugin) Name() string { return p.name }
55+
func (p mockPlugin) Version() plugin.Version { return p.version }
56+
func (p mockPlugin) SupportedProjectVersions() []config.Version { return p.projectVersions }
5657

5758
type mockDeprecatedPlugin struct { //nolint:maligned
5859
mockPlugin
5960
deprecation string
6061
}
6162

62-
func newMockDeprecatedPlugin(name, version, deprecation string, projVers ...string) plugin.Plugin {
63+
func newMockDeprecatedPlugin(name, version, deprecation string, projVers ...config.Version) plugin.Plugin {
6364
return mockDeprecatedPlugin{
6465
mockPlugin: newMockPlugin(name, version, projVers...).(mockPlugin),
6566
deprecation: deprecation,

0 commit comments

Comments
 (0)