Skip to content

Commit 1a463e0

Browse files
committed
resource requests and limits
1 parent ba37d16 commit 1a463e0

19 files changed

+254
-148
lines changed

api/v1alpha1/common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,22 @@ const (
154154
DefaultCluster string = "kubernetes"
155155
)
156156

157+
func validResourceRequirement(requirements corev1.ResourceRequirements) bool {
158+
return validResource(requirements.Requests) && validResource(requirements.Limits) &&
159+
requirements.Requests.Memory().Cmp(*requirements.Limits.Memory()) < 0 &&
160+
requirements.Requests.Cpu().Cmp(*requirements.Limits.Cpu()) < 0
161+
}
162+
157163
func validResource(resources corev1.ResourceList) bool {
158164
// cpu & memory > 0 and storage >= 0
159165
return resources.Cpu().Sign() == 1 &&
160166
resources.Memory().Sign() == 1 &&
161167
resources.Storage().Sign() >= 0
162168
}
169+
170+
func paddingResourceLimit(requirement *corev1.ResourceRequirements) {
171+
// TODO: better padding calculation
172+
requirement.Limits.Cpu().Set(requirement.Requests.Cpu().Value())
173+
requirement.Limits.Memory().Set(requirement.Requests.Memory().Value())
174+
requirement.Limits.Storage().Set(requirement.Requests.Storage().Value())
175+
}

api/v1alpha1/function_types.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ import (
2929
type FunctionSpec struct {
3030
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
3131
// Important: Run "make" to regenerate code after modifying this file
32-
Name string `json:"name,omitempty"`
33-
ClassName string `json:"className,omitempty"`
34-
Tenant string `json:"tenant,omitempty"`
35-
ClusterName string `json:"clusterName,omitempty"`
36-
SourceType string `json:"sourceType,omitempty"`
37-
SinkType string `json:"sinkType,omitempty"`
38-
Replicas *int32 `json:"replicas,omitempty"`
39-
MaxReplicas *int32 `json:"maxReplicas,omitempty"` // if provided, turn on autoscaling
40-
Input InputConf `json:"input,omitempty"`
41-
Output OutputConf `json:"output,omitempty"`
42-
LogTopic string `json:"logTopic,omitempty"`
43-
FuncConfig map[string]string `json:"funcConfig,omitempty"`
44-
Resources corev1.ResourceList `json:"resources,omitempty"`
45-
SecretsMap map[string]SecretRef `json:"secretsMap,omitempty"`
32+
Name string `json:"name,omitempty"`
33+
ClassName string `json:"className,omitempty"`
34+
Tenant string `json:"tenant,omitempty"`
35+
ClusterName string `json:"clusterName,omitempty"`
36+
SourceType string `json:"sourceType,omitempty"`
37+
SinkType string `json:"sinkType,omitempty"`
38+
Replicas *int32 `json:"replicas,omitempty"`
39+
MaxReplicas *int32 `json:"maxReplicas,omitempty"` // if provided, turn on autoscaling
40+
Input InputConf `json:"input,omitempty"`
41+
Output OutputConf `json:"output,omitempty"`
42+
LogTopic string `json:"logTopic,omitempty"`
43+
FuncConfig map[string]string `json:"funcConfig,omitempty"`
44+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
45+
SecretsMap map[string]SecretRef `json:"secretsMap,omitempty"`
4646

4747
Timeout int32 `json:"timeout,omitempty"`
4848
AutoAck *bool `json:"autoAck,omitempty"`

api/v1alpha1/function_webhook.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ func (r *Function) Default() {
8282
r.Spec.ForwardSourceMessageProperty = &trueVal
8383
}
8484

85-
if r.Spec.Resources.Cpu() == nil {
86-
r.Spec.Resources.Cpu().Set(int64(1))
85+
if r.Spec.Resources.Requests.Cpu() == nil {
86+
r.Spec.Resources.Requests.Cpu().Set(int64(1))
8787
}
8888

89-
if r.Spec.Resources.Memory() == nil {
90-
r.Spec.Resources.Memory().Set(int64(1073741824))
89+
if r.Spec.Resources.Requests.Memory() == nil {
90+
r.Spec.Resources.Requests.Memory().Set(int64(1073741824))
9191
}
9292

93+
if r.Spec.Resources.Limits == nil {
94+
paddingResourceLimit(&r.Spec.Resources)
95+
}
9396
}
9497

9598
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
@@ -118,8 +121,8 @@ func (r *Function) ValidateCreate() error {
118121
return errors.New("maxReplicas must be greater than or equal to replicas")
119122
}
120123

121-
if !validResource(r.Spec.Resources) {
122-
return errors.New("resource request is invalid. each resource value must be positive")
124+
if !validResourceRequirement(r.Spec.Resources) {
125+
return errors.New("resource requirement is invalid")
123126
}
124127

125128
if r.Spec.Timeout != 0 && r.Spec.ProcessingGuarantee != AtleastOnce {

api/v1alpha1/sink_types.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ import (
2929
type SinkSpec struct {
3030
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
3131
// Important: Run "make" to regenerate code after modifying this file
32-
Name string `json:"name,omitempty"`
33-
ClassName string `json:"className,omitempty"`
34-
ClusterName string `json:"clusterName,omitempty"`
35-
Tenant string `json:"tenant,omitempty"`
36-
SourceType string `json:"sourceType,omitempty"`
37-
SinkType string `json:"sinkType,omitempty"`
38-
Replicas *int32 `json:"replicas,omitempty"`
39-
MaxReplicas *int32 `json:"maxReplicas,omitempty"` // if provided, turn on autoscaling
40-
Input InputConf `json:"input,omitempty"`
41-
SinkConfig map[string]string `json:"sinkConfig,omitempty"`
42-
Resources corev1.ResourceList `json:"resources,omitempty"`
43-
SecretsMap map[string]SecretRef `json:"secretsMap,omitempty"`
32+
Name string `json:"name,omitempty"`
33+
ClassName string `json:"className,omitempty"`
34+
ClusterName string `json:"clusterName,omitempty"`
35+
Tenant string `json:"tenant,omitempty"`
36+
SourceType string `json:"sourceType,omitempty"`
37+
SinkType string `json:"sinkType,omitempty"`
38+
Replicas *int32 `json:"replicas,omitempty"`
39+
MaxReplicas *int32 `json:"maxReplicas,omitempty"` // if provided, turn on autoscaling
40+
Input InputConf `json:"input,omitempty"`
41+
SinkConfig map[string]string `json:"sinkConfig,omitempty"`
42+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
43+
SecretsMap map[string]SecretRef `json:"secretsMap,omitempty"`
4444

4545
Timeout int32 `json:"timeout,omitempty"`
4646
NegativeAckRedeliveryDelayMs int32 `json:"negativeAckRedeliveryDelayMs,omitempty"`

api/v1alpha1/sink_webhook.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ func (r *Sink) Default() {
7272
r.Spec.Tenant = DefaultTenant
7373
}
7474

75-
if r.Spec.Resources.Cpu() == nil {
76-
r.Spec.Resources.Cpu().Set(int64(1))
75+
if r.Spec.Resources.Requests.Cpu() == nil {
76+
r.Spec.Resources.Requests.Cpu().Set(int64(1))
7777
}
7878

79-
if r.Spec.Resources.Memory() == nil {
80-
r.Spec.Resources.Memory().Set(int64(1073741824))
79+
if r.Spec.Resources.Requests.Memory() == nil {
80+
r.Spec.Resources.Requests.Memory().Set(int64(1073741824))
81+
}
82+
83+
if r.Spec.Resources.Limits == nil {
84+
paddingResourceLimit(&r.Spec.Resources)
8185
}
8286
}
8387

@@ -107,8 +111,8 @@ func (r *Sink) ValidateCreate() error {
107111
return errors.New("maxReplicas must be greater than or equal to replicas")
108112
}
109113

110-
if !validResource(r.Spec.Resources) {
111-
return errors.New("resource request is invalid. each resource value must be positive")
114+
if !validResourceRequirement(r.Spec.Resources) {
115+
return errors.New("resource requirement is invalid")
112116
}
113117

114118
if r.Spec.Timeout != 0 && r.Spec.ProcessingGuarantee != AtleastOnce {

api/v1alpha1/source_types.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ import (
2929
type SourceSpec struct {
3030
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
3131
// Important: Run "make" to regenerate code after modifying this file
32-
Name string `json:"name,omitempty"`
33-
ClassName string `json:"className,omitempty"`
34-
Tenant string `json:"tenant,omitempty"`
35-
ClusterName string `json:"clusterName,omitempty"`
36-
SourceType string `json:"sourceType,omitempty"`
37-
SinkType string `json:"sinkType,omitempty"`
38-
Replicas *int32 `json:"replicas,omitempty"`
39-
MaxReplicas *int32 `json:"maxReplicas,omitempty"` // if provided, turn on autoscaling
40-
Output OutputConf `json:"output,omitempty"`
41-
SourceConfig map[string]string `json:"sourceConfig,omitempty"`
42-
Resources corev1.ResourceList `json:"resources,omitempty"`
43-
SecretsMap map[string]SecretRef `json:"secretsMap,omitempty"`
44-
ProcessingGuarantee ProcessGuarantee `json:"processingGuarantee,omitempty"`
45-
RuntimeFlags string `json:"runtimeFlags,omitempty"`
32+
Name string `json:"name,omitempty"`
33+
ClassName string `json:"className,omitempty"`
34+
Tenant string `json:"tenant,omitempty"`
35+
ClusterName string `json:"clusterName,omitempty"`
36+
SourceType string `json:"sourceType,omitempty"`
37+
SinkType string `json:"sinkType,omitempty"`
38+
Replicas *int32 `json:"replicas,omitempty"`
39+
MaxReplicas *int32 `json:"maxReplicas,omitempty"` // if provided, turn on autoscaling
40+
Output OutputConf `json:"output,omitempty"`
41+
SourceConfig map[string]string `json:"sourceConfig,omitempty"`
42+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
43+
SecretsMap map[string]SecretRef `json:"secretsMap,omitempty"`
44+
ProcessingGuarantee ProcessGuarantee `json:"processingGuarantee,omitempty"`
45+
RuntimeFlags string `json:"runtimeFlags,omitempty"`
4646

4747
Messaging `json:",inline"`
4848
Runtime `json:",inline"`

api/v1alpha1/source_webhook.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ func (r *Source) Default() {
7272
r.Spec.Tenant = DefaultTenant
7373
}
7474

75-
if r.Spec.Resources.Cpu() == nil {
76-
r.Spec.Resources.Cpu().Set(int64(1))
75+
if r.Spec.Resources.Requests.Cpu() == nil {
76+
r.Spec.Resources.Requests.Cpu().Set(int64(1))
7777
}
7878

79-
if r.Spec.Resources.Memory() == nil {
80-
r.Spec.Resources.Memory().Set(int64(1073741824))
79+
if r.Spec.Resources.Requests.Memory() == nil {
80+
r.Spec.Resources.Requests.Memory().Set(int64(1073741824))
81+
}
82+
83+
if r.Spec.Resources.Limits == nil {
84+
paddingResourceLimit(&r.Spec.Resources)
8185
}
8286
}
8387

@@ -106,8 +110,8 @@ func (r *Source) ValidateCreate() error {
106110
return errors.New("maxReplicas must be greater than or equal to replicas")
107111
}
108112

109-
if !validResource(r.Spec.Resources) {
110-
return errors.New("resource request is invalid. each resource value must be positive")
113+
if !validResourceRequirement(r.Spec.Resources) {
114+
return errors.New("resource requirement is invalid")
111115
}
112116

113117
if r.Spec.Java == nil && r.Spec.Python == nil && r.Spec.Golang == nil {

api/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/cloud.streamnative.io_functionmeshes.yaml

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,31 @@ spec:
167167
format: int32
168168
type: integer
169169
resources:
170-
additionalProperties:
171-
anyOf:
172-
- type: integer
173-
- type: string
174-
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
175-
x-kubernetes-int-or-string: true
176-
description: ResourceList is a set of (resource name, quantity)
177-
pairs.
170+
description: ResourceRequirements describes the compute resource
171+
requirements.
172+
properties:
173+
limits:
174+
additionalProperties:
175+
anyOf:
176+
- type: integer
177+
- type: string
178+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
179+
x-kubernetes-int-or-string: true
180+
description: 'Limits describes the maximum amount of compute
181+
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/'
182+
type: object
183+
requests:
184+
additionalProperties:
185+
anyOf:
186+
- type: integer
187+
- type: string
188+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
189+
x-kubernetes-int-or-string: true
190+
description: 'Requests describes the minimum amount of compute
191+
resources required. If Requests is omitted for a container,
192+
it defaults to Limits if that is explicitly specified, otherwise
193+
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/'
194+
type: object
178195
type: object
179196
retainKeyOrdering:
180197
type: boolean
@@ -305,14 +322,31 @@ spec:
305322
format: int32
306323
type: integer
307324
resources:
308-
additionalProperties:
309-
anyOf:
310-
- type: integer
311-
- type: string
312-
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
313-
x-kubernetes-int-or-string: true
314-
description: ResourceList is a set of (resource name, quantity)
315-
pairs.
325+
description: ResourceRequirements describes the compute resource
326+
requirements.
327+
properties:
328+
limits:
329+
additionalProperties:
330+
anyOf:
331+
- type: integer
332+
- type: string
333+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
334+
x-kubernetes-int-or-string: true
335+
description: 'Limits describes the maximum amount of compute
336+
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/'
337+
type: object
338+
requests:
339+
additionalProperties:
340+
anyOf:
341+
- type: integer
342+
- type: string
343+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
344+
x-kubernetes-int-or-string: true
345+
description: 'Requests describes the minimum amount of compute
346+
resources required. If Requests is omitted for a container,
347+
it defaults to Limits if that is explicitly specified, otherwise
348+
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/'
349+
type: object
316350
type: object
317351
retainOrdering:
318352
type: boolean
@@ -418,14 +452,31 @@ spec:
418452
format: int32
419453
type: integer
420454
resources:
421-
additionalProperties:
422-
anyOf:
423-
- type: integer
424-
- type: string
425-
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
426-
x-kubernetes-int-or-string: true
427-
description: ResourceList is a set of (resource name, quantity)
428-
pairs.
455+
description: ResourceRequirements describes the compute resource
456+
requirements.
457+
properties:
458+
limits:
459+
additionalProperties:
460+
anyOf:
461+
- type: integer
462+
- type: string
463+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
464+
x-kubernetes-int-or-string: true
465+
description: 'Limits describes the maximum amount of compute
466+
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/'
467+
type: object
468+
requests:
469+
additionalProperties:
470+
anyOf:
471+
- type: integer
472+
- type: string
473+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
474+
x-kubernetes-int-or-string: true
475+
description: 'Requests describes the minimum amount of compute
476+
resources required. If Requests is omitted for a container,
477+
it defaults to Limits if that is explicitly specified, otherwise
478+
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/'
479+
type: object
429480
type: object
430481
runtimeFlags:
431482
type: string

0 commit comments

Comments
 (0)