Skip to content

Commit

Permalink
Introduced resource container policies (#13)
Browse files Browse the repository at this point in the history
* Introduced resource container policies
  • Loading branch information
mikouaj authored Dec 4, 2023
1 parent ed681cc commit c30951b
Show file tree
Hide file tree
Showing 29 changed files with 1,024 additions and 298 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enabled.**
To install the latest release of Kube Startup CPU Boost in your cluster, run the following command:

```sh
kubectl apply -f https://github.com/google/kube-startup-cpu-boost/releases/download/v0.0.2/manifests.yaml
kubectl apply -f https://github.com/google/kube-startup-cpu-boost/releases/download/v0.1.0/manifests.yaml
```

The Kube Startup CPU Boost components run in `kube-startup-cpu-boost-system` namespace.
Expand Down Expand Up @@ -71,27 +71,32 @@ gcloud container clusters create poc \

1. Create `StartupCPUBoost` object in your workload's namespace

```sh
```yaml
apiVersion: autoscaling.x-k8s.io/v1alpha1
kind: StartupCPUBoost
metadata:
name: boost-001
namespace: demo
selector:
matchExpressions:
- key: app
- key: app.kubernetes.io/name
operator: In
values: ["app-001", "app-002"]
values: ["spring-rest-jpa"]
spec:
boostPercent: 50
resourcePolicy:
containerPolicies:
- containerName: spring-rest-jpa
percentageIncrease:
value: 50
durationPolicy:
podCondition:
type: Ready
status: "True"
```
The above example will boost CPU requests and limits of all PODs with `app=app-001` and `app=app-002`
labels in `demo` namespace. The resources will be increased by 50% until the
The above example will boost CPU requests and limits of a container `spring-rest-jpa` in a
PODs with `app.kubernetes.io/name=spring-rest-jpa` label in `demo` namespace.
The resources will be increased by 50% until the
[POD Condition](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-conditions)
`Ready` becomes `True`.

Expand Down
36 changes: 31 additions & 5 deletions api/v1alpha1/startupcpuboost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,42 @@ type DurationPolicy struct {
PodCondition *PodConditionDurationPolicy `json:"podCondition,omitempty"`
}

// PercentagePolicy defines the policy used to determine the target
// resources for a container
type PercentageIncrease struct {
// Value specifies the percentage value
// +kubebuilder:validation:Required
// +kubebuilder:validation:Minimum:=1
Value int64 `json:"value,omitempty"`
}

// ContainerPolicy defines the policy used to determine the target
// resources for a container
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"`
}

// ResourcePolicy defines the policy used to determine the target
// resources for a POD
type ResourcePolicy struct {
// ContainerPolicies specifies resource policies for the containers
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinItems:=1
ContainerPolicies []ContainerPolicy `json:"containerPolicies,omitempty"`
}

// StartupCPUBoostSpec defines the desired state of StartupCPUBoost
type StartupCPUBoostSpec struct {
// ResourcePolicy specifies policies for container resource increase
ResourcePolicy ResourcePolicy `json:"resourcePolicy,omitempty"`
// DurationPolicy specifies policies for resource boost duration
// +kubebuilder:validation:Required
DurationPolicy DurationPolicy `json:"durationPolicy,omitempty"`
// BootPercent defines the percent of CPU request increase that POD will get
// during the CPU boost time period
// +kubebuilder:validation:Required
// +kubebuilder:validation:Minimum:=1
BoostPercent int64 `json:"boostPercent,omitempty"`
}

// StartupCPUBoostStatus defines the observed state of StartupCPUBoost
Expand Down
52 changes: 52 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

35 changes: 29 additions & 6 deletions config/crd/bases/autoscaling.x-k8s.io_startupcpuboosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ spec:
spec:
description: StartupCPUBoostSpec defines the desired state of StartupCPUBoost
properties:
boostPercent:
description: BootPercent defines the percent of CPU request increase
that POD will get during the CPU boost time period
format: int64
minimum: 1
type: integer
durationPolicy:
description: DurationPolicy specifies policies for resource boost
duration
Expand Down Expand Up @@ -114,6 +108,35 @@ spec:
type: string
type: object
type: object
resourcePolicy:
description: ResourcePolicy specifies policies for container resource
increase
properties:
containerPolicies:
description: ContainerPolicies specifies resource policies for
the containers
items:
description: ContainerPolicy defines the policy used to determine
the target resources for a container
properties:
containerName:
description: ContainerName specifies the name of container
for a given policy
type: string
percentageIncrease:
description: PercentageIncrease specifies the percentage
increase policy for a container
properties:
value:
description: Value specifies the percentage value
format: int64
minimum: 1
type: integer
type: object
type: object
minItems: 1
type: array
type: object
type: object
status:
description: StartupCPUBoostStatus defines the observed state of StartupCPUBoost
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ require (
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
31 changes: 27 additions & 4 deletions internal/boost/boost_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,42 @@ func TestBoost(t *testing.T) {
}

var (
podTemplate *corev1.Pod
annotTemplate *bpod.BoostPodAnnotation
specTemplate *autoscaling.StartupCPUBoost
podTemplate *corev1.Pod
annotTemplate *bpod.BoostPodAnnotation
specTemplate *autoscaling.StartupCPUBoost
containerOneName string
containerTwoName string
containerOnePercValue int64
containerTwoPercValue int64
)

var _ = BeforeSuite(func() {
containerOneName = "container-one"
containerTwoName = "container-two"
containerOnePercValue = 120
containerTwoPercValue = 100
specTemplate = &autoscaling.StartupCPUBoost{
ObjectMeta: metav1.ObjectMeta{
Name: "boost-001",
Namespace: "demo",
},
Spec: autoscaling.StartupCPUBoostSpec{
BoostPercent: 55,
ResourcePolicy: autoscaling.ResourcePolicy{
ContainerPolicies: []autoscaling.ContainerPolicy{
{
ContainerName: containerOneName,
PercentageIncrease: autoscaling.PercentageIncrease{
Value: containerOnePercValue,
},
},
{
ContainerName: containerTwoName,
PercentageIncrease: autoscaling.PercentageIncrease{
Value: containerTwoPercValue,
},
},
},
},
},
}
annotTemplate = &bpod.BoostPodAnnotation{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package duration

import (
"time"
Expand All @@ -31,11 +31,11 @@ type FixedDurationPolicy struct {
duration time.Duration
}

func NewFixedDurationPolicy(duration time.Duration) DurationPolicy {
func NewFixedDurationPolicy(duration time.Duration) Policy {
return NewFixedDurationPolicyWithTimeFunc(time.Now, duration)
}

func NewFixedDurationPolicyWithTimeFunc(timeFunc TimeFunc, duration time.Duration) DurationPolicy {
func NewFixedDurationPolicyWithTimeFunc(timeFunc TimeFunc, duration time.Duration) Policy {
return &FixedDurationPolicy{
timeFunc: timeFunc,
duration: duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy_test
package duration_test

import (
"time"

bpolicy "github.com/google/kube-startup-cpu-boost/internal/boost/policy"
"github.com/google/kube-startup-cpu-boost/internal/boost/duration"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("FixedDurationPolicy", func() {
var policy bpolicy.DurationPolicy
var policy duration.Policy
var now time.Time
var duration time.Duration
var timeFunc bpolicy.TimeFunc
var timeDuration time.Duration
var timeFunc duration.TimeFunc

BeforeEach(func() {
now = time.Now()
duration = 5 * time.Second
timeDuration = 5 * time.Second
timeFunc = func() time.Time {
return now
}
policy = bpolicy.NewFixedDurationPolicyWithTimeFunc(timeFunc, duration)
policy = duration.NewFixedDurationPolicyWithTimeFunc(timeFunc, timeDuration)
})

Describe("Validates POD", func() {
When("the life time of a POD exceeds the policy duration", func() {
It("returns policy is not valid", func() {
creationTimesamp := now.Add(-1 * duration).Add(-1 * time.Minute)
creationTimesamp := now.Add(-1 * timeDuration).Add(-1 * time.Minute)
pod.CreationTimestamp = metav1.NewTime(creationTimesamp)
Expect(policy.Valid(pod)).To(BeFalse())
})
})
When("the life time of a POD is within policy duration", func() {
It("returns policy is valid", func() {
creationTimesamp := now.Add(-1 * duration).Add(1 * time.Minute)
creationTimesamp := now.Add(-1 * timeDuration).Add(1 * time.Minute)
pod.CreationTimestamp = metav1.NewTime(creationTimesamp)
Expect(policy.Valid(pod)).To(BeTrue())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package duration

import (
corev1 "k8s.io/api/core/v1"
Expand All @@ -27,7 +27,7 @@ type PodConditionPolicy struct {
status corev1.ConditionStatus
}

func NewPodConditionPolicy(condition corev1.PodConditionType, status corev1.ConditionStatus) DurationPolicy {
func NewPodConditionPolicy(condition corev1.PodConditionType, status corev1.ConditionStatus) Policy {
return &PodConditionPolicy{
condition: condition,
status: status,
Expand Down
Loading

0 comments on commit c30951b

Please sign in to comment.