Skip to content

Commit

Permalink
Fix crashes with Service of type ExternalName
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Dec 17, 2017
1 parent 83099d6 commit 265eb3d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
19 changes: 17 additions & 2 deletions lib/kubernetes-deploy/kubernetes_resource/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def sync
end

def status
if @num_pods_selected.blank?
if !requires_endpoints?
"Doesn't require any endpoint"
elsif @num_pods_selected.blank?
"Failed to count related pods"
elsif selects_some_pods?
"Selects at least 1 pod"
Expand All @@ -21,6 +23,7 @@ def status
end

def deploy_succeeded?
return exists? unless requires_endpoints?
# We can't use endpoints if we want the service to be able to fail fast when the pods are down
exposes_zero_replica_deployment? || selects_some_pods?
end
Expand All @@ -44,28 +47,40 @@ def exposes_zero_replica_deployment?
@related_deployment_replicas == 0
end

def requires_endpoints?
return false if external_name_svc?
return true if @related_deployment_replicas.blank?
@related_deployment_replicas > 0
end

def selects_some_pods?
return false unless @num_pods_selected
@num_pods_selected > 0
end

def selector
@selector ||= @definition["spec"]["selector"].map { |k, v| "#{k}=#{v}" }.join(",")
@selector ||= @definition["spec"].fetch("selector", []).map { |k, v| "#{k}=#{v}" }.join(",")
end

def fetch_related_pod_count
return 0 unless selector.present?
raw_json, _err, st = kubectl.run("get", "pods", "--selector=#{selector}", "--output=json")
return unless st.success?
JSON.parse(raw_json)["items"].length
end

def fetch_related_replica_count
return 0 unless selector.present?
raw_json, _err, st = kubectl.run("get", "deployments", "--selector=#{selector}", "--output=json")
return unless st.success?

deployments = JSON.parse(raw_json)["items"]
return unless deployments.length == 1
deployments.first["spec"]["replicas"].to_i
end

def external_name_svc?
@definition["spec"]["type"] == "ExternalName"
end
end
end
11 changes: 11 additions & 0 deletions test/fixtures/hello-cloud/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ spec:
selector:
name: redis
---
apiVersion: v1
kind: Service
metadata:
name: redis-external
labels:
name: redis-external
app: hello-cloud
spec:
externalName: external-redis.shopify
type: ExternalName
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
Expand Down
6 changes: 3 additions & 3 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_full_hello_cloud_set_deploy_succeeds
"Deploying ConfigMap/hello-cloud-configmap-data (timeout: 30s)",
"Hello from the command runner!", # unmanaged pod logs
"Result: SUCCESS",
"Successfully deployed 14 resources"
"Successfully deployed 15 resources"
], in_order: true)

assert_logs_match_all([
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_pruning_works
'daemonset "ds-app"',
'statefulset "stateful-busybox"'
] # not necessarily listed in this order
expected_msgs = [/Pruned 7 resources and successfully deployed 3 resources/]
expected_msgs = [/Pruned 7 resources and successfully deployed 4 resources/]
expected_pruned.map do |resource|
expected_msgs << /The following resources were pruned:.*#{resource}/
end
Expand Down Expand Up @@ -627,7 +627,7 @@ def test_can_deploy_deployment_with_zero_replicas
assert_equal 0, pods.length, "Pods were running from zero-replica deployment"

assert_logs_match_all([
%r{Service/web\s+Selects 0 pods},
%r{Service/web\s+Doesn't require any endpoint},
%r{Deployment/web\s+0 replicas}
])
end
Expand Down

0 comments on commit 265eb3d

Please sign in to comment.