Skip to content

Commit 3859f88

Browse files
authored
Merge pull request #317 from Liujingfang1/master
Set default to v1 commands
2 parents f0b1428 + 31a7658 commit 3859f88

File tree

8 files changed

+67
-21
lines changed

8 files changed

+67
-21
lines changed

cmd/kubebuilder/initproject/init.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ kubebuilder init repo --domain mydomain
5656
},
5757
}
5858

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

6566

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

7071
o.prj = projectForFlags(initCmd.Flags())

cmd/kubebuilder/initproject/project.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ type projectOptions struct {
4343
depFlag *flag.Flag
4444
}
4545

46-
var v1comment = "Works only with --project-version v1. "
47-
4846
func (o *projectOptions) RunInit() {
4947
if util.ProjectExist() {
5048
fmt.Println("Failed to initialize project bacause project is already initialized")
@@ -154,20 +152,18 @@ kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes autho
154152
// projectForFlags registers flags for Project fields and returns the Project
155153
func projectForFlags(f *flag.FlagSet) *project.Project {
156154
p := &project.Project{}
157-
f.StringVar(&p.Repo, "repo", "", v1comment+"name of the github repo. "+
155+
f.StringVar(&p.Repo, "repo", "","name of the github repo. "+
158156
"defaults to the go package of the current working directory.")
159-
p.Version = "2"
157+
p.Version = "1"
160158
p.Domain = "k8s.io"
161159
return p
162160
}
163161

164162
// boilerplateForFlags registers flags for Boilerplate fields and returns the Boilerplate
165163
func boilerplateForFlags(f *flag.FlagSet) *project.Boilerplate {
166164
b := &project.Boilerplate{}
167-
f.StringVar(&b.Path, "path", "", v1comment+"path for boilerplate")
168-
f.StringVar(&b.License, "license", "apache2",
169-
v1comment+"license to use to boilerplate. Maybe one of apache2,none")
170-
f.StringVar(&b.Owner, "owner", "",
171-
v1comment+"Owner to add to the copyright")
165+
f.StringVar(&b.Path, "path", "", "path for boilerplate")
166+
f.StringVar(&b.License, "license", "apache2","license to use to boilerplate. Maybe one of apache2,none")
167+
f.StringVar(&b.Owner, "owner", "","Owner to add to the copyright")
172168
return b
173169
}

cmd/kubebuilder/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
initproject.AddInit(cmd)
5353
version.AddVersion(cmd)
5454

55-
if util.IsNewVersion() {
55+
if util.IsNewVersion() || util.IsProjectNotInitialized() {
5656
v1.AddCmds(cmd)
5757
} else {
5858
v0.AddCmds(cmd)

cmd/kubebuilder/util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,18 @@ func IsNewVersion() bool {
180180
func ProjectExist() bool {
181181
return IsNewVersion()
182182
}
183+
184+
func IsProjectNotInitialized() bool {
185+
dirs := []string{
186+
"cmd",
187+
"hack",
188+
"pkg",
189+
"vendor",
190+
}
191+
for _, dir := range dirs {
192+
if _, err := os.Stat(dir); err == nil {
193+
return false
194+
}
195+
}
196+
return true
197+
}

cmd/kubebuilder/v1/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
func AddCmds(cmd *cobra.Command) {
2424
AddAPICommand(cmd)
2525
cmd.AddCommand(vendorUpdateCmd())
26+
cmd.AddCommand(docsCmd())
2627

2728
cmd.Example = `# Initialize your project
2829
kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors"

cmd/kubebuilder/v1/docs.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2018 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 v1
18+
19+
import (
20+
"fmt"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
func docsCmd() *cobra.Command {
25+
return &cobra.Command{
26+
Use: "docs",
27+
Short: "Generate API reference docs. Coming soon.",
28+
Long: `updates vendor dependencies. Coming soon.`,
29+
Run: func(cmd *cobra.Command, args []string) {
30+
fmt.Println("Coming soon.")
31+
},
32+
}
33+
}

testv0.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function generate_crd_resources {
2323
header_text "generating CRD resources and code"
2424

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

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

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

396396
# Update crd
@@ -489,7 +489,7 @@ function generate_coretype_controller {
489489
header_text "generating controller for coretype Deployment"
490490

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

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

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

testv1.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ source common.sh
2121

2222
function test_init_project {
2323
header_text "performing init project"
24-
kubebuilder init --project-version=v1 --domain example.com <<< "y"
24+
kubebuilder init --domain example.com <<< "y"
2525
make
2626
cache_dep
2727
}
@@ -30,7 +30,7 @@ function test_init_project {
3030

3131
function test_init_project_manual_dep_ensure {
3232
header_text "performing init project w/o dep ensure"
33-
kubebuilder init --project-version=v1 --domain example.com <<< "n"
33+
kubebuilder init --domain example.com <<< "n"
3434
dep ensure
3535
make
3636
}

0 commit comments

Comments
 (0)