Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
nhinze23 authored and cesmarvin committed Jun 8, 2022
2 parents 98a15d6 + c52085a commit f312a82
Show file tree
Hide file tree
Showing 48 changed files with 3,031 additions and 158 deletions.
7 changes: 7 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Usage:
# 1. Copy this file as `.env` into your project
# 2. Adapt the information below with the your personal data.
#
# The file `.env` is ignored by git. Note: DO NOT COMMIT your personal data.
export K8S_CLUSTER_ROOT=/home/jsprey/Documents/GIT/k3ces
export WATCH_NAMESPACE=$(shell kubectl config view --minify -o jsonpath='{..namespace}')
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Makefiles
.env

# Binaries for programs and plugins
*.so
*.dylib
Expand All @@ -22,4 +25,7 @@ vendor
*.swp
*.swo
.code
*~
*~

# Makefiles
.env
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.2.0] - 2022-06-08
### Added
- Warp menu generation
- Add runnable to the controller which observes keys in the etcd specified in a configmap `k8s-ces-warp-config`
and creates warp menu entries in `k8s-ces-menu-json` for the nginx-ingress dogu [#3].

### Changed
- Update makefiles to version 6.0.1 [#3]

## [v0.1.0] - 2022-04-20
### Added
- Automatically creates the ingress class `k8s-ecosystem-ces-service` in the current namespace.
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN make compile-generic
FROM gcr.io/distroless/static:nonroot
LABEL maintainer="[email protected]" \
NAME="k8s-service-discovery" \
VERSION="0.1.0"
VERSION="0.2.0"

WORKDIR /

Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ node('docker') {
}

stage('Generate k8s Resources') {
make 'k8s-generate'
make 'k8s-create-temporary-resource'
archiveArtifacts 'target/*.yaml'
}
}
Expand Down
152 changes: 25 additions & 127 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
# Set these to the desired values
ARTIFACT_ID=k8s-service-discovery
VERSION=0.1.0
VERSION=0.2.0

GOTAG?=1.17.7
MAKEFILES_VERSION=5.0.0

# Image URL to use all building/pushing image targets
## Image URL to use all building/pushing image targets
IMAGE_DEV=${K3CES_REGISTRY_URL_PREFIX}/${ARTIFACT_ID}:${VERSION}
IMAGE=cloudogu/${ARTIFACT_ID}:${VERSION}
GOTAG?=1.17.7
MAKEFILES_VERSION=6.0.1

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.23
K8S_INTEGRATION_TEST_DIR=${TARGET_DIR}/k8s-integration-test
K8S_RESOURCE_YAML=${TARGET_DIR}/${ARTIFACT_ID}_${VERSION}.yaml
DEFAULT_NAMESPACE?="$(shell kubectl config view | grep namespace | head -n 1 | sed "s/^.*namespace: //g")"

# make sure to create a statically linked binary otherwise it may quit with
# "exec user process caused: no such file or directory"
GO_BUILD_FLAGS=-mod=vendor -a -tags netgo,osusergo $(LDFLAGS) -o $(BINARY)
# remove DWARF symbol table and strip other symbols to shave ~13 MB from binary
ADDITIONAL_LDFLAGS=-extldflags -static -w -s
ADDITIONAL_CLEAN=dist-clean

.DEFAULT_GOAL:=help
K8S_RESOURCE_DIR=${WORKDIR}/k8s
K8S_WARP_CONFIG_RESOURCE_YAML=${K8S_RESOURCE_DIR}/k8s-ces-warp-config.yaml
K8S_WARP_MENU_JSON_YAML=${K8S_RESOURCE_DIR}/k8s-ces-menu-json.yaml

include build/make/variables.mk

ADDITIONAL_CLEAN=dist-clean
PRE_COMPILE=generate vet

include build/make/self-update.mk
include build/make/dependencies-gomod.mk
include build/make/build.mk
Expand All @@ -37,27 +25,12 @@ include build/make/static-analysis.mk
include build/make/clean.mk
include build/make/digital-signature.mk

K8S_RUN_PRE_TARGETS=setup-etcd-port-forward
PRE_COMPILE=generate vet
K8S_PRE_GENERATE_TARGETS=k8s-create-temporary-resource generate-warp-config generate-menu-json

# 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

##@ EcoSystem

.PHONY: build
build: docker-build image-import k8s-apply ## Builds a new version of the setup and deploys it into the K8s-EcoSystem.

##@ Development (without go container)

${STATIC_ANALYSIS_DIR}/report-govet.out: ${SRC} $(STATIC_ANALYSIS_DIR)
@go vet ./... | tee $@

.PHONY: vet
vet: ${STATIC_ANALYSIS_DIR}/report-govet.out ## Run go vet against code.
include build/make/k8s-controller.mk

##@ Kubernetes Controller

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
Expand All @@ -69,95 +42,20 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
@echo "Auto-generate deepcopy functions..."
@$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

$(K8S_INTEGRATION_TEST_DIR):
@mkdir -p $@

.PHONY: k8s-integration-test
k8s-integration-test: $(K8S_INTEGRATION_TEST_DIR) manifests generate vet envtest ## Run k8s integration tests.
@echo "Running k8s integration tests..."
@KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -tags=k8s_integration ./... -coverprofile ${K8S_INTEGRATION_TEST_DIR}/report-k8s-integration.out

##@ Build

.PHONY: build-controller
build-controller: ${SRC} compile ## Builds the controller Go binary.

.PHONY: run
run: manifests generate vet ## Run a controller from your host.
go run ./main.go

##@ Release

.PHONY: controller-release
controller-release: ## Interactively starts the release workflow.
@echo "Starting git flow release..."
@build/make/release.sh controller-tool

##@ Docker

.PHONY: docker-build
docker-build: ${SRC} ## Builds the docker image of the k8s-ces-setup `cloudogu/k8s-ces-setup:version`.
@echo "Building docker image of dogu..."
DOCKER_BUILDKIT=1 docker build . -t ${IMAGE}

${K8S_CLUSTER_ROOT}/image.tar: check-k8s-cluster-root-env-var
# Saves the `cloudogu/k8s-ces-setup:version` image into a file into the K8s root path to be available on all nodes.
docker save ${IMAGE} -o ${K8S_CLUSTER_ROOT}/image.tar

.PHONY: image-import
image-import: ${K8S_CLUSTER_ROOT}/image.tar
# Imports the currently available image `cloudogu/k8s-ces-setup:version` into the K8s cluster for all nodes.
@echo "Import docker image of dogu into all K8s nodes..."
@cd ${K8S_CLUSTER_ROOT} && \
for node in $$(vagrant status --machine-readable | grep "state,running" | awk -F',' '{print $$2}'); \
do \
echo "...$${node}"; \
vagrant ssh $${node} -- -t "sudo k3s ctr images import /vagrant/image.tar"; \
done;
@echo "Done."
rm ${K8S_CLUSTER_ROOT}/image.tar

.PHONY: check-k8s-cluster-root-env-var
check-k8s-cluster-root-env-var:
@echo "Checking if env var K8S_CLUSTER_ROOT is set..."
@bash -c export -p | grep K8S_CLUSTER_ROOT
@echo "Done."

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif

${K8S_RESOURCE_YAML}: ${TARGET_DIR} manifests kustomize
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMAGE}
$(KUSTOMIZE) build config/default > ${K8S_RESOURCE_YAML}

.PHONY: k8s-generate
k8s-generate: ${K8S_RESOURCE_YAML} ## Create required k8s resources in ./dist/...
@echo "Generating new kubernetes resources..."

.PHONY: k8s-apply
k8s-apply: k8s-generate ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cat ${K8S_RESOURCE_YAML} | sed "s/{{ .Namespace }}/${DEFAULT_NAMESPACE}/" | kubectl apply -f -
## Local Development

.PHONY: k8s-delete
k8s-delete: k8s-generate ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
cat ${K8S_RESOURCE_YAML} | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
.PHONY: setup-etcd-port-forward
setup-etcd-port-forward:
kubectl port-forward etcd-0 4001:2379 &

##@ Download Kubernetes Utility Tools
.PHONY: generate-warp-config
generate-warp-config:
@echo "---" >> $(K8S_RESOURCE_TEMP_YAML)
@cat $(K8S_WARP_CONFIG_RESOURCE_YAML) >> $(K8S_RESOURCE_TEMP_YAML)

CONTROLLER_GEN = $(UTILITY_BIN_PATH)/controller-gen
.PHONY: controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
.PHONY: generate-menu-json
generate-menu-json:
@echo "---" >> $(K8S_RESOURCE_TEMP_YAML)
@cat $(K8S_WARP_MENU_JSON_YAML) >> $(K8S_RESOURCE_TEMP_YAML)

KUSTOMIZE = $(UTILITY_BIN_PATH)/kustomize
.PHONY: kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

ENVTEST = $(UTILITY_BIN_PATH)/setup-envtest
.PHONY: envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
100 changes: 100 additions & 0 deletions build/make/k8s-controller.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This script can be used to build and deploy kubernetes controllers. It is required to implement the controller
# specific targets `manifests` and `generate`:
#
# Examples:
#
#.PHONY: manifests
#manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
# @echo "Generate manifests..."
# @$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
#
#.PHONY: generate
#generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
# @echo "Auto-generate deepcopy functions..."
# @$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."


# This script required the k8s.mk script
include $(WORKDIR)/build/make/k8s.mk

## Variables

# Contains the artifact yaml used as
K8S_RESOURCE_TEMP_YAML=${TARGET_DIR}/${ARTIFACT_ID}_${VERSION}.yaml

# 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

# make sure to create a statically linked binary otherwise it may quit with
# "exec user process caused: no such file or directory"
GO_BUILD_FLAGS=-mod=vendor -a -tags netgo,osusergo $(LDFLAGS) -o $(BINARY)

# remove DWARF symbol table and strip other symbols to shave ~13 MB from binary
ADDITIONAL_LDFLAGS=-extldflags -static -w -s

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.23
K8S_INTEGRATION_TEST_DIR=${TARGET_DIR}/k8s-integration-test

##@ K8s - EcoSystem

.PHONY: build
build: k8s-delete image-import k8s-apply ## Builds a new version of the dogu and deploys it into the K8s-EcoSystem.

##@ Release

.PHONY: controller-release
controller-release: ## Interactively starts the release workflow.
@echo "Starting git flow release..."
@build/make/release.sh controller-tool

##@ K8s - Development

.PHONY: build-controller
build-controller: ${SRC} compile ## Builds the controller Go binary.

# Allows to perform tasks before locally running the controller
K8S_RUN_PRE_TARGETS ?=
.PHONY: run
run: manifests generate vet $(K8S_RUN_PRE_TARGETS) ## Run a controller from your host.
go run -ldflags "-X main.Version=$(VERSION)" ./main.go

##@ K8s - Integration test with envtest

$(K8S_INTEGRATION_TEST_DIR):
@mkdir -p $@

.PHONY: k8s-integration-test
k8s-integration-test: $(K8S_INTEGRATION_TEST_DIR) manifests generate vet envtest ## Run k8s integration tests.
@echo "Running K8s integration tests..."
@KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -tags=k8s_integration ./... -coverprofile ${K8S_INTEGRATION_TEST_DIR}/report-k8s-integration.out

##@ K8s - Controller Resource

# The pre generation script creates a K8s resource yaml containing generated manager yaml.
.PHONY: k8s-create-temporary-resource
k8s-create-temporary-resource: ${TARGET_DIR} manifests kustomize
@echo "Generating temporary k8s resources $(K8S_RESOURCE_TEMP_YAML)..."
cd $(WORKDIR)/config/manager && $(KUSTOMIZE) edit set image controller=$(IMAGE)
$(KUSTOMIZE) build config/default > $(K8S_RESOURCE_TEMP_YAML)
@echo "Done."

##@ K8s - Download Kubernetes Utility Tools

CONTROLLER_GEN = $(UTILITY_BIN_PATH)/controller-gen
.PHONY: controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])

KUSTOMIZE = $(UTILITY_BIN_PATH)/kustomize
.PHONY: kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

ENVTEST = $(UTILITY_BIN_PATH)/setup-envtest
.PHONY: envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
50 changes: 50 additions & 0 deletions build/make/k8s-dogu.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

# Variables

# Path to the dogu json of the dogu
DOGU_JSON_FILE=$(WORKDIR)/dogu.json
DOGU_JSON_DEV_FILE=${TARGET_DIR}/dogu.json
# Name of the dogu is extracted from the dogu.json
ARTIFACT_ID=$(shell yq -e ".Name" $(DOGU_JSON_FILE) | sed "s|.*/||g")
# Namespace of the dogu is extracted from the dogu.json
ARTIFACT_NAMESPACE=$(shell yq -e ".Name" $(DOGU_JSON_FILE) | sed "s|/.*||g")
# Namespace of the dogu is extracted from the dogu.json
VERSION=$(shell yq -e ".Version" $(DOGU_JSON_FILE))
# Image of the dogu is extracted from the dogu.json
IMAGE=$(shell yq -e ".Image" $(DOGU_JSON_FILE)):$(VERSION)
IMAGE_DEV_WITHOUT_TAG=$(shell yq -e ".Image" $(DOGU_JSON_FILE) | sed "s|registry\.cloudogu\.com\(.\+\)|${K3CES_REGISTRY_URL_PREFIX}\1|g")
IMAGE_DEV=${IMAGE_DEV_WITHOUT_TAG}:${VERSION}

include $(WORKDIR)/build/make/k8s.mk

##@ K8s - EcoSystem

.PHONY: build
build: k8s-delete image-import install-dogu-descriptor k8s-apply ## Builds a new version of the dogu and deploys it into the K8s-EcoSystem.

##@ K8s - Dogu - Resource

# The additional k8s yaml files
K8S_RESOURCE_PRODUCTIVE_FOLDER ?= $(WORKDIR)/k8s
K8S_RESOURCE_PRODUCTIVE_YAML ?= $(K8S_RESOURCE_PRODUCTIVE_FOLDER)/$(ARTIFACT_ID).yaml
K8S_RESOURCE_DOGU_CR_TEMPLATE_YAML ?= $(WORKDIR)/build/make/k8s-dogu.tpl
# The pre generation script creates a k8s resource yaml containing the dogu crd and the content from the k8s folder.
.PHONY: k8s-create-temporary-resource
k8s-create-temporary-resource:
@echo "Generating temporary K8s resources $(K8S_RESOURCE_TEMP_YAML)..."
@rm -f $(K8S_RESOURCE_TEMP_YAML)
@test -f $(K8S_RESOURCE_PRODUCTIVE_YAML) && (cp $(K8S_RESOURCE_PRODUCTIVE_YAML) $(K8S_RESOURCE_TEMP_YAML)) || (touch $(K8S_RESOURCE_TEMP_YAML))
@echo "---" >> $(K8S_RESOURCE_TEMP_YAML)
@sed "s|NAMESPACE|$(ARTIFACT_NAMESPACE)|g" $(K8S_RESOURCE_DOGU_CR_TEMPLATE_YAML) | sed "s|NAME|$(ARTIFACT_ID)|g" | sed "s|VERSION|$(VERSION)|g" >> $(K8S_RESOURCE_TEMP_YAML)
@echo "Done."

##@ K8s - Dogu

.PHONY: install-dogu-descriptor
install-dogu-descriptor: ## Installs a configmap with current dogu.json into the cluster.
@echo "Generate configmap from dogu.json..."

@jq ".Image=\"${IMAGE_DEV_WITHOUT_TAG}\"" ${DOGU_JSON_FILE} > ${DOGU_JSON_DEV_FILE}

@kubectl create configmap "$(ARTIFACT_ID)-descriptor" --from-file=$(DOGU_JSON_DEV_FILE) --dry-run=client -o yaml | kubectl apply -f -
@echo "Done."
Loading

0 comments on commit f312a82

Please sign in to comment.