Skip to content

Commit 004676e

Browse files
authored
Merge pull request #66 from datum-cloud/feat/useextras
feat: use project from extra
2 parents be909d8 + dedaf38 commit 004676e

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

internal/webhook/server.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package webhook
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
7-
"net/url"
86

97
"sigs.k8s.io/controller-runtime/pkg/webhook"
108
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -18,17 +16,14 @@ var _ webhook.Server = &clusterAwareWebhookServer{}
1816

1917
func (s *clusterAwareWebhookServer) Register(path string, hook http.Handler) {
2018
if h, ok := hook.(*admission.Webhook); ok {
21-
h.WithContextFunc = func(ctx context.Context, req *http.Request) context.Context {
22-
clusterName, err := url.QueryUnescape(req.PathValue("cluster_name"))
23-
if err != nil {
24-
return ctx
25-
}
26-
return WithClusterName(ctx, clusterName)
27-
}
19+
orig := h.Handler
20+
h.Handler = admission.HandlerFunc(func(ctx context.Context, req admission.Request) admission.Response {
21+
c := clusterFromExtra(req.UserInfo.Extra)
22+
ctx = WithClusterName(ctx, c)
23+
return orig.Handle(ctx, req)
24+
})
2825
}
2926

30-
path = fmt.Sprintf("/clusters/{cluster_name}%s", path)
31-
3227
s.Server.Register(path, hook)
3328
}
3429

internal/webhook/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package webhook
2+
3+
import authv1 "k8s.io/api/authentication/v1"
4+
5+
const (
6+
ParentNameExtraKey = "iam.miloapis.com/parent-name"
7+
)
8+
9+
func clusterFromExtra(extra map[string]authv1.ExtraValue) string {
10+
if v, ok := extra[ParentNameExtraKey]; ok && len(v) > 0 && v[0] != "" {
11+
return v[0]
12+
}
13+
return ""
14+
}

0 commit comments

Comments
 (0)