Skip to content

Commit

Permalink
Implement proper generation matching for ds
Browse files Browse the repository at this point in the history
  • Loading branch information
karanthukral committed Jul 24, 2017
1 parent 8bd237d commit 9991eba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
25 changes: 14 additions & 11 deletions lib/kubernetes-deploy/kubernetes_resource/daemon_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ def sync

if @found
daemonset_data = JSON.parse(raw_json)
@desired_number = daemonset_data["status"]["desiredNumberScheduled"]
@rollout_data = daemonset_data["status"]
.slice("currentNumberScheduled", "desiredNumberScheduled", "numberReady")
.slice("currentNumberScheduled", "desiredNumberScheduled", "numberReady", "numberAvailable")
@status = @rollout_data.map { |state_replicas, num| "#{num} #{state_replicas.chop.pluralize(num)}" }.join(", ")
@pods = find_pods(daemonset_data)
else # reset
Expand All @@ -22,8 +21,8 @@ def sync
end

def deploy_succeeded?
@desired_number == @rollout_data["desiredNumberScheduled"].to_i &&
@desired_number == @rollout_data["numberReady"].to_i
@rollout_data["desiredNumberScheduled"] == @rollout_data["currentNumberScheduled"].to_i &&
@rollout_data["desiredNumberScheduled"] == @rollout_data["numberAvailable"].to_i
end

def deploy_failed?
Expand Down Expand Up @@ -54,10 +53,11 @@ def fetch_events
end

def fetch_logs
most_useful_pod = @pods.find(&:deploy_failed?) || @pods.find(&:deploy_timed_out?) || @pods.first
container_names.each_with_object({}) do |container_name, container_logs|
out, _err, _st = kubectl.run(
"logs",
id,
most_useful_pod.name,
"--container=#{container_name}",
"--since-time=#{@deploy_started.to_datetime.rfc3339}",
"--tail=#{LOG_LINE_COUNT}"
Expand All @@ -68,10 +68,6 @@ def fetch_logs

private

def unmanaged?
@parent.blank?
end

def container_names
@definition["spec"]["template"]["spec"]["containers"].map { |c| c["name"] }
end
Expand All @@ -82,8 +78,15 @@ def find_pods(ds_data)
return [] unless st.success?

all_pods = JSON.parse(raw_json)["items"]
all_pods.each_with_object([]) do |pod_data, relevant_pods|
next unless pod_data["metadata"]["ownerReferences"].any? { |ref| ref["uid"] == ds_data["metadata"]["uid"] }
current_generation = ds_data["metadata"]["generation"].to_s

latest_pods = all_pods.find_all do |pods|
pods["metadata"]["ownerReferences"].any? { |ref| ref["uid"] == ds_data["metadata"]["uid"] } &&
pods["metadata"]["labels"]["pod-template-generation"] == current_generation
end
return unless latest_pods.present?

latest_pods.each_with_object([]) do |pod_data, relevant_pods|
pod = Pod.new(
namespace: namespace,
context: context,
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/hello-cloud/daemon_set.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: busybox
name: nginx
spec:
template:
metadata:
labels:
app: busybox
app: hello-cloud
name: nginx
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "40"]
- name: nginx
image: nginx
ports:
- containerPort: 80
2 changes: 1 addition & 1 deletion test/helpers/fixture_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def assert_service_account_present(name)

def assert_daemon_set_present(name)
found = false
daemon_sets = v1beta1_kubeclient.get_daemon_sets(namespace: namespace, label_selector: "app=#{name}")
daemon_sets = v1beta1_kubeclient.get_daemon_sets(namespace: namespace, label_selector: "name=#{name},app=#{app_name}")
daemon_sets.each do |ds|
found = true if ds.metadata.name == name
end
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/fixture_sets/hello_cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def assert_all_service_accounts_up
end

def assert_daemon_set_up
assert_daemon_set_present("busybox")
assert_daemon_set_present("nginx")
end
end
end

0 comments on commit 9991eba

Please sign in to comment.