From f2769613e6a69388fac833c1e6fdc185e341cb67 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Thu, 30 Apr 2020 00:23:41 +0100 Subject: [PATCH] cleanup: remove v1 code source which is deprecated --- cmd/main.go | 6 +----- internal/config/config.go | 26 -------------------------- pkg/cli/cli.go | 11 +++++++---- pkg/model/config/project_type.go | 12 ++++++++++++ pkg/scaffold/api.go | 10 ++++++---- pkg/scaffold/init.go | 10 ++++++---- pkg/scaffold/webhook.go | 10 ++++++---- test/e2e/v2/e2e_suite.go | 2 +- test/e2e/v3/e2e_suite.go | 2 +- 9 files changed, 40 insertions(+), 49 deletions(-) create mode 100644 pkg/model/config/project_type.go diff --git a/cmd/main.go b/cmd/main.go index ad0fe414c7f..b22d517ac9d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,16 +20,11 @@ import ( "log" "sigs.k8s.io/kubebuilder/cmd/version" - "sigs.k8s.io/kubebuilder/internal/config" "sigs.k8s.io/kubebuilder/pkg/cli" pluginv2 "sigs.k8s.io/kubebuilder/pkg/plugin/v2" ) func main() { - if err := config.CheckProjectVersionSupported(); err != nil { - log.Fatal(err) - } - c, err := cli.New( cli.WithPlugins( &pluginv2.Plugin{}, @@ -39,6 +34,7 @@ func main() { ), cli.WithExtraCommands( newEditCmd(), + newUpdateCmd(), newCompletionCmd(), version.NewCmd(), ), diff --git a/internal/config/config.go b/internal/config/config.go index 48fdc0ecbff..2d91287f28a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -32,9 +32,6 @@ const ( // DefaultVersion is the version which will be used when the version flag is not provided DefaultVersion = config.Version3Alpha - - // To print error with colored - NoticeColor = "\033[1;36m%s\033[0m" ) func exists(fs afero.Fs, path string) (bool, error) { @@ -182,26 +179,3 @@ type saveError struct { func (e saveError) Error() string { return fmt.Sprintf("unable to save the configuration: %v", e.err) } - -// todo: check the best way we centralize it -// CheckProjectVersionSupported returns true if the project is already configured and it is v2 -func CheckProjectVersionSupported() error { - cfg, err := Read() - if os.IsNotExist(err) { - return nil - } - - if err != nil { - return err - } - - if cfg.IsV1() { - return fmt.Errorf(NoticeColor, "The v1 projects are no longer supported.\n"+ - "See how to upgrade your project to v2: https://book.kubebuilder.io/migration/guide.html\n") - } - - if !cfg.IsV2() && !cfg.IsV3() { - return fmt.Errorf("unknown project version %v", cfg.Version) - } - return nil -} diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 72af1db487c..1d9dacfd567 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -32,6 +32,7 @@ import ( ) const ( + noticeColor = "\033[1;36m%s\033[0m" runInProjectRootMsg = `For project-specific information, run this command in the root directory of a project. ` @@ -193,6 +194,11 @@ func (c *cli) initialize() error { } else if err == nil { c.configured = true c.projectVersion = projectConfig.Version + + if projectConfig.IsV1() { + return fmt.Errorf(noticeColor, "The v1 projects are no longer supported.\n"+ + "See how to upgrade your project to v2: https://book.kubebuilder.io/migration/guide.html\n") + } } else { return fmt.Errorf("failed to read config: %v", err) } @@ -248,7 +254,7 @@ func (c *cli) initialize() error { // Write deprecation notices after all commands have been constructed. for _, p := range c.resolvedPlugins { if d, isDeprecated := p.(plugin.Deprecated); isDeprecated { - fmt.Printf(internalconfig.NoticeColor, fmt.Sprintf("[Deprecation Notice] %s\n\n", + fmt.Printf(noticeColor, fmt.Sprintf("[Deprecation Notice] %s\n\n", d.DeprecationWarning())) } } @@ -358,9 +364,6 @@ func validatePlugins(plugins ...plugin.Base) error { // buildRootCmd returns a root command with a subcommand tree reflecting the // current project's state. func (c cli) buildRootCmd() *cobra.Command { - if err := internalconfig.CheckProjectVersionSupported(); err != nil { - log.Fatal(err) - } rootCmd := c.defaultCommand() // kubebuilder alpha diff --git a/pkg/model/config/project_type.go b/pkg/model/config/project_type.go new file mode 100644 index 00000000000..d1a2a01a4b2 --- /dev/null +++ b/pkg/model/config/project_type.go @@ -0,0 +1,12 @@ +package config + +// constants for scaffolding types +const ( + Go = "go" + Ansible = "Ansible" + Helm = "Helm" +) + +type Type struct { + Type string +} diff --git a/pkg/scaffold/api.go b/pkg/scaffold/api.go index c2573d05b59..e29367cc888 100644 --- a/pkg/scaffold/api.go +++ b/pkg/scaffold/api.go @@ -19,7 +19,6 @@ package scaffold import ( "fmt" - internalconfig "sigs.k8s.io/kubebuilder/internal/config" "sigs.k8s.io/kubebuilder/pkg/model" "sigs.k8s.io/kubebuilder/pkg/model/config" "sigs.k8s.io/kubebuilder/pkg/model/resource" @@ -66,10 +65,13 @@ func NewAPIScaffolder( // Scaffold implements Scaffolder func (s *apiScaffolder) Scaffold() error { fmt.Println("Writing scaffold for you to edit...") - if err := internalconfig.CheckProjectVersionSupported(); err != nil { - return err + + switch { + case s.config.IsV2(), s.config.IsV3(): + return s.scaffold() + default: + return fmt.Errorf("unknown project version %v", s.config.Version) } - return s.scaffold() } func (s *apiScaffolder) newUniverse() *model.Universe { diff --git a/pkg/scaffold/init.go b/pkg/scaffold/init.go index 2c6e00f08d5..5ba89138fa2 100644 --- a/pkg/scaffold/init.go +++ b/pkg/scaffold/init.go @@ -21,7 +21,6 @@ import ( "io/ioutil" "path/filepath" - internalconfig "sigs.k8s.io/kubebuilder/internal/config" "sigs.k8s.io/kubebuilder/pkg/model" "sigs.k8s.io/kubebuilder/pkg/model/config" "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/machinery" @@ -73,10 +72,13 @@ func (s *initScaffolder) newUniverse(boilerplate string) *model.Universe { // Scaffold implements Scaffolder func (s *initScaffolder) Scaffold() error { fmt.Println("Writing scaffold for you to edit...") - if err := internalconfig.CheckProjectVersionSupported(); err != nil { - return err + + switch { + case s.config.IsV2(), s.config.IsV3(): + return s.scaffold() + default: + return fmt.Errorf("unknown project version %v", s.config.Version) } - return s.scaffold() } func (s *initScaffolder) scaffold() error { diff --git a/pkg/scaffold/webhook.go b/pkg/scaffold/webhook.go index d0cc61fc9eb..b37167f7197 100644 --- a/pkg/scaffold/webhook.go +++ b/pkg/scaffold/webhook.go @@ -19,7 +19,6 @@ package scaffold import ( "fmt" - internalconfig "sigs.k8s.io/kubebuilder/internal/config" "sigs.k8s.io/kubebuilder/pkg/model" "sigs.k8s.io/kubebuilder/pkg/model/config" "sigs.k8s.io/kubebuilder/pkg/model/resource" @@ -61,10 +60,13 @@ func NewWebhookScaffolder( // Scaffold implements Scaffolder func (s *webhookScaffolder) Scaffold() error { fmt.Println("Writing scaffold for you to edit...") - if err := internalconfig.CheckProjectVersionSupported(); err != nil { - return err + + switch { + case s.config.IsV2(), s.config.IsV3(): + return s.scaffold() + default: + return fmt.Errorf("unknown project version %v", s.config.Version) } - return s.scaffold() } func (s *webhookScaffolder) newUniverse() *model.Universe { diff --git a/test/e2e/v2/e2e_suite.go b/test/e2e/v2/e2e_suite.go index 7e521910e3d..7d68102d3f9 100644 --- a/test/e2e/v2/e2e_suite.go +++ b/test/e2e/v2/e2e_suite.go @@ -66,7 +66,7 @@ var _ = Describe("kubebuilder", func() { By("init v2 project") err := kbc.Init( "--domain", kbc.Domain, - "--fetch-deps=false") + "--dep=false") Expect(err).Should(Succeed()) By("creating api definition") diff --git a/test/e2e/v3/e2e_suite.go b/test/e2e/v3/e2e_suite.go index 98f7eac2c3b..edd2836b27f 100644 --- a/test/e2e/v3/e2e_suite.go +++ b/test/e2e/v3/e2e_suite.go @@ -67,7 +67,7 @@ var _ = Describe("kubebuilder", func() { err := kbc.Init( "--project-version", "3-alpha", "--domain", kbc.Domain, - "--fetch-deps=false") + "--dep=false") Expect(err).Should(Succeed()) By("creating api definition")