Skip to content

Commit e3df0e5

Browse files
authored
Merge pull request #4069 from camilamacedo86/update-doc-sample-multi
📖 Upgrade the muilt-version sample to the latest
2 parents bcaf71c + 56068ae commit e3df0e5

21 files changed

+271
-45
lines changed

docs/book/src/multiversion-tutorial/testdata/project/.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

docs/book/src/multiversion-tutorial/testdata/project/Makefile

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,16 @@ $(LOCALBIN):
152152

153153
## Tool Binaries
154154
KUBECTL ?= kubectl
155-
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
156-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
157-
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
158-
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
155+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
156+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
157+
ENVTEST ?= $(LOCALBIN)/setup-envtest
158+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
159159

160160
## Tool Versions
161-
KUSTOMIZE_VERSION ?= v5.4.1
161+
KUSTOMIZE_VERSION ?= v5.4.2
162162
CONTROLLER_TOOLS_VERSION ?= v0.15.0
163163
ENVTEST_VERSION ?= release-0.18
164-
GOLANGCI_LINT_VERSION ?= v1.57.2
164+
GOLANGCI_LINT_VERSION ?= v1.59.1
165165

166166
.PHONY: kustomize
167167
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -181,18 +181,20 @@ $(ENVTEST): $(LOCALBIN)
181181
.PHONY: golangci-lint
182182
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
183183
$(GOLANGCI_LINT): $(LOCALBIN)
184-
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
184+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
185185

186186
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
187-
# $1 - target path with name of binary (ideally with version)
187+
# $1 - target path with name of binary
188188
# $2 - package url which can be installed
189189
# $3 - specific version of package
190190
define go-install-tool
191-
@[ -f $(1) ] || { \
191+
@[ -f "$(1)-$(3)" ] || { \
192192
set -e; \
193193
package=$(2)@$(3) ;\
194194
echo "Downloading $${package}" ;\
195+
rm -f $(1) || true ;\
195196
GOBIN=$(LOCALBIN) go install $${package} ;\
196-
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
197-
}
197+
mv $(1) $(1)-$(3) ;\
198+
} ;\
199+
ln -sf $(1)-$(3) $(1)
198200
endef

docs/book/src/multiversion-tutorial/testdata/project/api/v1/webhook_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var _ = BeforeSuite(func() {
138138
})
139139

140140
var _ = AfterSuite(func() {
141-
cancel()
142141
By("tearing down the test environment")
142+
cancel()
143143
err := testEnv.Stop()
144144
Expect(err).NotTo(HaveOccurred())
145145
})

docs/book/src/multiversion-tutorial/testdata/project/api/v2/webhook_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var _ = BeforeSuite(func() {
138138
})
139139

140140
var _ = AfterSuite(func() {
141-
cancel()
142141
By("tearing down the test environment")
142+
cancel()
143143
err := testEnv.Stop()
144144
Expect(err).NotTo(HaveOccurred())
145145
})

docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go

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

@@ -71,14 +72,15 @@ func main() {
7172
var probeAddr string
7273
var secureMetrics bool
7374
var enableHTTP2 bool
74-
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
75-
"Use the port :8080. If not set, it will be 0 in order to disable the metrics server")
75+
var tlsOpts []func(*tls.Config)
76+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
77+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
7678
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
7779
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
7880
"Enable leader election for controller manager. "+
7981
"Enabling this will ensure there is only one active controller manager.")
80-
flag.BoolVar(&secureMetrics, "metrics-secure", false,
81-
"If set the metrics endpoint is served securely")
82+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
83+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
8284
flag.BoolVar(&enableHTTP2, "enable-http2", false,
8385
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
8486
opts := zap.Options{
@@ -100,7 +102,6 @@ func main() {
100102
c.NextProtos = []string{"http/1.1"}
101103
}
102104

103-
tlsOpts := []func(*tls.Config){}
104105
if !enableHTTP2 {
105106
tlsOpts = append(tlsOpts, disableHTTP2)
106107
}
@@ -109,13 +110,33 @@ func main() {
109110
TLSOpts: tlsOpts,
110111
})
111112

113+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
114+
// More info:
115+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/server
116+
// - https://book.kubebuilder.io/reference/metrics.html
117+
metricsServerOptions := metricsserver.Options{
118+
BindAddress: metricsAddr,
119+
SecureServing: secureMetrics,
120+
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
121+
// not provided, self-signed certificates will be generated by default. This option is not recommended for
122+
// production environments as self-signed certificates do not offer the same level of trust and security
123+
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
124+
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
125+
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
126+
TLSOpts: tlsOpts,
127+
}
128+
129+
if secureMetrics {
130+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
131+
// These configurations ensure that only authorized users and service accounts
132+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
133+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization
134+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
135+
}
136+
112137
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
113-
Scheme: scheme,
114-
Metrics: metricsserver.Options{
115-
BindAddress: metricsAddr,
116-
SecureServing: secureMetrics,
117-
TLSOpts: tlsOpts,
118-
},
138+
Scheme: scheme,
139+
Metrics: metricsServerOptions,
119140
WebhookServer: webhookServer,
120141
HealthProbeBindAddress: probeAddr,
121142
LeaderElection: enableLeaderElection,

docs/book/src/multiversion-tutorial/testdata/project/config/default/kustomization.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ resources:
2525
- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2727
- ../prometheus
28-
# [METRICS] To enable the controller manager metrics service, uncomment the following line.
29-
#- metrics_service.yaml
28+
# [METRICS] Expose the controller manager metrics service.
29+
- metrics_service.yaml
30+
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
31+
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
32+
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
33+
# be able to communicate with the Webhook Server.
34+
#- ../network-policy
3035

3136
# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
3237
patches:
33-
# [METRICS] The following patch will enable the metrics endpoint. Ensure that you also protect this endpoint.
38+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
3439
# More info: https://book.kubebuilder.io/reference/metrics
35-
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
36-
#- path: manager_metrics_patch.yaml
37-
# target:
38-
# kind: Deployment
40+
- path: manager_metrics_patch.yaml
41+
target:
42+
kind: Deployment
3943

4044
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
4145
# crd/kustomization.yaml
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This patch adds the args to allow exposing the metrics endpoint securely
1+
# This patch adds the args to allow exposing the metrics endpoint using HTTPS
22
- op: add
33
path: /spec/template/spec/containers/0/args/0
4-
value: --metrics-bind-address=:8080
4+
value: --metrics-bind-address=:8443

docs/book/src/multiversion-tutorial/testdata/project/config/default/manager_webhook_patch.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ kind: Deployment
33
metadata:
44
name: controller-manager
55
namespace: system
6+
labels:
7+
app.kubernetes.io/name: project
8+
app.kubernetes.io/managed-by: kustomize
69
spec:
710
template:
811
spec:

docs/book/src/multiversion-tutorial/testdata/project/config/default/metrics_service.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ metadata:
99
namespace: system
1010
spec:
1111
ports:
12-
- name: http
13-
port: 8080
12+
- name: https
13+
port: 8443
1414
protocol: TCP
15-
targetPort: 8080
15+
targetPort: 8443
1616
selector:
1717
control-plane: controller-manager
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This NetworkPolicy allows ingress traffic
2+
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
3+
# namespaces are able to gathering data from the metrics endpoint.
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: project
9+
app.kubernetes.io/managed-by: kustomize
10+
name: allow-metrics-traffic
11+
namespace: system
12+
spec:
13+
podSelector:
14+
matchLabels:
15+
control-plane: controller-manager
16+
policyTypes:
17+
- Ingress
18+
ingress:
19+
# This allows ingress traffic from any namespace with the label metrics: enabled
20+
- from:
21+
- namespaceSelector:
22+
matchLabels:
23+
metrics: enabled # Only from namespaces with this label
24+
ports:
25+
- port: 8443
26+
protocol: TCP

0 commit comments

Comments
 (0)