diff --git a/plugins/kubernetes/app/models/kubernetes/template_filler.rb b/plugins/kubernetes/app/models/kubernetes/template_filler.rb index 595e70f96a..170437ee16 100644 --- a/plugins/kubernetes/app/models/kubernetes/template_filler.rb +++ b/plugins/kubernetes/app/models/kubernetes/template_filler.rb @@ -37,11 +37,7 @@ def to_hash(verification: false) set_history_limit end - if ['StatefulSet', 'Deployment'].include?(kind) - set_replica_target - else - validate_replica_target_is_supported - end + set_replica_target || validate_replica_target_is_supported make_stateful_set_match_service if kind == 'StatefulSet' set_pre_stop if kind == 'Deployment' @@ -278,7 +274,17 @@ def set_rc_unique_label_key end def set_replica_target - template.dig_set [:spec, :replicas], @doc.replica_target + key = [:spec, :replicas] + target = + if ['StatefulSet', 'Deployment'].include?(template[:kind]) + template + else + # custom resource that has replicas set on itself or it's template + containers = [template] + (template[:spec] || {}).values_at(*RoleConfigFile.template_keys(template)) + containers.detect { |c| c.dig(*key) } + end + + target&.dig_set key, @doc.replica_target end def validate_replica_target_is_supported diff --git a/plugins/kubernetes/test/models/kubernetes/template_filler_test.rb b/plugins/kubernetes/test/models/kubernetes/template_filler_test.rb index 2d8f978ca9..e3a1e7a0ff 100644 --- a/plugins/kubernetes/test/models/kubernetes/template_filler_test.rb +++ b/plugins/kubernetes/test/models/kubernetes/template_filler_test.rb @@ -144,6 +144,15 @@ def add_init_container(container) template.to_hash[:metadata][:name].must_equal "test-app-server" end + it "sets replicas for templates" do + raw_template[:kind] = "foobar" + raw_template[:spec].delete :replicas + raw_template[:spec][:template][:spec][:replicas] = 1 + result = template.to_hash + result[:spec][:replicas].must_be_nil + result[:spec][:template][:spec][:replicas].must_equal 2 + end + ['CustomResourceDefinition', 'APIService'].each do |kind| it "does not set override name for #{kind} since it follows a fixed naming pattern" do raw_template[:kind] = kind