Skip to content

Commit 86424d4

Browse files
zmotsoSergK
authored andcommitted
chore: Update Operator SDK from v1.39.2 to v1.41.1 (#199)
1 parent c3debc3 commit 86424d4

File tree

108 files changed

+1228
-873
lines changed

Some content is hidden

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

108 files changed

+1228
-873
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

.golangci.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,6 @@ linters:
756756
- decorder
757757
- forcetypeassert
758758
- funlen
759-
- gochecknoinits
760759
- wrapcheck
761760
- wsl
762761
- unused

Makefile

Lines changed: 27 additions & 10 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
@@ -158,16 +167,16 @@ install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~
158167
##@ Build Dependencies
159168

160169
## Tool Versions
161-
KUSTOMIZE_VERSION ?= v5.4.3
162-
CONTROLLER_TOOLS_VERSION ?= v0.16.5
163-
ENVTEST_VERSION ?= release-0.19
170+
KUSTOMIZE_VERSION ?= v5.6.0
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,9 +227,17 @@ 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
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+
}
237+
238+
ENVTEST ?= $(LOCALBIN)/setup-envtest
222239
.PHONY: envtest
223-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
240+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
224241
$(ENVTEST): $(LOCALBIN)
225242
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
226243

cmd/main.go

Lines changed: 106 additions & 28 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,11 +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"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook"
2729

2830
buildInfo "github.com/epam/edp-common/pkg/config"
2931

@@ -57,17 +59,27 @@ const (
5759
keycloakOperatorLock = "edp-keycloak-operator-lock"
5860
successReconcileTimeout = "SUCCESS_RECONCILE_TIMEOUT"
5961
operatorNamespaceEnv = "OPERATOR_NAMESPACE"
60-
managerPort = 9443
6162
)
6263

64+
func init() {
65+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
66+
utilruntime.Must(keycloakApi.AddToScheme(scheme))
67+
utilruntime.Must(keycloakApi1alpha1.AddToScheme(scheme))
68+
utilruntime.Must(corev1.AddToScheme(scheme))
69+
// +kubebuilder:scaffold:scheme
70+
}
71+
72+
// nolint:gocyclo
6373
func main() {
6474
var (
65-
metricsAddr string
66-
probeAddr string
67-
enableLeaderElection bool
68-
secureMetrics bool
69-
enableHTTP2 bool
70-
tlsOpts []func(*tls.Config)
75+
metricsAddr string
76+
metricsCertPath, metricsCertName, metricsCertKey string
77+
webhookCertPath, webhookCertName, webhookCertKey string
78+
enableLeaderElection bool
79+
probeAddr string
80+
secureMetrics bool
81+
enableHTTP2 bool
82+
tlsOpts []func(*tls.Config)
7183
)
7284

7385
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
@@ -78,13 +90,19 @@ func main() {
7890
"Enabling this will ensure there is only one active controller manager.")
7991
flag.BoolVar(&secureMetrics, "metrics-secure", true,
8092
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
93+
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
94+
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.")
95+
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
96+
flag.StringVar(&metricsCertPath, "metrics-cert-path", "",
97+
"The directory that contains the metrics server certificate.")
98+
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
99+
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
81100
flag.BoolVar(&enableHTTP2, "enable-http2", false,
82101
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
83102

84103
opts := zap.Options{
85104
Development: true,
86105
}
87-
88106
opts.BindFlags(flag.CommandLine)
89107
flag.Parse()
90108

@@ -101,6 +119,7 @@ func main() {
101119
"go-client", v.KubectlVersion,
102120
"platform", v.Platform,
103121
)
122+
104123
// if the enable-http2 flag is false (the default), http/2 should be disabled
105124
// due to its vulnerabilities. More specifically, disabling http/2 will
106125
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
@@ -117,48 +136,89 @@ func main() {
117136
tlsOpts = append(tlsOpts, disableHTTP2)
118137
}
119138

120-
webhookServer := ctrlwebhook.NewServer(ctrlwebhook.Options{
121-
TLSOpts: tlsOpts,
139+
// Create watchers for metrics and webhooks certificates
140+
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher
141+
142+
// Initial webhook TLS options
143+
webhookTLSOpts := tlsOpts
144+
145+
if len(webhookCertPath) > 0 {
146+
setupLog.Info("Initializing webhook certificate watcher using provided certificates",
147+
"webhook-cert-path", webhookCertPath, "webhook-cert-name", webhookCertName, "webhook-cert-key", webhookCertKey)
148+
149+
var err error
150+
151+
webhookCertWatcher, err = certwatcher.New(
152+
filepath.Join(webhookCertPath, webhookCertName),
153+
filepath.Join(webhookCertPath, webhookCertKey),
154+
)
155+
if err != nil {
156+
setupLog.Error(err, "Failed to initialize webhook certificate watcher")
157+
os.Exit(1)
158+
}
159+
160+
webhookTLSOpts = append(webhookTLSOpts, func(config *tls.Config) {
161+
config.GetCertificate = webhookCertWatcher.GetCertificate
162+
})
163+
}
164+
165+
webhookServer := webhook.NewServer(webhook.Options{
166+
TLSOpts: webhookTLSOpts,
122167
})
123168

124169
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
125170
// More info:
126-
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/server
171+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/metrics/server
127172
// - https://book.kubebuilder.io/reference/metrics.html
128173
metricsServerOptions := metricsserver.Options{
129174
BindAddress: metricsAddr,
130175
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,
176+
TLSOpts: tlsOpts,
138177
}
139178

140179
if secureMetrics {
141180
// FilterProvider is used to protect the metrics endpoint with authn/authz.
142181
// These configurations ensure that only authorized users and service accounts
143182
// 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
183+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
145184
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
146185
}
147186

148-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
149-
utilruntime.Must(keycloakApi.AddToScheme(scheme))
150-
utilruntime.Must(keycloakApi1alpha1.AddToScheme(scheme))
151-
utilruntime.Must(corev1.AddToScheme(scheme))
187+
// If the certificate is not specified, controller-runtime will automatically
188+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
189+
// this setup is not recommended for production.
190+
//
191+
// TODO(user): If you enable certManager, uncomment the following lines:
192+
// - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
193+
// managed by cert-manager for the metrics server.
194+
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
195+
if len(metricsCertPath) > 0 {
196+
setupLog.Info("Initializing metrics certificate watcher using provided certificates",
197+
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey)
198+
199+
var err error
200+
201+
metricsCertWatcher, err = certwatcher.New(
202+
filepath.Join(metricsCertPath, metricsCertName),
203+
filepath.Join(metricsCertPath, metricsCertKey),
204+
)
205+
if err != nil {
206+
setupLog.Error(err, "to initialize metrics certificate watcher", "error", err)
207+
os.Exit(1)
208+
}
209+
210+
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) {
211+
config.GetCertificate = metricsCertWatcher.GetCertificate
212+
})
213+
}
152214

153215
ns, err := util.GetWatchNamespace()
154216
if err != nil {
155217
setupLog.Error(err, "unable to get watch namespace")
156218
os.Exit(1)
157219
}
158220

159-
cfg := ctrl.GetConfigOrDie()
160-
161-
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
221+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
162222
Scheme: scheme,
163223
Metrics: metricsServerOptions,
164224
WebhookServer: webhookServer,
@@ -275,6 +335,24 @@ func main() {
275335
}
276336
//+kubebuilder:scaffold:builder
277337

338+
if metricsCertWatcher != nil {
339+
setupLog.Info("Adding metrics certificate watcher to manager")
340+
341+
if err := mgr.Add(metricsCertWatcher); err != nil {
342+
setupLog.Error(err, "Unable to add metrics certificate watcher to manager")
343+
os.Exit(1)
344+
}
345+
}
346+
347+
if webhookCertWatcher != nil {
348+
setupLog.Info("Adding webhook certificate watcher to manager")
349+
350+
if err := mgr.Add(webhookCertWatcher); err != nil {
351+
setupLog.Error(err, "Unable to add webhook certificate watcher to manager")
352+
os.Exit(1)
353+
}
354+
}
355+
278356
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
279357
setupLog.Error(err, "unable to set up health check")
280358
os.Exit(1)
@@ -285,7 +363,7 @@ func main() {
285363
os.Exit(1)
286364
}
287365

288-
setupLog.Info("starting manager")
366+
setupLog.Info("Starting manager")
289367

290368
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
291369
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

config/certmanager/issuer.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The following manifest contains a self-signed issuer CR.
2+
# More information can be found at https://docs.cert-manager.io
3+
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes.
4+
apiVersion: cert-manager.io/v1
5+
kind: Issuer
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: keycloak-operator
9+
app.kubernetes.io/managed-by: kustomize
10+
name: selfsigned-issuer
11+
namespace: system
12+
spec:
13+
selfSigned: {}

0 commit comments

Comments
 (0)