Skip to content

Commit

Permalink
Merge pull request #290 from Liujingfang1/master
Browse files Browse the repository at this point in the history
add scaffold from controller-tools with backward compatibility
  • Loading branch information
k8s-ci-robot committed Jun 28, 2018
2 parents 5db3f0d + c360931 commit 7107816
Show file tree
Hide file tree
Showing 140 changed files with 6,460 additions and 5,259 deletions.
20 changes: 14 additions & 6 deletions cmd/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@
[[constraint]]
version = "kubernetes-1.10.1"
name = "k8s.io/client-go"

[[constraint]]
branch = "master"
name = "sigs.k8s.io/controller-tools"
2 changes: 1 addition & 1 deletion cmd/kubebuilder/initproject/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// createAPIs creates a new package under pkg/apis
func createAPIs(boilerplate string) {
func createAPIs(boilerplate, domain string) {
fmt.Printf("\t%s/\n", filepath.Join("pkg", "apis"))
execute(
filepath.Join("pkg", "apis", "doc.go"),
Expand Down
80 changes: 56 additions & 24 deletions cmd/kubebuilder/initproject/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,56 @@ import (

"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-tools/pkg/scaffold/manager"
"sigs.k8s.io/controller-tools/pkg/scaffold/project"
)

var repoCmd = &cobra.Command{
Use: "init",
Short: "Initialize a new project",
Long: `Initialize a new project including vendor/ directory and go package directories.`,
Example: `# Initialize project structure

type initOptions struct {
domain string
copyright string
bazel bool
controllerOnly bool
projectVersion string
projectOptions
}

func AddInit(cmd *cobra.Command) {
o := initOptions{}

initCmd := &cobra.Command{
Use: "init",
Short: "Initialize a new project",
Long: `Initialize a new project including vendor/ directory and Go package directories.`,
Example: `# Initialize project structure
kubebuilder init repo --domain mydomain
`,
Run: runInitRepo,
}
Run: func(cmd *cobra.Command, args []string) {
o.runInitRepo()
},
}

var domain string
var copyright string
var bazel bool
var controllerOnly bool
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")

func AddInit(cmd *cobra.Command) {
cmd.AddCommand(repoCmd)
repoCmd.Flags().StringVar(&domain, "domain", "", "domain for the API groups")
repoCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
repoCmd.Flags().BoolVar(&bazel, "bazel", false, "if true, setup Bazel workspace artifacts")
repoCmd.Flags().BoolVar(&controllerOnly, "controller-only", false, "if true, setup controller only")

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

o.prj = projectForFlags(initCmd.Flags())
o.bp = boilerplateForFlags(initCmd.Flags())
o.gopkg = &project.GopkgToml{}
o.mgr = &manager.Cmd{}
o.dkr = &manager.Dockerfile{}

cmd.AddCommand(initCmd)
}

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

if len(domain) == 0 {
if o.projectVersion == "v1" {
if len(o.domain) != 0 {
o.prj.Domain = o.domain
}
o.RunInit()
return
}

if len(o.domain) == 0 {
log.Fatal("Must specify --domain")
}
cr := util.GetCopyright(copyright)
cr := util.GetCopyright(o.copyright)

fmt.Printf("Initializing project structure...\n")
if bazel {
if o.bazel {
createBazelWorkspace()
}
createControllerManager(cr)
//createInstaller(cr)
createAPIs(cr)
createAPIs(cr, o.domain)
//runCreateApiserver(cr)

pkgs := []string{
Expand All @@ -89,8 +121,8 @@ func runInitRepo(cmd *cobra.Command, args []string) {
}
doDockerfile()
doInject(cr)
doArgs(cr, controllerOnly)
RunVendorInstall(nil, nil)
doArgs(cr, o.controllerOnly)
RunVendorInstall(nil, []string{o.copyright})
createBoilerplate()
fmt.Printf("Next: Define a resource with:\n" +
"$ kubebuilder create resource\n")
Expand Down
158 changes: 158 additions & 0 deletions cmd/kubebuilder/initproject/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
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 initproject

import (
"fmt"
"log"
"os"
"os/exec"
"strings"

"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"sigs.k8s.io/controller-tools/pkg/scaffold"
"sigs.k8s.io/controller-tools/pkg/scaffold/input"
"sigs.k8s.io/controller-tools/pkg/scaffold/manager"
"sigs.k8s.io/controller-tools/pkg/scaffold/project"
)

type projectOptions struct {
prj *project.Project
bp *project.Boilerplate
gopkg *project.GopkgToml
mgr *manager.Cmd
dkr *manager.Dockerfile
dep bool
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")
return
}
// project and boilerplate must come before main so the boilerplate exists
s := &scaffold.Scaffold{
BoilerplateOptional: true,
ProjectOptional: true,
}

p, _ := o.prj.GetInput()
b, _ := o.bp.GetInput()
err := s.Execute(input.Options{ProjectPath: p.Path, BoilerplatePath: b.Path}, o.prj, o.bp)
if err != nil {
log.Fatal(err)
}

s = &scaffold.Scaffold{}
err = s.Execute(input.Options{ProjectPath: p.Path, BoilerplatePath: b.Path},
o.gopkg, o.mgr, &project.Makefile{}, o.dkr, &manager.APIs{}, &manager.Controller{}, &manager.Config{},
&project.GitIgnore{})
if err != nil {
log.Fatal(err)
}

if !o.depFlag.Changed {
fmt.Println("Run `dep ensure` to fetch dependencies (Recommended) [y/n]?")
o.dep = util.Yesno()
}
if o.dep {
c := exec.Command("dep", "ensure") // #nosec
c.Stderr = os.Stderr
c.Stdout = os.Stdout
fmt.Println(strings.Join(c.Args, " "))
if err := c.Run(); err != nil {
log.Fatal(err)
}

fmt.Println("Running make...")
c = exec.Command("make") // #nosec
c.Stderr = os.Stderr
c.Stdout = os.Stdout
fmt.Println(strings.Join(c.Args, " "))
if err := c.Run(); err != nil {
log.Fatal(err)
}
} else {
fmt.Println("Skipping `dep ensure`. Dependencies will not be fetched.")
}
fmt.Printf("Next: Define a resource with:\n" +
"$ kubebuilder create api\n")
}

func AddProjectCommand(cmd *cobra.Command) {
o := projectOptions{}

projectCmd := &cobra.Command{
Use: "init",
Short: "Scaffold a new project.",
Long: `Scaffold a project.
Writes the following files:
- a boilerplate license file
- a PROJECT file with the domain and repo
- a Makefile to build the project
- a Gopkg.toml with project dependencies
- a cmd/manager/main.go to run
project will prompt the user to run 'dep ensure' after writing the project files.
`,
Example: `# Scaffold a project using the apache2 license with "The Kubernetes authors" as owners
kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes authors"
`,
Run: func(cmd *cobra.Command, args []string) {
o.RunInit()
},
}

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

o.prj = projectForFlags(projectCmd.Flags())
o.bp = boilerplateForFlags(projectCmd.Flags())
o.gopkg = &project.GopkgToml{}
o.mgr = &manager.Cmd{}
o.dkr = &manager.Dockerfile{}

cmd.AddCommand(projectCmd)
}

// 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. "+
"defaults to the go package of the current working directory.")
p.Version = "2"
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")
return b
}
6 changes: 4 additions & 2 deletions cmd/kubebuilder/initproject/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ var builderCommit string
var Update bool

func RunVendorInstall(cmd *cobra.Command, args []string) {
cr := util.GetCopyright(copyright)
doImports(cr)
if len(args) > 0 {
cr := util.GetCopyright(args[0])
doImports(cr)
}
if !depExists() {
log.Fatalf("Dep is not installed. Follow steps at: https://golang.github.io/dep/docs/installation.html")
}
Expand Down
Loading

0 comments on commit 7107816

Please sign in to comment.