From 5324dc209831228005d5ee0d5705f9c2fa6b5615 Mon Sep 17 00:00:00 2001 From: Marco Colli Date: Thu, 20 Jun 2024 14:21:18 +0200 Subject: [PATCH] Add autoscaling using HorizontalPodAutoscaler (close #14) --- lib/cuber/cuberfile_validator.rb | 2 +- lib/cuber/templates/deployment.yml.erb | 30 +++++++++++++++++++++++++- lib/cuber/version.rb | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/cuber/cuberfile_validator.rb b/lib/cuber/cuberfile_validator.rb index fdb6b11..7b58314 100644 --- a/lib/cuber/cuberfile_validator.rb +++ b/lib/cuber/cuberfile_validator.rb @@ -81,7 +81,7 @@ def validate_procs @options[:procs].each do |procname, proc| @errors << "proc \"#{procname}\" name can only include lowercase letters" if procname !~ /\A[a-z]+\z/ @errors << "proc \"#{procname}\" command must be present" if proc[:cmd].to_s.strip.empty? - @errors << "proc \"#{procname}\" scale must be a positive number" unless proc[:scale].is_a?(Integer) && proc[:scale] > 0 + @errors << "proc \"#{procname}\" scale must be a positive number or a range" unless (proc[:scale].is_a?(Integer) && proc[:scale] > 0) || (proc[:scale].is_a?(Range) && proc[:scale].minmax.all? { |m| m.is_a?(Integer) && m > 0 }) @errors << "proc \"#{procname}\" cpu must be a positive number" unless proc[:cpu].nil? || proc[:cpu].is_a?(Numeric) && proc[:cpu] > 0 @errors << "proc \"#{procname}\" ram must be a positive number" unless proc[:ram].nil? || proc[:ram].is_a?(Numeric) && proc[:ram] > 0 @errors << "proc \"#{procname}\" term must be a positive number" unless proc[:term].is_a?(Integer) && proc[:term] > 0 diff --git a/lib/cuber/templates/deployment.yml.erb b/lib/cuber/templates/deployment.yml.erb index 1f19b8f..58f5c8e 100644 --- a/lib/cuber/templates/deployment.yml.erb +++ b/lib/cuber/templates/deployment.yml.erb @@ -131,7 +131,7 @@ metadata: app.kubernetes.io/managed-by: cuber spec: revisionHistoryLimit: 0 - replicas: <%= proc[:scale] %> + replicas: <%= proc[:scale].is_a?(Range) ? proc[:scale].min : proc[:scale] %> selector: matchLabels: app: <%= procname %>-proc @@ -225,6 +225,34 @@ spec: terminationGracePeriodSeconds: <%= proc[:term] %> <%- end -%> +<%- @options[:procs].select { |procname, proc| proc[:scale].is_a?(Range) }.each do |procname, proc| -%> +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: scale-<%= procname %> + namespace: <%= @options[:app] %> + labels: + app.kubernetes.io/name: <%= @options[:app].to_s.to_json %> + app.kubernetes.io/instance: <%= @options[:instance].to_s.to_json %> + app.kubernetes.io/version: <%= @options[:release].to_s.to_json %> + app.kubernetes.io/managed-by: cuber +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: <%= procname %> + minReplicas: <%= proc[:scale].min %> + maxReplicas: <%= proc[:scale].max %> + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 60 +<%- end -%> + <%- @options[:cron].each do |jobname, cron| -%> --- apiVersion: batch/v1 diff --git a/lib/cuber/version.rb b/lib/cuber/version.rb index 97d80c8..d322716 100644 --- a/lib/cuber/version.rb +++ b/lib/cuber/version.rb @@ -1,3 +1,3 @@ module Cuber - VERSION = '1.10.0'.freeze + VERSION = '1.11.0'.freeze end