forked from openshift/kubernetes-sigs-kueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TAS: Introduce scheduling gate utils (kubernetes-sigs#3234)
- Loading branch information
Showing
4 changed files
with
278 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
CCopyright The Kubernetes 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 pod | ||
|
||
import ( | ||
"slices" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// HasGate checks if the pod has a scheduling gate with a specified name. | ||
func HasGate(pod *corev1.Pod, gateName string) bool { | ||
return gateIndex(pod, gateName) >= 0 | ||
} | ||
|
||
// Ungate removes scheduling gate from the Pod if present. | ||
// Returns true if the pod has been updated and false otherwise. | ||
func Ungate(pod *corev1.Pod, gateName string) bool { | ||
if idx := gateIndex(pod, gateName); idx >= 0 { | ||
pod.Spec.SchedulingGates = slices.Delete(pod.Spec.SchedulingGates, idx, idx+1) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// Gate adds scheduling gate from the Pod if present. | ||
// Returns true if the pod has been updated and false otherwise. | ||
func Gate(pod *corev1.Pod, gateName string) bool { | ||
if !HasGate(pod, gateName) { | ||
pod.Spec.SchedulingGates = append(pod.Spec.SchedulingGates, corev1.PodSchedulingGate{ | ||
Name: gateName, | ||
}) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// gateIndex returns the index of the Kueue scheduling gate for corev1.Pod. | ||
// If the scheduling gate is not found, returns -1. | ||
func gateIndex(p *corev1.Pod, gateName string) int { | ||
return slices.IndexFunc(p.Spec.SchedulingGates, func(g corev1.PodSchedulingGate) bool { | ||
return g.Name == gateName | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/* | ||
CCopyright The Kubernetes 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 pod | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestHasGate(t *testing.T) { | ||
testCases := map[string]struct { | ||
gateName string | ||
pod corev1.Pod | ||
want bool | ||
}{ | ||
"scheduling gate present": { | ||
gateName: "example.com/gate", | ||
pod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
"another gate present": { | ||
gateName: "example.com/gate", | ||
pod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
"no scheduling gates": { | ||
pod: corev1.Pod{}, | ||
want: false, | ||
}, | ||
} | ||
|
||
for desc, tc := range testCases { | ||
t.Run(desc, func(t *testing.T) { | ||
got := HasGate(&tc.pod, tc.gateName) | ||
if got != tc.want { | ||
t.Errorf("Unexpected result: want=%v, got=%v", tc.want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUngate(t *testing.T) { | ||
testCases := map[string]struct { | ||
gateName string | ||
pod corev1.Pod | ||
wantPod corev1.Pod | ||
want bool | ||
}{ | ||
"ungate when scheduling gate present": { | ||
gateName: "example.com/gate", | ||
pod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantPod: corev1.Pod{}, | ||
want: true, | ||
}, | ||
"ungate when scheduling gate missing": { | ||
gateName: "example.com/gate", | ||
pod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantPod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
for desc, tc := range testCases { | ||
t.Run(desc, func(t *testing.T) { | ||
got := Ungate(&tc.pod, tc.gateName) | ||
if got != tc.want { | ||
t.Errorf("Unexpected result: want=%v, got=%v", tc.want, got) | ||
} | ||
if diff := cmp.Diff(tc.wantPod.Spec.SchedulingGates, tc.pod.Spec.SchedulingGates, cmpopts.EquateEmpty()); diff != "" { | ||
t.Errorf("Unexpected scheduling gates\ndiff=%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGate(t *testing.T) { | ||
testCases := map[string]struct { | ||
gateName string | ||
pod corev1.Pod | ||
wantPod corev1.Pod | ||
want bool | ||
}{ | ||
"gate when scheduling gate present": { | ||
gateName: "example.com/gate", | ||
pod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantPod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
"gate when scheduling gate missing": { | ||
gateName: "example.com/gate", | ||
pod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantPod: corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
SchedulingGates: []corev1.PodSchedulingGate{ | ||
{ | ||
Name: "example.com/gate2", | ||
}, | ||
{ | ||
Name: "example.com/gate", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
} | ||
|
||
for desc, tc := range testCases { | ||
t.Run(desc, func(t *testing.T) { | ||
got := Gate(&tc.pod, tc.gateName) | ||
if got != tc.want { | ||
t.Errorf("Unexpected result: want=%v, got=%v", tc.want, got) | ||
} | ||
if diff := cmp.Diff(tc.wantPod.Spec.SchedulingGates, tc.pod.Spec.SchedulingGates); diff != "" { | ||
t.Errorf("Unexpected scheduling gates\ndiff=%s", diff) | ||
} | ||
}) | ||
} | ||
} |