Skip to content

Commit

Permalink
moved v1 cmds to main pkg and restructured wiring of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
droot committed Sep 28, 2018
1 parent 9e5af44 commit 8915bdd
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 111 deletions.
10 changes: 5 additions & 5 deletions cmd/kubebuilder/v1/api.go → cmd/kubebuilder/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package main

import (
"bufio"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -124,7 +124,7 @@ func (o *apiOptions) RunAddAPI() {
}
}

func AddAPICommand(cmd *cobra.Command) {
func newAPICommand() *cobra.Command {
o := apiOptions{}

apiCmd := &cobra.Command{
Expand Down Expand Up @@ -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()
},
}

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubebuilder/v1/docs.go → cmd/kubebuilder/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
8 changes: 4 additions & 4 deletions cmd/kubebuilder/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()
},
}

Expand All @@ -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 {
Expand All @@ -85,7 +85,7 @@ type projectOptions struct {
depFlag *flag.Flag
}

func (o *projectOptions) RunInit() {
func (o *projectOptions) runInit() {
checkGoVersion()

if !depExists() {
Expand Down
77 changes: 61 additions & 16 deletions cmd/kubebuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 <group> --version <version> --kind <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()
},
}
}
71 changes: 0 additions & 71 deletions cmd/kubebuilder/v1/commands.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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.",
Expand Down
20 changes: 9 additions & 11 deletions cmd/kubebuilder/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 8915bdd

Please sign in to comment.