Skip to content

Commit c30951b

Browse files
authored
Introduced resource container policies (#13)
* Introduced resource container policies
1 parent ed681cc commit c30951b

29 files changed

+1024
-298
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enabled.**
3030
To install the latest release of Kube Startup CPU Boost in your cluster, run the following command:
3131

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

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

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

74-
```sh
74+
```yaml
7575
apiVersion: autoscaling.x-k8s.io/v1alpha1
7676
kind: StartupCPUBoost
7777
metadata:
7878
name: boost-001
7979
namespace: demo
8080
selector:
8181
matchExpressions:
82-
- key: app
82+
- key: app.kubernetes.io/name
8383
operator: In
84-
values: ["app-001", "app-002"]
84+
values: ["spring-rest-jpa"]
8585
spec:
86-
boostPercent: 50
86+
resourcePolicy:
87+
containerPolicies:
88+
- containerName: spring-rest-jpa
89+
percentageIncrease:
90+
value: 50
8791
durationPolicy:
8892
podCondition:
8993
type: Ready
9094
status: "True"
9195
```
9296
93-
The above example will boost CPU requests and limits of all PODs with `app=app-001` and `app=app-002`
94-
labels in `demo` namespace. The resources will be increased by 50% until the
97+
The above example will boost CPU requests and limits of a container `spring-rest-jpa` in a
98+
PODs with `app.kubernetes.io/name=spring-rest-jpa` label in `demo` namespace.
99+
The resources will be increased by 50% until the
95100
[POD Condition](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-conditions)
96101
`Ready` becomes `True`.
97102

api/v1alpha1/startupcpuboost_types.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,42 @@ type DurationPolicy struct {
6060
PodCondition *PodConditionDurationPolicy `json:"podCondition,omitempty"`
6161
}
6262

63+
// PercentagePolicy defines the policy used to determine the target
64+
// resources for a container
65+
type PercentageIncrease struct {
66+
// Value specifies the percentage value
67+
// +kubebuilder:validation:Required
68+
// +kubebuilder:validation:Minimum:=1
69+
Value int64 `json:"value,omitempty"`
70+
}
71+
72+
// ContainerPolicy defines the policy used to determine the target
73+
// resources for a container
74+
type ContainerPolicy struct {
75+
// ContainerName specifies the name of container for a given policy
76+
// +kubebuilder:validation:Required
77+
ContainerName string `json:"containerName,omitempty"`
78+
// PercentageIncrease specifies the percentage increase policy for a container
79+
// +kubebuilder:validation:Required
80+
PercentageIncrease PercentageIncrease `json:"percentageIncrease,omitempty"`
81+
}
82+
83+
// ResourcePolicy defines the policy used to determine the target
84+
// resources for a POD
85+
type ResourcePolicy struct {
86+
// ContainerPolicies specifies resource policies for the containers
87+
// +kubebuilder:validation:Required
88+
// +kubebuilder:validation:MinItems:=1
89+
ContainerPolicies []ContainerPolicy `json:"containerPolicies,omitempty"`
90+
}
91+
6392
// StartupCPUBoostSpec defines the desired state of StartupCPUBoost
6493
type StartupCPUBoostSpec struct {
94+
// ResourcePolicy specifies policies for container resource increase
95+
ResourcePolicy ResourcePolicy `json:"resourcePolicy,omitempty"`
6596
// DurationPolicy specifies policies for resource boost duration
6697
// +kubebuilder:validation:Required
6798
DurationPolicy DurationPolicy `json:"durationPolicy,omitempty"`
68-
// BootPercent defines the percent of CPU request increase that POD will get
69-
// during the CPU boost time period
70-
// +kubebuilder:validation:Required
71-
// +kubebuilder:validation:Minimum:=1
72-
BoostPercent int64 `json:"boostPercent,omitempty"`
7399
}
74100

75101
// StartupCPUBoostStatus defines the observed state of StartupCPUBoost

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/autoscaling.x-k8s.io_startupcpuboosts.yaml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ spec:
7878
spec:
7979
description: StartupCPUBoostSpec defines the desired state of StartupCPUBoost
8080
properties:
81-
boostPercent:
82-
description: BootPercent defines the percent of CPU request increase
83-
that POD will get during the CPU boost time period
84-
format: int64
85-
minimum: 1
86-
type: integer
8781
durationPolicy:
8882
description: DurationPolicy specifies policies for resource boost
8983
duration
@@ -114,6 +108,35 @@ spec:
114108
type: string
115109
type: object
116110
type: object
111+
resourcePolicy:
112+
description: ResourcePolicy specifies policies for container resource
113+
increase
114+
properties:
115+
containerPolicies:
116+
description: ContainerPolicies specifies resource policies for
117+
the containers
118+
items:
119+
description: ContainerPolicy defines the policy used to determine
120+
the target resources for a container
121+
properties:
122+
containerName:
123+
description: ContainerName specifies the name of container
124+
for a given policy
125+
type: string
126+
percentageIncrease:
127+
description: PercentageIncrease specifies the percentage
128+
increase policy for a container
129+
properties:
130+
value:
131+
description: Value specifies the percentage value
132+
format: int64
133+
minimum: 1
134+
type: integer
135+
type: object
136+
type: object
137+
minItems: 1
138+
type: array
139+
type: object
117140
type: object
118141
status:
119142
description: StartupCPUBoostStatus defines the observed state of StartupCPUBoost

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ require (
6060
golang.org/x/text v0.13.0 // indirect
6161
golang.org/x/time v0.3.0 // indirect
6262
golang.org/x/tools v0.9.3 // indirect
63-
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
63+
gomodules.xyz/jsonpatch/v2 v2.3.0
6464
google.golang.org/appengine v1.6.7 // indirect
6565
google.golang.org/protobuf v1.30.0 // indirect
6666
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl
163163
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
164164
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
165165
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
166-
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
166+
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
167167
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
168168
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
169169
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

internal/boost/boost_suite_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,42 @@ func TestBoost(t *testing.T) {
3333
}
3434

3535
var (
36-
podTemplate *corev1.Pod
37-
annotTemplate *bpod.BoostPodAnnotation
38-
specTemplate *autoscaling.StartupCPUBoost
36+
podTemplate *corev1.Pod
37+
annotTemplate *bpod.BoostPodAnnotation
38+
specTemplate *autoscaling.StartupCPUBoost
39+
containerOneName string
40+
containerTwoName string
41+
containerOnePercValue int64
42+
containerTwoPercValue int64
3943
)
4044

4145
var _ = BeforeSuite(func() {
46+
containerOneName = "container-one"
47+
containerTwoName = "container-two"
48+
containerOnePercValue = 120
49+
containerTwoPercValue = 100
4250
specTemplate = &autoscaling.StartupCPUBoost{
4351
ObjectMeta: metav1.ObjectMeta{
4452
Name: "boost-001",
4553
Namespace: "demo",
4654
},
4755
Spec: autoscaling.StartupCPUBoostSpec{
48-
BoostPercent: 55,
56+
ResourcePolicy: autoscaling.ResourcePolicy{
57+
ContainerPolicies: []autoscaling.ContainerPolicy{
58+
{
59+
ContainerName: containerOneName,
60+
PercentageIncrease: autoscaling.PercentageIncrease{
61+
Value: containerOnePercValue,
62+
},
63+
},
64+
{
65+
ContainerName: containerTwoName,
66+
PercentageIncrease: autoscaling.PercentageIncrease{
67+
Value: containerTwoPercValue,
68+
},
69+
},
70+
},
71+
},
4972
},
5073
}
5174
annotTemplate = &bpod.BoostPodAnnotation{

internal/boost/policy/fixed.go renamed to internal/boost/duration/fixed.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package policy
15+
package duration
1616

1717
import (
1818
"time"
@@ -31,11 +31,11 @@ type FixedDurationPolicy struct {
3131
duration time.Duration
3232
}
3333

34-
func NewFixedDurationPolicy(duration time.Duration) DurationPolicy {
34+
func NewFixedDurationPolicy(duration time.Duration) Policy {
3535
return NewFixedDurationPolicyWithTimeFunc(time.Now, duration)
3636
}
3737

38-
func NewFixedDurationPolicyWithTimeFunc(timeFunc TimeFunc, duration time.Duration) DurationPolicy {
38+
func NewFixedDurationPolicyWithTimeFunc(timeFunc TimeFunc, duration time.Duration) Policy {
3939
return &FixedDurationPolicy{
4040
timeFunc: timeFunc,
4141
duration: duration,

internal/boost/policy/fixed_test.go renamed to internal/boost/duration/fixed_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,43 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package policy_test
15+
package duration_test
1616

1717
import (
1818
"time"
1919

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

2626
var _ = Describe("FixedDurationPolicy", func() {
27-
var policy bpolicy.DurationPolicy
27+
var policy duration.Policy
2828
var now time.Time
29-
var duration time.Duration
30-
var timeFunc bpolicy.TimeFunc
29+
var timeDuration time.Duration
30+
var timeFunc duration.TimeFunc
3131

3232
BeforeEach(func() {
3333
now = time.Now()
34-
duration = 5 * time.Second
34+
timeDuration = 5 * time.Second
3535
timeFunc = func() time.Time {
3636
return now
3737
}
38-
policy = bpolicy.NewFixedDurationPolicyWithTimeFunc(timeFunc, duration)
38+
policy = duration.NewFixedDurationPolicyWithTimeFunc(timeFunc, timeDuration)
3939
})
4040

4141
Describe("Validates POD", func() {
4242
When("the life time of a POD exceeds the policy duration", func() {
4343
It("returns policy is not valid", func() {
44-
creationTimesamp := now.Add(-1 * duration).Add(-1 * time.Minute)
44+
creationTimesamp := now.Add(-1 * timeDuration).Add(-1 * time.Minute)
4545
pod.CreationTimestamp = metav1.NewTime(creationTimesamp)
4646
Expect(policy.Valid(pod)).To(BeFalse())
4747
})
4848
})
4949
When("the life time of a POD is within policy duration", func() {
5050
It("returns policy is valid", func() {
51-
creationTimesamp := now.Add(-1 * duration).Add(1 * time.Minute)
51+
creationTimesamp := now.Add(-1 * timeDuration).Add(1 * time.Minute)
5252
pod.CreationTimestamp = metav1.NewTime(creationTimesamp)
5353
Expect(policy.Valid(pod)).To(BeTrue())
5454
})

internal/boost/policy/podcondition.go renamed to internal/boost/duration/podcondition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package policy
15+
package duration
1616

1717
import (
1818
corev1 "k8s.io/api/core/v1"
@@ -27,7 +27,7 @@ type PodConditionPolicy struct {
2727
status corev1.ConditionStatus
2828
}
2929

30-
func NewPodConditionPolicy(condition corev1.PodConditionType, status corev1.ConditionStatus) DurationPolicy {
30+
func NewPodConditionPolicy(condition corev1.PodConditionType, status corev1.ConditionStatus) Policy {
3131
return &PodConditionPolicy{
3232
condition: condition,
3333
status: status,

0 commit comments

Comments
 (0)