Skip to content

Commit

Permalink
fix: any layout should keep api/ as dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila Macedo committed May 8, 2020
1 parent 09fca78 commit fa16bcf
Show file tree
Hide file tree
Showing 130 changed files with 2,447 additions and 68 deletions.
6 changes: 3 additions & 3 deletions docs/book/src/migration/multi-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Then, we'll rename `api` to `apis` to be more clear, and we'll move our
existing APIs into a new subdirectory, "batch":

```bash
mkdir apis/batch
mv api/* apis/batch
mkdir api/batch
mv api/* api/batch
# After ensuring that all was moved successfully remove the old directory `api/`
rm -rf api/
```
Expand Down Expand Up @@ -72,7 +72,7 @@ Note that this option indicates to KubeBuilder that this is a multi-group projec

In this way, if the project is not new and has previous APIs already implemented will be in the previous structure.
Notice that with the `multi-group` project the Kind API's files are
created under `apis/<group>/<version>` instead of `api/<version>`.
created under `api/<group>/<version>` instead of `api/<version>`.
Also, note that the controllers will be created under `controllers/<group>` instead of `controllers`.
That is the reason why we moved the previously generated APIs with the provided scripts in the previous steps.
Remember to update the references afterwards.
Expand Down
4 changes: 2 additions & 2 deletions docs/testing/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ package parent
import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
childapis "k8s.io/child/pkg/apis"
childv1alpha1 "k8s.io/childrepo/pkg/apis/child/v1alpha1"
childv1alpha1 "k8s.io/childrepo/pkg/api/child/v1alpha1"
parentapis "k8s.io/parent/pkg/apis"
parentv1alpha1 "k8s.io/parentrepo/pkg/apis/parent/v1alpha1"
parentv1alpha1 "k8s.io/parentrepo/pkg/api/parent/v1alpha1"
...<other import items>...
)
Expand Down
10 changes: 5 additions & 5 deletions docs/using_an_external_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ kubebuilder create api --group mygroup --version $APIVERSION --kind MyKind

Edit the following file to the pkg/apis directory to append their `AddToScheme` to your `AddToSchemes`:

file: pkg/apis/mytype_addtoscheme.go
file: pkg/api/mytype_addtoscheme.go
```
package apis
import (
mygroupv1alpha1 "github.com/myuser/myrepo/apis/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/apis/theirgroup/v1alpha1"
mygroupv1alpha1 "github.com/myuser/myrepo/api/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/api/theirgroup/v1alpha1"
)
func init() {
Expand All @@ -88,8 +88,8 @@ func init() {
file: pkg/controllers/mytype_controller.go
```
import (
mygroupv1alpha1 "github.com/myuser/myrepo/apis/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/apis/theirgroup/v1alpha1"
mygroupv1alpha1 "github.com/myuser/myrepo/api/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/api/theirgroup/v1alpha1"
)
```

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/resource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource {

pkg := replacer.Replace(path.Join(c.Repo, "api", "%[version]"))
if c.MultiGroup {
pkg = replacer.Replace(path.Join(c.Repo, "apis", "%[group]", "%[version]"))
pkg = replacer.Replace(path.Join(c.Repo, "api", "%[group]", "%[version]"))
}
domain := c.Domain

Expand Down
6 changes: 3 additions & 3 deletions pkg/model/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ = Describe("Resource", func() {
Expect(resource.Kind).To(Equal(options.Kind))
Expect(resource.Plural).To(Equal("firstmates"))
Expect(resource.ImportAlias).To(Equal("crewv1"))
Expect(resource.Package).To(Equal(path.Join("test", "apis", "crew", "v1")))
Expect(resource.Package).To(Equal(path.Join("test", "api", "crew", "v1")))
Expect(resource.Domain).To(Equal("crew.test.io"))
})

Expand Down Expand Up @@ -156,7 +156,7 @@ var _ = Describe("Resource", func() {
Expect(resource.Group).To(Equal(options.Group))
Expect(resource.GroupPackageName).To(Equal("myproject"))
Expect(resource.ImportAlias).To(Equal("myprojectv1"))
Expect(resource.Package).To(Equal(path.Join("test", "apis", "my-project", "v1")))
Expect(resource.Package).To(Equal(path.Join("test", "api", "my-project", "v1")))
Expect(resource.Domain).To(Equal("my-project.test.io"))

options = &Options{Group: "my.project", Version: "v1", Kind: "FirstMate"}
Expand All @@ -173,7 +173,7 @@ var _ = Describe("Resource", func() {
Expect(resource.Group).To(Equal(options.Group))
Expect(resource.GroupPackageName).To(Equal("myproject"))
Expect(resource.ImportAlias).To(Equal("myprojectv1"))
Expect(resource.Package).To(Equal(path.Join("test", "apis", "my.project", "v1")))
Expect(resource.Package).To(Equal(path.Join("test", "api", "my.project", "v1")))
Expect(resource.Domain).To(Equal("my.project.test.io"))
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/internal/templates/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Group struct {
func (f *Group) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "groupversion_info.go")
f.Path = filepath.Join("api", "%[group]", "%[version]", "groupversion_info.go")
} else {
f.Path = filepath.Join("api", "%[version]", "groupversion_info.go")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/internal/templates/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Types struct {
func (f *Types) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "%[kind]_types.go")
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_types.go")
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_types.go")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/internal/templates/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Webhook struct { // nolint:maligned
func (f *Webhook) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "%[kind]_webhook.go")
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_webhook.go")
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_webhook.go")
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/addon/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ReplaceTypes(u *model.Universe) error {

var path string
if u.Config.MultiGroup {
path = filepath.Join("apis", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go")
path = filepath.Join("api", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go")
} else {
path = filepath.Join("api", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go")
}
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ func (kc *KBTestContext) CreateAPI(resourceOptions ...string) error {
return err
}


// Edit is for running `kubebuilder edit`
func (kc *KBTestContext) Edit(editOptions ...string) error {
editOptions = append([]string{"edit"}, editOptions...)
cmd := exec.Command("kubebuilder", editOptions...)
_, err := kc.Run(cmd)
return err
}

// CreateWebhook is for running `kubebuilder create webhook`
func (kc *KBTestContext) CreateWebhook(resourceOptions ...string) error {
resourceOptions = append([]string{"create", "webhook"}, resourceOptions...)
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/v2/e2e-ymsx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~
27 changes: 27 additions & 0 deletions test/e2e/v2/e2e-ymsx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Build the manager binary
FROM golang:1.13 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER nonroot:nonroot

ENTRYPOINT ["/manager"]
95 changes: 95 additions & 0 deletions test/e2e/v2/e2e-ymsx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

all: manager

# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
go build -o bin/manager main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
go run ./main.go

# Install CRDs into a cluster
install: manifests kustomize
$(KUSTOMIZE) build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests kustomize
$(KUSTOMIZE) build config/crd | kubectl delete -f -

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests kustomize
cd config/manager && kustomize edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run go fmt against code
fmt:
go fmt ./...

# Run go vet against code
vet:
go vet ./...

# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

# Build the docker image
docker-build: test
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif
7 changes: 7 additions & 0 deletions test/e2e/v2/e2e-ymsx/PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
domain: example.comymsx
repo: sigs.k8s.io/kubebuilder
resources:
- group: barymsx
kind: Fooymsx
version: v1alpha1
version: "2"
65 changes: 65 additions & 0 deletions test/e2e/v2/e2e-ymsx/api/v1alpha1/fooymsx_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// FooymsxSpec defines the desired state of Fooymsx
type FooymsxSpec struct {
// +optional
Count int `json:"count,omitempty"`
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Fooymsx. Edit Fooymsx_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

// FooymsxStatus defines the observed state of Fooymsx
type FooymsxStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true

// Fooymsx is the Schema for the fooymsxes API
type Fooymsx struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec FooymsxSpec `json:"spec,omitempty"`
Status FooymsxStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// FooymsxList contains a list of Fooymsx
type FooymsxList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Fooymsx `json:"items"`
}

func init() {
SchemeBuilder.Register(&Fooymsx{}, &FooymsxList{})
}
Loading

0 comments on commit fa16bcf

Please sign in to comment.