Skip to content

Commit

Permalink
Check Queue exist in admission controller
Browse files Browse the repository at this point in the history
  • Loading branch information
thandayuthapani committed May 30, 2019
1 parent d724454 commit 9aa1d9a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cmd/admission/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/admission/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions installer/chart/templates/admission.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["create", "get", "patch"]
- apiGroups: ["scheduling.incubator.k8s.io"]
resources: ["queues"]
verbs: ["get", "list"]

---
kind: ClusterRoleBinding
Expand Down
9 changes: 9 additions & 0 deletions pkg/admission/admit_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {

Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/admission/admit_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -48,6 +51,7 @@ func TestValidateExecution(t *testing.T) {
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Queue: "default",
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Expand Down Expand Up @@ -83,6 +87,7 @@ func TestValidateExecution(t *testing.T) {
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Queue: "default",
Tasks: []v1alpha1.TaskSpec{
{
Name: "duplicated-task-1",
Expand Down Expand Up @@ -135,6 +140,7 @@ func TestValidateExecution(t *testing.T) {
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Queue: "default",
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Expand Down Expand Up @@ -180,6 +186,7 @@ func TestValidateExecution(t *testing.T) {
},
Spec: v1alpha1.JobSpec{
MinAvailable: 2,
Queue: "default",
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Expand Down Expand Up @@ -215,6 +222,7 @@ func TestValidateExecution(t *testing.T) {
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Queue: "default",
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Expand Down Expand Up @@ -253,6 +261,7 @@ func TestValidateExecution(t *testing.T) {
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Queue: "default",
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Expand Down Expand Up @@ -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 == "" {
Expand Down

0 comments on commit 9aa1d9a

Please sign in to comment.