Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
add max resource logic to the parallel steps
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-prindle committed Dec 21, 2018
1 parent b8d0318 commit b9e7928
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export K8S_CLUSTER_OVERRIDE=CLUSTER_NOT_SET
export K8S_USER_OVERRIDE=USER_NOT_SET
export DOCKER_REPO_OVERRIDE=DOCKER_NOT_SET

# Build the base image for creds-init and git images.
# Build the base image for creds-init and git images.
docker build -t ${BUILD_BASE_GCR} -f images/Dockerfile images/

echo "Building build-crd"
Expand Down
75 changes: 74 additions & 1 deletion pkg/reconciler/build/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,64 @@ func GetRemoteEntrypoint(cache *Cache, image string, kubeclient kubernetes.Inter
// TODO(aaron-prindle) setup the cache properly
var cache = NewCache()

func compareResources(a *corev1.ResourceRequirements, b *corev1.ResourceRequirements) bool {
equal := true
if a != nil {
equal = compareResoucesAssumeFirstNotNil(a, b)
}
if equal && (b != nil) {
equal = compareResoucesAssumeFirstNotNil(b, a)
}

return equal
}

func compareResoucesAssumeFirstNotNil(a *corev1.ResourceRequirements, b *corev1.ResourceRequirements) bool {
if b == nil || (len(b.Requests) == 0) {
return len(a.Requests) == 0
}
for k, v := range a.Requests {
if (&v).Cmp(b.Requests[k]) != 0 {
return false
}
}
for k, v := range a.Limits {
if (&v).Cmp(b.Limits[k]) != 0 {
return false
}
}
return true

}

func getMaxMemFromContainers(containers []corev1.Container) resource.Quantity {
if len(containers) == 0 {
return resource.Quantity{}
}
maxMem := *containers[0].Resources.Limits.Memory()
for _, c := range containers {
cmp := maxMem.Cmp(*c.Resources.Limits.Memory())
if cmp == -1 { //if max is less than this value
maxMem = *c.Resources.Limits.Cpu()
}
}
return maxMem
}

func getMaxCPUFromContainers(containers []corev1.Container) resource.Quantity {
if len(containers) == 0 {
return resource.Quantity{}
}
maxCPU := *containers[0].Resources.Limits.Cpu()
for _, c := range containers {
cmp := maxCPU.Cmp(*c.Resources.Limits.Cpu())
if cmp == -1 { //if max is less than this value
maxCPU = *c.Resources.Limits.Cpu()
}
}
return maxCPU
}

// RedirectSteps will modify each of the steps/containers such that
// the binary being run is no longer the one specified by the Command
// and the Args, but is instead the entrypoint binary, which will
Expand All @@ -873,7 +931,22 @@ func RedirectSteps(steps []corev1.Container, kubeclient kubernetes.Interface, bu
step.Command = ep
}
e, err := getEnvVar(step.Command, step.Args, i)
if i != 0 {

// As containers are now all started together but only one container is ran at a time
// rewrite the ResourceRequirements to reflect only one container will run
if i == 0 {
// find the max resource requirement
maxCPU := getMaxCPUFromContainers(steps)
maxMem := getMaxMemFromContainers(steps)
step.Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
// Must set memory limit to get MemoryStats.AvailableBytes
corev1.ResourceCPU: maxCPU,
corev1.ResourceMemory: maxMem,
},
}

} else {
step.Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
// Must set memory limit to get MemoryStats.AvailableBytes
Expand Down

0 comments on commit b9e7928

Please sign in to comment.