Skip to content

Commit

Permalink
testdata: add go/v3-alpha plugin testdata
Browse files Browse the repository at this point in the history
generate_testdata.sh: generalize script and add plugin option
  • Loading branch information
estroz committed Jun 4, 2020
1 parent 4f9ee37 commit 190ab2f
Show file tree
Hide file tree
Showing 236 changed files with 8,640 additions and 13 deletions.
38 changes: 26 additions & 12 deletions generate_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ build_kb() {
# project-version.
#
scaffold_test_project() {
project=$1
version=$2
local project=$1
local version=$2
local plugin=${3:-""}

testdata_dir=$(pwd)/testdata
mkdir -p ./testdata/$project
rm -rf ./testdata/$project/*
pushd .
cd testdata/$project
kb=$testdata_dir/../bin/kubebuilder
oldgopath=$GOPATH
# Set "--plugins $plugin" if $plugin is not null.
local plugin_flag="${plugin:+--plugins $plugin}"

if [ $version == "2" ] || [ $version == "3-alpha" ]; then
if [ $version == "2" ] && [ -n "$plugin_flag" ]; then
echo "--plugins flag may not be set for project versions less than 3"
exit 1
fi

header_text "Starting to generate projects with version $version"
header_text "Generating $project"

Expand All @@ -46,16 +56,9 @@ scaffold_test_project() {
go mod init sigs.k8s.io/kubebuilder/testdata/$project # our repo autodetection will traverse up to the kb module if we don't do this

header_text "initializing $project ..."
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"
$kb init $plugin_flag --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"

if [ $project == "project-v2" ] || [ $project == "project-v3" ]; then
header_text 'Creating APIs ...'
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
$kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false
elif [ $project == "project-v2-multigroup" ] || [ $project == "project-v3-multigroup" ]; then
if [[ $project =~ .+-multigroup ]]; then
header_text 'Switching to multigroup layout ...'
$kb edit --multigroup=true

Expand All @@ -69,13 +72,21 @@ scaffold_test_project() {
$kb create api --group sea-creatures --version v1beta1 --kind Kraken --controller=true --resource=true --make=false
$kb create api --group sea-creatures --version v1beta2 --kind Leviathan --controller=true --resource=true --make=false
$kb create api --group foo.policy --version v1 --kind HealthCheckPolicy --controller=true --resource=true --make=false
elif [ $project == "project-v2-addon" ] || [ $project == "project-v3-addon" ]; then
elif [[ $project =~ .+-addon ]]; then
header_text 'enabling --pattern flag ...'
export KUBEBUILDER_ENABLE_PLUGINS=1

header_text 'Creating APIs ...'
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --pattern=addon
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false --pattern=addon
$kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false --pattern=addon
else
header_text 'Creating APIs ...'
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
$kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false
fi
fi
make all test
Expand All @@ -94,3 +105,6 @@ scaffold_test_project project-v2-addon 2
scaffold_test_project project-v3 3-alpha
scaffold_test_project project-v3-multigroup 3-alpha
scaffold_test_project project-v3-addon 3-alpha
scaffold_test_project plugin-v3 3-alpha go/v3-alpha
scaffold_test_project plugin-v3-multigroup 3-alpha go/v3-alpha
scaffold_test_project plugin-v3-addon 3-alpha go/v3-alpha
2 changes: 1 addition & 1 deletion pkg/plugin/v3/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type webhookScaffolder struct {
boilerplate string
resource *resource.Resource

// v2
// Webhook type options.
defaulting, validation, conversion bool
}

Expand Down
24 changes: 24 additions & 0 deletions testdata/plugin-v3-addon/.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 testdata/plugin-v3-addon/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 testdata/plugin-v3-addon/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
14 changes: 14 additions & 0 deletions testdata/plugin-v3-addon/PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
domain: testproject.org
layout: go.kubebuilder.io/v3-alpha
repo: sigs.k8s.io/kubebuilder/testdata/plugin-v3-addon
resources:
- group: crew
kind: Captain
version: v1
- group: crew
kind: FirstMate
version: v1
- group: crew
kind: Admiral
version: v1
version: 3-alpha
91 changes: 91 additions & 0 deletions testdata/plugin-v3-addon/api/v1/admiral_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2020 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 v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
addonv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
)

// 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.

// AdmiralSpec defines the desired state of Admiral
type AdmiralSpec struct {
addonv1alpha1.CommonSpec `json:",inline"`
addonv1alpha1.PatchSpec `json:",inline"`

// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// AdmiralStatus defines the observed state of Admiral
type AdmiralStatus struct {
addonv1alpha1.CommonStatus `json:",inline"`

// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster

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

Spec AdmiralSpec `json:"spec,omitempty"`
Status AdmiralStatus `json:"status,omitempty"`
}

var _ addonv1alpha1.CommonObject = &Admiral{}

func (o *Admiral) ComponentName() string {
return "admiral"
}

func (o *Admiral) CommonSpec() addonv1alpha1.CommonSpec {
return o.Spec.CommonSpec
}

func (o *Admiral) PatchSpec() addonv1alpha1.PatchSpec {
return o.Spec.PatchSpec
}

func (o *Admiral) GetCommonStatus() addonv1alpha1.CommonStatus {
return o.Status.CommonStatus
}

func (o *Admiral) SetCommonStatus(s addonv1alpha1.CommonStatus) {
o.Status.CommonStatus = s
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Cluster

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

func init() {
SchemeBuilder.Register(&Admiral{}, &AdmiralList{})
}
Loading

0 comments on commit 190ab2f

Please sign in to comment.