Skip to content

Commit

Permalink
rebasing apiserver-runtime pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
yue9944882 committed Oct 22, 2020
1 parent fe1e2a0 commit e2e7c35
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 102 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ 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; cd test; cp Makefile.test test/
export PATH=${PATH}:${GOBIN}
make test -f Makefile.test
basic-example-build:
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
2 changes: 1 addition & 1 deletion 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 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
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
3 changes: 3 additions & 0 deletions cmd/apiserver-boot/boot/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"repo.go",
"untar.go",
"util.go",
"x509.go",
Expand All @@ -11,7 +12,9 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"@com_github_markbates_inflect//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@io_k8s_apiserver//pkg/server:go_default_library",
"@io_k8s_klog//:go_default_library",
"@org_golang_x_mod//modfile:go_default_library",
],
)
69 changes: 69 additions & 0 deletions cmd/apiserver-boot/boot/util/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package util

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"golang.org/x/mod/modfile"
"k8s.io/klog"
)

var repo string

func LoadRepoFromGoPath() error {
gopath := os.Getenv("GOPATH")
if len(gopath) == 0 {
return fmt.Errorf("GOPATH not defined")
}
goSrc := filepath.Join(gopath, "src")
wd, err := os.Getwd()
if err != nil {
return err
}
if !strings.HasPrefix(filepath.Dir(wd), goSrc) {
return fmt.Errorf("apiserver-boot must be run from the directory containing the go package to "+
"bootstrap. This must be under $GOPATH/src/<package>. "+
"\nCurrent GOPATH=%s. \nCurrent directory=%s", gopath, wd)
}
repo = strings.Replace(wd, goSrc+string(filepath.Separator), "", 1)
return nil
}

func LoadRepoFromGoMod() error {
mod, err := ioutil.ReadFile("go.mod")
if err != nil {
return errors.Wrap(err, "failed reading go.mod file")
}
modPath := modfile.ModulePath(mod)
if len(modPath) == 0 {
return fmt.Errorf("failed parsing go.mod, empty module path")
}
repo = modPath
return nil
}

func LoadRepoFromGoPathOrGoMod() error {
if err := LoadRepoFromGoPath(); err != nil {
// reading from go mod
return LoadRepoFromGoMod()
}
return nil
}

func GetRepo() string {
if len(repo) > 0 {
return repo
}
if err := LoadRepoFromGoPathOrGoMod(); err != nil {
klog.Fatal(err)
}
return repo
}

func SetRepo(r string) {
repo = r
}
Loading

0 comments on commit e2e7c35

Please sign in to comment.