From 8915bdd837d3270208f2a8a3ec12035eefdcd909 Mon Sep 17 00:00:00 2001 From: Sunil Arora Date: Fri, 28 Sep 2018 15:42:19 -0700 Subject: [PATCH] moved v1 cmds to main pkg and restructured wiring of commands --- cmd/kubebuilder/{v1 => }/api.go | 10 +-- cmd/kubebuilder/{v1 => }/docs.go | 4 +- cmd/kubebuilder/init_project.go | 8 +-- cmd/kubebuilder/main.go | 77 ++++++++++++++++++----- cmd/kubebuilder/v1/commands.go | 71 --------------------- cmd/kubebuilder/{v1 => }/vendor_update.go | 20 +++++- cmd/kubebuilder/version/version.go | 20 +++--- 7 files changed, 99 insertions(+), 111 deletions(-) rename cmd/kubebuilder/{v1 => }/api.go (97%) rename cmd/kubebuilder/{v1 => }/docs.go (94%) delete mode 100644 cmd/kubebuilder/v1/commands.go rename cmd/kubebuilder/{v1 => }/vendor_update.go (50%) diff --git a/cmd/kubebuilder/v1/api.go b/cmd/kubebuilder/api.go similarity index 97% rename from cmd/kubebuilder/v1/api.go rename to cmd/kubebuilder/api.go index 38ddaa75daa..dda5fd23ba9 100644 --- a/cmd/kubebuilder/v1/api.go +++ b/cmd/kubebuilder/api.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package v1 +package main import ( "bufio" @@ -42,7 +42,7 @@ type apiOptions struct { // APICmd represents the resource command -func (o *apiOptions) RunAddAPI() { +func (o *apiOptions) runAddAPI() { dieIfNoProject() reader := bufio.NewReader(os.Stdin) @@ -124,7 +124,7 @@ func (o *apiOptions) RunAddAPI() { } } -func AddAPICommand(cmd *cobra.Command) { +func newAPICommand() *cobra.Command { o := apiOptions{} apiCmd := &cobra.Command{ @@ -157,7 +157,7 @@ After the scaffold is written, api will run make on the project. make run `, Run: func(cmd *cobra.Command, args []string) { - o.RunAddAPI() + o.runAddAPI() }, } @@ -171,7 +171,7 @@ After the scaffold is written, api will run make on the project. o.controllerFlag = apiCmd.Flag("controller") o.r = ResourceForFlags(apiCmd.Flags()) - cmd.AddCommand(apiCmd) + return apiCmd } // dieIfNoProject checks to make sure the command is run from a directory containing a project file. diff --git a/cmd/kubebuilder/v1/docs.go b/cmd/kubebuilder/docs.go similarity index 94% rename from cmd/kubebuilder/v1/docs.go rename to cmd/kubebuilder/docs.go index e424fc701b4..04f9d81253c 100644 --- a/cmd/kubebuilder/v1/docs.go +++ b/cmd/kubebuilder/docs.go @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -package v1 +package main import ( "fmt" "github.com/spf13/cobra" ) -func docsCmd() *cobra.Command { +func newDocsCmd() *cobra.Command { return &cobra.Command{ Use: "docs", Short: "Generate API reference docs. Coming soon.", diff --git a/cmd/kubebuilder/init_project.go b/cmd/kubebuilder/init_project.go index 71d8b690ac6..d1d918389e7 100644 --- a/cmd/kubebuilder/init_project.go +++ b/cmd/kubebuilder/init_project.go @@ -35,7 +35,7 @@ import ( "sigs.k8s.io/controller-tools/pkg/scaffold/project" ) -func addInit(cmd *cobra.Command) { +func newInitProjectCmd() *cobra.Command { o := projectOptions{} initCmd := &cobra.Command{ @@ -58,7 +58,7 @@ project will prompt the user to run 'dep ensure' after writing the project files kubebuilder init --domain example.org --license apache2 --owner "The Kubernetes authors" `, Run: func(cmd *cobra.Command, args []string) { - o.RunInit() + o.runInit() }, } @@ -72,7 +72,7 @@ kubebuilder init --domain example.org --license apache2 --owner "The Kubernetes o.mgr = &manager.Cmd{} o.dkr = &manager.Dockerfile{} - cmd.AddCommand(initCmd) + return initCmd } type projectOptions struct { @@ -85,7 +85,7 @@ type projectOptions struct { depFlag *flag.Flag } -func (o *projectOptions) RunInit() { +func (o *projectOptions) runInit() { checkGoVersion() if !depExists() { diff --git a/cmd/kubebuilder/main.go b/cmd/kubebuilder/main.go index e7b9e4cd34f..5109c2ab0b6 100644 --- a/cmd/kubebuilder/main.go +++ b/cmd/kubebuilder/main.go @@ -24,7 +24,6 @@ import ( "strings" "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util" - "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/v1" "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version" "github.com/spf13/cobra" ) @@ -48,26 +47,72 @@ func main() { } util.Repo = strings.Replace(wd, util.GoSrc+string(filepath.Separator), "", 1) - // add init command - addInit(cmd) + rootCmd := defaultCommand() - // add version command - version.AddVersion(cmd) + rootCmd.AddCommand( + newInitProjectCmd(), + newAPICommand(), + version.NewVersionCmd(), + newDocsCmd(), + newVendorUpdateCmd(), + ) - // add all commands corresponding to v1 project - v1.AddCmds(cmd) - - if err := cmd.Execute(); err != nil { + if err := rootCmd.Execute(); err != nil { log.Fatal(err) } } -var cmd = &cobra.Command{ - Use: "kubebuilder", - Short: "Development kit for building Kubernetes extensions and tools.", - Run: RunMain, -} +func defaultCommand() *cobra.Command { + return &cobra.Command{ + Use: "kubebuilder", + Short: "Development kit for building Kubernetes extensions and tools.", + Long: ` +Development kit for building Kubernetes extensions and tools. + +Provides libraries and tools to create new projects, APIs and controllers. +Includes tools for packaging artifacts into an installer container. + +Typical project lifecycle: + +- initialize a project: + + kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes authors + +- create one or more a new resource APIs and add your code to them: + + kubebuilder create api --group --version --kind + +create resource will prompt the user for if it should scaffold the Resource and / or Controller. To only +scaffold a Controller for an existing Resource, select "n" for Resource. To only define +the schema for a Resource without writing a Controller, select "n" for Controller. -func RunMain(cmd *cobra.Command, args []string) { - cmd.Help() +After the scaffold is written, api will run make on the project. +`, + Example: ` + # Initialize your project + kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors" + + # Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate + kubebuilder create api --group ship --version v1beta1 --kind Frigate + + # Edit the API Scheme + nano pkg/apis/ship/v1beta1/frigate_types.go + + # Edit the Controller + nano pkg/controller/frigate/frigate_controller.go + + # Edit the Controller Test + nano pkg/controller/frigate/frigate_controller_test.go + + # Install CRDs into the Kubernetes cluster using kubectl apply + make install + + # Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config + make run +`, + + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, + } } diff --git a/cmd/kubebuilder/v1/commands.go b/cmd/kubebuilder/v1/commands.go deleted file mode 100644 index 58f5989e7d3..00000000000 --- a/cmd/kubebuilder/v1/commands.go +++ /dev/null @@ -1,71 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1 - -import ( - "github.com/spf13/cobra" -) - -func AddCmds(cmd *cobra.Command) { - AddAPICommand(cmd) - cmd.AddCommand(vendorUpdateCmd()) - cmd.AddCommand(docsCmd()) - - cmd.Example = `# Initialize your project - kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors" - - # Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate - kubebuilder create api --group ship --version v1beta1 --kind Frigate - - # Edit the API Scheme - nano pkg/apis/ship/v1beta1/frigate_types.go - - # Edit the Controller - nano pkg/controller/frigate/frigate_controller.go - - # Edit the Controller Test - nano pkg/controller/frigate/frigate_controller_test.go - - # Install CRDs into the Kubernetes cluster using kubectl apply - make install - - # Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config - make run` - - cmd.Long = ` -Development kit for building Kubernetes extensions and tools. - -Provides libraries and tools to create new projects, APIs and controllers. -Includes tools for packaging artifacts into an installer container. - -Typical project lifecycle: - -- initialize a project: - - kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes authors - -- create one or more a new resource APIs and add your code to them: - - kubebuilder create api --group --version --kind - -create resource will prompt the user for if it should scaffold the Resource and / or Controller. To only -scaffold a Controller for an existing Resource, select "n" for Resource. To only define -the schema for a Resource without writing a Controller, select "n" for Controller. - -After the scaffold is written, api will run make on the project. - ` -} diff --git a/cmd/kubebuilder/v1/vendor_update.go b/cmd/kubebuilder/vendor_update.go similarity index 50% rename from cmd/kubebuilder/v1/vendor_update.go rename to cmd/kubebuilder/vendor_update.go index f3c77048126..793b1ef10ac 100644 --- a/cmd/kubebuilder/v1/vendor_update.go +++ b/cmd/kubebuilder/vendor_update.go @@ -1,4 +1,20 @@ -package v1 +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main import ( "log" @@ -9,7 +25,7 @@ import ( "sigs.k8s.io/controller-tools/pkg/scaffold/project" ) -func vendorUpdateCmd() *cobra.Command { +func newVendorUpdateCmd() *cobra.Command { return &cobra.Command{ Use: "update", Short: "updates vendor dependencies.", diff --git a/cmd/kubebuilder/version/version.go b/cmd/kubebuilder/version/version.go index eb52006feb2..a5330cd5d8f 100644 --- a/cmd/kubebuilder/version/version.go +++ b/cmd/kubebuilder/version/version.go @@ -56,18 +56,16 @@ func (v Version) Print() { fmt.Printf("Version: %#v\n", v) } -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print the kubebuilder version.", - Long: `Print the kubebuilder version.`, - Example: `kubebuilder version`, - Run: RunVersion, -} - -func AddVersion(cmd *cobra.Command) { - cmd.AddCommand(versionCmd) +func NewVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Print the kubebuilder version.", + Long: `Print the kubebuilder version.`, + Example: `kubebuilder version`, + Run: runVersion, + } } -func RunVersion(cmd *cobra.Command, args []string) { +func runVersion(cmd *cobra.Command, args []string) { GetVersion().Print() }