diff --git a/lib/kubernetes-deploy/kubernetes_resource/service.rb b/lib/kubernetes-deploy/kubernetes_resource/service.rb index 3408d5488..d2ddb3ba5 100644 --- a/lib/kubernetes-deploy/kubernetes_resource/service.rb +++ b/lib/kubernetes-deploy/kubernetes_resource/service.rb @@ -11,16 +11,19 @@ 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" else - "Selects 0 pods" + "Unknown" end 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 @@ -44,22 +47,30 @@ 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? @@ -67,5 +78,9 @@ def fetch_related_replica_count return unless deployments.length == 1 deployments.first["spec"]["replicas"].to_i end + + def external_name_svc? + @definition["spec"]["type"] == "ExternalName" + end end end diff --git a/test/fixtures/hello-cloud/redis.yml b/test/fixtures/hello-cloud/redis.yml index e9e9abf91..6d9c14c48 100644 --- a/test/fixtures/hello-cloud/redis.yml +++ b/test/fixtures/hello-cloud/redis.yml @@ -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: diff --git a/test/integration/kubernetes_deploy_test.rb b/test/integration/kubernetes_deploy_test.rb index 7224c5d58..48b7b678d 100644 --- a/test/integration/kubernetes_deploy_test.rb +++ b/test/integration/kubernetes_deploy_test.rb @@ -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([ @@ -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 @@ -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