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

Fixed resources #18

Merged
merged 5 commits into from
Dec 29, 2023
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Note: this is not an officially supported Google product.
* [Features](#features)
* [[Boost target] POD label selector](#boost-target-pod-label-selector)
* [[Boost resources] percentage increase](#boost-resources-percentage-increase)
* [[Boost resources] fixed target](#boost-resources-fixed-target)
* [[Boost duration] fixed time](#boost-duration-fixed-time)
* [[Boost duration] POD condition](#boost-duration-pod-condition)
* [License](#license)
Expand Down Expand Up @@ -144,6 +145,21 @@ spec:
value: 50
```

### [Boost resources] fixed target

Define the fixed resources for a target container(s). The CPU requests and limits of selected
container(s) will be set to the given values. Note that specified requests and limits have to be
higher than the ones in the container.

```yaml
spec:
containerPolicies:
- containerName: spring-rest-jpa
fixedResources:
requests: "1"
limits: "2"
```

### [Boost duration] fixed time

Define the fixed amount of time, the resource boost effect will last for it since the POD creation.
Expand Down
27 changes: 22 additions & 5 deletions api/v1alpha1/startupcpuboost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -60,8 +61,19 @@ type DurationPolicy struct {
PodCondition *PodConditionDurationPolicy `json:"podCondition,omitempty"`
}

// PercentageIncrease defines the policy used to determine the target
// resources for a container
// FixedResources defines the CPU resource policy that sets CPU resources
// to the given values
type FixedResources struct {
// Requests specifies the CPU requests
// +kubebuilder:validation:Required
Requests resource.Quantity `json:"requests,omitempty"`
// Limits specifies the CPU requests
// +kubebuilder:validation:Optional
Limits resource.Quantity `json:"limits,omitempty"`
}

// PercentageIncrease defines the CPU resource policy that increases
// CPU resources by the given percentage value
type PercentageIncrease struct {
// Value specifies the percentage value
// +kubebuilder:validation:Required
Expand All @@ -75,9 +87,14 @@ type ContainerPolicy struct {
// ContainerName specifies the name of container for a given policy
// +kubebuilder:validation:Required
ContainerName string `json:"containerName,omitempty"`
// PercentageIncrease specifies the percentage increase policy for a container
// +kubebuilder:validation:Required
PercentageIncrease PercentageIncrease `json:"percentageIncrease,omitempty"`
// PercentageIncrease specifies the CPU resource policy that increases
// CPU resources by the given percentage value
// +kubebuilder:validation:Optional
PercentageIncrease *PercentageIncrease `json:"percentageIncrease,omitempty"`
// FixedResources specifies the CPU resource policy that sets the CPU
// resources to the given values
// +kubebuilder:validation:Optional
FixedResources *FixedResources `json:"fixedResources,omitempty"`
}

// ResourcePolicy defines the policy used to determine the target
Expand Down
32 changes: 30 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions config/crd/bases/autoscaling.x-k8s.io_startupcpuboosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,29 @@ spec:
description: ContainerName specifies the name of container
for a given policy
type: string
fixedResources:
description: FixedResources specifies the CPU resource policy
that sets the CPU resources to the given values
properties:
limits:
anyOf:
- type: integer
- type: string
description: Limits specifies the CPU requests
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
requests:
anyOf:
- type: integer
- type: string
description: Requests specifies the CPU requests
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
type: object
percentageIncrease:
description: PercentageIncrease specifies the percentage
increase policy for a container
description: PercentageIncrease specifies the CPU resource
policy that increases CPU resources by the given percentage
value
properties:
value:
description: Value specifies the percentage value
Expand Down
19 changes: 1 addition & 18 deletions internal/boost/boost_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,8 @@ var _ = BeforeSuite(func() {
Name: "boost-001",
Namespace: "demo",
},
Spec: autoscaling.StartupCPUBoostSpec{
ResourcePolicy: autoscaling.ResourcePolicy{
ContainerPolicies: []autoscaling.ContainerPolicy{
{
ContainerName: containerOneName,
PercentageIncrease: autoscaling.PercentageIncrease{
Value: containerOnePercValue,
},
},
{
ContainerName: containerTwoName,
PercentageIncrease: autoscaling.PercentageIncrease{
Value: containerTwoPercValue,
},
},
},
},
},
}

annotTemplate = &bpod.BoostPodAnnotation{
BoostTimestamp: time.Now(),
InitCPURequests: map[string]string{
Expand Down
69 changes: 69 additions & 0 deletions internal/boost/resource/fixed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023 Google LLC
//
// 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
//
// https://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 resource

import (
"context"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apiResource "k8s.io/apimachinery/pkg/api/resource"
ctrl "sigs.k8s.io/controller-runtime"
)

type FixedPolicy struct {
cpuRequests apiResource.Quantity
cpuLimits apiResource.Quantity
}

func NewFixedPolicy(requests apiResource.Quantity, limits apiResource.Quantity) ContainerPolicy {
return &FixedPolicy{
cpuRequests: requests,
cpuLimits: limits,
}
}

func (p *FixedPolicy) Requests() apiResource.Quantity {
return p.cpuRequests
}

func (p *FixedPolicy) Limits() apiResource.Quantity {
return p.cpuLimits
}

func (p *FixedPolicy) NewResources(ctx context.Context, container *corev1.Container) *corev1.ResourceRequirements {
log := ctrl.LoggerFrom(ctx).WithName("fixed-cpu-policy").
WithValues("newCPURequsts", p.cpuRequests.String()).
WithValues("newCPULimits", p.cpuLimits.String())
result := container.Resources.DeepCopy()
p.setResource(corev1.ResourceCPU, result.Requests, p.cpuRequests, log)
p.setResource(corev1.ResourceCPU, result.Limits, p.cpuLimits, log)
return result
}

func (p *FixedPolicy) setResource(resource corev1.ResourceName, resources corev1.ResourceList, target apiResource.Quantity, log logr.Logger) {
if target.IsZero() {
return
}
current, ok := resources[resource]
if !ok {
return
}
if target.Cmp(current) < 0 {
log.V(2).Info("container has higher CPU requests than policy")
return
}
resources[resource] = target
}
Loading