Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
allow to set resources requests/limits of containers
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Jun 5, 2024
1 parent d7043c4 commit 37052da
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 10 deletions.
200 changes: 200 additions & 0 deletions charts/mahout/templates/mahout.anqou.net_mastodons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,56 @@ spec:
type: string
replicas:
type: integer
resources:
description: ResourceRequirements describes the compute resource
requirements.
properties:
claims:
description: "Claims lists the names of resources, defined
in spec.resourceClaims, that are used by this container.
\n This is an alpha field and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable. It can only be
set for containers."
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: Name must match the name of one entry in
pod.spec.resourceClaims of the Pod where this field
is used. It makes that resource available inside a
container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount of compute
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount of compute
resources required. If Requests is omitted for a container,
it defaults to Limits if that is explicitly specified, otherwise
to an implementation-defined value. Requests cannot exceed
Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
required:
- image
type: object
Expand All @@ -93,6 +143,56 @@ spec:
type: object
replicas:
type: integer
resources:
description: ResourceRequirements describes the compute resource
requirements.
properties:
claims:
description: "Claims lists the names of resources, defined
in spec.resourceClaims, that are used by this container.
\n This is an alpha field and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable. It can only be
set for containers."
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: Name must match the name of one entry in
pod.spec.resourceClaims of the Pod where this field
is used. It makes that resource available inside a
container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount of compute
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount of compute
resources required. If Requests is omitted for a container,
it defaults to Limits if that is explicitly specified, otherwise
to an implementation-defined value. Requests cannot exceed
Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
type: object
streaming:
properties:
Expand All @@ -102,6 +202,56 @@ spec:
type: object
replicas:
type: integer
resources:
description: ResourceRequirements describes the compute resource
requirements.
properties:
claims:
description: "Claims lists the names of resources, defined
in spec.resourceClaims, that are used by this container.
\n This is an alpha field and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable. It can only be
set for containers."
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: Name must match the name of one entry in
pod.spec.resourceClaims of the Pod where this field
is used. It makes that resource available inside a
container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount of compute
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount of compute
resources required. If Requests is omitted for a container,
it defaults to Limits if that is explicitly specified, otherwise
to an implementation-defined value. Requests cannot exceed
Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
type: object
web:
properties:
Expand All @@ -116,6 +266,56 @@ spec:
type: object
replicas:
type: integer
resources:
description: ResourceRequirements describes the compute resource
requirements.
properties:
claims:
description: "Claims lists the names of resources, defined
in spec.resourceClaims, that are used by this container.
\n This is an alpha field and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable. It can only be
set for containers."
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: Name must match the name of one entry in
pod.spec.resourceClaims of the Pod where this field
is used. It makes that resource available inside a
container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount of compute
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount of compute
resources required. If Requests is omitted for a container,
it defaults to Limits if that is explicitly specified, otherwise
to an implementation-defined value. Requests cannot exceed
Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
type: object
required:
- gateway
Expand Down
31 changes: 31 additions & 0 deletions e2e/e2e.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ let check_schema_migrations_count ~expected =
failwithf "check_schema_migrations_count: got %d, expected %d" count
expected

let check_deploy_resources ~n ?limits_cpu ?limits_memory ?requests_cpu
?requests_memory deploy_name =
let got_resources =
kubectl
(Printf.sprintf
{|get -n %s deploy %s -o json | jq -r '.spec.template.spec.containers[0].resources'|}
n deploy_name)
|> fst |> String.trim |> Yojson.Safe.from_string
|> Yojson.Safe.Util.to_assoc
in
let check_value key1 key2 expected =
let got =
let ( let* ) = Option.bind in
let* v1 = List.assoc_opt key1 got_resources in
let* v2 = v1 |> Yojson.Safe.Util.to_assoc |> List.assoc_opt key2 in
Yojson.Safe.Util.to_string_option v2
in
if got <> expected then
failwithf "check_deploy_resources: got '%s', expected '%s'"
(match got with None -> "None" | Some x -> "Some " ^ x)
(match expected with None -> "None" | Some x -> "Some " ^ x)
in
check_value "limits" "cpu" limits_cpu;
check_value "limits" "memory" limits_memory;
check_value "requests" "cpu" requests_cpu;
check_value "requests" "memory" requests_memory;
()

let check_deploy_annotation ~n deploy_name key expected_value =
let got_value =
kubectl
Expand Down Expand Up @@ -164,6 +192,9 @@ let () =
wait_deploy_available ~n:"e2e" "mastodon0-streaming";
wait_deploy_available ~n:"e2e" "mastodon0-web";

check_deploy_resources ~n:"e2e" ~limits_cpu:"1" ~limits_memory:"1000Mi"
~requests_cpu:"100m" ~requests_memory:"100Mi" "mastodon0-web";

check_deploy_annotation ~n:"e2e" "mastodon0-gateway-nginx"
"test.mahout.anqou.net/role" "gateway";
check_deploy_annotation ~n:"e2e" "mastodon0-sidekiq"
Expand Down
7 changes: 7 additions & 0 deletions e2e/manifests/mastodon0-v4.1.9.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ spec:
replicas: 1
annotations:
test.mahout.anqou.net/role: web
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: "1"
memory: 1000Mi
1 change: 0 additions & 1 deletion e2e/manifests/mastodon0-v4.2.0-restart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ spec:
test.mahout.anqou.net/role: web
periodicRestart:
schedule: "* * * * *"

14 changes: 13 additions & 1 deletion lib/k.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1227,9 +1227,20 @@ module Role_binding = struct
let make = Io_k8s_api_rbac_v1_role_binding.make
end

module Container = struct
include Io_k8s_api_core_v1_container

let with_ (x : t option) ?resources ~name () =
let x = match x with None -> make ~name:(name None) () | Some x -> x in
{
x with
resources =
(match resources with None -> x.resources | Some f -> f x.resources);
}
end

(* Not implemented *)
module Config_map_volume_source = Io_k8s_api_core_v1_config_map_volume_source
module Container = Io_k8s_api_core_v1_container
module Container_port = Io_k8s_api_core_v1_container_port
module Cron_job_spec = Io_k8s_api_batch_v1_cron_job_spec
module Empty_dir_volume_source = Io_k8s_api_core_v1_empty_dir_volume_source
Expand All @@ -1249,6 +1260,7 @@ module Subject = Io_k8s_api_rbac_v1_subject
module Tcp_socket_action = Io_k8s_api_core_v1_tcp_socket_action
module Volume = Io_k8s_api_core_v1_volume
module Volume_mount = Io_k8s_api_core_v1_volume_mount
module Resource_requirements = Io_k8s_api_core_v1_resource_requirements

let tls_authenticator =
try
Expand Down
27 changes: 19 additions & 8 deletions lib/mastodon_reconciler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ let create_or_update_mastodon_deployment client
~(mastodon : Net_anqou_mahout.V1alpha1.Mastodon.t) ~image ~kind =
let ( let* ) = Result.bind in

let spec = Option.get mastodon.spec in

let name = Option.get (Option.get mastodon.metadata).name in
let namespace = Option.get (Option.get mastodon.metadata).namespace in
let env_from = (Option.get mastodon.spec).env_from in
let env_from = spec.env_from in

let deploy_name = get_deploy_name name kind in
let selector, labels =
Expand All @@ -103,14 +105,13 @@ let create_or_update_mastodon_deployment client

let replicas =
match kind with
| `Sidekiq -> (Option.get (Option.get mastodon.spec).sidekiq).replicas
| `Streaming -> (Option.get (Option.get mastodon.spec).streaming).replicas
| `Web -> (Option.get (Option.get mastodon.spec).web).replicas
| `Sidekiq -> (Option.get spec.sidekiq).replicas
| `Streaming -> (Option.get spec.streaming).replicas
| `Web -> (Option.get spec.web).replicas
in
let replicas = Option.value ~default:1l replicas in

let deploy_annotations =
let spec = mastodon.spec |> Option.get in
(match kind with
| `Web -> (spec.web |> Option.get).annotations
| `Sidekiq -> (spec.sidekiq |> Option.get).annotations
Expand Down Expand Up @@ -138,15 +139,24 @@ let create_or_update_mastodon_deployment client
`Assoc annotations
in

let resources =
match kind with
| `Sidekiq -> (
match spec.sidekiq with None -> None | Some x -> x.resources)
| `Streaming -> (
match spec.streaming with None -> None | Some x -> x.resources)
| `Web -> ( match spec.web with None -> None | Some x -> x.resources)
in

let container =
let open K.Container in
match kind with
| `Sidekiq ->
make ~name:"sidekiq" ~image
make ~name:"sidekiq" ~image ?resources
~command:[ "bash"; "-c"; "bundle exec sidekiq" ]
~env_from ()
| `Streaming ->
make ~name:"streaming" ~image
make ~name:"streaming" ~image ?resources
~command:[ "bash"; "-c"; "node ./streaming" ]
~env_from
~ports:
Expand All @@ -168,7 +178,7 @@ let create_or_update_mastodon_deployment client
())
()
| `Web ->
make ~name:"web" ~image
make ~name:"web" ~image ?resources
~command:[ "bash"; "-c"; "bundle exec puma -C config/puma.rb" ]
~env_from
~ports:
Expand Down Expand Up @@ -340,6 +350,7 @@ let create_or_update_gateway client
K.Container.
[
make ~name:"copy-assets" ~image
?resources:(mastodon.spec |> Option.get).gateway.resources
~command:
[ "bash"; "-c"; "cp -ra /opt/mastodon/public/* /mnt/public/" ]
~volume_mounts:
Expand Down
Loading

0 comments on commit 37052da

Please sign in to comment.