Skip to content

Commit

Permalink
Merge pull request #2948 from zendesk/grosser/jobs
Browse files Browse the repository at this point in the history
PAAS-2142 do not allow setting 0/2+ replicas for resources that do not support …
  • Loading branch information
grosser authored Oct 3, 2018
2 parents 6b8477b + a9e23f0 commit 29c1d0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
6 changes: 1 addition & 5 deletions plugins/kubernetes/app/models/kubernetes/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def uid
end

def desired_pod_count
replica_source.dig_fetch(:spec, :replicas)
@delete_resource ? 0 : replica_source.dig(:spec, :replicas) || 1
end

private
Expand Down Expand Up @@ -463,10 +463,6 @@ def deploy
delete
create
end

def desired_pod_count
@delete_resource ? 0 : 1
end
end

class PodDisruptionBudget < Base
Expand Down
14 changes: 13 additions & 1 deletion plugins/kubernetes/app/models/kubernetes/template_filler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def to_hash(verification: false)
set_history_limit
end

set_replica_target unless ['DaemonSet', 'Pod'].include?(kind)
if ['StatefulSet', 'Deployment'].include?(kind)
set_replica_target
else
validate_replica_target_is_supported
end

make_stateful_set_match_service if kind == 'StatefulSet'
set_pre_stop if kind == 'Deployment'
Expand Down Expand Up @@ -262,6 +266,14 @@ def set_replica_target
template.dig_set [:spec, :replicas], @doc.replica_target
end

def validate_replica_target_is_supported
return if @doc.replica_target == 1 || (@doc.replica_target == 0 && @doc.delete_resource)
raise(
Samson::Hooks::UserError,
"A #{template[:kind]} can either have 1 replica or be marked for deletion."
)
end

def set_name
name = @doc.kubernetes_role.resource_name
name += "-#{blue_green_color}" if blue_green_color
Expand Down
21 changes: 10 additions & 11 deletions plugins/kubernetes/test/models/kubernetes/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ def delete_resource!
resource.desired_pod_count.must_equal 2
end
end

it "is 1 when not set" do
template[:spec].delete :replicas
resource.desired_pod_count.must_equal 1
end

it "is 0 when pod is deleted" do
delete_resource!
resource.desired_pod_count.must_equal 0
end
end

describe "#loop_sleep" do
Expand Down Expand Up @@ -848,17 +858,6 @@ def deployment_stub(replica_count)
end
end
end

describe "#desired_pod_count" do
it "is 1" do
resource.desired_pod_count.must_equal 1
end

it "is 0 when pod is deleted" do
delete_resource!
resource.desired_pod_count.must_equal 0
end
end
end

describe Kubernetes::Resource::HorizontalPodAutoscaler do
Expand Down
17 changes: 17 additions & 0 deletions plugins/kubernetes/test/models/kubernetes/template_filler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ def secret_annotations(hash)
before do
raw_template[:kind] = 'DaemonSet'
raw_template[:spec].delete(:replicas)
doc.replica_target = 1
end

it "does not add replicas since they are not supported" do
Expand All @@ -707,6 +708,7 @@ def secret_annotations(hash)
raw_template[:metadata].merge!(original_metadata)
raw_template[:kind] = "Pod"
raw_template[:spec].delete :replicas
doc.replica_target = 1
end

it "fills out everything" do
Expand All @@ -722,11 +724,26 @@ def secret_annotations(hash)
result = template.to_hash
refute result[:spec].key?(:replicas)
end

it "complains on invalid replica settings" do
doc.replica_target = 0
assert_raises(Samson::Hooks::UserError) { template.to_hash }

doc.replica_target = 2
assert_raises(Samson::Hooks::UserError) { template.to_hash }
end

it "allows deletion" do
doc.replica_target = 0
doc.delete_resource = true
template.to_hash
end
end

describe "cronjob" do
before do
raw_template.replace(YAML.safe_load(read_kubernetes_sample_file('kubernetes_cron_job.yml')).deep_symbolize_keys)
doc.replica_target = 1
end

it "works" do
Expand Down

0 comments on commit 29c1d0d

Please sign in to comment.