-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98855c0
commit 03a0002
Showing
6 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
lib/kubernetes-deploy/kubernetes_resource/daemon_set.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
module KubernetesDeploy | ||
class DaemonSet < KubernetesResource | ||
TIMEOUT = 5.minutes | ||
|
||
def sync | ||
raw_json, _err, st = kubectl.run("get", type, @name, "--output=json") | ||
@found = st.success? | ||
|
||
if @found | ||
daemonset_data = JSON.parse(raw_json) | ||
@desired_number = daemonset_data["status"]["desiredNumberScheduled"] | ||
@rollout_data = daemonset_data["status"] | ||
.slice("currentNumberScheduled", "desiredNumberScheduled", "numberReady") | ||
@status = @rollout_data.map { |state_replicas, num| "#{num} #{state_replicas.chop.pluralize(num)}" }.join(", ") | ||
@pods = find_pods(daemonset_data) | ||
else # reset | ||
@rollout_data = { "replicas" => 0 } | ||
@status = nil | ||
@pods = [] | ||
end | ||
end | ||
|
||
def deploy_succeeded? | ||
@desired_number == @rollout_data["desiredNumberScheduled"].to_i && | ||
@desired_number == @rollout_data["numberReady"].to_i | ||
end | ||
|
||
def deploy_failed? | ||
@pods.present? && @pods.all?(&:deploy_failed?) | ||
end | ||
|
||
def failure_message | ||
@pods.map(&:failure_message).compact.uniq.join("\n") | ||
end | ||
|
||
def timeout_message | ||
@pods.map(&:timeout_message).compact.uniq.join("\n") | ||
end | ||
|
||
def deploy_timed_out? | ||
super || @pods.present? && @pods.all?(&:deploy_timed_out?) | ||
end | ||
|
||
def exists? | ||
@found | ||
end | ||
|
||
def fetch_events | ||
own_events = super | ||
return own_events unless @pods.present? | ||
most_useful_pod = @pods.find(&:deploy_failed?) || @pods.find(&:deploy_timed_out?) || @pods.first | ||
own_events.merge(most_useful_pod.fetch_events) | ||
end | ||
|
||
def fetch_logs | ||
container_names.each_with_object({}) do |container_name, container_logs| | ||
out, _err, _st = kubectl.run( | ||
"logs", | ||
id, | ||
"--container=#{container_name}", | ||
"--since-time=#{@deploy_started.to_datetime.rfc3339}", | ||
"--tail=#{LOG_LINE_COUNT}" | ||
) | ||
container_logs[container_name] = out.split("\n") | ||
end | ||
end | ||
|
||
private | ||
|
||
def unmanaged? | ||
@parent.blank? | ||
end | ||
|
||
def container_names | ||
@definition["spec"]["template"]["spec"]["containers"].map { |c| c["name"] } | ||
end | ||
|
||
def find_pods(daemonset_data) | ||
label_string = daemonset_data["spec"]["selector"]["matchLabels"].map { |k, v| "#{k}=#{v}" }.join(",") | ||
raw_json, _err, st = kubectl.run("get", "pods", "-a", "--output=json", "--selector=#{label_string}") | ||
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"] == daemonset_data["metadata"]["uid"] } | ||
pod = Pod.new( | ||
namespace: namespace, | ||
context: context, | ||
definition: pod_data, | ||
logger: @logger, | ||
parent: "#{@name.capitalize} replica set", | ||
deploy_started: @deploy_started | ||
) | ||
pod.sync(pod_data) | ||
relevant_pods << pod | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: extensions/v1beta1 | ||
kind: DaemonSet | ||
metadata: | ||
name: busybox | ||
spec: | ||
template: | ||
metadata: | ||
labels: | ||
app: busybox | ||
spec: | ||
containers: | ||
- name: busybox | ||
image: busybox | ||
command: ["sleep", "40"] | ||
ports: | ||
- containerPort: 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters