Skip to content

Commit 4a79ee6

Browse files
committed
Made job namespace a field of Webhook
1 parent 71ce0da commit 4a79ee6

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

pkg/webhook/webhook.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ const (
5050
)
5151

5252
type WebHook struct {
53-
clientset kubernetes.Interface
54-
server *http.Server
55-
cert *certBundle
56-
serviceRef *v1beta1.ServiceReference
53+
clientset kubernetes.Interface
54+
server *http.Server
55+
cert *certBundle
56+
serviceRef *v1beta1.ServiceReference
57+
sparkJobNamespace string
5758
}
5859

59-
var sparkJobNamespace string
60-
6160
func New(
6261
clientset kubernetes.Interface,
6362
certDir string,
@@ -71,13 +70,12 @@ func New(
7170
caCertFile: filepath.Join(certDir, caCertFile),
7271
}
7372
path := "/webhook"
74-
sparkJobNamespace = jobNamespace
7573
serviceRef := &v1beta1.ServiceReference{
7674
Namespace: webhookServiceNamespace,
7775
Name: webhookServiceName,
7876
Path: &path,
7977
}
80-
hook := &WebHook{clientset: clientset, cert: cert, serviceRef: serviceRef}
78+
hook := &WebHook{clientset: clientset, cert: cert, serviceRef: serviceRef, sparkJobNamespace: jobNamespace}
8179

8280
mux := http.NewServeMux()
8381
mux.HandleFunc(path, hook.serve)
@@ -151,7 +149,7 @@ func (wh *WebHook) serve(w http.ResponseWriter, r *http.Request) {
151149
glog.Error(err)
152150
reviewResponse = toAdmissionResponse(err)
153151
} else {
154-
reviewResponse = mutatePods(review)
152+
reviewResponse = mutatePods(review, wh.sparkJobNamespace)
155153
}
156154

157155
response := admissionv1beta1.AdmissionReview{}
@@ -237,7 +235,7 @@ func (wh *WebHook) selfDeregistration(webhookConfigName string) error {
237235
return client.Delete(webhookConfigName, metav1.NewDeleteOptions(0))
238236
}
239237

240-
func mutatePods(review *admissionv1beta1.AdmissionReview) *admissionv1beta1.AdmissionResponse {
238+
func mutatePods(review *admissionv1beta1.AdmissionReview, sparkJobNs string) *admissionv1beta1.AdmissionResponse {
241239
podResource := metav1.GroupVersionResource{
242240
Group: corev1.SchemeGroupVersion.Group,
243241
Version: corev1.SchemeGroupVersion.Version,
@@ -257,7 +255,7 @@ func mutatePods(review *admissionv1beta1.AdmissionReview) *admissionv1beta1.Admi
257255

258256
response := &admissionv1beta1.AdmissionResponse{Allowed: true}
259257

260-
if !isSparkPod(pod) || !inSparkJobNamespace(review.Request.Namespace) {
258+
if !isSparkPod(pod) || !inSparkJobNamespace(review.Request.Namespace, sparkJobNs) {
261259
glog.V(2).Info(pod.Name, " in namespace ", review.Request.Namespace, " not mutated")
262260
return response
263261
}
@@ -291,7 +289,7 @@ func toAdmissionResponse(err error) *admissionv1beta1.AdmissionResponse {
291289
}
292290
}
293291

294-
func inSparkJobNamespace(podNs string) bool {
292+
func inSparkJobNamespace(podNs string, sparkJobNamespace string) bool {
295293
if sparkJobNamespace == apiv1.NamespaceAll {
296294
return true
297295
}

0 commit comments

Comments
 (0)