Top Level Namespace

Defined Under Namespace

Modules: HelmUpgradeLogs

Instance Method Summary collapse

Instance Method Details

#add_ns(kube_query) ⇒ Object

Add namespace to kube query



92
93
94
95
# File 'lib/helm_upgrade_logs.rb', line 92

def add_ns(kube_query)
  kube_query += " -n #{@namespace}" if @namespace
  kube_query
end

#add_ns_file(kube_query, pod) ⇒ Object

Add namespace to kube query



98
99
100
101
102
# File 'lib/helm_upgrade_logs.rb', line 98

def add_ns_file(kube_query, pod)
  kube_query += " -n #{@namespace}" if @namespace
  kube_query += "> #{pod}.log"
  kube_query
end

#namespace_from_args(args) ⇒ Object

Parameters:

  • args (Array)


58
59
60
61
62
63
# File 'lib/helm_upgrade_logs.rb', line 58

def namespace_from_args(args)
  match_index = args.find_index { |arg| %w[-n --namespace].include?(arg) }
  return nil unless match_index

  args[match_index + 1]
end

#options_with_argsObject



65
66
67
68
69
70
# File 'lib/helm_upgrade_logs.rb', line 65

def options_with_args
  %w[--set --ca-file --cert-file --description --history-max --key-file --keyring --output --password --post-renderer
     --repo --set --set-file --set-string --timeout --username --values --version --kube-apiserver --kube-as-group
     --kube-as-user --kube-as-file --kube-context --kube-token --kubeconfig --namespace --registry-config
     --repository-cache --repository-config -n -f -o]
end

#read_podsObject

Get pods



48
49
50
51
52
53
54
55
# File 'lib/helm_upgrade_logs.rb', line 48

def read_pods
  stdout, stderr, = Open3.capture3(add_ns("kubectl get pods -lapp.kubernetes.io/instance=#{@release_name} --field-selector 'status.phase!=Failed' -o name"))
  if stderr.empty?
    stdout.lines.collect(&:strip)
  else
    []
  end
end

#release_name_from_args(args) ⇒ Object

Returns Release name from command line arguments.

Returns:

  • Release name from command line arguments



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/helm_upgrade_logs.rb', line 73

def release_name_from_args(args)
  arg_list = args.clone

  loop do
    option_with_val = false
    arg_list.each_with_index do |arg, index|
      if options_with_args.include?(arg_list[index - 1])
        arg_list.delete_at(index - 1)
        arg_list.delete_at(index - 1)
        option_with_val = true
        break
      end
    end
    break if option_with_val == false
  end
  arg_list.find { |arg| !arg.start_with?("-") }
end

#wait_for_container_readyObject

Approach not ideal as it will for all containers to be ready and want to stream logs before that



11
12
13
14
# File 'lib/helm_upgrade_logs.rb', line 11

def wait_for_container_ready
  wait_pid = Process.spawn 'kubectl wait --for=condition=ContainersReady pod --selector "app.kubernetes.io/managed-by=Helm" --timeout=30s'
  Process.wait wait_pid
end

#wait_for_pod_to_logObject

Wait for very first pods with logs to be present Not ideal due to github.com/kubernetes/kubernetes/issues/28746



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/helm_upgrade_logs.rb', line 18

def wait_for_pod_to_log
  ENV["helm_upgrade_logs_log_start"].to_i.times do |i|
    break unless Process.waitpid(@helm_pid, Process::WNOHANG).nil?

    sleep 1
    stdout, stderr, = Open3.capture3(add_ns("kubectl logs -lapp.kubernetes.io/instance=#{@release_name}"))
    if stderr.empty? && !stdout.strip.empty?
      puts "[INFO] Pods with logs found"
      break
    elsif (i % 3).zero?
      puts "[INFO] Waiting for pod logs: #{stderr}"
    end
  end
end

#wait_for_specific_pod_to_log(pod_name) ⇒ Object

Wait for logs from a specific pod



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/helm_upgrade_logs.rb', line 34

def wait_for_specific_pod_to_log(pod_name)
  ENV["helm_upgrade_logs_pod_start"].to_i.times do |i|
    sleep 1
    stdout, stderr, = Open3.capture3(add_ns("kubectl logs #{pod_name}"))
    if stderr.empty? && !stdout.strip.empty?
      puts "[INFO] Pod #{pod_name} with logs found"
      break
    elsif i.even?
      puts "[INFO] Waiting for pod #{pod_name} logs: #{stderr}"
    end
  end
end