Skip to content

Commit 8915bdd

Browse files
committed
moved v1 cmds to main pkg and restructured wiring of commands
1 parent 9e5af44 commit 8915bdd

File tree

7 files changed

+99
-111
lines changed

7 files changed

+99
-111
lines changed

cmd/kubebuilder/v1/api.go renamed to cmd/kubebuilder/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1
17+
package main
1818

1919
import (
2020
"bufio"
@@ -42,7 +42,7 @@ type apiOptions struct {
4242

4343
// APICmd represents the resource command
4444

45-
func (o *apiOptions) RunAddAPI() {
45+
func (o *apiOptions) runAddAPI() {
4646
dieIfNoProject()
4747

4848
reader := bufio.NewReader(os.Stdin)
@@ -124,7 +124,7 @@ func (o *apiOptions) RunAddAPI() {
124124
}
125125
}
126126

127-
func AddAPICommand(cmd *cobra.Command) {
127+
func newAPICommand() *cobra.Command {
128128
o := apiOptions{}
129129

130130
apiCmd := &cobra.Command{
@@ -157,7 +157,7 @@ After the scaffold is written, api will run make on the project.
157157
make run
158158
`,
159159
Run: func(cmd *cobra.Command, args []string) {
160-
o.RunAddAPI()
160+
o.runAddAPI()
161161
},
162162
}
163163

@@ -171,7 +171,7 @@ After the scaffold is written, api will run make on the project.
171171
o.controllerFlag = apiCmd.Flag("controller")
172172
o.r = ResourceForFlags(apiCmd.Flags())
173173

174-
cmd.AddCommand(apiCmd)
174+
return apiCmd
175175
}
176176

177177
// dieIfNoProject checks to make sure the command is run from a directory containing a project file.

cmd/kubebuilder/v1/docs.go renamed to cmd/kubebuilder/docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1
17+
package main
1818

1919
import (
2020
"fmt"
2121
"github.com/spf13/cobra"
2222
)
2323

24-
func docsCmd() *cobra.Command {
24+
func newDocsCmd() *cobra.Command {
2525
return &cobra.Command{
2626
Use: "docs",
2727
Short: "Generate API reference docs. Coming soon.",

cmd/kubebuilder/init_project.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"sigs.k8s.io/controller-tools/pkg/scaffold/project"
3636
)
3737

38-
func addInit(cmd *cobra.Command) {
38+
func newInitProjectCmd() *cobra.Command {
3939
o := projectOptions{}
4040

4141
initCmd := &cobra.Command{
@@ -58,7 +58,7 @@ project will prompt the user to run 'dep ensure' after writing the project files
5858
kubebuilder init --domain example.org --license apache2 --owner "The Kubernetes authors"
5959
`,
6060
Run: func(cmd *cobra.Command, args []string) {
61-
o.RunInit()
61+
o.runInit()
6262
},
6363
}
6464

@@ -72,7 +72,7 @@ kubebuilder init --domain example.org --license apache2 --owner "The Kubernetes
7272
o.mgr = &manager.Cmd{}
7373
o.dkr = &manager.Dockerfile{}
7474

75-
cmd.AddCommand(initCmd)
75+
return initCmd
7676
}
7777

7878
type projectOptions struct {
@@ -85,7 +85,7 @@ type projectOptions struct {
8585
depFlag *flag.Flag
8686
}
8787

88-
func (o *projectOptions) RunInit() {
88+
func (o *projectOptions) runInit() {
8989
checkGoVersion()
9090

9191
if !depExists() {

cmd/kubebuilder/main.go

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"strings"
2525

2626
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
27-
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/v1"
2827
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version"
2928
"github.com/spf13/cobra"
3029
)
@@ -48,26 +47,72 @@ func main() {
4847
}
4948
util.Repo = strings.Replace(wd, util.GoSrc+string(filepath.Separator), "", 1)
5049

51-
// add init command
52-
addInit(cmd)
50+
rootCmd := defaultCommand()
5351

54-
// add version command
55-
version.AddVersion(cmd)
52+
rootCmd.AddCommand(
53+
newInitProjectCmd(),
54+
newAPICommand(),
55+
version.NewVersionCmd(),
56+
newDocsCmd(),
57+
newVendorUpdateCmd(),
58+
)
5659

57-
// add all commands corresponding to v1 project
58-
v1.AddCmds(cmd)
59-
60-
if err := cmd.Execute(); err != nil {
60+
if err := rootCmd.Execute(); err != nil {
6161
log.Fatal(err)
6262
}
6363
}
6464

65-
var cmd = &cobra.Command{
66-
Use: "kubebuilder",
67-
Short: "Development kit for building Kubernetes extensions and tools.",
68-
Run: RunMain,
69-
}
65+
func defaultCommand() *cobra.Command {
66+
return &cobra.Command{
67+
Use: "kubebuilder",
68+
Short: "Development kit for building Kubernetes extensions and tools.",
69+
Long: `
70+
Development kit for building Kubernetes extensions and tools.
71+
72+
Provides libraries and tools to create new projects, APIs and controllers.
73+
Includes tools for packaging artifacts into an installer container.
74+
75+
Typical project lifecycle:
76+
77+
- initialize a project:
78+
79+
kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes authors
80+
81+
- create one or more a new resource APIs and add your code to them:
82+
83+
kubebuilder create api --group <group> --version <version> --kind <Kind>
84+
85+
create resource will prompt the user for if it should scaffold the Resource and / or Controller. To only
86+
scaffold a Controller for an existing Resource, select "n" for Resource. To only define
87+
the schema for a Resource without writing a Controller, select "n" for Controller.
7088
71-
func RunMain(cmd *cobra.Command, args []string) {
72-
cmd.Help()
89+
After the scaffold is written, api will run make on the project.
90+
`,
91+
Example: `
92+
# Initialize your project
93+
kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors"
94+
95+
# Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate
96+
kubebuilder create api --group ship --version v1beta1 --kind Frigate
97+
98+
# Edit the API Scheme
99+
nano pkg/apis/ship/v1beta1/frigate_types.go
100+
101+
# Edit the Controller
102+
nano pkg/controller/frigate/frigate_controller.go
103+
104+
# Edit the Controller Test
105+
nano pkg/controller/frigate/frigate_controller_test.go
106+
107+
# Install CRDs into the Kubernetes cluster using kubectl apply
108+
make install
109+
110+
# Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config
111+
make run
112+
`,
113+
114+
Run: func(cmd *cobra.Command, args []string) {
115+
cmd.Help()
116+
},
117+
}
73118
}

cmd/kubebuilder/v1/commands.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

cmd/kubebuilder/v1/vendor_update.go renamed to cmd/kubebuilder/vendor_update.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
package v1
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
218

319
import (
420
"log"
@@ -9,7 +25,7 @@ import (
925
"sigs.k8s.io/controller-tools/pkg/scaffold/project"
1026
)
1127

12-
func vendorUpdateCmd() *cobra.Command {
28+
func newVendorUpdateCmd() *cobra.Command {
1329
return &cobra.Command{
1430
Use: "update",
1531
Short: "updates vendor dependencies.",

cmd/kubebuilder/version/version.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ func (v Version) Print() {
5656
fmt.Printf("Version: %#v\n", v)
5757
}
5858

59-
var versionCmd = &cobra.Command{
60-
Use: "version",
61-
Short: "Print the kubebuilder version.",
62-
Long: `Print the kubebuilder version.`,
63-
Example: `kubebuilder version`,
64-
Run: RunVersion,
65-
}
66-
67-
func AddVersion(cmd *cobra.Command) {
68-
cmd.AddCommand(versionCmd)
59+
func NewVersionCmd() *cobra.Command {
60+
return &cobra.Command{
61+
Use: "version",
62+
Short: "Print the kubebuilder version.",
63+
Long: `Print the kubebuilder version.`,
64+
Example: `kubebuilder version`,
65+
Run: runVersion,
66+
}
6967
}
7068

71-
func RunVersion(cmd *cobra.Command, args []string) {
69+
func runVersion(cmd *cobra.Command, args []string) {
7270
GetVersion().Print()
7371
}

0 commit comments

Comments
 (0)