Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admission for queue #659

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/webhook-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
_ "volcano.sh/volcano/pkg/webhooks/admission/jobs/mutate"
_ "volcano.sh/volcano/pkg/webhooks/admission/jobs/validate"
_ "volcano.sh/volcano/pkg/webhooks/admission/pods"
_ "volcano.sh/volcano/pkg/webhooks/admission/queues/mutate"
_ "volcano.sh/volcano/pkg/webhooks/admission/queues/validate"
)

var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes")
Expand Down
1 change: 1 addition & 0 deletions hack/.golint_failures
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ volcano.sh/volcano/pkg/scheduler/actions/enqueue
volcano.sh/volcano/pkg/scheduler/actions/preempt
volcano.sh/volcano/pkg/scheduler/actions/reclaim
volcano.sh/volcano/pkg/webhooks/admission/jobs/mutate
volcano.sh/volcano/pkg/webhooks/admission/queues/mutate
volcano.sh/volcano/pkg/webhooks/router
volcano.sh/volcano/pkg/webhooks/schema
volcano.sh/volcano/test/e2e
9 changes: 8 additions & 1 deletion pkg/webhooks/admission/jobs/validate/admit_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
k8scorevalid "k8s.io/kubernetes/pkg/apis/core/validation"

"volcano.sh/volcano/pkg/apis/batch/v1alpha1"
schedulingv1alpha2 "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
"volcano.sh/volcano/pkg/controllers/job/plugins"
"volcano.sh/volcano/pkg/webhooks/router"
"volcano.sh/volcano/pkg/webhooks/schema"
Expand Down Expand Up @@ -177,11 +178,17 @@ func validateJob(job *v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) s
}

// Check whether Queue already present or not
if _, err := config.VolcanoClient.SchedulingV1alpha2().Queues().Get(job.Spec.Queue, metav1.GetOptions{}); err != nil {
queue, err := config.VolcanoClient.SchedulingV1alpha2().Queues().Get(job.Spec.Queue, metav1.GetOptions{})
if err != nil {
// TODO: deprecate v1alpha1
if _, err := config.VolcanoClient.SchedulingV1alpha1().Queues().Get(job.Spec.Queue, metav1.GetOptions{}); err != nil {
msg = msg + fmt.Sprintf(" unable to find job queue: %v", err)
}
} else {
if queue.Status.State != schedulingv1alpha2.QueueStateOpen {
msg = msg + fmt.Sprintf("can only submit job to queue with state `Open`, "+
"queue `%s` status is `%s`", queue.Name, queue.Spec.State)
}
}

if msg != "" {
Expand Down
3 changes: 3 additions & 0 deletions pkg/webhooks/admission/jobs/validate/admit_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,9 @@ func TestValidateExecution(t *testing.T) {
Spec: schedulingv1aplha2.QueueSpec{
Weight: 1,
},
Status: schedulingv1aplha2.QueueStatus{
State: schedulingv1aplha2.QueueStateOpen,
},
}
// create fake volcano clientset
config.VolcanoClient = fakeclient.NewSimpleClientset()
Expand Down
121 changes: 121 additions & 0 deletions pkg/webhooks/admission/queues/mutate/mutate_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2018 The Volcano Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mutate

import (
"encoding/json"
"fmt"

"k8s.io/api/admission/v1beta1"
whv1beta1 "k8s.io/api/admissionregistration/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"

schedulingv1alpha2 "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
"volcano.sh/volcano/pkg/webhooks/router"
"volcano.sh/volcano/pkg/webhooks/schema"
"volcano.sh/volcano/pkg/webhooks/util"
)

func init() {
router.RegisterAdmission(service)
}

var service = &router.AdmissionService{
Path: "/queues/mutate",
Func: MutateQueues,

MutatingConfig: &whv1beta1.MutatingWebhookConfiguration{
Webhooks: []whv1beta1.Webhook{{
Name: "mutatequeue.volcano.sh",
Rules: []whv1beta1.RuleWithOperations{
{
Operations: []whv1beta1.OperationType{whv1beta1.Create},
Rule: whv1beta1.Rule{
APIGroups: []string{schedulingv1alpha2.SchemeGroupVersion.Group},
APIVersions: []string{schedulingv1alpha2.SchemeGroupVersion.Version},
Resources: []string{"queues"},
},
},
},
}},
},
}

type patchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}

// MutateQueues mutate queues
func MutateQueues(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
klog.V(3).Infof("Mutating %s queue %s.", ar.Request.Operation, ar.Request.Name)

queue, err := schema.DecodeQueue(ar.Request.Object, ar.Request.Resource)
if err != nil {
return util.ToAdmissionResponse(err)
}

var patchBytes []byte
switch ar.Request.Operation {
case v1beta1.Create:
patchBytes, err = createQueuePatch(queue)

break
default:
return util.ToAdmissionResponse(fmt.Errorf("invalid operation `%s`, "+
"expect operation to be `CREATE`", ar.Request.Operation))
}

if err != nil {
return &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{Message: err.Error()},
}
}

pt := v1beta1.PatchTypeJSONPatch
return &v1beta1.AdmissionResponse{
Allowed: true,
Patch: patchBytes,
PatchType: &pt,
}
}

func createQueuePatch(queue *schedulingv1alpha2.Queue) ([]byte, error) {
var patch []patchOperation

if len(queue.Spec.State) == 0 {
patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/state",
Value: schedulingv1alpha2.QueueStateOpen,
})
}

trueValue := true
if queue.Spec.Reclaimable == nil {
patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/reclaimable",
Value: &trueValue,
})
}

return json.Marshal(patch)
}
195 changes: 195 additions & 0 deletions pkg/webhooks/admission/queues/mutate/mutate_queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
Copyright 2018 The Volcano Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mutate

import (
"encoding/json"
"fmt"
"reflect"
"testing"

schedulingv1alpha2 "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
"volcano.sh/volcano/pkg/webhooks/util"

"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func TestMutateQueues(t *testing.T) {
trueValue := true
stateNotSetReclaimableNotSet := schedulingv1alpha2.Queue{
ObjectMeta: metav1.ObjectMeta{
Name: "normal-case-refresh-default-state",
},
Spec: schedulingv1alpha2.QueueSpec{
Weight: 1,
},
}

stateNotSetJSON, err := json.Marshal(stateNotSetReclaimableNotSet)
if err != nil {
t.Errorf("Marshal queue without state set failed for %v.", err)
}

openStateReclaimableSet := schedulingv1alpha2.Queue{
ObjectMeta: metav1.ObjectMeta{
Name: "normal-case-set-open",
},
Spec: schedulingv1alpha2.QueueSpec{
Weight: 1,
State: schedulingv1alpha2.QueueStateOpen,
Reclaimable: &trueValue,
},
}

openStateJSON, err := json.Marshal(openStateReclaimableSet)
if err != nil {
t.Errorf("Marshal queue with open state failed for %v.", err)
}

pt := v1beta1.PatchTypeJSONPatch

var refreshPatch []patchOperation
refreshPatch = append(refreshPatch, patchOperation{
Op: "add",
Path: "/spec/state",
Value: schedulingv1alpha2.QueueStateOpen,
}, patchOperation{
Op: "add",
Path: "/spec/reclaimable",
Value: &trueValue,
})

refreshPatchJSON, err := json.Marshal(refreshPatch)
if err != nil {
t.Errorf("Marshal queue patch failed for %v.", err)
}

var openStatePatch []patchOperation
openStatePatchJSON, err := json.Marshal(openStatePatch)
if err != nil {
t.Errorf("Marshal null patch failed for %v.", err)
}

testCases := []struct {
Name string
AR v1beta1.AdmissionReview
reviewResponse *v1beta1.AdmissionResponse
}{
{
Name: "Normal Case Refresh Default Open State and Reclaimable For Queue",
AR: v1beta1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1beta1",
},
Request: &v1beta1.AdmissionRequest{
Kind: metav1.GroupVersionKind{
Group: "scheduling.sigs.dev",
Version: "v1alpha2",
Kind: "Queue",
},
Resource: metav1.GroupVersionResource{
Group: "scheduling.sigs.dev",
Version: "v1alpha2",
Resource: "queues",
},
Name: "normal-case-refresh-default-state",
Operation: "CREATE",
Object: runtime.RawExtension{
Raw: stateNotSetJSON,
},
},
},
reviewResponse: &v1beta1.AdmissionResponse{
Allowed: true,
PatchType: &pt,
Patch: refreshPatchJSON,
},
},
{
Name: "Normal Case Without Queue State or Reclaimable Patch",
AR: v1beta1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1beta1",
},
Request: &v1beta1.AdmissionRequest{
Kind: metav1.GroupVersionKind{
Group: "scheduling.sigs.dev",
Version: "v1alpha2",
Kind: "Queue",
},
Resource: metav1.GroupVersionResource{
Group: "scheduling.sigs.dev",
Version: "v1alpha2",
Resource: "queues",
},
Name: "normal-case-set-open",
Operation: "CREATE",
Object: runtime.RawExtension{
Raw: openStateJSON,
},
},
},
reviewResponse: &v1beta1.AdmissionResponse{
Allowed: true,
PatchType: &pt,
Patch: openStatePatchJSON,
},
},
{
Name: "Invalid Action",
AR: v1beta1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1beta1",
},
Request: &v1beta1.AdmissionRequest{
Kind: metav1.GroupVersionKind{
Group: "scheduling.sigs.dev",
Version: "v1alpha2",
Kind: "Queue",
},
Resource: metav1.GroupVersionResource{
Group: "scheduling.sigs.dev",
Version: "v1alpha2",
Resource: "queues",
},
Name: "normal-case-set-open",
Operation: "Invalid",
Object: runtime.RawExtension{
Raw: openStateJSON,
},
},
},
reviewResponse: util.ToAdmissionResponse(fmt.Errorf("invalid operation `%s`, "+
"expect operation to be `CREATE`", "Invalid")),
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
reviewResponse := MutateQueues(testCase.AR)
if !reflect.DeepEqual(reviewResponse, testCase.reviewResponse) {
t.Errorf("Test case %s failed, expect %v, got %v", testCase.Name,
reviewResponse, testCase.reviewResponse)
}
})
}
}
Loading