Skip to content

Commit 1d757ea

Browse files
wangyuqing (C)wangyuqing4
wangyuqing (C)
authored andcommitted
add admitPod
1 parent 11b5ad3 commit 1d757ea

23 files changed

+618
-158
lines changed

cmd/admission/app/configure/configure.go

+42
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Config struct {
4444
PrintVersion bool
4545
AdmissionServiceName string
4646
AdmissionServiceNamespace string
47+
SchedulerName string
4748
}
4849

4950
// NewConfig create new config
@@ -73,6 +74,7 @@ func (c *Config) AddFlags() {
7374
flag.BoolVar(&c.PrintVersion, "version", false, "Show version and quit")
7475
flag.StringVar(&c.AdmissionServiceNamespace, "webhook-namespace", "default", "The namespace of this webhook")
7576
flag.StringVar(&c.AdmissionServiceName, "webhook-service-name", "admission-service", "The name of this admission service")
77+
flag.StringVar(&c.SchedulerName, "scheduler-name", "volcano", "Volcano will handle pods whose .spec.SchedulerName is same as scheduler-name")
7678
}
7779

7880
const (
@@ -84,6 +86,10 @@ const (
8486
ValidateHookName = "validatejob.volcano.sh"
8587
// MutateHookName Default name for webhooks in MutatingWebhookConfiguration
8688
MutateHookName = "mutatejob.volcano.sh"
89+
// ValidatePodConfigName ValidatingWebhookPodConfiguration name format
90+
ValidatePodConfigName = "%s-validate-pod"
91+
// ValidatePodHookName Default name for webhooks in ValidatingWebhookPodConfiguration
92+
ValidatePodHookName = "validatepod.volcano.sh"
8793
)
8894

8995
// CheckPortOrDie check valid port range
@@ -177,6 +183,42 @@ func RegisterWebhooks(c *Config, clienset *kubernetes.Clientset, cabundle []byte
177183
return err
178184
}
179185

186+
// Prepare validate pods
187+
path = "/pods"
188+
PodValidateHooks := v1beta1.ValidatingWebhookConfiguration{
189+
ObjectMeta: metav1.ObjectMeta{
190+
Name: useGeneratedNameIfRequired("",
191+
fmt.Sprintf(ValidatePodConfigName, c.AdmissionServiceName)),
192+
},
193+
Webhooks: []v1beta1.Webhook{{
194+
Name: useGeneratedNameIfRequired("", ValidatePodHookName),
195+
Rules: []v1beta1.RuleWithOperations{
196+
{
197+
Operations: []v1beta1.OperationType{v1beta1.Create},
198+
Rule: v1beta1.Rule{
199+
APIGroups: []string{""},
200+
APIVersions: []string{"v1"},
201+
Resources: []string{"pods"},
202+
},
203+
},
204+
},
205+
ClientConfig: v1beta1.WebhookClientConfig{
206+
Service: &v1beta1.ServiceReference{
207+
Name: c.AdmissionServiceName,
208+
Namespace: c.AdmissionServiceNamespace,
209+
Path: &path,
210+
},
211+
CABundle: cabundle,
212+
},
213+
FailurePolicy: &ignorePolicy,
214+
}},
215+
}
216+
217+
if err := registerValidateWebhook(clienset.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations(),
218+
[]v1beta1.ValidatingWebhookConfiguration{PodValidateHooks}); err != nil {
219+
return err
220+
}
221+
180222
return nil
181223

182224
}

cmd/admission/app/server.go

+2-66
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,13 @@ package app
1818

1919
import (
2020
"crypto/tls"
21-
"encoding/json"
22-
"io/ioutil"
23-
"net/http"
2421

2522
"github.com/golang/glog"
26-
"volcano.sh/volcano/pkg/client/clientset/versioned"
2723

28-
"k8s.io/api/admission/v1beta1"
29-
"k8s.io/apimachinery/pkg/runtime"
3024
"k8s.io/client-go/kubernetes"
3125
restclient "k8s.io/client-go/rest"
32-
3326
appConf "volcano.sh/volcano/cmd/admission/app/configure"
34-
admissioncontroller "volcano.sh/volcano/pkg/admission"
35-
)
36-
37-
const (
38-
//CONTENTTYPE http content-type
39-
CONTENTTYPE = "Content-Type"
40-
41-
//APPLICATIONJSON json content
42-
APPLICATIONJSON = "application/json"
27+
"volcano.sh/volcano/pkg/client/clientset/versioned"
4328
)
4429

4530
// GetClient Get a clientset with restConfig.
@@ -51,7 +36,7 @@ func GetClient(restConfig *restclient.Config) *kubernetes.Clientset {
5136
return clientset
5237
}
5338

54-
//GetKubeBatchClient get a clientset for kubebatch
39+
// GetKubeBatchClient get a clientset for volcano
5540
func GetKubeBatchClient(restConfig *restclient.Config) *versioned.Clientset {
5641
clientset, err := versioned.NewForConfig(restConfig)
5742
if err != nil {
@@ -89,52 +74,3 @@ func ConfigTLS(config *appConf.Config, restConfig *restclient.Config) *tls.Confi
8974
glog.Fatal("tls: failed to find any tls config data")
9075
return &tls.Config{}
9176
}
92-
93-
//Serve the http request
94-
func Serve(w http.ResponseWriter, r *http.Request, admit admissioncontroller.AdmitFunc) {
95-
var body []byte
96-
if r.Body != nil {
97-
if data, err := ioutil.ReadAll(r.Body); err == nil {
98-
body = data
99-
}
100-
}
101-
102-
// verify the content type is accurate
103-
contentType := r.Header.Get(CONTENTTYPE)
104-
if contentType != APPLICATIONJSON {
105-
glog.Errorf("contentType=%s, expect application/json", contentType)
106-
return
107-
}
108-
109-
var reviewResponse *v1beta1.AdmissionResponse
110-
ar := v1beta1.AdmissionReview{}
111-
deserializer := admissioncontroller.Codecs.UniversalDeserializer()
112-
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
113-
reviewResponse = admissioncontroller.ToAdmissionResponse(err)
114-
} else {
115-
reviewResponse = admit(ar)
116-
}
117-
glog.V(3).Infof("sending response: %v", reviewResponse)
118-
119-
response := createResponse(reviewResponse, &ar)
120-
resp, err := json.Marshal(response)
121-
if err != nil {
122-
glog.Error(err)
123-
}
124-
if _, err := w.Write(resp); err != nil {
125-
glog.Error(err)
126-
}
127-
}
128-
129-
func createResponse(reviewResponse *v1beta1.AdmissionResponse, ar *v1beta1.AdmissionReview) v1beta1.AdmissionReview {
130-
response := v1beta1.AdmissionReview{}
131-
if reviewResponse != nil {
132-
response.Response = reviewResponse
133-
response.Response.UID = ar.Request.UID
134-
}
135-
// reset the Object and OldObject, they are not needed in a response.
136-
ar.Request.Object = runtime.RawExtension{}
137-
ar.Request.OldObject = runtime.RawExtension{}
138-
139-
return response
140-
}

cmd/admission/main.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ package main
1717

1818
import (
1919
"flag"
20-
"github.com/golang/glog"
2120
"io/ioutil"
2221
"net/http"
2322
"os"
2423
"os/signal"
2524
"strconv"
2625
"syscall"
2726

27+
"github.com/golang/glog"
28+
2829
"k8s.io/client-go/tools/clientcmd"
2930

3031
"volcano.sh/volcano/cmd/admission/app"
@@ -34,11 +35,11 @@ import (
3435
)
3536

3637
func serveJobs(w http.ResponseWriter, r *http.Request) {
37-
app.Serve(w, r, admissioncontroller.AdmitJobs)
38+
admissioncontroller.Serve(w, r, admissioncontroller.AdmitJobs)
3839
}
3940

4041
func serveMutateJobs(w http.ResponseWriter, r *http.Request) {
41-
app.Serve(w, r, admissioncontroller.MutateJobs)
42+
admissioncontroller.Serve(w, r, admissioncontroller.MutateJobs)
4243
}
4344

4445
func main() {
@@ -65,6 +66,8 @@ func main() {
6566

6667
admissioncontroller.KubeBatchClientSet = app.GetKubeBatchClient(restConfig)
6768

69+
servePods(config)
70+
6871
caBundle, err := ioutil.ReadFile(config.CaCertFile)
6972
if err != nil {
7073
glog.Fatalf("Unable to read cacert file: %v\n", err)
@@ -101,3 +104,13 @@ func main() {
101104
return
102105
}
103106
}
107+
108+
func servePods(config *appConf.Config) {
109+
admController := &admissioncontroller.Controller{
110+
KbClients: admissioncontroller.KubeBatchClientSet,
111+
SchedulerName: config.SchedulerName,
112+
}
113+
http.HandleFunc(admissioncontroller.AdmitPodPath, admController.ServerPods)
114+
115+
return
116+
}

cmd/controllers/app/options/options.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
)
2424

2525
const (
26-
defaultQPS = 50.0
27-
defaultBurst = 100
28-
defaultWorkers = 3
26+
defaultQPS = 50.0
27+
defaultBurst = 100
28+
defaultWorkers = 3
29+
defaultSchedulerName = "volcano"
2930
)
3031

3132
// ServerOption is the main context object for the controller manager.
@@ -40,6 +41,7 @@ type ServerOption struct {
4041
// WorkerThreads is the number of threads syncing job operations
4142
// concurrently. Larger number = faster job updating,but more CPU load.
4243
WorkerThreads uint32
44+
SchedulerName string
4345
}
4446

4547
// NewServerOption creates a new CMServer with a default config.
@@ -60,6 +62,7 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {
6062
fs.BoolVar(&s.PrintVersion, "version", false, "Show version and quit")
6163
fs.Uint32Var(&s.WorkerThreads, "worker-threads", defaultWorkers, "The number of threads syncing job operations concurrently. "+
6264
"Larger number = faster job updating, but more CPU load")
65+
fs.StringVar(&s.SchedulerName, "scheduler-name", defaultSchedulerName, "Volcano will handle pods whose .spec.SchedulerName is same as scheduler-name")
6366
}
6467

6568
// CheckOptionOrDie checks the LockObjectNamespace

cmd/controllers/app/options/options_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestAddFlags(t *testing.T) {
4040
KubeAPIBurst: 200,
4141
PrintVersion: false,
4242
WorkerThreads: defaultWorkers,
43+
SchedulerName: defaultSchedulerName,
4344
}
4445

4546
if !reflect.DeepEqual(expected, s) {

cmd/controllers/app/server.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import (
3838
"k8s.io/client-go/tools/leaderelection/resourcelock"
3939
"k8s.io/client-go/tools/record"
4040

41-
kbver "volcano.sh/volcano/pkg/client/clientset/versioned"
42-
4341
"volcano.sh/volcano/cmd/controllers/app/options"
4442
vkclient "volcano.sh/volcano/pkg/client/clientset/versioned"
4543
"volcano.sh/volcano/pkg/controllers/garbagecollector"
@@ -82,11 +80,10 @@ func Run(opt *options.ServerOption) error {
8280

8381
// TODO: add user agent for different controllers
8482
kubeClient := clientset.NewForConfigOrDie(config)
85-
kbClient := kbver.NewForConfigOrDie(config)
8683
vkClient := vkclient.NewForConfigOrDie(config)
8784

88-
jobController := job.NewJobController(kubeClient, kbClient, vkClient, opt.WorkerThreads)
89-
queueController := queue.NewQueueController(kubeClient, kbClient)
85+
jobController := job.NewJobController(kubeClient, vkClient, opt.WorkerThreads)
86+
queueController := queue.NewQueueController(kubeClient, vkClient)
9087
garbageCollector := garbagecollector.New(vkClient)
9188

9289
run := func(ctx context.Context) {

hack/e2e-admission-config.yaml

-44
This file was deleted.

installer/helm/chart/volcano/templates/admission.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ rules:
3232
- apiGroups: [""]
3333
resources: ["services"]
3434
verbs: ["get"]
35+
- apiGroups: ["scheduling.incubator.k8s.io"]
36+
resources: ["podgroups"]
37+
verbs: ["get", "list", "watch"]
3538

3639
---
3740
kind: ClusterRoleBinding

pkg/admission/admission_controller.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,31 @@ import (
3131
"k8s.io/apimachinery/pkg/util/validation/field"
3232

3333
"volcano.sh/volcano/pkg/apis/batch/v1alpha1"
34+
kbver "volcano.sh/volcano/pkg/client/clientset/versioned"
3435
)
3536

3637
const (
37-
//AdmitJobPath is the pattern for the jobs admission
38+
// AdmitJobPath is the pattern for the jobs admission
3839
AdmitJobPath = "/jobs"
39-
//MutateJobPath is the pattern for the mutating jobs
40+
// MutateJobPath is the pattern for the mutating jobs
4041
MutateJobPath = "/mutating-jobs"
42+
// AdmitPodPath is the pattern for the pods admission
43+
AdmitPodPath = "/pods"
44+
// CONTENTTYPE http content-type
45+
CONTENTTYPE = "Content-Type"
46+
// APPLICATIONJSON json content
47+
APPLICATIONJSON = "application/json"
4148
)
4249

4350
//The AdmitFunc returns response
4451
type AdmitFunc func(v1beta1.AdmissionReview) *v1beta1.AdmissionResponse
4552

53+
// Controller the Admission Controller type
54+
type Controller struct {
55+
KbClients kbver.Interface
56+
SchedulerName string
57+
}
58+
4659
var scheme = runtime.NewScheme()
4760

4861
//Codecs is for retrieving serializers for the supported wire formats

0 commit comments

Comments
 (0)