Skip to content

Commit

Permalink
Merge pull request #552 from yue9944882/rebase-apiserver-runtime-pt2
Browse files Browse the repository at this point in the history
Rebasing apiserver-runtime pt2
  • Loading branch information
k8s-ci-robot committed Oct 22, 2020
2 parents fe1e2a0 + 3ffb1cc commit 82a5320
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 114 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ jobs:
GOPATH: ${{ github.workspace }}/go
run: make install
- name: Testing on new project
working-directory: ${{ github.workspace }}/go/src/sigs.k8s.io/apiserver-builder-alpha/test
env:
GOPATH: ${{ github.workspace }}/go
GOBIN: ${{ github.workspace }}/go/bin
run: |
mkdir -p test
cp ${{ github.workspace }}/go/src/sigs.k8s.io/apiserver-builder-alpha/Makefile.test test/
cd test
touch boilerplate.go.txt
export PATH=${PATH}:${GOBIN}
make test -f Makefile.test
basic-example-build:
Expand Down Expand Up @@ -105,7 +108,7 @@ jobs:
controller-gen +object +paths=./pkg/apis/...
export IMAGE="example.io/basic"
apiserver-boot build container --image ${IMAGE} --generate=false
apiserver-boot build container --image ${IMAGE}
kind load docker-image ${IMAGE} --name chart-testing
kubectl create -f config/apiserver.yaml
Expand Down Expand Up @@ -170,7 +173,7 @@ jobs:
controller-gen +object +paths=./pkg/apis/...
export IMAGE="example.io/kine"
apiserver-boot build container --image ${IMAGE} --generate=false
apiserver-boot build container --image ${IMAGE}
kind load docker-image ${IMAGE} --name chart-testing
kubectl create -f config/apiserver.yaml
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ gazelle:

NAME=apiserver-builder-alpha
VENDOR=kubernetes-sigs
VERSION=$(shell cat VERSION)
COMMIT=$(shell git rev-parse --verify HEAD)
VERSION=$(shell git rev-parse --verify --short HEAD)
DESCRIPTION=apiserver-builder implements libraries and tools to quickly and easily build Kubernetes apiservers to support custom resource types.
MAINTAINER=The Kubernetes Authors
URL=https://github.com/$(VENDOR)/$(NAME)
Expand Down Expand Up @@ -53,7 +52,6 @@ clean:

.PHONY: build
build: clean ## Create release artefacts for darwin:amd64, linux:amd64 and windows:amd64. Requires etcd, glide, hg.
go mod vendor
mkdir -p release/$(VERSION)/src
bazel build --platforms=@io_bazel_rules_go//go/toolchain:$(GOOS)_$(GOARCH) cmd:apiserver-builder
ls -lh bazel-bin/cmd
Expand Down
4 changes: 2 additions & 2 deletions test/Makefile.test → Makefile.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

all: test

NON_INTERACTIVE_FLAG=--skip-resource=false --skip-controller=false --skip-admission-controller=false
NON_INTERACTIVE_FLAG=--skip-resource=false --skip-controller=false --with-status-subresource=true

test: cmds skeleton check
go test ./pkg/...
Expand All @@ -26,7 +26,7 @@ check: build
go vet $$(go list ./... | grep -vE '(clientset|listers|informers)_generated')

skeleton:
apiserver-boot init repo --domain sample.kubernetes.io
apiserver-boot init repo --domain sample.kubernetes.io --module-name=example.io
apiserver-boot create group version resource --group storage --version v1 --kind VolumeClaim $(NON_INTERACTIVE_FLAG)
apiserver-boot create group version resource --group storage --version v1 --kind SnapshotClaim $(NON_INTERACTIVE_FLAG)
apiserver-boot create group version resource --group storage --version v1 --kind Volume --non-namespaced $(NON_INTERACTIVE_FLAG)
Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

1 change: 0 additions & 1 deletion cmd/apiserver-boot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ go_library(
"//cmd/apiserver-boot/boot/create:go_default_library",
"//cmd/apiserver-boot/boot/init_repo:go_default_library",
"//cmd/apiserver-boot/boot/run:go_default_library",
"//cmd/apiserver-boot/boot/util:go_default_library",
"//cmd/apiserver-boot/boot/version:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
"@io_k8s_klog//:go_default_library",
Expand Down
1 change: 0 additions & 1 deletion cmd/apiserver-boot/boot/build/build_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func AddBuildContainer(cmd *cobra.Command) {

func AddBuildContainerFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&Image, "image", "", "name of the image with tag")
cmd.Flags().BoolVar(&GenerateForBuild, "generate", true, "if true, generate code before building")
cmd.Flags().StringArrayVar(&buildTargets, "targets", []string{apiserverTarget, controllerTarget}, "The target binaries to build")
}

Expand Down
5 changes: 0 additions & 5 deletions cmd/apiserver-boot/boot/build/build_executables.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/klog"
)

var GenerateForBuild bool = true
var goos string = "linux"
var goarch string = "amd64"
var outputdir string = "bin"
Expand Down Expand Up @@ -57,9 +56,6 @@ apiserver-boot build executables --bazel --gazelle
# Run Bazel without generating BUILD files
apiserver-boot build executables --bazel
# Run Bazel without generating BUILD files or generated code
apiserver-boot build executables --bazel --generate=false
`,
Run: RunBuildExecutables,
}
Expand All @@ -68,7 +64,6 @@ func AddBuildExecutables(cmd *cobra.Command) {
cmd.AddCommand(createBuildExecutablesCmd)

createBuildExecutablesCmd.Flags().StringVar(&vendorDir, "vendor-dir", "", "Location of directory containing vendor files.")
createBuildExecutablesCmd.Flags().BoolVar(&GenerateForBuild, "generate", true, "if true, generate code before building")
createBuildExecutablesCmd.Flags().StringVar(&goos, "goos", "", "if specified, set this GOOS")
createBuildExecutablesCmd.Flags().StringVar(&goarch, "goarch", "", "if specified, set this GOARCH")
createBuildExecutablesCmd.Flags().StringVar(&outputdir, "output", "bin", "if set, write the binaries to this directory")
Expand Down
5 changes: 1 addition & 4 deletions cmd/apiserver-boot/boot/create/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ var groupTemplate = `
{{.BoilerPlate}}
//go:generate deepcopy-gen -O zz_generated.deepcopy -i . -h ../../../boilerplate.go.txt
//go:generate defaulter-gen -O zz_generated.defaults -i . -h ../../../boilerplate.go.txt
// +k8s:deepcopy-gen=package,register
// +groupName={{.Name}}.{{.Domain}}
Expand All @@ -105,4 +102,4 @@ var installTemplate = `
{{.BoilerPlate}}
package {{.Name}}
`
`
49 changes: 30 additions & 19 deletions cmd/apiserver-boot/boot/create/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var nonNamespacedKind bool
var skipGenerateAdmissionController bool
var skipGenerateResource bool
var skipGenerateController bool
var withStatusSubresource bool

var createResourceCmd = &cobra.Command{
Use: "resource",
Expand All @@ -59,6 +60,8 @@ func AddCreateResource(cmd *cobra.Command) {
createResourceCmd.Flags().BoolVar(&skipGenerateResource, "skip-resource", false, "if set, the resources will not be generated")
createResourceCmd.Flags().BoolVar(&skipGenerateController, "skip-controller", false, "if set, the controller will not be generated")
createResourceCmd.Flags().BoolVar(&skipGenerateAdmissionController, "skip-admission-controller", false, "if set, the admission controller will not be generated")
createResourceCmd.Flags().MarkDeprecated("skip-admission-controller", "")
createResourceCmd.Flags().BoolVar(&withStatusSubresource, "with-status-subresource", true, "if set, the status sub-resource will be generated")

cmd.AddCommand(createResourceCmd)
}
Expand Down Expand Up @@ -114,10 +117,10 @@ func createResource(boilerplate string) {
kindName,
resourceName,
shortName,
util.Repo,
util.GetRepo(),
inflect.NewDefaultRuleset().Pluralize(kindName),
nonNamespacedKind,
false,
withStatusSubresource,
}

found := false
Expand Down Expand Up @@ -145,7 +148,7 @@ func createResource(boilerplate string) {
scaffoldRegister = "// +kubebuilder:scaffold:resource-register"
)
mainFile := filepath.Join("cmd", "apiserver", "main.go")
newImport := fmt.Sprintf(`%s%s "%s/pkg/apis/%s/%s"`, groupName, versionName, util.Repo, groupName, versionName)
newImport := fmt.Sprintf(`%s%s "%s/pkg/apis/%s/%s"`, groupName, versionName, util.GetRepo(), groupName, versionName)
if err := appendMixin(mainFile, scaffoldImports, newImport); err != nil {
klog.Fatal(err)
}
Expand Down Expand Up @@ -211,14 +214,14 @@ func createResource(boilerplate string) {
Version: versionName,
Kind: kindName,
Plural: resourceName,
Package: filepath.Join(util.Repo, "pkg", "apis", groupName, versionName),
Package: filepath.Join(util.GetRepo(), "pkg", "apis", groupName, versionName),
ImportAlias: resourceName,
}
scaffolder := scaffolds.NewAPIScaffolder(
&config.Config{
MultiGroup: true,
Domain: util.Domain,
Repo: util.Repo,
Repo: util.GetRepo(),
Version: config.Version3Alpha,
},
boilerplate, // TODO
Expand All @@ -240,17 +243,16 @@ func createResource(boilerplate string) {
}

type resourceTemplateArgs struct {
BoilerPlate string
Domain string
Group string
Version string
Kind string
Resource string
ShortName string
Repo string
PluralizedKind string
NonNamespacedKind bool

BoilerPlate string
Domain string
Group string
Version string
Kind string
Resource string
ShortName string
Repo string
PluralizedKind string
NonNamespacedKind bool
WithStatusSubResource bool
}

Expand Down Expand Up @@ -340,9 +342,6 @@ type {{.Kind}}Status struct {
var _ resource.Object = &{{.Kind}}{}
var _ resource.ObjectList = &{{.Kind}}List{}
var _ resourcestrategy.Validater = &{{.Kind}}{}
{{- if .WithStatusSubResource }}
var _ resource.ObjectWithStatusSubResource = &{{.Kind}}{}
{{- end }}
func (in *{{.Kind}}) GetObjectMeta() *metav1.ObjectMeta {
Expand Down Expand Up @@ -380,6 +379,18 @@ func (in *{{.Kind}}) Validate(ctx context.Context) field.ErrorList {
func (in *{{.Kind}}List) GetListMeta() *metav1.ListMeta {
return &in.ListMeta
}
{{- if .WithStatusSubResource }}
var _ resource.ObjectWithStatusSubResource = &{{.Kind}}{}
func (in *{{.Kind}}) SetStatus(statusSubResource interface{}) {
in.Status = statusSubResource.({{.Kind}}Status)
}
func (in *{{.Kind}}) GetStatus() (statusSubResource interface{}) {
return in.Status
}
{{- end }}
`

var resourceSuiteTestTemplate = `
Expand Down
2 changes: 1 addition & 1 deletion cmd/apiserver-boot/boot/create/subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func createSubresource(boilerplate string) {
boilerplate,
subresourceName,
strings.Title(kindName) + strings.Title(subresourceName),
util.Repo,
util.GetRepo(),
groupName,
versionName,
kindName,
Expand Down
2 changes: 1 addition & 1 deletion cmd/apiserver-boot/boot/create/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"regexp"
"strings"

"github.com/pkg/errors"
"github.com/markbates/inflect"
"github.com/pkg/errors"
"github.com/spf13/cobra"

utilvalidation "k8s.io/apimachinery/pkg/util/validation"
Expand Down
6 changes: 1 addition & 5 deletions cmd/apiserver-boot/boot/create/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func createVersion(boilerplate string) {
util.Domain,
groupName,
versionName,
util.Repo,
util.GetRepo(),
})

path = filepath.Join(dir, "pkg", "apis", groupName, versionName, "register.go")
Expand Down Expand Up @@ -118,10 +118,6 @@ var versionTemplate = `
// backward compatibility by support multiple concurrent versions
// of the same resource
//go:generate deepcopy-gen -O zz_generated.deepcopy -i . -h ../../../../boilerplate.go.txt
//go:generate defaulter-gen -O zz_generated.defaults -i . -h ../../../../boilerplate.go.txt
//go:generate conversion-gen -O zz_generated.conversion -i . -h ../../../../boilerplate.go.txt
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen={{.Repo}}/pkg/apis/{{.Group}}
Expand Down
23 changes: 17 additions & 6 deletions cmd/apiserver-boot/boot/init_repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ var repoCmd = &cobra.Command{

var domain string
var copyright string
var moduleName string

func AddInitRepo(cmd *cobra.Command) {
cmd.AddCommand(repoCmd)
repoCmd.Flags().StringVar(&domain, "domain", "", "domain the api groups live under")

// Hide this flag by default
repoCmd.Flags().StringVar(&copyright, "copyright", "boilerplate.go.txt", "Location of copyright boilerplate file.")
repoCmd.Flags().StringVar(&moduleName, "module-name", "",
"the module name of the go mod project, required if the project uses go module outside GOPATH")
}

func RunInitRepo(cmd *cobra.Command, args []string) {
Expand All @@ -51,6 +54,14 @@ func RunInitRepo(cmd *cobra.Command, args []string) {
}
cr := util.GetCopyright(copyright)

if len(moduleName) == 0 {
if err := util.LoadRepoFromGoPath(); err != nil {
klog.Fatal(err)
}
} else {
util.SetRepo(moduleName)
}

createControllerManager(cr)
createGoMod()
createKubeBuilderProjectFile()
Expand Down Expand Up @@ -79,7 +90,7 @@ func createKubeBuilderProjectFile() {
}
path := filepath.Join(dir, "PROJECT")
util.WriteIfNotFound(path, "project-template", projectFileTemplate,
buildTemplateArguments{domain, util.Repo})
buildTemplateArguments{domain, util.GetRepo()})
}

var projectFileTemplate = `
Expand All @@ -97,15 +108,15 @@ func createBazelWorkspace() {
util.WriteIfNotFound(path, "bazel-workspace-template", workspaceTemplate, nil)
path = filepath.Join(dir, "BUILD.bazel")
util.WriteIfNotFound(path, "bazel-build-template",
buildTemplate, buildTemplateArguments{domain, util.Repo})
buildTemplate, buildTemplateArguments{domain, util.GetRepo()})
}

func createControllerManager(boilerplate string) {
scaffolder := scaffolds.NewInitScaffolder(
&config.Config{
MultiGroup: true,
Domain: util.Domain,
Repo: util.Repo,
Repo: util.GetRepo(),
Version: config.Version3Alpha,
},
"",
Expand Down Expand Up @@ -157,7 +168,7 @@ func createApiserver(boilerplate string) {
apiserverTemplateArguments{
domain,
boilerplate,
util.Repo,
util.GetRepo(),
})

}
Expand Down Expand Up @@ -212,7 +223,7 @@ func createGoMod() {
path := filepath.Join(dir, "go.mod")
util.Overwrite(path, "gomod-template", goModTemplate,
goModTemplateArguments{
util.Repo,
util.GetRepo(),
})
}

Expand Down Expand Up @@ -255,7 +266,7 @@ load("@io_k8s_repo_infra//:repos.bzl", "configure")
# use k8s.io/repo-infra to configure go and bazel
# default minimum_bazel_version is 0.29.1
configure(
go_version = "1.13",
go_version = "1.15",
rbe_name = None,
)
`
Expand Down
1 change: 0 additions & 1 deletion cmd/apiserver-boot/boot/run/in_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func AddInCluster(cmd *cobra.Command) {
cmd.AddCommand(runInClusterCmd)

build.AddBuildResourceConfigFlags(runInClusterCmd)
runInClusterCmd.Flags().BoolVar(&build.GenerateForBuild, "generate", true, "if true, generate code before building the container image")
runInClusterCmd.Flags().BoolVar(&buildImage, "build-image", true, "if true, build the container image and push it to the image repo.")
}

Expand Down
1 change: 0 additions & 1 deletion cmd/apiserver-boot/boot/run/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func RunLocal(cmd *cobra.Command, args []string) {
if buildBin {
build.Bazel = bazel
build.Gazelle = gazelle
build.GenerateForBuild = generate
build.RunBuildExecutables(cmd, args)
}

Expand Down
1 change: 0 additions & 1 deletion cmd/apiserver-boot/boot/run/local_aggregated.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func RunLocalMinikube(cmd *cobra.Command, args []string) {
if buildBin {
build.Bazel = bazel
build.Gazelle = gazelle
build.GenerateForBuild = generate
build.RunBuildExecutables(cmd, args)
}

Expand Down
Loading

0 comments on commit 82a5320

Please sign in to comment.