helm_upgrade_logs 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5195e2b08d8a4c6838d8cbf16fa070551bed1953008f46873d6f41ac07c829d
4
- data.tar.gz: 338bab0411ebc7e0a059b66392a30342187a6a9fb1ba623a3796baee331a5119
3
+ metadata.gz: b2dc5816493e1b7e6382d703dcc55063f5a6dd59557e233aeb8ad8f355a542c1
4
+ data.tar.gz: 2d5d74dc6af95bf6ff06ee3d3c5ad7c6d76baade4d405e141c274041c8ac5914
5
5
  SHA512:
6
- metadata.gz: 5d7b9c585181eef228e5bdaebacbc3d5d1764e6d0bbcb6aeb62db8389482f2011bba423d4d836dae44c8c16d0e2c94cab8352ef60b5376cb884b46ed2e858c75
7
- data.tar.gz: 1417cbbe9bcbcc02501d62c30eac1b6d13b9ee8bbe060f577e2342d188539e215ea73c32d25a072bb3f401a29fd3436a5a8de524ebdd73fce829d820eed459a3
6
+ metadata.gz: c7ac65c07401d1a198e3e29ae710cf843b1601e0d7b4f24ae3401aa72b36e1b1bdd182e101df25bcafb01078fb8dae1af296b383b5ae77d3136ba84348d5dfc2
7
+ data.tar.gz: d4a00643a7461d16198e7a9f9dec6a93f782cb8790a53efc40344a3303a2c37dcbd939b88ab5d9ff95e482593a16c53283ef0acb4cf24161593aba14b9c3812a
@@ -30,8 +30,6 @@ jobs:
30
30
  ruby-version: ${{ matrix.ruby-version }}
31
31
  - name: Start KIND cluster
32
32
  run: bash -f bin/start_kind.sh
33
- - name: Waiting for KIND cluster to get ready
34
- run: sleep 70
35
33
  - name: Install gem
36
34
  run: gem install helm_upgrade_logs
37
35
  - name: Add bitnami repo
@@ -39,6 +37,21 @@ jobs:
39
37
  - name: Helm install debugging logs
40
38
  run: helm_upgrade_logs --install nginx bitnami/nginx --wait --debug --set service.type=ClusterIP
41
39
 
40
+ unit_test:
41
+ runs-on: ubuntu-latest
42
+ strategy:
43
+ matrix:
44
+ ruby-version: ['3.0']
45
+ steps:
46
+ - uses: actions/checkout@v3
47
+ - name: Set up Ruby
48
+ uses: ruby/setup-ruby@v1
49
+ with:
50
+ ruby-version: ${{ matrix.ruby-version }}
51
+ bundler-cache: true
52
+ - name: Run tests
53
+ run: bundle exec rake
54
+
42
55
  test_exe:
43
56
  runs-on: ubuntu-latest
44
57
  strategy:
@@ -49,11 +62,11 @@ jobs:
49
62
  - uses: actions/checkout@v3
50
63
  - name: Start KIND cluster
51
64
  run: bash -f bin/start_kind.sh
52
- # - name: Waiting for KIND cluster to get ready
53
- # run: sleep 70
54
65
  - name: Add bitnami repo
55
66
  run: helm repo add bitnami https://charts.bitnami.com/bitnami
56
67
  - name: Install bitnami
57
68
  run: ./exe/helm_upgrade_logs --install nginx bitnami/nginx --wait --debug --set service.type=ClusterIP --set replicaCount=2
69
+ - name: Test bitnami
70
+ run: ./exe/helm_test_logs nginx
58
71
  - name: Install Redis
59
72
  run: ./exe/helm_upgrade_logs --install redis bitnami/redis --set auth.enabled=false --version 14.0.2 --wait
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.4
2
+ TargetRubyVersion: 3.0
3
3
 
4
4
  Style/StringLiterals:
5
5
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.2.4]
2
+
3
+ - Handle `n`, `--namespace` from helm command
4
+ - Exit if there is failure on helm command while trying to get logs
5
+
1
6
  ## [0.2.3] - 2022-04-20
2
7
 
3
8
  - Configuration on initial wait for pods logs and subsequent wait limits
data/README.md CHANGED
@@ -51,9 +51,6 @@ However if you do want to try it, to install `nginx` from the chart `bitnami/ngi
51
51
  curl -s https://raw.githubusercontent.com/SamuelGarrattIqa/helm_upgrade_logs/main/bin/helm_upgrade_logs.sh | bash -s -- --install nginx bitnami/nginx
52
52
  ```
53
53
 
54
- ## TODO
55
- * Make library handle `n`, `--namespace` from helm command
56
-
57
54
  ## Development
58
55
 
59
56
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -9,4 +9,4 @@ require "rubocop/rake_task"
9
9
 
10
10
  RuboCop::RakeTask.new
11
11
 
12
- task default: %i[spec rubocop]
12
+ task default: %i[spec]
data/exe/helm_test_logs CHANGED
@@ -2,13 +2,14 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
5
- require 'helm_upgrade_logs'
5
+ require "helm_upgrade_logs"
6
6
 
7
- $release_name = ARGV.find { |arg| !arg.start_with?('-') }
7
+ @release_name = ARGV.find { |arg| !arg.start_with?("-") }
8
+ @namespace = namespace_from_args(ARGV)
8
9
 
9
- helm_pid = Process.spawn "helm test #{ARGV.join(' ')}"
10
+ helm_pid = Process.spawn "helm test #{ARGV.join(" ")}"
10
11
 
11
- log_pid = Process.spawn "kubectl logs -lapp.kubernetes.io/managed-by=Helm,app.kubernetes.io/instance=#{$release_name} -f --all-containers --prefix --ignore-errors=true --max-log-requests=20 --timestamps=true"
12
+ log_pid = Process.spawn(add_ns("kubectl logs -lapp.kubernetes.io/instance=#{@release_name} -f --all-containers --prefix --ignore-errors=true --max-log-requests=20 --timestamps=true --since=1s"))
12
13
 
13
14
  Process.wait helm_pid
14
- puts `kill #{log_pid}`
15
+ `kill #{log_pid}`
@@ -1,42 +1,49 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'open3'
4
+ require "open3"
5
5
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
6
- require 'helm_upgrade_logs'
6
+ require "helm_upgrade_logs"
7
7
 
8
- $release_name = ARGV.find { |arg| !arg.start_with?('-') }
8
+ @release_name = ARGV.find { |arg| !arg.start_with?("-") }
9
+ @namespace = namespace_from_args(ARGV)
9
10
 
10
- helm_pid = Process.spawn "helm upgrade #{ARGV.join(' ')}"
11
+ @helm_pid = Process.spawn "helm upgrade #{ARGV.join(" ")}"
11
12
 
12
- event_pid = Process.spawn 'kubectl get events --watch-only=true'
13
- service_pid = Process.spawn 'kubectl get services --watch-only=true'
13
+ event_pid = Process.spawn(add_ns("kubectl get events --watch-only=true"))
14
+ service_pid = Process.spawn(add_ns("kubectl get services --watch-only=true"))
14
15
 
15
16
  wait_for_pod_to_log
16
17
 
17
18
  @pods = []
18
19
  @pod_pids = {}
19
20
 
20
- while Process.waitpid(helm_pid, Process::WNOHANG) == nil do
21
- pods = get_pods
21
+ begin
22
+ Process.waitpid(@helm_pid, Process::WNOHANG)
23
+ rescue Errno::ECHILD
24
+ `kill #{event_pid}`
25
+ `kill #{service_pid}`
26
+ raise HelmUpgradeLogs::Error, "Failed to find logs before helm finished"
27
+ end
28
+
29
+ while Process.waitpid(@helm_pid, Process::WNOHANG).nil?
30
+ pods = read_pods
22
31
  if pods != @pods
23
32
  @pods = pods
24
- puts "[INFO] Pods: #{pods.join(',')}"
33
+ puts "[INFO] Pods: #{pods.join(",")}"
25
34
  # Could change this each to Parallel.each
26
35
  @pods.each do |pod|
27
- if @pod_pids[pod].nil?
28
- wait_for_specific_pod_to_log pod
29
- log_pid = Process.spawn "kubectl logs #{pod} -f --all-containers --prefix --ignore-errors=true --timestamps=true"
30
- @pod_pids[pod] = log_pid
31
- end
36
+ next unless @pod_pids[pod].nil?
37
+
38
+ wait_for_specific_pod_to_log pod
39
+ log_pid = Process.spawn(add_ns("kubectl logs #{pod} -f --all-containers --prefix --ignore-errors=true --timestamps=true"))
40
+ @pod_pids[pod] = log_pid
32
41
  end
33
42
  end
34
43
  sleep 1
35
44
  end
36
45
  `kill #{event_pid}`
37
46
  `kill #{service_pid}`
38
- @pod_pids.each do |pod, pid|
47
+ @pod_pids.each do |_pod, pid|
39
48
  `kill #{pid}`
40
49
  end
41
-
42
-
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HelmUpgradeLogs
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'open3'
3
+ require "open3"
4
4
  require_relative "helm_upgrade_logs/version"
5
5
 
6
- ENV['helm_upgrade_logs_log_start'] ||= '90'
7
- ENV['helm_upgrade_logs_pod_start'] ||= '35'
6
+ ENV["helm_upgrade_logs_log_start"] ||= "90"
7
+ ENV["helm_upgrade_logs_pod_start"] ||= "35"
8
8
 
9
9
  # Approach not ideal as it will for all containers to be ready and want to stream logs before that
10
10
  def wait_for_container_ready
@@ -12,43 +12,61 @@ def wait_for_container_ready
12
12
  Process.wait wait_pid
13
13
  end
14
14
 
15
- # Wait for pods with logs to be present
15
+ # Wait for very first pods with logs to be present
16
16
  # Not ideal due to https://github.com/kubernetes/kubernetes/issues/28746
17
17
  def wait_for_pod_to_log
18
- ENV['helm_upgrade_logs_log_start'].to_i.times do |i|
18
+ ENV["helm_upgrade_logs_log_start"].to_i.times do |i|
19
+ break unless Process.waitpid(@helm_pid, Process::WNOHANG).nil?
20
+
19
21
  sleep 1
20
- stdout, stderr, _ = Open3.capture3 "kubectl logs -lapp.kubernetes.io/instance=#{$release_name}"
22
+ stdout, stderr, = Open3.capture3(add_ns("kubectl logs -lapp.kubernetes.io/instance=#{@release_name}"))
21
23
  if stderr.empty? && !stdout.strip.empty?
22
- puts '[INFO] Pods with logs found'
24
+ puts "[INFO] Pods with logs found"
23
25
  break
24
- else
25
- puts "[INFO] Waiting for pod logs: #{stderr}" if i % 3 == 0
26
+ elsif (i % 3).zero?
27
+ puts "[INFO] Waiting for pod logs: #{stderr}"
26
28
  end
27
29
  end
28
30
  end
29
31
 
32
+ # Wait for logs from a specific pod
30
33
  def wait_for_specific_pod_to_log(pod_name)
31
- ENV['helm_upgrade_logs_pod_start'].to_i.times do |i|
34
+ ENV["helm_upgrade_logs_pod_start"].to_i.times do |i|
32
35
  sleep 1
33
- stdout, stderr, _ = Open3.capture3 "kubectl logs #{pod_name}"
36
+ stdout, stderr, = Open3.capture3(add_ns("kubectl logs #{pod_name}"))
34
37
  if stderr.empty? && !stdout.strip.empty?
35
38
  puts "[INFO] Pod #{pod_name} with logs found"
36
39
  break
37
- else
38
- puts "[INFO] Waiting for pod #{pod_name} logs: #{stderr}" if i % 2 == 0
40
+ elsif i.even?
41
+ puts "[INFO] Waiting for pod #{pod_name} logs: #{stderr}"
39
42
  end
40
43
  end
41
44
  end
42
45
 
43
- def get_pods
44
- stdout, stderr, _ = Open3.capture3 "kubectl get pods -lapp.kubernetes.io/instance=#{$release_name} -o name"
46
+ # Get pods
47
+ def read_pods
48
+ stdout, stderr, = Open3.capture3(add_ns("kubectl get pods -lapp.kubernetes.io/instance=#{@release_name} -o name"))
45
49
  if stderr.empty?
46
- stdout.lines.collect { |pod| pod.strip }
50
+ stdout.lines.collect(&:strip)
47
51
  else
48
52
  []
49
53
  end
50
54
  end
51
55
 
56
+ # @param [Array] args
57
+ def namespace_from_args(args)
58
+ match_index = args.find_index { |arg| %w[-n --namespace].include?(arg) }
59
+ return nil unless match_index
60
+
61
+ args[match_index + 1]
62
+ end
63
+
64
+ # Add namespace to kube query
65
+ def add_ns(kube_query)
66
+ kube_query += " -n #{@namespace}" if @namespace
67
+ kube_query
68
+ end
69
+
52
70
  module HelmUpgradeLogs
53
71
  class Error < StandardError; end
54
72
  # Your code goes here...
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helm_upgrade_logs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
@@ -41,7 +41,6 @@ files:
41
41
  - bin/start_kind.sh
42
42
  - exe/helm_test_logs
43
43
  - exe/helm_upgrade_logs
44
- - get_pods.rb
45
44
  - helm_upgrade_logs.gemspec
46
45
  - lib/helm_upgrade_logs.rb
47
46
  - lib/helm_upgrade_logs/version.rb
data/get_pods.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'open3'
2
-
3
- $release_name = 'redis'
4
- stdout, stderr, _ = Open3.capture3 "kubectl get pods -lapp.kubernetes.io/managed-by=Helm,app.kubernetes.io/instance=#{$release_name} -o name"
5
- if stderr.empty?
6
- puts stdout.lines.join(',')
7
- end