Class: Kube::Cluster::Manifest::Middleware::HPAForDeployment
- Inherits:
-
Kube::Cluster::Manifest::Middleware
- Object
- Kube::Cluster::Manifest::Middleware
- Kube::Cluster::Manifest::Middleware::HPAForDeployment
- Defined in:
- lib/kube/cluster/manifest/middleware/hpa_for_deployment.rb
Overview
Generates a HorizontalPodAutoscaler for every pod-bearing resource that carries the app.kubernetes.io/autoscale label.
The label value encodes the min and max replicas as “min-max”:
.labels = { "app.kubernetes.io/autoscale": "1-5" }
Options:
cpu: — target CPU utilization percentage (default: 75)
memory: — target memory utilization percentage (default: 80)
stack do
use Middleware::HPAForDeployment
use Middleware::HPAForDeployment, cpu: 60, memory: 70
end
Constant Summary collapse
- LABEL =
:"app.kubernetes.io/autoscale"
Constants inherited from Kube::Cluster::Manifest::Middleware
CLUSTER_SCOPED_KINDS, POD_BEARING_KINDS
Instance Method Summary collapse
- #call(resource) ⇒ Object
-
#initialize(cpu: 75, memory: 80) ⇒ HPAForDeployment
constructor
A new instance of HPAForDeployment.
Constructor Details
#initialize(cpu: 75, memory: 80) ⇒ HPAForDeployment
Returns a new instance of HPAForDeployment.
26 27 28 29 |
# File 'lib/kube/cluster/manifest/middleware/hpa_for_deployment.rb', line 26 def initialize(cpu: 75, memory: 80) @cpu = cpu @memory = memory end |
Instance Method Details
#call(resource) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kube/cluster/manifest/middleware/hpa_for_deployment.rb', line 31 def call(resource) return resource unless pod_bearing?(resource) value = label(resource, LABEL) return resource unless value min, max = parse_range(value) h = resource.to_h name = h.dig(:metadata, :name) namespace = h.dig(:metadata, :namespace) labels = h.dig(:metadata, :labels) || {} api_version = h[:apiVersion] || "apps/v1" resource_kind = kind(resource) # Capture ivars as locals — the block runs via instance_exec # on a BlackHoleStruct, so @ivars would resolve on the BHS. cpu_target = @cpu memory_target = @memory hpa = Kube::Schema["HorizontalPodAutoscaler"].new { .name = name .namespace = namespace if namespace .labels = labels.reject { |k, _| k == LABEL } spec.scaleTargetRef = { apiVersion: api_version, kind: resource_kind, name: name, } spec.minReplicas = min spec.maxReplicas = max spec.metrics = [ { type: "Resource", resource: { name: "cpu", target: { type: "Utilization", averageUtilization: cpu_target }, }, }, { type: "Resource", resource: { name: "memory", target: { type: "Utilization", averageUtilization: memory_target }, }, }, ] } [resource, hpa] end |