Skip to content

Commit

Permalink
operator: migrate to kubebuilder v2
Browse files Browse the repository at this point in the history
Reorganized repository into v2 format, where code is organized in root
directory instead of pkg dir.
Added cert manager managing self signed certificate by default.
Modernized Dockerfile which takes adventage of Go module cache.

Fixes #56
  • Loading branch information
zimnx committed Jul 16, 2020
1 parent bc214d9 commit 5ae79fe
Show file tree
Hide file tree
Showing 100 changed files with 25,897 additions and 1,641 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
bin
config
docs
examples
hack
scripts
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*.dll
*.so
*.dylib
bin
bin/deps
bin/scylla-operator
dist/

# Test binary, build with `go test -c`
Expand All @@ -14,6 +15,10 @@ dist/
# 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
Expand Down
9 changes: 5 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
builds:
-
binary: manager
main: ./cmd
binary: scylla-operator
main: ./pkg/cmd
env:
- CGO_ENABLED=0
ldflags:
Expand All @@ -15,10 +15,11 @@ dockers:
- image_templates:
- 'scylladb/scylla-operator:{{ .Tag }}'
binaries:
- manager
- scylla-operator
skip_push: auto
dockerfile: Dockerfile
extra_files:
- cmd
- go.mod
- go.sum
- pkg
- vendor
34 changes: 17 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
FROM alpine:3.11
# Build the manager binary
FROM golang:1.13 as builder

# Run tini as PID 1 and avoid signal handling issues
ADD https://github.com/krallin/tini/releases/download/v0.18.0/tini-static-amd64 /usr/local/bin/tini
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

# Add exec permissions
RUN chmod +x /usr/local/bin/tini
# Copy source
COPY . .

# Add files for the sidecar
RUN mkdir -p /sidecar
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o scylla-operator github.com/scylladb/scylla-operator/pkg/cmd

# Add tini to sidecar
RUN cp /usr/local/bin/tini /sidecar/tini
FROM alpine:3.12
WORKDIR /
COPY --from=builder /workspace/scylla-operator .

# Add operator binary
COPY manager /usr/local/bin/scylla-operator
RUN chmod +x /usr/local/bin/scylla-operator

# Add executables to sidecar folder
RUN cp /usr/local/bin/scylla-operator /sidecar/scylla-operator

ENTRYPOINT ["tini", "--", "scylla-operator"]
ENTRYPOINT ["/scylla-operator"]
107 changes: 67 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,104 @@ all: test local-build
REPO ?= scylladb/scylla-operator
TAG ?= $(shell git describe --tags --always)
IMG ?= $(REPO):$(TAG)
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

.EXPORT_ALL_VARIABLES:
DOCKER_BUILDKIT := 1
GO111MODULE := off
KUBEBUILDER_ASSETS := $(CURDIR)/bin/deps
PATH := $(CURDIR)/bin/deps/go/bin:$(CURDIR)/bin/deps:$(PATH)
PATH := $(CURDIR)/bin/deps:$(PATH):
PATH := $(CURDIR)/bin/deps/go/bin:$(PATH):
GOROOT := $(CURDIR)/bin/deps/go
GOVERSION := $(shell go version)

# Run tests
.PHONY: test
test: fmt vet vendor
go test ./pkg/... ./cmd/... -coverprofile cover.out
# Default package
PKG := ./pkg/...

# Build local-build binary
.PHONY: local-build
local-build: fmt vet vendor
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o bin/manager github.com/scylladb/scylla-operator/cmd
# Run tests
test: generate fmt vet manifests
go test $(PKG) -coverprofile cover.out

# Run against the configured Kubernetes cluster in ~/.kube/config
.PHONY: run
run: fmt vet vendor
run: generate fmt vet manifests
go run ./cmd operator --image="$(IMG)" --enable-admission-webhook=false

# Install CRDs into a cluster
.PHONY: install
install: manifests
kubectl apply -f config/crds
install: manifests cert-manager
kustomize build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -
kubectl delete -f examples/generic/cert-manager.yaml

cert-manager:
cat config/certmanager/cert-manager.yaml > examples/generic/cert-manager.yaml
cat config/certmanager/cert-manager.yaml > examples/gke/cert-manager.yaml
kubectl apply -f examples/generic/cert-manager.yaml

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
.PHONY: deploy
deploy: install
kubectl apply -f config/rbac
kustomize build config | kubectl apply -f -
deploy: manifests cert-manager
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -
kustomize build config/default > examples/generic/operator.yaml
kustomize build config/default > examples/gke/operator.yaml

# Generate manifests e.g. CRD, RBAC etc.
.PHONY: manifests
manifests: bin/deps
go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go all
cd config && kustomize edit set image scylladb/scylla-operator="$(IMG)"
kustomize build config > examples/generic/operator.yaml
kustomize build config > examples/gke/operator.yaml
manifests: bin/deps controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) webhook rbac:roleName=manager-role paths="$(PKG)" output:crd:artifacts:config=config/crd/bases output:rbac:artifacts:config=config/rbac/bases

# Run go fmt against code
.PHONY: fmt
fmt: bin/deps
go fmt ./pkg/... ./cmd/...
fmt:
go fmt $(PKG)

# Run go vet against code
.PHONY: vet
vet: bin/deps
go vet ./pkg/... ./cmd/...
vet:
go vet $(PKG)

# Generate code
.PHONY: generate
generate: bin/deps
go generate ./pkg/... ./cmd/...

# Ensure dependencies
.PHONY: vendor
vendor:
dep ensure -v
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="$(PKG)"

# Build the docker image
.PHONY: docker-build
docker-build: bin/deps
goreleaser --skip-validate --skip-publish --rm-dist

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

# Ensure dependencies
.PHONY: vendor
vendor:
go mod vendor

# Build local-build binary
.PHONY: local-build
local-build: fmt vet vendor
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o bin/scylla-operator github.com/scylladb/scylla-operator/pkg/cmd

# 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/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

release: bin/deps
goreleaser --rm-dist

bin/deps: hack/binary_deps.py
mkdir -p bin/deps
hack/binary_deps.py bin/deps
hack/binary_deps.py bin/deps
8 changes: 6 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
version: "1"
domain: scylladb.com
domain: scylla.scylladb.com
repo: github.com/scylladb/scylla-operator
resources:
- group: scylla
kind: Cluster
version: v1alpha1
version: "2"
4 changes: 4 additions & 0 deletions bin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this file prevents go compiler from looking into this directory and compiling
# go source which lies in deps/go.
# Please leave it here, unless there's a way to tell compiler which directories
# should be ignored.
54 changes: 0 additions & 54 deletions cmd/webhook.go

This file was deleted.

Loading

0 comments on commit 5ae79fe

Please sign in to comment.