From 9aa1d9a3e1a2f7f5dae8fb287993d9faa811c155 Mon Sep 17 00:00:00 2001 From: thandayuthapani Date: Thu, 30 May 2019 18:30:06 +0530 Subject: [PATCH] Check Queue exist in admission controller --- Gopkg.lock | 6 +++++- cmd/admission/app/server.go | 9 ++++++++ cmd/admission/main.go | 2 ++ installer/chart/templates/admission.yaml | 3 +++ pkg/admission/admit_job.go | 9 ++++++++ pkg/admission/admit_job_test.go | 26 ++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Gopkg.lock b/Gopkg.lock index 60dff4092a..0fa26df6f3 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -204,7 +204,7 @@ [[projects]] branch = "master" - digest = "1:986c933d3d3c05ab20d42462475be937ee26238500e58f5b53b5ae77b60f49ad" + digest = "1:407852f4bc58f1ffb25131dbdce512b3c5a2dd0d880567d3189c60a3eb069f45" name = "github.com/kubernetes-sigs/kube-batch" packages = [ "cmd/kube-batch/app", @@ -212,8 +212,10 @@ "pkg/apis/scheduling/v1alpha1", "pkg/apis/utils", "pkg/client/clientset/versioned", + "pkg/client/clientset/versioned/fake", "pkg/client/clientset/versioned/scheme", "pkg/client/clientset/versioned/typed/scheduling/v1alpha1", + "pkg/client/clientset/versioned/typed/scheduling/v1alpha1/fake", "pkg/client/informers/externalversions", "pkg/client/informers/externalversions/internalinterfaces", "pkg/client/informers/externalversions/scheduling", @@ -1047,6 +1049,7 @@ "github.com/kubernetes-sigs/kube-batch/cmd/kube-batch/app/options", "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1", "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned", + "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned/fake", "github.com/kubernetes-sigs/kube-batch/pkg/client/informers/externalversions", "github.com/kubernetes-sigs/kube-batch/pkg/client/informers/externalversions/scheduling/v1alpha1", "github.com/kubernetes-sigs/kube-batch/pkg/client/listers/scheduling/v1alpha1", @@ -1112,6 +1115,7 @@ "k8s.io/kubernetes/pkg/apis/core", "k8s.io/kubernetes/pkg/apis/core/v1", "k8s.io/kubernetes/pkg/apis/core/validation", + "k8s.io/kubernetes/pkg/controller", "k8s.io/kubernetes/pkg/scheduler/api", ] solver-name = "gps-cdcl" diff --git a/cmd/admission/app/server.go b/cmd/admission/app/server.go index 05e79f7b73..d6fbaeb76e 100644 --- a/cmd/admission/app/server.go +++ b/cmd/admission/app/server.go @@ -22,6 +22,7 @@ import ( "net/http" "github.com/golang/glog" + "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned" "k8s.io/api/admission/v1beta1" "k8s.io/apimachinery/pkg/runtime" @@ -46,6 +47,14 @@ func GetClient(restConfig *restclient.Config) *kubernetes.Clientset { return clientset } +func GetKubeBatchClient(restConfig *restclient.Config) *versioned.Clientset { + clientset, err := versioned.NewForConfig(restConfig) + if err != nil { + glog.Fatal(err) + } + return clientset +} + // ConfigTLS is a helper function that generate tls certificates from directly defined tls config or kubeconfig // These are passed in as command line for cluster certification. If tls config is passed in, we use the directly // defined tls config, else use that defined in kubeconfig diff --git a/cmd/admission/main.go b/cmd/admission/main.go index e60a6ef50f..82ab5dcd68 100644 --- a/cmd/admission/main.go +++ b/cmd/admission/main.go @@ -65,6 +65,8 @@ func main() { clientset := app.GetClient(restConfig) + admissioncontroller.KubeBatchClientSet = app.GetKubeBatchClient(restConfig) + caCertPem, err := ioutil.ReadFile(config.CaCertFile) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) diff --git a/installer/chart/templates/admission.yaml b/installer/chart/templates/admission.yaml index 9b57314d0e..436b6b34d1 100644 --- a/installer/chart/templates/admission.yaml +++ b/installer/chart/templates/admission.yaml @@ -29,6 +29,9 @@ rules: - apiGroups: [""] resources: ["secrets"] verbs: ["create", "get", "patch"] + - apiGroups: ["scheduling.incubator.k8s.io"] + resources: ["queues"] + verbs: ["get", "list"] --- kind: ClusterRoleBinding diff --git a/pkg/admission/admit_job.go b/pkg/admission/admit_job.go index 586451d3e8..819cebfa95 100644 --- a/pkg/admission/admit_job.go +++ b/pkg/admission/admit_job.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/golang/glog" + "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned" "k8s.io/api/admission/v1beta1" "k8s.io/api/core/v1" @@ -35,6 +36,9 @@ import ( "volcano.sh/volcano/pkg/controllers/job/plugins" ) +//KubeBatchClientSet is kube-batch clientset +var KubeBatchClientSet versioned.Interface + // job admit. func AdmitJobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse { @@ -146,6 +150,11 @@ func validateJob(job v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) st msg = msg + validateInfo } + // Check whether Queue already present or not + if _, err := KubeBatchClientSet.SchedulingV1alpha1().Queues().Get(job.Spec.Queue, metav1.GetOptions{}); err != nil { + msg = msg + fmt.Sprintf("Job not created with error: %v", err) + } + if msg != "" { reviewResponse.Allowed = false } diff --git a/pkg/admission/admit_job_test.go b/pkg/admission/admit_job_test.go index b62b1a94a6..43f509c4bb 100644 --- a/pkg/admission/admit_job_test.go +++ b/pkg/admission/admit_job_test.go @@ -20,10 +20,13 @@ import ( "strings" "testing" + kubebatchclient "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned/fake" + "k8s.io/api/admission/v1beta1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kbv1aplha1 "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1" v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1" ) @@ -48,6 +51,7 @@ func TestValidateExecution(t *testing.T) { }, Spec: v1alpha1.JobSpec{ MinAvailable: 1, + Queue: "default", Tasks: []v1alpha1.TaskSpec{ { Name: "task-1", @@ -83,6 +87,7 @@ func TestValidateExecution(t *testing.T) { }, Spec: v1alpha1.JobSpec{ MinAvailable: 1, + Queue: "default", Tasks: []v1alpha1.TaskSpec{ { Name: "duplicated-task-1", @@ -135,6 +140,7 @@ func TestValidateExecution(t *testing.T) { }, Spec: v1alpha1.JobSpec{ MinAvailable: 1, + Queue: "default", Tasks: []v1alpha1.TaskSpec{ { Name: "task-1", @@ -180,6 +186,7 @@ func TestValidateExecution(t *testing.T) { }, Spec: v1alpha1.JobSpec{ MinAvailable: 2, + Queue: "default", Tasks: []v1alpha1.TaskSpec{ { Name: "task-1", @@ -215,6 +222,7 @@ func TestValidateExecution(t *testing.T) { }, Spec: v1alpha1.JobSpec{ MinAvailable: 1, + Queue: "default", Tasks: []v1alpha1.TaskSpec{ { Name: "task-1", @@ -253,6 +261,7 @@ func TestValidateExecution(t *testing.T) { }, Spec: v1alpha1.JobSpec{ MinAvailable: 1, + Queue: "default", Tasks: []v1alpha1.TaskSpec{ { Name: "task-1", @@ -283,6 +292,23 @@ func TestValidateExecution(t *testing.T) { for _, testCase := range testCases { + defaultqueue := kbv1aplha1.Queue{ + ObjectMeta: metav1.ObjectMeta{ + Name: "default", + }, + Spec: kbv1aplha1.QueueSpec{ + Weight: 1, + }, + } + // create fake kube-batch clientset + KubeBatchClientSet = kubebatchclient.NewSimpleClientset() + + //create default queue + _, err := KubeBatchClientSet.SchedulingV1alpha1().Queues().Create(&defaultqueue) + if err != nil { + t.Error("Queue Creation Failed") + } + ret := validateJob(testCase.Job, &testCase.reviewResponse) //fmt.Printf("test-case name:%s, ret:%v testCase.reviewResponse:%v \n", testCase.Name, ret,testCase.reviewResponse) if testCase.ExpectErr == true && ret == "" {