#!/usr/bin/env ruby # frozen_string_literal: true require "open3" $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib") require "helm_upgrade_logs" @release_name = ARGV.find { |arg| !arg.start_with?("-") } @namespace = namespace_from_args(ARGV) @helm_pid = Process.spawn "helm upgrade #{ARGV.join(" ")}" pods_before_upgrade = read_pods puts "[INFO] Pods before upgrade #{pods_before_upgrade}" if pods_before_upgrade.size.positive? event_pid = Process.spawn(add_ns("kubectl get events --watch-only=true")) service_pid = Process.spawn(add_ns("kubectl get services --watch-only=true")) wait_for_pod_to_log @pods = [] @pod_pids = {} begin Process.waitpid(@helm_pid, Process::WNOHANG) rescue Errno::ECHILD `kill #{event_pid}` `kill #{service_pid}` raise HelmUpgradeLogs::Error, "Failed to find logs before helm finished" end while Process.waitpid(@helm_pid, Process::WNOHANG).nil? pods = read_pods - pods_before_upgrade if pods != @pods @pods = pods puts "[INFO] Pods: #{pods.join(",")}" # Could change this each to Parallel.each @pods.each do |pod| next unless @pod_pids[pod].nil? wait_for_specific_pod_to_log pod log_pid = Process.spawn(add_ns("kubectl logs #{pod} -f --all-containers --prefix --ignore-errors=true --timestamps=true")) @pod_pids[pod] = log_pid end end sleep 1 end helm_status = $CHILD_STATUS.exitstatus `kill #{event_pid}` `kill #{service_pid}` @pod_pids.each do |_pod, pid| `kill #{pid}` end exit helm_status