-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add webhook support for hub-agent (#224)
* feat: Add webhook support for hub-agent * fix review comments * Add a missing err handling * fix lint error Co-authored-by: guofei <[email protected]>
- Loading branch information
Showing
7 changed files
with
426 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# We use a headless service for webhook assuming the apiserver's dns can resolve it. | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
{{- include "hub-agent.labels" . | nindent 4 }} | ||
name: fleetwebhook | ||
namespace: {{ .Values.namespace }} | ||
spec: | ||
clusterIP: None | ||
clusterIPs: | ||
- None | ||
ipFamilies: | ||
- IPv4 | ||
ipFamilyPolicy: SingleStack | ||
ports: | ||
- name: client | ||
port: 9443 | ||
protocol: TCP | ||
targetPort: 9443 | ||
selector: | ||
{{- include "hub-agent.selectorLabels" . | nindent 4 }} | ||
sessionAffinity: None | ||
type: ClusterIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ image: | |
|
||
logVerbosity: 2 | ||
|
||
enableWebhook: false | ||
|
||
namespace: | ||
fleet-system | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
package webhook | ||
|
||
import ( | ||
"go.goms.io/fleet/pkg/webhook/pod" | ||
) | ||
|
||
func init() { | ||
AddToManagerFuncs = append(AddToManagerFuncs, pod.Add) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
package pod | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// Add registers the webhook for K8s bulit-in object types. | ||
func Add(mgr manager.Manager) error { | ||
hookServer := mgr.GetWebhookServer() | ||
hookServer.Register("/validate-v1-pod", &webhook.Admission{Handler: &podValidator{Client: mgr.GetClient()}}) | ||
return nil | ||
} | ||
|
||
type podValidator struct { | ||
Client client.Client | ||
decoder *admission.Decoder | ||
} | ||
|
||
// podValidator denies a pod if it is not created in the system namespaces. | ||
func (v *podValidator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
if req.Operation == admissionv1.Create { | ||
pod := &corev1.Pod{} | ||
err := v.decoder.Decode(req, pod) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
if pod.Namespace != "kube-system" && pod.Namespace != "fleet-system" { | ||
return admission.Denied(fmt.Sprintf("Pod %s/%s creation is disallowed in the fleet hub cluster", pod.Namespace, pod.Name)) | ||
} | ||
} | ||
return admission.Allowed("") | ||
} | ||
|
||
// InjectDecoder injects the decoder. | ||
func (v *podValidator) InjectDecoder(d *admission.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} |
Oops, something went wrong.