Class: Kube::Cluster::Middleware::HPAForDeployment
- Inherits:
-
Kube::Cluster::Middleware
- Object
- Kube::Cluster::Middleware
- Kube::Cluster::Middleware::HPAForDeployment
- Defined in:
- lib/kube/cluster/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"
Instance Method Summary collapse
- #call(manifest) ⇒ 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.
25 26 27 28 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 25 def initialize(cpu: 75, memory: 80) @cpu = cpu @memory = memory end |
Instance Method Details
#call(manifest) ⇒ Object
30 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 83 84 85 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 30 def call(manifest) generated = [] manifest.resources.each do |resource| next unless resource.pod_bearing? value = resource.label(LABEL) next 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 = resource.kind # 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 generated << Kube::Cluster["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 }, }, }, ] } end manifest.resources.concat(generated) end |