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

Make generated pods only request the maximum necessary resources #723

Merged
merged 1 commit into from
Apr 12, 2019
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
6 changes: 6 additions & 0 deletions docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ or container images that you define:
the configuration file.
- Each container image runs until completion or until the first failure is
detected.
- The CPU, memory, and ephemeral storage resource requests will be set to zero
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts on a better place to put this documentation or a better way to phrase it would be welcome!

if the container image does not have the largest resource request out of all
container images in the Task. This ensures that the Pod that executes the Task
will only request the resources necessary to execute any single container
image in the Task, rather than requesting the sum of all of the container
image's resource requests.

### Inputs

Expand Down
46 changes: 46 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -74,6 +75,8 @@ var (
VolumeSource: emptyVolumeSource,
}}

zeroQty = resource.MustParse("0")

// Random byte reader used for pod name generation.
// var for testing.
randReader = rand.Reader
Expand Down Expand Up @@ -175,6 +178,8 @@ func MakePod(taskRun *v1alpha1.TaskRun, taskSpec v1alpha1.TaskSpec, kubeclient k
initContainers := []corev1.Container{*cred}
podContainers := []corev1.Container{}

maxIndicesByResource := findMaxResourceRequest(taskSpec.Steps, corev1.ResourceCPU, corev1.ResourceMemory, corev1.ResourceEphemeralStorage)

for i, step := range taskSpec.Steps {
step.Env = append(implicitEnvVars, step.Env...)
// TODO(mattmoor): Check that volumeMounts match volumes.
Expand Down Expand Up @@ -203,6 +208,7 @@ func MakePod(taskRun *v1alpha1.TaskRun, taskSpec v1alpha1.TaskSpec, kubeclient k
if step.Name == names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("%v%v", containerPrefix, entrypoint.InitContainerName)) {
initContainers = append(initContainers, step)
} else {
zeroNonMaxResourceRequests(&step, i, maxIndicesByResource)
podContainers = append(podContainers, step)
}
}
Expand Down Expand Up @@ -264,3 +270,43 @@ func makeLabels(s *v1alpha1.TaskRun) map[string]string {
labels[pipeline.GroupName+pipeline.TaskRunLabelKey] = s.Name
return labels
}

// zeroNonMaxResourceRequests zeroes out the container's cpu, memory, or
// ephemeral storage resource requests if the container does not have the
// largest request out of all containers in the pod. This is done because Tekton
// overwrites each container's entrypoint to make containers effectively execute
// one at a time, so we want pods to only request the maximum resources needed
// at any single point in time. If no contianer has an explicit resource
// request, all requests are set to 0.
func zeroNonMaxResourceRequests(container *corev1.Container, containerIndex int, maxIndicesByResource map[corev1.ResourceName]int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these functions are excellent! nice focused interface and clear short functions. My only too-late-to-the-party request would be to have unit tests covering these functions directly as well but im fully expecting to be ignored since im so late haha :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'm going to file an issue for a followup I thought of the other day, and if I'm in this area again I'll add some unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, that sounds great @dwnusbaum ❤️ !!

if container.Resources.Requests == nil {
container.Resources.Requests = corev1.ResourceList{}
}
for name, maxIdx := range maxIndicesByResource {
if maxIdx != containerIndex {
container.Resources.Requests[name] = zeroQty
}
}
}

// findMaxResourceRequest returns the index of the container with the maximum
// request for the given resource from among the given set of containers.
func findMaxResourceRequest(containers []corev1.Container, resourceNames ...corev1.ResourceName) map[corev1.ResourceName]int {
maxIdxs := make(map[corev1.ResourceName]int, len(resourceNames))
maxReqs := make(map[corev1.ResourceName]resource.Quantity, len(resourceNames))
for _, name := range resourceNames {
maxIdxs[name] = -1
maxReqs[name] = zeroQty
}
for i, c := range containers {
for _, name := range resourceNames {
maxReq := maxReqs[name]
req, exists := c.Resources.Requests[name]
if exists && req.Cmp(maxReq) > 0 {
maxIdxs[name] = i
maxReqs[name] = req
}
}
}
return maxIdxs
}
37 changes: 33 additions & 4 deletions pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -34,8 +33,10 @@ import (
)

var (
ignorePrivateResourceFields = cmpopts.IgnoreUnexported(resource.Quantity{})
nopContainer = corev1.Container{
resourceQuantityCmp = cmp.Comparer(func(x, y resource.Quantity) bool {
return x.Cmp(y) == 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this comparator in 3 places. It would be nice to have a default comparator with some helpful default settings for use in tests, but I didn't see any kind of util package or anything for that kind of thing. Is there a place for something like that?

})
nopContainer = corev1.Container{
Name: "nop",
Image: *nopImage,
Command: []string{"/builder/tools/entrypoint"},
Expand Down Expand Up @@ -110,6 +111,13 @@ func TestMakePod(t *testing.T) {
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
WorkingDir: workspaceDir,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0"),
corev1.ResourceMemory: resource.MustParse("0"),
corev1.ResourceEphemeralStorage: resource.MustParse("0"),
},
},
},
nopContainer,
},
Expand Down Expand Up @@ -149,6 +157,13 @@ func TestMakePod(t *testing.T) {
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
WorkingDir: workspaceDir,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0"),
corev1.ResourceMemory: resource.MustParse("0"),
corev1.ResourceEphemeralStorage: resource.MustParse("0"),
},
},
},
nopContainer,
},
Expand Down Expand Up @@ -182,6 +197,13 @@ func TestMakePod(t *testing.T) {
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
WorkingDir: workspaceDir,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0"),
corev1.ResourceMemory: resource.MustParse("0"),
corev1.ResourceEphemeralStorage: resource.MustParse("0"),
},
},
},
nopContainer,
},
Expand Down Expand Up @@ -215,6 +237,13 @@ func TestMakePod(t *testing.T) {
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
WorkingDir: workspaceDir,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0"),
corev1.ResourceMemory: resource.MustParse("0"),
corev1.ResourceEphemeralStorage: resource.MustParse("0"),
},
},
},
nopContainer,
},
Expand Down Expand Up @@ -264,7 +293,7 @@ func TestMakePod(t *testing.T) {
t.Errorf("Pod name got %q, want %q", got.Name, wantName)
}

if d := cmp.Diff(&got.Spec, c.want, ignorePrivateResourceFields); d != "" {
if d := cmp.Diff(&got.Spec, c.want, resourceQuantityCmp); d != "" {
t.Errorf("Diff spec:\n%s", d)
}

Expand Down
Loading