Skip to content

Commit 56f1e64

Browse files
committed
Update
1 parent 3c5d0d8 commit 56f1e64

4 files changed

Lines changed: 111 additions & 9 deletions

File tree

helm/dataflow-operator/templates/clusterrole.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ metadata:
66
labels:
77
{{- include "dataflow-operator.labels" . | nindent 4 }}
88
rules:
9+
# Leader election: оператор использует Lease для выбора лидера (coordination.k8s.io)
10+
- apiGroups:
11+
- coordination.k8s.io
12+
resources:
13+
- leases
14+
verbs:
15+
- create
16+
- get
17+
- list
18+
- update
19+
- patch
20+
- watch
921
# EventRecorder: создание событий для DataFlow (kubectl describe dataflow, UI событий)
1022
- apiGroups:
1123
- ""

internal/webhookenv/webhookenv.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhookenv
18+
19+
import (
20+
"os"
21+
"strings"
22+
)
23+
24+
// GetWebhookCertDir returns WEBHOOK_CERT_DIR when webhook is enabled (certs mounted). Empty means webhook disabled.
25+
func GetWebhookCertDir() string {
26+
return strings.TrimSpace(os.Getenv("WEBHOOK_CERT_DIR"))
27+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhookenv
18+
19+
import (
20+
"os"
21+
"testing"
22+
)
23+
24+
const envKey = "WEBHOOK_CERT_DIR"
25+
26+
func TestGetWebhookCertDir(t *testing.T) {
27+
restore := os.Getenv(envKey)
28+
defer func() {
29+
if restore == "" {
30+
os.Unsetenv(envKey)
31+
} else {
32+
_ = os.Setenv(envKey, restore)
33+
}
34+
}()
35+
36+
t.Run("unset", func(t *testing.T) {
37+
os.Unsetenv(envKey)
38+
if got := GetWebhookCertDir(); got != "" {
39+
t.Errorf("GetWebhookCertDir() = %q, want empty", got)
40+
}
41+
})
42+
43+
t.Run("set", func(t *testing.T) {
44+
want := "/tmp/k8s-webhook-server/serving-certs"
45+
_ = os.Setenv(envKey, want)
46+
if got := GetWebhookCertDir(); got != want {
47+
t.Errorf("GetWebhookCertDir() = %q, want %q", got, want)
48+
}
49+
})
50+
51+
t.Run("trimmed", func(t *testing.T) {
52+
_ = os.Setenv(envKey, " /path/to/certs ")
53+
if got := GetWebhookCertDir(); got != "/path/to/certs" {
54+
t.Errorf("GetWebhookCertDir() = %q, want /path/to/certs", got)
55+
}
56+
})
57+
}

main.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
dataflowv1 "github.com/dataflow-operator/dataflow/api/v1"
4242
"github.com/dataflow-operator/dataflow/internal/controller"
4343
_ "github.com/dataflow-operator/dataflow/internal/metrics" // Импортируем для регистрации метрик
44+
"github.com/dataflow-operator/dataflow/internal/webhookenv"
4445
//+kubebuilder:scaffold:imports
4546
)
4647

@@ -131,20 +132,21 @@ func main() {
131132
ctrl.SetLogger(zaprctrl.New(zapOpts...))
132133
}
133134

134-
webhookOpts := webhook.Options{Port: 9443}
135-
if certDir := os.Getenv("WEBHOOK_CERT_DIR"); certDir != "" {
136-
webhookOpts.CertDir = certDir
137-
}
138-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
135+
// Webhook server включается только при заданном WEBHOOK_CERT_DIR (в e2e и при отключённом webhook в Helm сертификаты не монтируются).
136+
certDir := webhookenv.GetWebhookCertDir()
137+
mgrOpts := ctrl.Options{
139138
Scheme: scheme,
140139
Metrics: metricsserver.Options{
141140
BindAddress: metricsAddr,
142141
},
143-
WebhookServer: webhook.NewServer(webhookOpts),
144142
HealthProbeBindAddress: probeAddr,
145143
LeaderElection: true, // Always HA-ready: only one active controller across replicas
146144
LeaderElectionID: "dataflow-operator.dataflow.io",
147-
})
145+
}
146+
if certDir != "" {
147+
mgrOpts.WebhookServer = webhook.NewServer(webhook.Options{Port: 9443, CertDir: certDir})
148+
}
149+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), mgrOpts)
148150
if err != nil {
149151
setupLog.Error(err, "unable to start manager")
150152
os.Exit(1)
@@ -154,8 +156,12 @@ func main() {
154156
setupLog.Error(err, "unable to create controller", "controller", "DataFlow")
155157
os.Exit(1)
156158
}
157-
validator := admission.WithCustomValidator(mgr.GetScheme(), &dataflowv1.DataFlow{}, &dataflowv1.DataFlow{})
158-
mgr.GetWebhookServer().Register("/validate-dataflow-dataflow-io-v1-dataflow", validator)
159+
if certDir != "" {
160+
validator := admission.WithCustomValidator(mgr.GetScheme(), &dataflowv1.DataFlow{}, &dataflowv1.DataFlow{})
161+
mgr.GetWebhookServer().Register("/validate-dataflow-dataflow-io-v1-dataflow", validator)
162+
} else {
163+
setupLog.Info("webhook disabled (WEBHOOK_CERT_DIR not set), skipping validator registration")
164+
}
159165
//+kubebuilder:scaffold:builder
160166

161167
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

0 commit comments

Comments
 (0)