Skip to content

Commit a11f705

Browse files
authored
feat!: Goodbye, kube-rbac-proxy (#33)
1 parent 7071f70 commit a11f705

23 files changed

+362
-355
lines changed

.golangci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
run:
2-
deadline: 5m
2+
timeout: 5m
33
allow-parallel-runners: true
44

55
issues:
@@ -22,6 +22,7 @@ linters:
2222
- dupl
2323
- errcheck
2424
- exportloopref
25+
- ginkgolinter
2526
- goconst
2627
- gocyclo
2728
- gofmt
@@ -33,8 +34,14 @@ linters:
3334
- misspell
3435
- nakedret
3536
- prealloc
37+
- revive
3638
- staticcheck
3739
- typecheck
3840
- unconvert
3941
- unparam
4042
- unused
43+
44+
linters-settings:
45+
revive:
46+
rules:
47+
- name: comment-spacings

Makefile

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ IMAGE_TAG_BASE ?= ghcr.io/isometry/$(OPERATOR_NAME)
3636

3737
# BUNDLE_IMG defines the image:tag used for the bundle.
3838
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
39-
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)/bundle:v$(VERSION)
39+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
4040

4141
# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
4242
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
@@ -51,7 +51,7 @@ endif
5151

5252
# Set the Operator SDK version to use. By default, what is installed on the system is used.
5353
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
54-
OPERATOR_SDK_VERSION ?= v1.37.0
54+
OPERATOR_SDK_VERSION ?= v1.38.0
5555

5656
# Image URL to use all building/pushing image targets
5757
IMG ?= $(IMAGE_TAG_BASE):latest
@@ -132,17 +132,9 @@ test: manifests generate fmt vet envtest ## Run tests.
132132
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
133133
test-e2e:
134134
go test ./test/e2e/ -v -ginkgo.v
135-
136-
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
137-
GOLANGCI_LINT_VERSION ?= v1.61.0
138-
golangci-lint:
139-
@[ -f $(GOLANGCI_LINT) ] || { \
140-
set -e ;\
141-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
142-
}
143135

144136
.PHONY: lint
145-
lint: golangci-lint ## Run golangci-lint linter & yamllint
137+
lint: golangci-lint ## Run golangci-lint linter
146138
$(GOLANGCI_LINT) run
147139

148140
.PHONY: lint-fix
@@ -181,12 +173,18 @@ PLATFORMS ?= linux/arm64,linux/amd64
181173
docker-buildx: ## Build and push docker image for the manager for cross-platform support
182174
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
183175
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
184-
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
185-
$(CONTAINER_TOOL) buildx use project-v3-builder
176+
- $(CONTAINER_TOOL) buildx create --name github-token-manager-builder
177+
$(CONTAINER_TOOL) buildx use github-token-manager-builder
186178
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
187-
- $(CONTAINER_TOOL) buildx rm project-v3-builder
179+
- $(CONTAINER_TOOL) buildx rm github-token-manager-builder
188180
rm Dockerfile.cross
189181

182+
.PHONY: build-installer
183+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
184+
mkdir -p dist
185+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
186+
$(KUSTOMIZE) build config/default > dist/install.yaml
187+
190188
.PHONY: ko-build
191189
ko-build: ## Build the manager image using ko.
192190
KO_DOCKER_REPO=$(IMAGE_TAG_BASE) \
@@ -213,10 +211,10 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
213211
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
214212

215213
.PHONY: undeploy
216-
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.
214+
undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
217215
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
218216

219-
##@ Build Dependencies
217+
##@ Dependencies
220218

221219
## Location to install dependencies to
222220
LOCALBIN ?= $(shell pwd)/bin
@@ -228,30 +226,49 @@ KUBECTL ?= kubectl
228226
KUSTOMIZE ?= $(LOCALBIN)/kustomize
229227
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
230228
ENVTEST ?= $(LOCALBIN)/setup-envtest
229+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
231230

232231
## Tool Versions
233232
KUSTOMIZE_VERSION ?= v5.5.0
234233
CONTROLLER_TOOLS_VERSION ?= v0.16.4
234+
ENVTEST_VERSION ?= release-0.18
235+
GOLANGCI_LINT_VERSION ?= v1.59.1
235236

236237
.PHONY: kustomize
237-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
238+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
238239
$(KUSTOMIZE): $(LOCALBIN)
239-
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
240-
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
241-
rm -rf $(LOCALBIN)/kustomize; \
242-
fi
243-
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
240+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
244241

245242
.PHONY: controller-gen
246-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
243+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
247244
$(CONTROLLER_GEN): $(LOCALBIN)
248-
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
249-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
245+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
250246

251247
.PHONY: envtest
252-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
248+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
253249
$(ENVTEST): $(LOCALBIN)
254-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
250+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
251+
252+
.PHONY: golangci-lint
253+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
254+
$(GOLANGCI_LINT): $(LOCALBIN)
255+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
256+
257+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
258+
# $1 - target path with name of binary
259+
# $2 - package url which can be installed
260+
# $3 - specific version of package
261+
define go-install-tool
262+
@[ -f "$(1)-$(3)" ] || { \
263+
set -e; \
264+
package=$(2)@$(3) ;\
265+
echo "Downloading $${package}" ;\
266+
rm -f $(1) || true ;\
267+
GOBIN=$(LOCALBIN) go install $${package} ;\
268+
mv $(1) $(1)-$(3) ;\
269+
} ;\
270+
ln -sf $(1)-$(3) $(1)
271+
endef
255272

256273
.PHONY: operator-sdk
257274
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
@@ -307,7 +324,7 @@ endif
307324
BUNDLE_IMGS ?= $(BUNDLE_IMG)
308325

309326
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
310-
CATALOG_IMG ?= $(IMAGE_TAG_BASE)/catalog:v$(VERSION)
327+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
311328

312329
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
313330
ifneq ($(origin CATALOG_BASE_IMG), undefined)

cmd/manager/main.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
ctrl "sigs.k8s.io/controller-runtime"
3232
"sigs.k8s.io/controller-runtime/pkg/healthz"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3435
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3536
"sigs.k8s.io/controller-runtime/pkg/webhook"
3637

@@ -57,13 +58,15 @@ func main() {
5758
var probeAddr string
5859
var secureMetrics bool
5960
var disableHTTP2 bool
60-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
61+
var tlsOpts []func(*tls.Config)
62+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
63+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
6164
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6265
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6366
"Enable leader election for controller manager. "+
6467
"Enabling this will ensure there is only one active controller manager.")
65-
flag.BoolVar(&secureMetrics, "metrics-secure", false,
66-
"If set the metrics endpoint is served securely")
68+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
69+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
6770
flag.BoolVar(&disableHTTP2, "disable-http2", false,
6871
"If set, HTTP/2 will be disabled for the metrics and webhook servers")
6972
opts := zap.Options{
@@ -74,8 +77,6 @@ func main() {
7477

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

77-
tlsOpts := []func(*tls.Config){}
78-
7980
if disableHTTP2 {
8081
forceHTTP11 := func(c *tls.Config) {
8182
setupLog.Info("disabling http/2")
@@ -88,13 +89,33 @@ func main() {
8889
TLSOpts: tlsOpts,
8990
})
9091

92+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
93+
// More info:
94+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server
95+
// - https://book.kubebuilder.io/reference/metrics.html
96+
metricsServerOptions := metricsserver.Options{
97+
BindAddress: metricsAddr,
98+
SecureServing: secureMetrics,
99+
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
100+
// not provided, self-signed certificates will be generated by default. This option is not recommended for
101+
// production environments as self-signed certificates do not offer the same level of trust and security
102+
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
103+
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
104+
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
105+
TLSOpts: tlsOpts,
106+
}
107+
108+
if secureMetrics {
109+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
110+
// These configurations ensure that only authorized users and service accounts
111+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
112+
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
113+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
114+
}
115+
91116
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
92-
Scheme: scheme,
93-
Metrics: metricsserver.Options{
94-
BindAddress: metricsAddr,
95-
SecureServing: secureMetrics,
96-
TLSOpts: tlsOpts,
97-
},
117+
Scheme: scheme,
118+
Metrics: metricsServerOptions,
98119
WebhookServer: webhookServer,
99120
HealthProbeBindAddress: probeAddr,
100121
LeaderElection: enableLeaderElection,

config/default/kustomization.yaml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ resources:
1818
- ../crd
1919
- ../rbac
2020
- ../manager
21-
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
22-
# crd/kustomization.yaml
23-
#- ../webhook
24-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
25-
#- ../certmanager
26-
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
27-
#- ../prometheus
21+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
22+
# crd/kustomization.yaml
23+
#- ../webhook
24+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
25+
#- ../certmanager
26+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
27+
#- ../prometheus
28+
# [METRICS] Expose the controller manager metrics service.
29+
- metrics_service.yaml
2830

31+
# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
2932
patches:
30-
# Protect the /metrics endpoint by putting it behind auth.
31-
# If you want your manager to expose the /metrics
32-
# endpoint w/o any authn/z, please comment the following line.
33-
- path: manager_auth_proxy_patch.yaml
33+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
34+
# More info: https://book.kubebuilder.io/reference/metrics
35+
- path: manager_metrics_patch.yaml
36+
target:
37+
kind: Deployment
3438
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3539
# crd/kustomization.yaml
3640
#- path: manager_webhook_patch.yaml

config/default/manager_auth_proxy_patch.yaml

Lines changed: 0 additions & 38 deletions
This file was deleted.

config/default/manager_config_patch.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This patch adds the args to allow exposing the metrics endpoint using HTTPS
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8443
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ kind: Service
33
metadata:
44
labels:
55
control-plane: manager
6-
app.kubernetes.io/name: service
7-
app.kubernetes.io/instance: manager-metrics-service
8-
app.kubernetes.io/component: kube-rbac-proxy
9-
app.kubernetes.io/created-by: github-token-manager
10-
app.kubernetes.io/part-of: github-token-manager
6+
app.kubernetes.io/name: github-token-manager
117
app.kubernetes.io/managed-by: kustomize
128
name: manager-metrics-service
139
namespace: system
@@ -16,6 +12,6 @@ spec:
1612
- name: https
1713
port: 8443
1814
protocol: TCP
19-
targetPort: https
15+
targetPort: 8443
2016
selector:
2117
control-plane: manager

config/manager/manager.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ spec:
5959
- /ko-app/manager
6060
args:
6161
- --leader-elect
62+
- --health-probe-bind-address=:8081
6263
image: controller:latest
6364
name: manager
6465
securityContext:

config/prometheus/monitor.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,19 @@ metadata:
1515
spec:
1616
endpoints:
1717
- path: /metrics
18-
port: https
18+
port: https # Ensure this is the name of the port that exposes HTTPS metrics
1919
scheme: https
2020
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
2121
tlsConfig:
22+
# TODO(user): The option insecureSkipVerify: true is not recommended for production since it disables
23+
# certificate verification. This poses a significant security risk by making the system vulnerable to
24+
# man-in-the-middle attacks, where an attacker could intercept and manipulate the communication between
25+
# Prometheus and the monitored services. This could lead to unauthorized access to sensitive metrics data,
26+
# compromising the integrity and confidentiality of the information.
27+
# Please use the following options for secure configurations:
28+
# caFile: /etc/metrics-certs/ca.crt
29+
# certFile: /etc/metrics-certs/tls.crt
30+
# keyFile: /etc/metrics-certs/tls.key
2231
insecureSkipVerify: true
2332
selector:
2433
matchLabels:

0 commit comments

Comments
 (0)