generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Fix workload would be created and deleted indefinitely #597
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -16,6 +16,10 @@ limitations under the License. | |
|
||
package api | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
maxEventMsgSize = 1024 | ||
maxConditionMsgSize = 32 * 1024 | ||
|
@@ -38,3 +42,35 @@ func truncateMessage(message string, limit int) string { | |
suffix := " ..." | ||
return message[:limit-len(suffix)] + suffix | ||
} | ||
|
||
// SetContainersDefaults will fill up the containers with default values. | ||
// Note that this is aimed to bridge the gap between job and workload, | ||
// E.g. job may have resource limits configured but no requests, | ||
// however Kueue depends on the necessary workload requests for computing, | ||
// so we need a function like this to make up for this. | ||
// | ||
// This is inspired by Kubernetes pod defaulting: | ||
// https://github.com/kubernetes/kubernetes/blob/ab002db78835a94bd19ce4aaa46fb39b4d9c276f/pkg/apis/core/v1/defaults.go#L144 | ||
func SetContainersDefaults(containers []v1.Container) []v1.Container { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we support LimitRange later, we can also add here. If the LimitRange changes, workload will be non-equal to job. |
||
// This only happens in tests for containers should always exist. | ||
// We return containers itself here rather than nil or empty slice directly for | ||
// not failing the unit tests or integration tests. | ||
if len(containers) == 0 { | ||
return containers | ||
} | ||
res := make([]v1.Container, len(containers)) | ||
for i, c := range containers { | ||
kerthcet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if c.Resources.Limits != nil { | ||
if c.Resources.Requests == nil { | ||
c.Resources.Requests = make(v1.ResourceList) | ||
} | ||
for k, v := range c.Resources.Limits { | ||
if _, exists := c.Resources.Requests[k]; !exists { | ||
c.Resources.Requests[k] = v.DeepCopy() | ||
} | ||
} | ||
} | ||
res[i] = c | ||
} | ||
return res | ||
} |
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,106 @@ | ||
/* | ||
Copyright 2023 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 api | ||
|
||
import ( | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
|
||
utiltesting "sigs.k8s.io/kueue/pkg/util/testing" | ||
) | ||
|
||
func TestSetContainersDefaults(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
containers []v1.Container | ||
wantContainers []v1.Container | ||
}{ | ||
{ | ||
name: "container with no resources", | ||
containers: []v1.Container{ | ||
*utiltesting.MakeContainer("no-resources").Obj(), | ||
}, | ||
wantContainers: []v1.Container{ | ||
*utiltesting.MakeContainer("no-resources").Obj(), | ||
}, | ||
}, | ||
{ | ||
name: "container with resource requests only", | ||
containers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-requests-only").Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m"}).Obj(), | ||
}, | ||
wantContainers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-requests-only").Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m"}).Obj(), | ||
}, | ||
}, | ||
{ | ||
name: "container with resource limits only", | ||
containers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-limits-only"). | ||
Limit(map[v1.ResourceName]string{v1.ResourceCPU: "100m"}). | ||
Obj(), | ||
}, | ||
wantContainers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-limits-only"). | ||
Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m"}). | ||
Limit(map[v1.ResourceName]string{v1.ResourceCPU: "100m"}). | ||
Obj(), | ||
}, | ||
}, | ||
{ | ||
name: "container with both resource requests and limits, values equal", | ||
containers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-requests-and-limits"). | ||
Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m"}). | ||
Limit(map[v1.ResourceName]string{v1.ResourceMemory: "200Mi"}). | ||
Obj(), | ||
}, | ||
wantContainers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-requests-and-limits"). | ||
Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m", v1.ResourceMemory: "200Mi"}). | ||
Limit(map[v1.ResourceName]string{v1.ResourceMemory: "200Mi"}). | ||
Obj(), | ||
}, | ||
}, | ||
{ | ||
name: "container with both resource requests and limits, values not equal", | ||
containers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-requests-and-limits"). | ||
Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m", v1.ResourceMemory: "100Mi"}). | ||
Limit(map[v1.ResourceName]string{v1.ResourceMemory: "200Mi"}). | ||
Obj(), | ||
}, | ||
wantContainers: []v1.Container{ | ||
*utiltesting.MakeContainer("with-requests-and-limits"). | ||
Requests(map[v1.ResourceName]string{v1.ResourceCPU: "100m", v1.ResourceMemory: "100Mi"}). | ||
Limit(map[v1.ResourceName]string{v1.ResourceMemory: "200Mi"}). | ||
Obj(), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
containers := SetContainersDefaults(tc.containers) | ||
if !equality.Semantic.DeepEqual(tc.wantContainers, containers) { | ||
t.Errorf("containers are not semantically equal, expected: %v, got: %v", tc.wantContainers, containers) | ||
} | ||
}) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced about putting this function here. But we can revisit once we have the jobs library completed, which will probably use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deal. Didn't find where to place this.