forked from tektoncd/pipeline
-
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.
[TEP-0104] Update Pod with Task-level Requirements
The task-level compute resource requirements will be applied on the containers created by the Pod.
- Loading branch information
1 parent
73f4e0d
commit 34c5af6
Showing
5 changed files
with
482 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2020 The Tekton 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 tasklevel | ||
|
||
import ( | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
// ApplyTaskLevelComputeResources applies the task-level compute resource requirements to each Step. | ||
func ApplyTaskLevelComputeResources(steps []v1beta1.Step, computeResources *corev1.ResourceRequirements) { | ||
if computeResources == nil { | ||
return | ||
} | ||
if computeResources.Requests == nil && computeResources.Limits == nil { | ||
return | ||
} | ||
averageRequests := computeAverageRequests(computeResources.Requests, len(steps)) | ||
averageLimits := computeAverageRequests(computeResources.Limits, len(steps)) | ||
for i := range steps { | ||
// if no requests are specified in step or task level, the limits are used to avoid | ||
// unnecessary higher requests by Kubernetes default behavior. | ||
if steps[i].Resources.Requests == nil && computeResources.Requests == nil { | ||
steps[i].Resources.Requests = averageLimits | ||
} else { | ||
steps[i].Resources.Requests = averageRequests | ||
} | ||
steps[i].Resources.Limits = computeResources.Limits | ||
} | ||
} | ||
|
||
// computeAverageRequests computes the average of the requests of all the steps. | ||
func computeAverageRequests(requests corev1.ResourceList, steps int) corev1.ResourceList { | ||
if len(requests) == 0 || steps == 0 { | ||
return nil | ||
} | ||
averageRequests := corev1.ResourceList{} | ||
for k, v := range requests { | ||
averageRequests[k] = *resource.NewMilliQuantity(v.MilliValue()/int64(steps), requests[k].Format) | ||
} | ||
return averageRequests | ||
} |
206 changes: 206 additions & 0 deletions
206
pkg/internal/computeresources/tasklevel/tasklevel_test.go
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,206 @@ | ||
/* | ||
Copyright 2019 The Tekton 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 tasklevel_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
"github.com/tektoncd/pipeline/pkg/internal/computeresources/tasklevel" | ||
"github.com/tektoncd/pipeline/test/diff" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func TestApplyTaskLevelResourceRequirements(t *testing.T) { | ||
testcases := []struct { | ||
desc string | ||
Steps []v1beta1.Step | ||
ComputeResources corev1.ResourceRequirements | ||
expectedComputeResources []corev1.ResourceRequirements | ||
}{{ | ||
desc: "only with requests", | ||
Steps: []v1beta1.Step{{ | ||
Name: "1st-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}, { | ||
Name: "2nd-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}}, | ||
ComputeResources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, | ||
expectedComputeResources: []corev1.ResourceRequirements{{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, { | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}}, | ||
}, { | ||
desc: "only with limits", | ||
Steps: []v1beta1.Step{{ | ||
Name: "1st-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}, { | ||
Name: "2nd-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}}, | ||
ComputeResources: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
}, | ||
expectedComputeResources: []corev1.ResourceRequirements{{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("250m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
}, { | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("250m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
}}, | ||
}, { | ||
desc: "both with requests and limits", | ||
Steps: []v1beta1.Step{{ | ||
Name: "1st-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}, { | ||
Name: "2nd-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}}, | ||
ComputeResources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, | ||
expectedComputeResources: []corev1.ResourceRequirements{{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, { | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}}, | ||
}, { | ||
desc: "steps with compute resources are overridden by task-level compute resources", | ||
Steps: []v1beta1.Step{{ | ||
Name: "1st-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
}, { | ||
Name: "2nd-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
}}, | ||
ComputeResources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, | ||
expectedComputeResources: []corev1.ResourceRequirements{{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, { | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}}, | ||
}, { | ||
desc: "steps with partial compute resources are overridden by task-level compute resources", | ||
Steps: []v1beta1.Step{{ | ||
Name: "1st-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
}, { | ||
Name: "2nd-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
}}, | ||
ComputeResources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, | ||
expectedComputeResources: []corev1.ResourceRequirements{{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}, { | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("500m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("2")}, | ||
}}, | ||
}, { | ||
desc: "steps with compute resources are preserved, if there are no task-level compute resources", | ||
Steps: []v1beta1.Step{{ | ||
Name: "1st-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
}, { | ||
Name: "2nd-step", | ||
Image: "image", | ||
Command: []string{"cmd"}, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
}}, | ||
ComputeResources: corev1.ResourceRequirements{}, | ||
expectedComputeResources: []corev1.ResourceRequirements{{ | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, { | ||
Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}, | ||
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}}, | ||
}} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
tasklevel.ApplyTaskLevelComputeResources(tc.Steps, &tc.ComputeResources) | ||
|
||
if err := verifyTaskLevelComputeResources(tc.Steps, tc.expectedComputeResources); err != nil { | ||
t.Errorf("verifyTaskLevelComputeResources: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// verifyTaskLevelComputeResources verifies that the given TaskRun's containers have the expected compute resources. | ||
func verifyTaskLevelComputeResources(steps []v1beta1.Step, expectedComputeResources []corev1.ResourceRequirements) error { | ||
if len(expectedComputeResources) != len(steps) { | ||
return fmt.Errorf("expected %d compute resource requirements, got %d", len(expectedComputeResources), len(steps)) | ||
} | ||
for id, step := range steps { | ||
if d := cmp.Diff(expectedComputeResources[id], step.Resources); d != "" { | ||
return fmt.Errorf("container \"#%d\" resource requirements don't match %s", id, diff.PrintWantGot(d)) | ||
} | ||
} | ||
return nil | ||
} |
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
Oops, something went wrong.