Skip to content

Commit

Permalink
Merge pull request #317 from Liujingfang1/master
Browse files Browse the repository at this point in the history
Set default to v1 commands
  • Loading branch information
k8s-ci-robot committed Jul 19, 2018
2 parents f0b1428 + 31a7658 commit 3859f88
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
11 changes: 6 additions & 5 deletions cmd/kubebuilder/initproject/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ kubebuilder init repo --domain mydomain
},
}

v0comment := "Works only with project-version v0, "
initCmd.Flags().StringVar(&o.domain, "domain", "", "domain for the API groups")
initCmd.Flags().StringVar(&o.copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
initCmd.Flags().BoolVar(&o.bazel, "bazel", false, "if true, setup Bazel workspace artifacts")
initCmd.Flags().BoolVar(&o.controllerOnly, "controller-only", false, "if true, setup controller only")
initCmd.Flags().StringVar(&o.projectVersion, "project-version", "v0", "if set to v1, init project with kubebuilder 1.0")
initCmd.Flags().StringVar(&o.copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), v0comment + "Location of copyright boilerplate file.")
initCmd.Flags().BoolVar(&o.bazel, "bazel", false, v0comment + "if true, setup Bazel workspace artifacts")
initCmd.Flags().BoolVar(&o.controllerOnly, "controller-only", false, v0comment + "if true, setup controller only")
initCmd.Flags().StringVar(&o.projectVersion, "project-version", "v1", "if set to v0, init project with kubebuilder legacy version")


initCmd.Flags().BoolVar(
&o.dep, "dep", true, v1comment + "if specified, determines whether dep will be used.")
&o.dep, "dep", true,"if specified, determines whether dep will be used.")
o.depFlag = initCmd.Flag("dep")

o.prj = projectForFlags(initCmd.Flags())
Expand Down
14 changes: 5 additions & 9 deletions cmd/kubebuilder/initproject/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ type projectOptions struct {
depFlag *flag.Flag
}

var v1comment = "Works only with --project-version v1. "

func (o *projectOptions) RunInit() {
if util.ProjectExist() {
fmt.Println("Failed to initialize project bacause project is already initialized")
Expand Down Expand Up @@ -154,20 +152,18 @@ kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes autho
// projectForFlags registers flags for Project fields and returns the Project
func projectForFlags(f *flag.FlagSet) *project.Project {
p := &project.Project{}
f.StringVar(&p.Repo, "repo", "", v1comment+"name of the github repo. "+
f.StringVar(&p.Repo, "repo", "","name of the github repo. "+
"defaults to the go package of the current working directory.")
p.Version = "2"
p.Version = "1"
p.Domain = "k8s.io"
return p
}

// boilerplateForFlags registers flags for Boilerplate fields and returns the Boilerplate
func boilerplateForFlags(f *flag.FlagSet) *project.Boilerplate {
b := &project.Boilerplate{}
f.StringVar(&b.Path, "path", "", v1comment+"path for boilerplate")
f.StringVar(&b.License, "license", "apache2",
v1comment+"license to use to boilerplate. Maybe one of apache2,none")
f.StringVar(&b.Owner, "owner", "",
v1comment+"Owner to add to the copyright")
f.StringVar(&b.Path, "path", "", "path for boilerplate")
f.StringVar(&b.License, "license", "apache2","license to use to boilerplate. Maybe one of apache2,none")
f.StringVar(&b.Owner, "owner", "","Owner to add to the copyright")
return b
}
2 changes: 1 addition & 1 deletion cmd/kubebuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
initproject.AddInit(cmd)
version.AddVersion(cmd)

if util.IsNewVersion() {
if util.IsNewVersion() || util.IsProjectNotInitialized() {
v1.AddCmds(cmd)
} else {
v0.AddCmds(cmd)
Expand Down
15 changes: 15 additions & 0 deletions cmd/kubebuilder/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,18 @@ func IsNewVersion() bool {
func ProjectExist() bool {
return IsNewVersion()
}

func IsProjectNotInitialized() bool {
dirs := []string{
"cmd",
"hack",
"pkg",
"vendor",
}
for _, dir := range dirs {
if _, err := os.Stat(dir); err == nil {
return false
}
}
return true
}
1 change: 1 addition & 0 deletions cmd/kubebuilder/v1/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
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"
Expand Down
33 changes: 33 additions & 0 deletions cmd/kubebuilder/v1/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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 (
"fmt"
"github.com/spf13/cobra"
)

func docsCmd() *cobra.Command {
return &cobra.Command{
Use: "docs",
Short: "Generate API reference docs. Coming soon.",
Long: `updates vendor dependencies. Coming soon.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Coming soon.")
},
}
}
8 changes: 4 additions & 4 deletions testv0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function generate_crd_resources {
header_text "generating CRD resources and code"

# Run the commands
kubebuilder init repo --domain sample.kubernetes.io
kubebuilder init repo --domain sample.kubernetes.io --project-version v0
kubebuilder create resource --group insect --version v1beta1 --kind Bee

header_text "editing generated files to simulate a user"
Expand Down Expand Up @@ -390,7 +390,7 @@ function test_crd_validation {
export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd

kubebuilder init repo --domain sample.kubernetes.io
kubebuilder init repo --domain sample.kubernetes.io --project-version v0
kubebuilder create resource --group got --version v1beta1 --kind House

# Update crd
Expand Down Expand Up @@ -489,7 +489,7 @@ function generate_coretype_controller {
header_text "generating controller for coretype Deployment"

# Run the commands
kubebuilder init repo --domain sample.kubernetes.io --controller-only
kubebuilder init repo --domain sample.kubernetes.io --controller-only --project-version v0
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type

# Fill the required fileds of Deployment object so that the Deployment instance can be successfully created
Expand All @@ -500,7 +500,7 @@ function generate_resource_with_coretype_controller {
header_text "generating CRD resource as well as controller for coretype Deployment"

# Run the commands
kubebuilder init repo --domain sample.kubernetes.io
kubebuilder init repo --domain sample.kubernetes.io --project-version v0
kubebuilder create resource --group ant --version v1beta1 --kind Ant
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type

Expand Down
4 changes: 2 additions & 2 deletions testv1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ source common.sh

function test_init_project {
header_text "performing init project"
kubebuilder init --project-version=v1 --domain example.com <<< "y"
kubebuilder init --domain example.com <<< "y"
make
cache_dep
}
Expand All @@ -30,7 +30,7 @@ function test_init_project {

function test_init_project_manual_dep_ensure {
header_text "performing init project w/o dep ensure"
kubebuilder init --project-version=v1 --domain example.com <<< "n"
kubebuilder init --domain example.com <<< "n"
dep ensure
make
}
Expand Down

0 comments on commit 3859f88

Please sign in to comment.