Skip to content

Commit

Permalink
allow deploying with only pod-less resources
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Dec 27, 2018
1 parent eb7f267 commit 06d71f7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 32 deletions.
13 changes: 8 additions & 5 deletions plugins/kubernetes/app/models/kubernetes/release_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class ReleaseDoc < ActiveRecord::Base
belongs_to :deploy_group

serialize :resource_template, JSON
delegate :desired_pod_count, :prerequisite?, to: :primary_resource
delegate :build_selectors, to: :verification_template

validates :deploy_group, presence: true
Expand Down Expand Up @@ -56,20 +55,24 @@ def resources
# Temporary template we run validations on ... so can be cheap / not fully fleshed out
# and only be the primary since services/configmaps are not very interesting anyway
def verification_template
primary_config = raw_template.detect { |e| Kubernetes::RoleConfigFile.primary?(e) }
primary_config = raw_template.detect { |e| Kubernetes::RoleConfigFile.primary?(e) } || raw_template.first
Kubernetes::TemplateFiller.new(self, primary_config, index: 0)
end

def blue_green_color
kubernetes_release.blue_green_color if kubernetes_role.blue_green?
end

private
def prerequisite?
resources.any?(&:prerequisite?)
end

def primary_resource
resources.detect(&:primary?)
def desired_pod_count
resources.sum(&:desired_pod_count)
end

private

def resource_template=(value)
@resource_template = nil
super
Expand Down
10 changes: 5 additions & 5 deletions plugins/kubernetes/app/models/kubernetes/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ def prerequisite?
@template.dig(*RoleConfigFile::PREREQUISITE)
end

def primary?
Kubernetes::RoleConfigFile.primary?(@template)
end

def deploy
if exist?
if @delete_resource
Expand Down Expand Up @@ -86,7 +82,11 @@ def uid
end

def desired_pod_count
@delete_resource ? 0 : replica_source.dig(:spec, :replicas) || 1
if @delete_resource
0
else
replica_source.dig(:spec, :replicas) || (RoleConfigFile.primary?(@template) ? 1 : 0)
end
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class RoleConfigFile
SERVICE_KINDS = ['Service'].freeze
PREREQUISITE = [:metadata, :annotations, :'samson/prerequisite'].freeze

# TODO: rename to has_pods? or so
def self.primary?(resource)
templates(resource).any?
end
Expand Down
6 changes: 4 additions & 2 deletions plugins/kubernetes/app/models/kubernetes/role_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ def validate_env_values

def validate_prerequisites
allowed = ["Job", "Pod"]
bad = (@elements + templates).any? do |e|
bad = (@elements + templates).select do |e|
e.dig(*RoleConfigFile::PREREQUISITE) && !allowed.include?(e[:kind]) && RoleConfigFile.primary?(e)
end
@errors << "Only elements with type #{allowed.join(", ")} can be prerequisites." if bad
bad.compact!
bad.map! { |e| e[:kind] }
@errors << "Elements with type #{bad.join(", ")} cannot be prerequisites." if bad.any?
end

# comparing all directories with trailing / so we can use simple matching logic
Expand Down
9 changes: 5 additions & 4 deletions plugins/kubernetes/app/models/kubernetes/template_filler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ def expand_secret_annotations
end

def pod_annotations
pod_template[:metadata][:annotations] ||= {}
pod_template ? pod_template[:metadata][:annotations] ||= {} : {}
end

def pod_template
@pod_template ||= RoleConfigFile.templates(template).first
return @pod_template if defined?(@pod_template)
@pod_template = RoleConfigFile.templates(template).first
end

def secret_annotations
Expand Down Expand Up @@ -532,11 +533,11 @@ def set_pre_stop
end

def init_containers
@init_containers ||= Api::Pod.init_containers(pod_template)
@init_containers ||= pod_template ? Api::Pod.init_containers(pod_template) : []
end

def containers
pod_template.dig_fetch(:spec, :containers)
pod_template ? pod_template.dig_fetch(:spec, :containers) : []
end
end
end
6 changes: 4 additions & 2 deletions plugins/kubernetes/test/models/kubernetes/release_doc_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,16 @@ def create!
end

describe "#desired_pod_count" do
it "delegates to primary resource" do
it "delegates to resource" do
doc.desired_pod_count.must_equal 2
end
end

describe "#prerequisite?" do
it "delegates to primary resource" do
it "delegates to resources" do
refute doc.prerequisite?
doc.resources.first.stubs(:prerequisite?).returns(true)
assert doc.prerequisite?
end
end

Expand Down
19 changes: 6 additions & 13 deletions plugins/kubernetes/test/models/kubernetes/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,6 @@ def assert_create_and_delete_requests(**args, &block)
end
end

describe "#primary?" do
it "is primary when it is a primary resource" do
template[:kind] = "Deployment"
assert resource.primary?
end

it "is not primary when it is a secondary resource" do
template[:spec][:template][:spec].delete :containers
refute resource.primary?
end
end

describe "#desired_pod_count" do
it "reads the value from config" do
template[:spec] = {replicas: 3}
Expand All @@ -320,11 +308,16 @@ def assert_create_and_delete_requests(**args, &block)
end
end

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

it "is 0 when not set for config" do
template.delete :spec
resource.desired_pod_count.must_equal 0
end

it "is 0 when pod is deleted" do
delete_resource!
resource.desired_pod_count.must_equal 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@

it "reports invalid prerequisites" do
role.first[:kind] = "Deployment"
errors.must_include "Only elements with type Job, Pod can be prerequisites."
errors.must_include "Elements with type Deployment cannot be prerequisites."
end

it "allows static configuration" do
Expand Down
10 changes: 10 additions & 0 deletions plugins/kubernetes/test/models/kubernetes/template_filler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,11 @@ def secret_annotations(hash)
template.send(:verify_env)
end

it "passes when resource has no containers" do
raw_template.delete :spec
template.send(:verify_env)
end

describe "when something is required" do
let(:pod_annotations) { raw_template[:spec][:template][:metadata][:annotations] = {} }

Expand Down Expand Up @@ -954,6 +959,11 @@ def secret_annotations(hash)
template.build_selectors.must_equal []
end

it "returns empty when resource has no containers" do
raw_template.delete :spec
template.build_selectors.must_equal []
end

describe "when user selected to not enforce docker images" do
with_env KUBERNETES_ADDITIONAL_CONTAINERS_WITHOUT_DOCKERFILE: 'true'

Expand Down

0 comments on commit 06d71f7

Please sign in to comment.