Skip to content

Commit

Permalink
Merge pull request #18 from uselagoon/kube-rbac-proxy
Browse files Browse the repository at this point in the history
chore: remove kube-rbac-proxy, adjust metrics endpoint, e2e-test
  • Loading branch information
shreddedbacon authored Dec 18, 2024
2 parents c429f46 + 593353d commit 14ef05b
Show file tree
Hide file tree
Showing 27 changed files with 1,025 additions and 197 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/storage-calculator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Lagoon Storage Controller Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test-suite:
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: false
matrix:
kindest_node_version: [v1.25.16, v1.26.15]
experimental: [false]
include:
- kindest_node_version: v1.27.13
experimental: false
- kindest_node_version: v1.28.9
experimental: false
- kindest_node_version: v1.29.4
experimental: true
- kindest_node_version: v1.30.2
experimental: true
steps:
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
timeout-minutes: 1
continue-on-error: true
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: "0"
- name: Set up testing dependencies
run: sudo apt-get update && sudo apt-get -y install build-essential && sudo apt-get clean
- name: Setup correct Go version
uses: actions/setup-go@v2
with:
go-version: '1.22'

- name: Create kind cluster
uses: helm/[email protected]
with:
version: v0.24.0
cluster_name: storage-calculator
node_image: kindest/node:${{ matrix.kindest_node_version }}
kubectl_version: v1.30.4


- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Run test-e2e
run: |
make test-e2e
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY cmd/ cmd/
COPY internal/ internal/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
93 changes: 74 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24.2

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -11,6 +9,19 @@ else
GOBIN=$(shell go env GOBIN)
endif

# 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

KIND_CLUSTER ?= storage-calculator
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.29.0
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
ENVTEST_VERSION ?= latest

# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
Expand Down Expand Up @@ -40,7 +51,7 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook paths="./..."

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
Expand All @@ -56,17 +67,17 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out

##@ Build

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

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

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
Expand All @@ -84,11 +95,9 @@ endif

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

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
Expand All @@ -99,6 +108,47 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: envtest
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
$(ENVTEST): $(LOCALBIN)
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))

.PHONY: create-kind-cluster
create-kind-cluster:
docker network inspect $(KIND_CLUSTER) >/dev/null || docker network create $(KIND_CLUSTER) \
&& kind create cluster --wait=60s --name=$(KIND_CLUSTER)

# Create a kind cluster locally and run the test e2e test suite against it
.PHONY: kind/test-e2e # Run the e2e tests against a Kind k8s instance that is spun up locally
kind/test-e2e: create-kind-cluster kind/re-test-e2e

.PHONY: local-kind/test-e2e # Run the e2e tests against a Kind k8s instance that is spun up locally
kind/re-test-e2e:
export KIND_CLUSTER=$(KIND_CLUSTER) && \
kind export kubeconfig --name=$(KIND_CLUSTER) && \
$(MAKE) test-e2e

.PHONY: clean
kind/clean:
kind delete cluster --name=$(KIND_CLUSTER) && docker network rm $(KIND_CLUSTER)

# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up inside github action.
test-e2e:
go test ./test/e2e/ -v -ginkgo.v

.PHONY: kind/set-kubeconfig
kind/set-kubeconfig:
export KIND_CLUSTER=$(KIND_CLUSTER) && \
kind export kubeconfig --name=$(KIND_CLUSTER)

.PHONY: kind/logs-controller
kind/logs-controller:
export KIND_CLUSTER=$(KIND_CLUSTER) && \
kind export kubeconfig --name=$(KIND_CLUSTER) && \
kubectl -n storage-calculator-system logs -f \
$$(kubectl -n storage-calculator-system get pod -l control-plane=controller-manager -o jsonpath="{.items[0].metadata.name}") \
-c manager
##@ Build Dependencies

## Location to install dependencies to
Expand All @@ -108,8 +158,6 @@ $(LOCALBIN):

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
Expand All @@ -121,12 +169,19 @@ kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
# 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 install 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
35 changes: 32 additions & 3 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"crypto/tls"
"flag"
"fmt"
"os"
Expand All @@ -42,6 +43,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
//+kubebuilder:scaffold:imports
)

Expand All @@ -65,6 +68,8 @@ func init() {

func main() {
var metricsAddr string
var secureMetrics bool
var enableHTTP2 bool
var enableLeaderElection bool
var probeAddr string
var ignoreRegex string
Expand All @@ -78,7 +83,12 @@ func main() {
var mqWorkers int
var rabbitRetryInterval int
var exportPrometheusMetrics bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
flag.BoolVar(&secureMetrics, "metrics-secure", true,
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down Expand Up @@ -115,10 +125,29 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

disableHTTP2 := func(c *tls.Config) {
setupLog.Info("disabling http/2")
c.NextProtos = []string{"http/1.1"}
}
tlsOpts := []func(*tls.Config){}
if !enableHTTP2 {
tlsOpts = append(tlsOpts, disableHTTP2)
}
metricsServerOptions := metricsserver.Options{
BindAddress: metricsAddr,
SecureServing: secureMetrics,
TLSOpts: tlsOpts,
}
if secureMetrics {
// FilterProvider is used to protect the metrics endpoint with authn/authz.
// These configurations ensure that only authorized users and service accounts
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Metrics: metricsServerOptions,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "14209f0a.uselagoon.sh",
Expand Down
3 changes: 2 additions & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ namePrefix: storage-calculator-
# someName: someValue

bases:
- ../crd
- ../rbac
- ../manager
# [METRICS] Expose the controller manager metrics service.
- metrics_service.yaml
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
Expand Down
33 changes: 9 additions & 24 deletions config/default/manager_auth_proxy_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@ spec:
template:
spec:
containers:
- name: kube-rbac-proxy
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- "ALL"
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0
- name: manager
args:
- "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8080/"
- "--logtostderr=true"
- "--v=0"
- "--metrics-bind-address=:8443"
- "--leader-elect"
- "--health-probe-bind-address=:8081"
- "--calculator-cron=* * * * *"
- "--prometheus-metrics=true"
ports:
- containerPort: 8443
protocol: TCP
name: https
resources:
limits:
cpu: 500m
memory: 128Mi
requests:
cpu: 5m
memory: 64Mi
- name: manager
args:
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
env:
- name: RABBITMQ_HOSTNAME
value: '172.17.0.1:5672'
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ spec:
protocol: TCP
targetPort: https
selector:
control-plane: controller-manager
control-plane: controller-manager
10 changes: 8 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- name: manager-config
files:
- files:
- controller_manager_config.yaml
name: manager-config
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: example.com/storage-calculator
newTag: v0.0.1
Loading

0 comments on commit 14ef05b

Please sign in to comment.