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

[bugfix]fix creating a hierarchical sub-queue will be rejected #4033

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 30 additions & 12 deletions pkg/webhooks/admission/queues/validate/validate_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,9 @@ func validateQueueDeleting(queueName string) error {
return err
}

queueList, err := config.QueueLister.List(labels.Everything())
childQueueNames, err := listQueueChild(queue.Name)
if err != nil {
return fmt.Errorf("failed to list queues: %v", err)
}
childQueueNames := make([]string, 0)
for _, childQueue := range queueList {
if childQueue.Spec.Parent != queueName {
continue
}
childQueueNames = append(childQueueNames, childQueue.Name)
return fmt.Errorf("failed to list child queues: %v", err)
}

if len(childQueueNames) > 0 {
Expand All @@ -251,17 +244,42 @@ func validateHierarchicalQueue(queue *schedulingv1beta1.Queue) error {
if queue.Spec.Parent == "" || queue.Spec.Parent == "root" {
return nil
}

parentQueue, err := config.QueueLister.Get(queue.Spec.Parent)
if err != nil {
return fmt.Errorf("failed to get parent queue of queue %s: %v", queue.Name, err)
}

if allocated, ok := parentQueue.Status.Allocated[v1.ResourcePods]; ok && !allocated.IsZero() {
return fmt.Errorf("queue %s cannot be the parent queue of queue %s because it has allocated Pods: %d",
parentQueue.Name, queue.Name, allocated.Value())
childQueueNames, err := listQueueChild(queue.Name)
if err != nil {
return fmt.Errorf("failed to list child queues: %v", err)
}

if len(childQueueNames) == 0 {
if allocated, ok := parentQueue.Status.Allocated[v1.ResourcePods]; ok && !allocated.IsZero() {
return fmt.Errorf("queue %s cannot be the parent queue of queue %s because it has allocated Pods: %d",
parentQueue.Name, queue.Name, allocated.Value())
}
}

klog.V(3).Infof("Validation passed for hierarchical queue %s with parent queue %s",
queue.Name, parentQueue.Name)
return nil
}

func listQueueChild(queueName string) ([]string, error) {
queueList, err := config.QueueLister.List(labels.Everything())
if err != nil {
return nil, fmt.Errorf("failed to list queues: %v", err)
}

childQueueNames := make([]string, 0)
for _, childQueue := range queueList {
if childQueue.Spec.Parent != queueName {
continue
}
childQueueNames = append(childQueueNames, childQueue.Name)
}

return childQueueNames, nil
}
78 changes: 78 additions & 0 deletions pkg/webhooks/admission/queues/validate/validate_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,45 @@ func TestAdmitHierarchicalQueues(t *testing.T) {
t.Errorf("Marshal queue with child queue failed for %v.", err)
}

parentQueueWithChildWithJobs := schedulingv1beta1.Queue{
ObjectMeta: metav1.ObjectMeta{
Name: "parent-queue-has-child-with-jobs",
},
Spec: schedulingv1beta1.QueueSpec{
Parent: "root",
},
}

childQueueAWithJobs := schedulingv1beta1.Queue{
ObjectMeta: metav1.ObjectMeta{
Name: "child-queue-a-with-jobs",
},
Spec: schedulingv1beta1.QueueSpec{
Parent: "parent-queue-has-child-with-jobs",
Weight: 1,
},
Status: schedulingv1beta1.QueueStatus{
Allocated: v1.ResourceList{
v1.ResourcePods: resource.MustParse("1"),
},
},
}

childQueueB := schedulingv1beta1.Queue{
ObjectMeta: metav1.ObjectMeta{
Name: "child-queue-b",
},
Spec: schedulingv1beta1.QueueSpec{
Parent: "parent-queue-has-child-with-jobs",
Weight: 1,
},
}

childQueueBJSON, err := json.Marshal(childQueueB)
if err != nil {
t.Errorf("Marshal queue with parent queue failed for %v.", err)
}

config.VolcanoClient = fakeclient.NewSimpleClientset()
informerFactory := informers.NewSharedInformerFactory(config.VolcanoClient, 0)
queueInformer := informerFactory.Scheduling().V1beta1().Queues()
Expand Down Expand Up @@ -1101,6 +1140,16 @@ func TestAdmitHierarchicalQueues(t *testing.T) {
t.Errorf("Create queue failed for %v.", err)
}

_, err = config.VolcanoClient.SchedulingV1beta1().Queues().Create(context.TODO(), &parentQueueWithChildWithJobs, metav1.CreateOptions{})
if err != nil {
t.Errorf("Create parent queue failed for %v.", err)
}

_, err = config.VolcanoClient.SchedulingV1beta1().Queues().Create(context.TODO(), &childQueueAWithJobs, metav1.CreateOptions{})
if err != nil {
t.Errorf("Create child queue with jobs failed for %v.", err)
}

_, err = config.VolcanoClient.SchedulingV1beta1().Queues().Create(context.TODO(), &queueWithoutChild, metav1.CreateOptions{})
if err != nil {
t.Errorf("Create queue failed for %v.", err)
Expand Down Expand Up @@ -1172,6 +1221,35 @@ func TestAdmitHierarchicalQueues(t *testing.T) {
Allowed: true,
},
},
{
Name: "Parent Queue has child with jobs",
AR: admissionv1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1beta1",
},
Request: &admissionv1.AdmissionRequest{
Kind: metav1.GroupVersionKind{
Group: "scheduling.volcano.sh",
Version: "v1beta1",
Kind: "Queue",
},
Resource: metav1.GroupVersionResource{
Group: "scheduling.volcano.sh",
Version: "v1beta1",
Resource: "queues",
},
Name: "brother-queue-has-jobs",
Operation: "CREATE",
Object: runtime.RawExtension{
Raw: childQueueBJSON,
},
},
},
reviewResponse: &admissionv1.AdmissionResponse{
Allowed: true,
},
},
{
Name: "Delete queue with child queue",
AR: admissionv1.AdmissionReview{
Expand Down