Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Migrate to Kubebuilder v3 schema #396

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore all files which are not go type
!**/*.go
!**/*.mod
!**/*.sum
2 changes: 1 addition & 1 deletion .github/workflows/prbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ jobs:
echo "$GITHUB_WORKSPACE/bin" >> $GITHUB_PATH
- name: generate
run: |
make generate generate-contour-crds example manifests
make generate generate-contour-crds example manifests
./hack/actions/check-uncommitted-codegen.sh
18 changes: 11 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@

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

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

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

# Operator bin directory
bin/
# Kubernetes Generated files - skip generated files, except for vendored files

# Dependency directories (remove the comment below to include it)
vendor/
!vendor/**/zz_generated.*

# Goland
.idea/
# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG BUILDPLATFORM=linux/amd64
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.16.5 as builder

WORKDIR /
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
Expand All @@ -25,9 +25,9 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build

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

ENTRYPOINT ["/contour-operator"]
224 changes: 111 additions & 113 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@ NEW_VERSION ?= $(OLD_VERSION)

# Used as a go test argument for running e2e tests.
TEST ?= .*
TEST_K8S_VERSION ?= 1.19.2

# Image URL to use all building/pushing image targets
IMAGE ?= docker.io/projectcontour/contour-operator

# Need v1 to support defaults in CRDs, unfortunately limiting us to k8s 1.16+
CRD_OPTIONS ?= "crd:crdVersions=v1"

# 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

CONTROLLER_GEN := go run sigs.k8s.io/controller-tools/cmd/controller-gen
TAG_LATEST ?= false

# Platforms to build the multi-arch image for.
IMAGE_PLATFORMS ?= linux/amd64,linux/arm64
Expand Down Expand Up @@ -57,8 +47,6 @@ DOCKER_BUILD_LABELS = \
--label "org.opencontainers.image.title=contour-operator" \
--label "org.opencontainers.image.description=Deploy and manage Contour using an operator."

TAG_LATEST ?= false

ifeq ($(TAG_LATEST), true)
IMAGE_TAGS = \
--tag $(IMAGE):$(VERSION) \
Expand All @@ -68,116 +56,85 @@ else
--tag $(IMAGE):$(VERSION)
endif

all: manager

# Run tests & validate against linters
.PHONY: check
check: test lint-golint lint-codespell

# Run tests
test: generate fmt vet manifests
go test \
-mod=readonly \
-covermode=atomic \
-coverprofile coverage.out \
./...

lint-golint:
@echo Running Go linter ...
@./hack/golangci-lint.sh run --build-tags=e2e

.PHONY: lint-codespell
lint-codespell: CODESPELL_SKIP := $(shell cat .codespell.skip | tr \\n ',')
lint-codespell:
@echo Running Codespell ...
@./hack/codespell.sh --skip $(CODESPELL_SKIP) --ignore-words .codespell.ignorewords --check-filenames --check-hidden -q2

# Build manager binary
manager: generate fmt vet
go build -mod=readonly -o bin/contour-operator cmd/contour-operator.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests install
go run ./cmd/contour-operator.go

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -
# 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

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"

deploy: ## Deploy the operator to a Kubernetes cluster. This assumes a kubeconfig in ~/.kube/config
deploy: manifests
cd config/manager
kustomize build config/default | kubectl apply -f -
CONTROLLER_GEN := go run sigs.k8s.io/controller-tools/cmd/controller-gen
KUSTOMIZE := go run sigs.k8s.io/kustomize/kustomize/v3
SETUP_ENVTEST := go run sigs.k8s.io/controller-runtime/tools/setup-envtest

load-image: ## Load the operator image to a kind cluster
load-image: container
./hack/load-image.sh $(IMAGE) $(VERSION)
CODESPELL_SKIP := $(shell cat .codespell.skip | tr \\n ',')

# Remove the operator deployment. This assumes a kubeconfig in ~/.kube/config
undeploy:
cd config/manager
kustomize build config/default | kubectl delete -f -
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

example: ## Generate the example operator manifest.
example:
cd config/manager
kustomize build config/default > examples/operator/operator.yaml
all: build

test-example: ## Test the example Contour.
.PHONY: test-example
test-example:
./hack/test-example.sh
##@ General

verify-image: ## Verifies operator image references and pull policy.
.PHONY: verify-image
verify-image:
./hack/verify-image.sh $(NEW_VERSION)
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

reset-image: ## Resets operator image references and pull policy.
.PHONY: reset-image
reset-image:
./hack/reset-image.sh $(IMAGE) $(OLD_VERSION)
##@ Development

# Generate Contour's rendered CRD manifest (i.e. HTTPProxy).
# Remove when https://github.com/projectcontour/contour-operator/issues/42 is fixed.
.PHONY: generate-contour-crds
generate-contour-crds:
generate-contour-crds: ## Generate Contour's rendered CRD manifest (i.e. HTTPProxy).
@./hack/generate-contour-crds.sh $(NEW_VERSION)

manifests: ## Generate manifests e.g. CRD, RBAC etc.
manifests: generate-contour-crds
manifests: generate-contour-crds ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=contour-operator webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run go fmt against code
fmt:
generate: ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

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

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

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

multiarch-build-push: ## Build and push a multi-arch contour-operator container image to the Docker registry
docker buildx build \
--platform $(IMAGE_PLATFORMS) \
--build-arg "BUILD_VERSION=$(BUILD_VERSION)" \
--build-arg "BUILD_BRANCH=$(BUILD_BRANCH)" \
--build-arg "BUILD_SHA=$(BUILD_SHA)" \
$(DOCKER_BUILD_LABELS) \
$(IMAGE_TAGS) \
--push \
.
.PHONY: check
check: test lint-golint lint-codespell ## Run tests and validate against linters

test: manifests generate fmt vet ## Run tests.
$(SETUP_ENVTEST) use -p path $(TEST_K8S_VERSION); KUBEBUILDER_ASSETS=${HOME}/.local/share/kubebuilder-envtest/k8s/$(TEST_K8S_VERSION)-$(shell go env GOHOSTOS)-$(shell go env GOHOSTARCH) go test ./... -coverprofile cover.out

lint-golint: ## Run golangci-lint against code.
@echo Running Go linter ...
@./hack/golangci-lint.sh run --build-tags=e2e

.PHONY: lint-codespell
lint-codespell: ## Run codespell against code.
@echo Running Codespell ...
@./hack/codespell.sh --skip $(CODESPELL_SKIP) --ignore-words .codespell.ignorewords --check-filenames --check-hidden -q2

.PHONY: test-e2e
test-e2e: deploy ## Run e2e tests.
go test -mod=readonly -timeout 20m -count 1 -v -tags e2e -run "$(TEST)" ./test/e2e

##@ Build

container: ## Build the contour-operator container image
container: test
build: generate fmt vet ## Build manager binary.
go build -mod=readonly -o bin/contour-operator cmd/contour-operator.go

run: manifests generate fmt vet install ## Run a controller from your host.
go run ./cmd/contour-operator.go

docker-build: test ## Build the contour-operator container image.
docker build \
--build-arg "BUILD_VERSION=$(BUILD_VERSION)" \
--build-arg "BUILD_BRANCH=$(BUILD_BRANCH)" \
Expand All @@ -186,23 +143,64 @@ container: test
$(shell pwd) \
--tag $(IMAGE):$(VERSION)

push: ## Push the contour-operator container image to the Docker registry
push: container
docker push $(IMAGE):$(VERSION)
docker-push: docker-build ## Push the contour-operator container image to the Docker registry.
docker push ${IMAGE}:$(VERSION)
ifeq ($(TAG_LATEST), true)
docker tag $(IMAGE):$(VERSION) $(IMAGE):latest
docker push $(IMAGE):latest
endif

local-cluster: # Create a local kind cluster
multiarch-build-push: ## Build and push a multi-arch contour-operator container image to the Docker registry.
docker buildx build \
--platform $(IMAGE_PLATFORMS) \
--build-arg "BUILD_VERSION=$(BUILD_VERSION)" \
--build-arg "BUILD_BRANCH=$(BUILD_BRANCH)" \
--build-arg "BUILD_SHA=$(BUILD_SHA)" \
$(DOCKER_BUILD_LABELS) \
$(IMAGE_TAGS) \
--push \
.

##@ Deployment

install: manifests ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

uninstall: manifests ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -

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

undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -

##@ Kind cluster

local-cluster: ## Create a local kind cluster
./hack/kind-dev-cluster.sh

release: ## Prepares a tagged release of the operator.
load-image: docker-build ## Load the operator image to a Kind cluster
./hack/load-image.sh $(IMAGE) $(VERSION)

example: ## Generate example operator manifest.
$(KUSTOMIZE) build config/default > config/samples/operator/operator.yaml

.PHONY: test-example
test-example: ## Test example operator.
./hack/test-example.sh

##@ Release

.PHONY: verify-image
verify-image: ## Verifies operator image references and pull policy.
./hack/verify-image.sh $(NEW_VERSION)

.PHONY: reset-image
reset-image: ## Resets operator image references and pull policy
./hack/reset-image.sh $(IMAGE) $(OLD_VERSION)

.PHONY: release
release:
release: ## Prepares a tagged release of the operator.
./hack/release/make-release-tag.sh $(OLD_VERSION) $(NEW_VERSION)

test-e2e: ## Runs e2e tests.
.PHONY: test-e2e
test-e2e: deploy
go test -mod=readonly -timeout 20m -count 1 -v -tags e2e -run "$(TEST)" ./test/e2e
12 changes: 10 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
domain: projectcontour.io
layout:
- go.kubebuilder.io/v3
projectName: contour-operator
repo: github.com/projectcontour/contour-operator
resources:
- group: operator
- api:
crdVersion: v1
namespaced: true
domain: projectcontour.io
group: operator
kind: Contour
path: github.com/projectcontour/contour-operator/api/v1alpha1
version: v1alpha1
version: "2"
version: "3"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ operator pattern.

Install the Contour Operator & Contour CRDs:
```
$ kubectl apply -f https://raw.githubusercontent.com/projectcontour/contour-operator/main/examples/operator/operator.yaml
$ kubectl apply -f https://raw.githubusercontent.com/projectcontour/contour-operator/main/config/samples/operator/operator.yaml
```

Verify the deployment is available:
Expand All @@ -26,7 +26,7 @@ contour-operator 1/1 1 1 1m

Install an instance of the `Contour` custom resource:
```
$ kubectl apply -f https://raw.githubusercontent.com/projectcontour/contour-operator/main/examples/contour/contour.yaml
$ kubectl apply -f https://raw.githubusercontent.com/projectcontour/contour-operator/main/config/samples/contour/contour.yaml
```

Verify the `Contour` custom resource is available:
Expand Down
Loading