runtime: resource limits (kubernetes implementation) (#1931)
* runtime: add resource limit CreateOptions * util/kubernetes/client: implement support for resource limits * runtime/kubernetes: set resource limits for k8s deployments * util/kubernetes: remove template check for ints * util/kubernetes: fix incorrect yaml syntax * runtime/kubernetes: fix incorrect units * runtime: update create options to use Resources struct
This commit is contained in:
parent
374aae1490
commit
5a88ea7247
@ -2,6 +2,7 @@ package kubernetes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -102,6 +103,22 @@ func newService(s *runtime.Service, c runtime.CreateOptions) *service {
|
||||
kdeploy.Spec.Template.PodSpec.Containers[0].Args = c.Args
|
||||
}
|
||||
|
||||
// apply resource limits
|
||||
if c.Resources != nil {
|
||||
resLimits := &client.ResourceLimits{}
|
||||
if c.Resources.CPU > 0 {
|
||||
resLimits.CPU = fmt.Sprintf("%vm", c.Resources.CPU)
|
||||
}
|
||||
if c.Resources.Mem > 0 {
|
||||
resLimits.Memory = fmt.Sprintf("%vMi", c.Resources.Mem)
|
||||
}
|
||||
if c.Resources.Disk > 0 {
|
||||
resLimits.EphemeralStorage = fmt.Sprintf("%vMi", c.Resources.Disk)
|
||||
}
|
||||
|
||||
kdeploy.Spec.Template.PodSpec.Containers[0].Resources = &client.ResourceRequirements{Limits: resLimits}
|
||||
}
|
||||
|
||||
return &service{
|
||||
Service: s,
|
||||
kservice: kservice,
|
||||
|
@ -84,6 +84,8 @@ type CreateOptions struct {
|
||||
Context context.Context
|
||||
// Secrets to use
|
||||
Secrets map[string]string
|
||||
// Resources to allocate the service
|
||||
Resources *Resources
|
||||
}
|
||||
|
||||
// ReadOptions queries runtime services
|
||||
@ -176,6 +178,13 @@ func WithOutput(out io.Writer) CreateOption {
|
||||
}
|
||||
}
|
||||
|
||||
// ResourceLimits sets the resources for the service to use
|
||||
func ResourceLimits(r *Resources) CreateOption {
|
||||
return func(o *CreateOptions) {
|
||||
o.Resources = r
|
||||
}
|
||||
}
|
||||
|
||||
// ReadService returns services with the given name
|
||||
func ReadService(service string) ReadOption {
|
||||
return func(o *ReadOptions) {
|
||||
|
@ -104,3 +104,16 @@ type Service struct {
|
||||
// Metadata stores metadata
|
||||
Metadata map[string]string
|
||||
}
|
||||
|
||||
// Resources which are allocated to a serivce
|
||||
type Resources struct {
|
||||
// CPU is the maximum amount of CPU the service will be allocated (unit millicpu)
|
||||
// e.g. 0.25CPU would be passed as 250
|
||||
CPU int
|
||||
// Mem is the maximum amount of memory the service will be allocated (unit mebibyte)
|
||||
// e.g. 128 MiB of memory would be passed as 128
|
||||
Mem int
|
||||
// Disk is the maximum amount of disk space the service will be allocated (unit mebibyte)
|
||||
// e.g. 128 MiB of memory would be passed as 128
|
||||
Disk int
|
||||
}
|
||||
|
@ -90,8 +90,8 @@ spec:
|
||||
{{- range . }}
|
||||
- containerPort: {{ .ContainerPort }}
|
||||
name: {{ .Name }}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if .ReadinessProbe }}
|
||||
{{- with .ReadinessProbe }}
|
||||
readinessProbe:
|
||||
@ -106,6 +106,39 @@ spec:
|
||||
periodSeconds: {{ .PeriodSeconds }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if .Resources }}
|
||||
{{- with .Resources }}
|
||||
resources:
|
||||
{{- if .Limits }}
|
||||
{{- with .Limits }}
|
||||
limits:
|
||||
{{- if .Memory }}
|
||||
memory: {{ .Memory }}
|
||||
{{- end }}
|
||||
{{- if .CPU }}
|
||||
cpu: {{ .CPU }}
|
||||
{{- end }}
|
||||
{{- if .EphemeralStorage }}
|
||||
ephemeral-storage: {{ .EphemeralStorage }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if .Requests }}
|
||||
{{- with .Requests }}
|
||||
requests:
|
||||
{{- if .Memory }}
|
||||
memory: {{ .Memory }}
|
||||
{{- end }}
|
||||
{{- if .CPU }}
|
||||
cpu: {{ .CPU }}
|
||||
{{- end }}
|
||||
{{- if .EphemeralStorage }}
|
||||
ephemeral-storage: {{ .EphemeralStorage }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
`
|
||||
|
@ -35,13 +35,14 @@ type Condition struct {
|
||||
|
||||
// Container defined container runtime values
|
||||
type Container struct {
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Env []EnvVar `json:"env,omitempty"`
|
||||
Command []string `json:"command,omitempty"`
|
||||
Args []string `json:"args,omitempty"`
|
||||
Ports []ContainerPort `json:"ports,omitempty"`
|
||||
ReadinessProbe *Probe `json:"readinessProbe,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Env []EnvVar `json:"env,omitempty"`
|
||||
Command []string `json:"command,omitempty"`
|
||||
Args []string `json:"args,omitempty"`
|
||||
Ports []ContainerPort `json:"ports,omitempty"`
|
||||
ReadinessProbe *Probe `json:"readinessProbe,omitempty"`
|
||||
Resources *ResourceRequirements `json:"resources,omitempty"`
|
||||
}
|
||||
|
||||
// DeploymentSpec defines micro deployment spec
|
||||
@ -234,3 +235,16 @@ type TCPSocketAction struct {
|
||||
Host string `json:"host,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
}
|
||||
|
||||
// ResourceRequirements describes the compute resource requirements.
|
||||
type ResourceRequirements struct {
|
||||
Limits *ResourceLimits `json:"limits,omitempty"`
|
||||
Requests *ResourceLimits `json:"requests,omitempty"`
|
||||
}
|
||||
|
||||
// ResourceLimits describes the limits for a service
|
||||
type ResourceLimits struct {
|
||||
Memory string `json:"memory,omitempty"`
|
||||
CPU string `json:"cpu,omitempty"`
|
||||
EphemeralStorage string `json:"ephemeral-storage,omitempty"`
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user