Skip to content

Commit d670c69

Browse files
committed
chore: Update Operator SDK from v1.39.2 to v1.41.1 (#199)
1 parent c3debc3 commit d670c69

File tree

107 files changed

+1219
-892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1219
-892
lines changed

.devcontainer/devcontainer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.24",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+

.devcontainer/post-install.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client

Makefile

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ help: ## Display this help
8787
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
8888
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=deploy-templates/crds
8989
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
90+
$(MAKE) api-docs
9091

9192
.PHONY: generate
9293
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
@@ -98,7 +99,7 @@ validate-docs: api-docs helm-docs ## Validate helm and api docs
9899
@git diff -s --exit-code docs/api.md || (echo " Run 'make api-docs' to address the issue." && git diff && exit 1)
99100

100101
# Run tests
101-
test: fmt vet envtest
102+
test: fmt vet setup-envtest
102103
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) --arch=amd64 use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" \
103104
TEST_KEYCLOAK_URL=${TEST_KEYCLOAK_URL} \
104105
go test ./... -coverprofile=coverage.out `go list ./...`
@@ -118,8 +119,16 @@ vet: ## Run go vet
118119
go vet ./...
119120

120121
.PHONY: lint
121-
lint: golangci-lint ## Run go lint
122-
${GOLANGCI_LINT} run -v -c .golangci.yaml ./...
122+
lint: golangci-lint ## Run golangci-lint linter
123+
$(GOLANGCI_LINT) run
124+
125+
.PHONY: lint-fix
126+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
127+
$(GOLANGCI_LINT) run --fix
128+
129+
.PHONY: lint-config
130+
lint-config: golangci-lint ## Verify golangci-lint linter configuration
131+
$(GOLANGCI_LINT) config verify
123132

124133
.PHONY: build
125134
build: ## build operator's binary
@@ -159,15 +168,15 @@ install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~
159168

160169
## Tool Versions
161170
KUSTOMIZE_VERSION ?= v5.4.3
162-
CONTROLLER_TOOLS_VERSION ?= v0.16.5
163-
ENVTEST_VERSION ?= release-0.19
171+
CONTROLLER_TOOLS_VERSION ?= v0.18.0
172+
ENVTEST_VERSION := $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
173+
ENVTEST_K8S_VERSION := $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
164174
GOLANGCI_LINT_VERSION ?= v1.64.7
165175
MOCKERY_VERSION ?= v2.53.2
166176
HELMDOCS_VERSION ?= v1.14.2
167177
GITCHGLOG_VERSION ?= v0.15.4
168178
CRDOC_VERSION ?= v0.6.4
169-
ENVTEST_K8S_VERSION = 1.31.0
170-
OPERATOR_SDK_VERSION ?= v1.39.2
179+
OPERATOR_SDK_VERSION ?= v1.41.1
171180

172181
KUSTOMIZE ?= $(LOCALBIN)/kustomize
173182
.PHONY: kustomize
@@ -218,11 +227,13 @@ bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metada
218227
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
219228
$(OPERATOR_SDK) bundle validate ./bundle
220229

221-
ENVTEST=$(LOCALBIN)/setup-envtest
222-
.PHONY: envtest
223-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
224-
$(ENVTEST): $(LOCALBIN)
225-
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
230+
.PHONY: setup-envtest
231+
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
232+
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."
233+
@$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path || { \
234+
echo "Error: Failed to set up envtest binaries for version $(ENVTEST_K8S_VERSION)."; \
235+
exit 1; \
236+
}
226237

227238
.PHONY: start-kind
228239
start-kind: ## Start kind cluster

cmd/main.go

Lines changed: 102 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"path/filepath"
89
"strconv"
910
"strings"
1011
"time"
@@ -19,13 +20,12 @@ import (
1920
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2021
ctrl "sigs.k8s.io/controller-runtime"
2122
"sigs.k8s.io/controller-runtime/pkg/cache"
23+
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
2224
"sigs.k8s.io/controller-runtime/pkg/healthz"
2325
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2426
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
2527
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
26-
ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
27-
28-
buildInfo "github.com/epam/edp-common/pkg/config"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook"
2929

3030
keycloakApi "github.com/epam/edp-keycloak-operator/api/v1"
3131
keycloakApi1alpha1 "github.com/epam/edp-keycloak-operator/api/v1alpha1"
@@ -57,19 +57,26 @@ const (
5757
keycloakOperatorLock = "edp-keycloak-operator-lock"
5858
successReconcileTimeout = "SUCCESS_RECONCILE_TIMEOUT"
5959
operatorNamespaceEnv = "OPERATOR_NAMESPACE"
60-
managerPort = 9443
6160
)
6261

63-
func main() {
64-
var (
65-
metricsAddr string
66-
probeAddr string
67-
enableLeaderElection bool
68-
secureMetrics bool
69-
enableHTTP2 bool
70-
tlsOpts []func(*tls.Config)
71-
)
62+
func init() {
63+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
64+
utilruntime.Must(keycloakApi.AddToScheme(scheme))
65+
utilruntime.Must(keycloakApi1alpha1.AddToScheme(scheme))
66+
utilruntime.Must(corev1.AddToScheme(scheme))
67+
// +kubebuilder:scaffold:scheme
68+
}
7269

70+
// nolint:gocyclo
71+
func main() {
72+
var metricsAddr string
73+
var metricsCertPath, metricsCertName, metricsCertKey string
74+
var webhookCertPath, webhookCertName, webhookCertKey string
75+
var enableLeaderElection bool
76+
var probeAddr string
77+
var secureMetrics bool
78+
var enableHTTP2 bool
79+
var tlsOpts []func(*tls.Config)
7380
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
7481
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
7582
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -78,29 +85,23 @@ func main() {
7885
"Enabling this will ensure there is only one active controller manager.")
7986
flag.BoolVar(&secureMetrics, "metrics-secure", true,
8087
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
88+
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
89+
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.")
90+
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
91+
flag.StringVar(&metricsCertPath, "metrics-cert-path", "",
92+
"The directory that contains the metrics server certificate.")
93+
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
94+
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
8195
flag.BoolVar(&enableHTTP2, "enable-http2", false,
8296
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
83-
8497
opts := zap.Options{
8598
Development: true,
8699
}
87-
88100
opts.BindFlags(flag.CommandLine)
89101
flag.Parse()
90102

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

93-
v := buildInfo.Get()
94-
95-
setupLog.Info("Starting the Keycloak Operator",
96-
"version", v.Version,
97-
"git-commit", v.GitCommit,
98-
"git-tag", v.GitTag,
99-
"build-date", v.BuildDate,
100-
"go-version", v.Go,
101-
"go-client", v.KubectlVersion,
102-
"platform", v.Platform,
103-
)
104105
// if the enable-http2 flag is false (the default), http/2 should be disabled
105106
// due to its vulnerabilities. More specifically, disabling http/2 will
106107
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
@@ -109,56 +110,94 @@ func main() {
109110
// - https://github.com/advisories/GHSA-4374-p667-p6c8
110111
disableHTTP2 := func(c *tls.Config) {
111112
setupLog.Info("disabling http/2")
112-
113113
c.NextProtos = []string{"http/1.1"}
114114
}
115115

116116
if !enableHTTP2 {
117117
tlsOpts = append(tlsOpts, disableHTTP2)
118118
}
119119

120-
webhookServer := ctrlwebhook.NewServer(ctrlwebhook.Options{
121-
TLSOpts: tlsOpts,
120+
// Create watchers for metrics and webhooks certificates
121+
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher
122+
123+
// Initial webhook TLS options
124+
webhookTLSOpts := tlsOpts
125+
126+
if len(webhookCertPath) > 0 {
127+
setupLog.Info("Initializing webhook certificate watcher using provided certificates",
128+
"webhook-cert-path", webhookCertPath, "webhook-cert-name", webhookCertName, "webhook-cert-key", webhookCertKey)
129+
130+
var err error
131+
webhookCertWatcher, err = certwatcher.New(
132+
filepath.Join(webhookCertPath, webhookCertName),
133+
filepath.Join(webhookCertPath, webhookCertKey),
134+
)
135+
if err != nil {
136+
setupLog.Error(err, "Failed to initialize webhook certificate watcher")
137+
os.Exit(1)
138+
}
139+
140+
webhookTLSOpts = append(webhookTLSOpts, func(config *tls.Config) {
141+
config.GetCertificate = webhookCertWatcher.GetCertificate
142+
})
143+
}
144+
145+
webhookServer := webhook.NewServer(webhook.Options{
146+
TLSOpts: webhookTLSOpts,
122147
})
123148

124149
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
125150
// More info:
126-
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/server
151+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/metrics/server
127152
// - https://book.kubebuilder.io/reference/metrics.html
128153
metricsServerOptions := metricsserver.Options{
129154
BindAddress: metricsAddr,
130155
SecureServing: secureMetrics,
131-
// TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
132-
// not provided, self-signed certificates will be generated by default. This option is not recommended for
133-
// production environments as self-signed certificates do not offer the same level of trust and security
134-
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
135-
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
136-
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
137-
TLSOpts: tlsOpts,
156+
TLSOpts: tlsOpts,
138157
}
139158

140159
if secureMetrics {
141160
// FilterProvider is used to protect the metrics endpoint with authn/authz.
142161
// These configurations ensure that only authorized users and service accounts
143162
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
144-
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
163+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
145164
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
146165
}
147166

148-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
149-
utilruntime.Must(keycloakApi.AddToScheme(scheme))
150-
utilruntime.Must(keycloakApi1alpha1.AddToScheme(scheme))
151-
utilruntime.Must(corev1.AddToScheme(scheme))
167+
// If the certificate is not specified, controller-runtime will automatically
168+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
169+
// this setup is not recommended for production.
170+
//
171+
// TODO(user): If you enable certManager, uncomment the following lines:
172+
// - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
173+
// managed by cert-manager for the metrics server.
174+
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
175+
if len(metricsCertPath) > 0 {
176+
setupLog.Info("Initializing metrics certificate watcher using provided certificates",
177+
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey)
178+
179+
var err error
180+
metricsCertWatcher, err = certwatcher.New(
181+
filepath.Join(metricsCertPath, metricsCertName),
182+
filepath.Join(metricsCertPath, metricsCertKey),
183+
)
184+
if err != nil {
185+
setupLog.Error(err, "to initialize metrics certificate watcher", "error", err)
186+
os.Exit(1)
187+
}
188+
189+
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) {
190+
config.GetCertificate = metricsCertWatcher.GetCertificate
191+
})
192+
}
152193

153194
ns, err := util.GetWatchNamespace()
154195
if err != nil {
155196
setupLog.Error(err, "unable to get watch namespace")
156197
os.Exit(1)
157198
}
158199

159-
cfg := ctrl.GetConfigOrDie()
160-
161-
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
200+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
162201
Scheme: scheme,
163202
Metrics: metricsServerOptions,
164203
WebhookServer: webhookServer,
@@ -275,6 +314,23 @@ func main() {
275314
}
276315
//+kubebuilder:scaffold:builder
277316

317+
318+
if metricsCertWatcher != nil {
319+
setupLog.Info("Adding metrics certificate watcher to manager")
320+
if err := mgr.Add(metricsCertWatcher); err != nil {
321+
setupLog.Error(err, "Unable to add metrics certificate watcher to manager")
322+
os.Exit(1)
323+
}
324+
}
325+
326+
if webhookCertWatcher != nil {
327+
setupLog.Info("Adding webhook certificate watcher to manager")
328+
if err := mgr.Add(webhookCertWatcher); err != nil {
329+
setupLog.Error(err, "Unable to add webhook certificate watcher to manager")
330+
os.Exit(1)
331+
}
332+
}
333+
278334
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
279335
setupLog.Error(err, "unable to set up health check")
280336
os.Exit(1)
@@ -285,7 +341,7 @@ func main() {
285341
os.Exit(1)
286342
}
287343

288-
setupLog.Info("starting manager")
344+
setupLog.Info("Starting manager")
289345

290346
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
291347
setupLog.Error(err, "problem running manager")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The following manifests contain a self-signed issuer CR and a metrics certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
apiVersion: cert-manager.io/v1
4+
kind: Certificate
5+
metadata:
6+
labels:
7+
app.kubernetes.io/name: keycloak-operator
8+
app.kubernetes.io/managed-by: kustomize
9+
name: metrics-certs # this name should match the one appeared in kustomizeconfig.yaml
10+
namespace: system
11+
spec:
12+
dnsNames:
13+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
14+
# replacements in the config/default/kustomization.yaml file.
15+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
16+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
17+
issuerRef:
18+
kind: Issuer
19+
name: selfsigned-issuer
20+
secretName: metrics-server-cert

0 commit comments

Comments
 (0)