Skip to content

Commit 7107816

Browse files
authored
Merge pull request #290 from Liujingfang1/master
add scaffold from controller-tools with backward compatibility
2 parents 5db3f0d + c360931 commit 7107816

File tree

140 files changed

+6460
-5259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+6460
-5259
lines changed

cmd/Gopkg.lock

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@
4343
[[constraint]]
4444
version = "kubernetes-1.10.1"
4545
name = "k8s.io/client-go"
46+
47+
[[constraint]]
48+
branch = "master"
49+
name = "sigs.k8s.io/controller-tools"

cmd/kubebuilder/initproject/apis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
// createAPIs creates a new package under pkg/apis
25-
func createAPIs(boilerplate string) {
25+
func createAPIs(boilerplate, domain string) {
2626
fmt.Printf("\t%s/\n", filepath.Join("pkg", "apis"))
2727
execute(
2828
filepath.Join("pkg", "apis", "doc.go"),

cmd/kubebuilder/initproject/init.go

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,56 @@ import (
2727

2828
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2929
"github.com/spf13/cobra"
30+
"sigs.k8s.io/controller-tools/pkg/scaffold/manager"
31+
"sigs.k8s.io/controller-tools/pkg/scaffold/project"
3032
)
3133

32-
var repoCmd = &cobra.Command{
33-
Use: "init",
34-
Short: "Initialize a new project",
35-
Long: `Initialize a new project including vendor/ directory and go package directories.`,
36-
Example: `# Initialize project structure
34+
35+
type initOptions struct {
36+
domain string
37+
copyright string
38+
bazel bool
39+
controllerOnly bool
40+
projectVersion string
41+
projectOptions
42+
}
43+
44+
func AddInit(cmd *cobra.Command) {
45+
o := initOptions{}
46+
47+
initCmd := &cobra.Command{
48+
Use: "init",
49+
Short: "Initialize a new project",
50+
Long: `Initialize a new project including vendor/ directory and Go package directories.`,
51+
Example: `# Initialize project structure
3752
kubebuilder init repo --domain mydomain
3853
`,
39-
Run: runInitRepo,
40-
}
54+
Run: func(cmd *cobra.Command, args []string) {
55+
o.runInitRepo()
56+
},
57+
}
4158

42-
var domain string
43-
var copyright string
44-
var bazel bool
45-
var controllerOnly bool
59+
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")
4664

47-
func AddInit(cmd *cobra.Command) {
48-
cmd.AddCommand(repoCmd)
49-
repoCmd.Flags().StringVar(&domain, "domain", "", "domain for the API groups")
50-
repoCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
51-
repoCmd.Flags().BoolVar(&bazel, "bazel", false, "if true, setup Bazel workspace artifacts")
52-
repoCmd.Flags().BoolVar(&controllerOnly, "controller-only", false, "if true, setup controller only")
65+
66+
initCmd.Flags().BoolVar(
67+
&o.dep, "dep", true, v1comment + "if specified, determines whether dep will be used.")
68+
o.depFlag = initCmd.Flag("dep")
69+
70+
o.prj = projectForFlags(initCmd.Flags())
71+
o.bp = boilerplateForFlags(initCmd.Flags())
72+
o.gopkg = &project.GopkgToml{}
73+
o.mgr = &manager.Cmd{}
74+
o.dkr = &manager.Dockerfile{}
75+
76+
cmd.AddCommand(initCmd)
5377
}
5478

55-
func runInitRepo(cmd *cobra.Command, args []string) {
79+
func (o *initOptions) runInitRepo() {
5680
version := runtime.Version()
5781
if versionCmp(version, "go1.10") < 0 {
5882
log.Fatalf("The go version is %v, must be 1.10+", version)
@@ -61,18 +85,26 @@ func runInitRepo(cmd *cobra.Command, args []string) {
6185
log.Fatalf("Dep is not installed. Follow steps at: https://golang.github.io/dep/docs/installation.html")
6286
}
6387

64-
if len(domain) == 0 {
88+
if o.projectVersion == "v1" {
89+
if len(o.domain) != 0 {
90+
o.prj.Domain = o.domain
91+
}
92+
o.RunInit()
93+
return
94+
}
95+
96+
if len(o.domain) == 0 {
6597
log.Fatal("Must specify --domain")
6698
}
67-
cr := util.GetCopyright(copyright)
99+
cr := util.GetCopyright(o.copyright)
68100

69101
fmt.Printf("Initializing project structure...\n")
70-
if bazel {
102+
if o.bazel {
71103
createBazelWorkspace()
72104
}
73105
createControllerManager(cr)
74106
//createInstaller(cr)
75-
createAPIs(cr)
107+
createAPIs(cr, o.domain)
76108
//runCreateApiserver(cr)
77109

78110
pkgs := []string{
@@ -89,8 +121,8 @@ func runInitRepo(cmd *cobra.Command, args []string) {
89121
}
90122
doDockerfile()
91123
doInject(cr)
92-
doArgs(cr, controllerOnly)
93-
RunVendorInstall(nil, nil)
124+
doArgs(cr, o.controllerOnly)
125+
RunVendorInstall(nil, []string{o.copyright})
94126
createBoilerplate()
95127
fmt.Printf("Next: Define a resource with:\n" +
96128
"$ kubebuilder create resource\n")
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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 initproject
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"os"
23+
"os/exec"
24+
"strings"
25+
26+
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
27+
"github.com/spf13/cobra"
28+
flag "github.com/spf13/pflag"
29+
"sigs.k8s.io/controller-tools/pkg/scaffold"
30+
"sigs.k8s.io/controller-tools/pkg/scaffold/input"
31+
"sigs.k8s.io/controller-tools/pkg/scaffold/manager"
32+
"sigs.k8s.io/controller-tools/pkg/scaffold/project"
33+
)
34+
35+
type projectOptions struct {
36+
prj *project.Project
37+
bp *project.Boilerplate
38+
gopkg *project.GopkgToml
39+
mgr *manager.Cmd
40+
dkr *manager.Dockerfile
41+
dep bool
42+
depFlag *flag.Flag
43+
}
44+
45+
var v1comment = "Works only with --project-version v1. "
46+
47+
func (o *projectOptions) RunInit() {
48+
if util.ProjectExist() {
49+
fmt.Println("Failed to initialize project bacause project is already initialized")
50+
return
51+
}
52+
// project and boilerplate must come before main so the boilerplate exists
53+
s := &scaffold.Scaffold{
54+
BoilerplateOptional: true,
55+
ProjectOptional: true,
56+
}
57+
58+
p, _ := o.prj.GetInput()
59+
b, _ := o.bp.GetInput()
60+
err := s.Execute(input.Options{ProjectPath: p.Path, BoilerplatePath: b.Path}, o.prj, o.bp)
61+
if err != nil {
62+
log.Fatal(err)
63+
}
64+
65+
s = &scaffold.Scaffold{}
66+
err = s.Execute(input.Options{ProjectPath: p.Path, BoilerplatePath: b.Path},
67+
o.gopkg, o.mgr, &project.Makefile{}, o.dkr, &manager.APIs{}, &manager.Controller{}, &manager.Config{},
68+
&project.GitIgnore{})
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
73+
if !o.depFlag.Changed {
74+
fmt.Println("Run `dep ensure` to fetch dependencies (Recommended) [y/n]?")
75+
o.dep = util.Yesno()
76+
}
77+
if o.dep {
78+
c := exec.Command("dep", "ensure") // #nosec
79+
c.Stderr = os.Stderr
80+
c.Stdout = os.Stdout
81+
fmt.Println(strings.Join(c.Args, " "))
82+
if err := c.Run(); err != nil {
83+
log.Fatal(err)
84+
}
85+
86+
fmt.Println("Running make...")
87+
c = exec.Command("make") // #nosec
88+
c.Stderr = os.Stderr
89+
c.Stdout = os.Stdout
90+
fmt.Println(strings.Join(c.Args, " "))
91+
if err := c.Run(); err != nil {
92+
log.Fatal(err)
93+
}
94+
} else {
95+
fmt.Println("Skipping `dep ensure`. Dependencies will not be fetched.")
96+
}
97+
fmt.Printf("Next: Define a resource with:\n" +
98+
"$ kubebuilder create api\n")
99+
}
100+
101+
func AddProjectCommand(cmd *cobra.Command) {
102+
o := projectOptions{}
103+
104+
projectCmd := &cobra.Command{
105+
Use: "init",
106+
Short: "Scaffold a new project.",
107+
Long: `Scaffold a project.
108+
109+
Writes the following files:
110+
- a boilerplate license file
111+
- a PROJECT file with the domain and repo
112+
- a Makefile to build the project
113+
- a Gopkg.toml with project dependencies
114+
- a cmd/manager/main.go to run
115+
116+
project will prompt the user to run 'dep ensure' after writing the project files.
117+
`,
118+
Example: `# Scaffold a project using the apache2 license with "The Kubernetes authors" as owners
119+
kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes authors"
120+
`,
121+
Run: func(cmd *cobra.Command, args []string) {
122+
o.RunInit()
123+
},
124+
}
125+
126+
projectCmd.Flags().BoolVar(
127+
&o.dep, "dep", true, "if specified, determines whether dep will be used.")
128+
o.depFlag = projectCmd.Flag("dep")
129+
130+
o.prj = projectForFlags(projectCmd.Flags())
131+
o.bp = boilerplateForFlags(projectCmd.Flags())
132+
o.gopkg = &project.GopkgToml{}
133+
o.mgr = &manager.Cmd{}
134+
o.dkr = &manager.Dockerfile{}
135+
136+
cmd.AddCommand(projectCmd)
137+
}
138+
139+
// projectForFlags registers flags for Project fields and returns the Project
140+
func projectForFlags(f *flag.FlagSet) *project.Project {
141+
p := &project.Project{}
142+
f.StringVar(&p.Repo, "repo", "", v1comment + "name of the github repo. "+
143+
"defaults to the go package of the current working directory.")
144+
p.Version = "2"
145+
p.Domain = "k8s.io"
146+
return p
147+
}
148+
149+
// boilerplateForFlags registers flags for Boilerplate fields and returns the Boilerplate
150+
func boilerplateForFlags(f *flag.FlagSet) *project.Boilerplate {
151+
b := &project.Boilerplate{}
152+
f.StringVar(&b.Path, "path", "", v1comment + "path for boilerplate")
153+
f.StringVar(&b.License, "license", "apache2",
154+
v1comment + "license to use to boilerplate. Maybe one of apache2,none")
155+
f.StringVar(&b.Owner, "owner", "",
156+
v1comment + "Owner to add to the copyright")
157+
return b
158+
}

cmd/kubebuilder/initproject/vendor.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ var builderCommit string
5252
var Update bool
5353

5454
func RunVendorInstall(cmd *cobra.Command, args []string) {
55-
cr := util.GetCopyright(copyright)
56-
doImports(cr)
55+
if len(args) > 0 {
56+
cr := util.GetCopyright(args[0])
57+
doImports(cr)
58+
}
5759
if !depExists() {
5860
log.Fatalf("Dep is not installed. Follow steps at: https://golang.github.io/dep/docs/installation.html")
5961
}

0 commit comments

Comments
 (0)