mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-08 22:23:15 +00:00
Some checks failed
Run the E2E test on kind / build (push) Failing after 6m48s
Run the E2E test on kind / setup-test-matrix (push) Successful in 3s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 35s
Close stale issues and PRs / stale (push) Successful in 8s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 1m11s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 47s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 49s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 43s
* lchore: define common alias for k8s packages Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> * Update .golangci.yaml Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> * Update .golangci.yaml Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> * Update .golangci.yaml Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> --------- Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
81 lines
2.8 KiB
Go
81 lines
2.8 KiB
Go
/*
|
|
Copyright 2019 the Velero contributors.
|
|
|
|
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 kube
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
corev1api "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
)
|
|
|
|
// ParseResourceRequirements takes a set of CPU and memory requests and limit string
|
|
// values and returns a ResourceRequirements struct to be used in a Container.
|
|
// An error is returned if we cannot parse the request/limit.
|
|
func ParseResourceRequirements(cpuRequest, memRequest, cpuLimit, memLimit string) (corev1api.ResourceRequirements, error) {
|
|
resources := corev1api.ResourceRequirements{
|
|
Requests: corev1api.ResourceList{},
|
|
Limits: corev1api.ResourceList{},
|
|
}
|
|
|
|
parsedCPURequest, err := resource.ParseQuantity(cpuRequest)
|
|
if err != nil {
|
|
return resources, errors.Wrapf(err, `couldn't parse CPU request "%s"`, cpuRequest)
|
|
}
|
|
|
|
parsedMemRequest, err := resource.ParseQuantity(memRequest)
|
|
if err != nil {
|
|
return resources, errors.Wrapf(err, `couldn't parse memory request "%s"`, memRequest)
|
|
}
|
|
|
|
parsedCPULimit, err := resource.ParseQuantity(cpuLimit)
|
|
if err != nil {
|
|
return resources, errors.Wrapf(err, `couldn't parse CPU limit "%s"`, cpuLimit)
|
|
}
|
|
|
|
parsedMemLimit, err := resource.ParseQuantity(memLimit)
|
|
if err != nil {
|
|
return resources, errors.Wrapf(err, `couldn't parse memory limit "%s"`, memLimit)
|
|
}
|
|
|
|
// A quantity of 0 is treated as unbounded
|
|
unbounded := resource.MustParse("0")
|
|
|
|
if parsedCPULimit != unbounded && parsedCPURequest.Cmp(parsedCPULimit) > 0 {
|
|
return resources, errors.WithStack(errors.Errorf(`CPU request "%s" must be less than or equal to CPU limit "%s"`, cpuRequest, cpuLimit))
|
|
}
|
|
|
|
if parsedMemLimit != unbounded && parsedMemRequest.Cmp(parsedMemLimit) > 0 {
|
|
return resources, errors.WithStack(errors.Errorf(`Memory request "%s" must be less than or equal to Memory limit "%s"`, memRequest, memLimit))
|
|
}
|
|
|
|
// Only set resources if they are not unbounded
|
|
if parsedCPURequest != unbounded {
|
|
resources.Requests[corev1api.ResourceCPU] = parsedCPURequest
|
|
}
|
|
if parsedMemRequest != unbounded {
|
|
resources.Requests[corev1api.ResourceMemory] = parsedMemRequest
|
|
}
|
|
if parsedCPULimit != unbounded {
|
|
resources.Limits[corev1api.ResourceCPU] = parsedCPULimit
|
|
}
|
|
if parsedMemLimit != unbounded {
|
|
resources.Limits[corev1api.ResourceMemory] = parsedMemLimit
|
|
}
|
|
|
|
return resources, nil
|
|
}
|