Skip to content

Commit 86c5061

Browse files
committed
WIP: webhooks
1 parent fb467ab commit 86c5061

File tree

9 files changed

+2024
-3
lines changed

9 files changed

+2024
-3
lines changed

Diff for: mockkubeapiserver/apiserver.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ func NewMockKubeAPIServer(addr string, options ...Option) (*MockKubeAPIServer, e
5050
s.storage = storage
5151
}
5252

53+
s.hookSystem = hooks.NewHooks()
54+
5355
// These hooks mock behaviour that would otherwise require full controllers
5456
s.storage.AddStorageHook(&hooks.CRDHook{Storage: s.storage})
5557
s.storage.AddStorageHook(&hooks.NamespaceHook{})
5658
s.storage.AddStorageHook(&hooks.DeploymentHook{})
59+
s.storage.AddStorageHook(&hooks.WebhookHook{Webhooks: s.hookSystem})
5760

5861
return s, nil
5962
}
@@ -64,11 +67,12 @@ type MockKubeAPIServer struct {
6467

6568
storage storage.Storage
6669

67-
hooks []Hook
70+
httpHooks []Hook
71+
hookSystem *hooks.Webhooks
6872
}
6973

7074
func (s *MockKubeAPIServer) AddHook(hook Hook) {
71-
s.hooks = append(s.hooks, hook)
75+
s.httpHooks = append(s.httpHooks, hook)
7276
}
7377

7478
func (s *MockKubeAPIServer) StartServing() (net.Addr, error) {
@@ -95,7 +99,7 @@ func (s *MockKubeAPIServer) Stop() error {
9599
func (s *MockKubeAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
96100
ctx := r.Context()
97101

98-
for _, hook := range s.hooks {
102+
for _, hook := range s.httpHooks {
99103
op := &HTTPOperation{
100104
Request: r,
101105
}

Diff for: mockkubeapiserver/hooks/interceptwebhooks.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
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 hooks
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/runtime"
21+
"k8s.io/apimachinery/pkg/runtime/schema"
22+
"k8s.io/klog/v2"
23+
admissionregistrationv1 "sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver/internal/api/admissionregistration/v1"
24+
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver/storage"
25+
)
26+
27+
type WebhookHook struct {
28+
Webhooks *Webhooks
29+
}
30+
31+
func (s *WebhookHook) OnWatchEvent(ev *storage.WatchEvent) {
32+
switch ev.GroupKind() {
33+
case schema.GroupKind{Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration"}:
34+
35+
// // TODO: Deleted / changed webhooks
36+
37+
u := ev.Unstructured()
38+
39+
webhook := &admissionregistrationv1.MutatingWebhookConfiguration{}
40+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, webhook); err != nil {
41+
klog.Fatalf("failed to parse webhook: %v", err)
42+
}
43+
44+
if err := s.Webhooks.update(webhook); err != nil {
45+
klog.Fatalf("failed to update webhook: %v", err)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)