helm_upgrade_logs 0.2.1 → 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: 3895eb29c6282255ad15985d3fb057a584f032613003a2f53387eb60d2e8dd7b
4
- data.tar.gz: 968a9e4f986be46ea5af92fcde269814ba88d6e16f28a1b4990ea772239a05e4
3
+ metadata.gz: b2dc5816493e1b7e6382d703dcc55063f5a6dd59557e233aeb8ad8f355a542c1
4
+ data.tar.gz: 2d5d74dc6af95bf6ff06ee3d3c5ad7c6d76baade4d405e141c274041c8ac5914
5
5
  SHA512:
6
- metadata.gz: 34fe9047d27e355b20976072d23ccbfac89c477d7eaab32edbe90550fbd50447c68a05b8234727d53107717ec1e24f12fe048e8e653e73c4f4a2f87643b1f75f
7
- data.tar.gz: 2eb9d830804820033eaf8a80c8c49a8a4e0120f247261e059794530a3cb46837ccd917b30f53d72d42c71f8999b92a9832cbef6d4705bb735c13725e8b30df20
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,16 @@
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
+
6
+ ## [0.2.3] - 2022-04-20
7
+
8
+ - Configuration on initial wait for pods logs and subsequent wait limits
9
+
10
+ ## [0.2.2] - 2022-04-20
11
+
12
+ - Wait for logs on each pod having a process for logging each pod
13
+
1
14
  ## [0.2.0] - 2022-04-20
2
15
 
3
16
  - Monitoring pods don't filter managed by helm as some pods don't have that with selectorLabels
data/README.md CHANGED
@@ -20,7 +20,9 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- This gem is purely about an `exe` to wrap around `helm` commands and to log helpful commands.
23
+ This gem is purely about an `exe` to wrap around `helm` commands and to log events and pod logs that happen as
24
+ part of that release while it is being released.
25
+
24
26
  Once installed, install/upgrade a helm chart with
25
27
 
26
28
  ```bash
@@ -29,12 +31,21 @@ helm_upgrade_logs --install redis bitnami/redis --set auth.enabled=false --versi
29
31
 
30
32
  After the `helm_upgrade_logs` command put any options that would normally add after `helm upgrade`.
31
33
 
34
+ This library waits for logs to start in pods for 90 seconds or controlled by environment variable `helm_upgrade_logs_log_start`.
35
+ After that it waits 35 seconds for logs to start in pods after the first one.
36
+ This is controlled by environment variable `helm_upgrade_logs_pod_start`
37
+
32
38
  To test a chart showing it logs, in a similar way run
33
39
  ```bash
34
40
  helm_test_logs redis
35
41
  ```
36
42
 
37
- This also has a bash script which can be used directly. E.g., to install `nginx` from the chart `bitnami/nginx`.
43
+ ### Aside bash script
44
+
45
+ This also has a bash script which can be used directly although it will probably not work on CI
46
+ and also does not close background processes so not ideal.
47
+
48
+ However if you do want to try it, to install `nginx` from the chart `bitnami/nginx` run
38
49
 
39
50
  ```
40
51
  curl -s https://raw.githubusercontent.com/SamuelGarrattIqa/helm_upgrade_logs/main/bin/helm_upgrade_logs.sh | bash -s -- --install nginx bitnami/nginx
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,31 +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 = []
19
+ @pod_pids = {}
18
20
 
19
- while Process.waitpid(helm_pid, Process::WNOHANG) == nil do
20
- 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
21
31
  if pods != @pods
22
32
  @pods = pods
23
- puts "[INFO] Pods: #{pods.join(',')}"
24
- puts `kill #{@log_pid}` if @log_pid
25
- @log_pid = Process.spawn "kubectl logs -lapp.kubernetes.io/instance=#{$release_name} -f --all-containers --prefix --ignore-errors=true --max-log-requests=20 --timestamps=true --since=0s"
33
+ puts "[INFO] Pods: #{pods.join(",")}"
34
+ # Could change this each to Parallel.each
35
+ @pods.each do |pod|
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
41
+ end
26
42
  end
27
- sleep 2.5
43
+ sleep 1
44
+ end
45
+ `kill #{event_pid}`
46
+ `kill #{service_pid}`
47
+ @pod_pids.each do |_pod, pid|
48
+ `kill #{pid}`
28
49
  end
29
- puts `kill #{@log_pid}`
30
- puts `kill #{event_pid}`
31
- puts `kill #{service_pid}`
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HelmUpgradeLogs
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -1,38 +1,72 @@
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"
8
+
6
9
  # Approach not ideal as it will for all containers to be ready and want to stream logs before that
7
10
  def wait_for_container_ready
8
11
  wait_pid = Process.spawn 'kubectl wait --for=condition=ContainersReady pod --selector "app.kubernetes.io/managed-by=Helm" --timeout=30s'
9
12
  Process.wait wait_pid
10
13
  end
11
14
 
12
- # Wait for pods with logs to be present
15
+ # Wait for very first pods with logs to be present
13
16
  # Not ideal due to https://github.com/kubernetes/kubernetes/issues/28746
14
17
  def wait_for_pod_to_log
15
- 90.times {
18
+ ENV["helm_upgrade_logs_log_start"].to_i.times do |i|
19
+ break unless Process.waitpid(@helm_pid, Process::WNOHANG).nil?
20
+
16
21
  sleep 1
17
- stdout, stderr, _ = Open3.capture3 "kubectl logs -lapp.kubernetes.io/managed-by=Helm,app.kubernetes.io/instance=#{$release_name}"
22
+ stdout, stderr, = Open3.capture3(add_ns("kubectl logs -lapp.kubernetes.io/instance=#{@release_name}"))
18
23
  if stderr.empty? && !stdout.strip.empty?
19
- puts 'Pods with logs found'
24
+ puts "[INFO] Pods with logs found"
20
25
  break
21
- else
22
- puts "Waiting for pod logs: #{stderr}"
26
+ elsif (i % 3).zero?
27
+ puts "[INFO] Waiting for pod logs: #{stderr}"
23
28
  end
24
- }
29
+ end
25
30
  end
26
31
 
27
- def get_pods
28
- stdout, stderr, _ = Open3.capture3 "kubectl get pods -lapp.kubernetes.io/instance=#{$release_name} -o name"
32
+ # Wait for logs from a specific pod
33
+ def wait_for_specific_pod_to_log(pod_name)
34
+ ENV["helm_upgrade_logs_pod_start"].to_i.times do |i|
35
+ sleep 1
36
+ stdout, stderr, = Open3.capture3(add_ns("kubectl logs #{pod_name}"))
37
+ if stderr.empty? && !stdout.strip.empty?
38
+ puts "[INFO] Pod #{pod_name} with logs found"
39
+ break
40
+ elsif i.even?
41
+ puts "[INFO] Waiting for pod #{pod_name} logs: #{stderr}"
42
+ end
43
+ end
44
+ end
45
+
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"))
29
49
  if stderr.empty?
30
- stdout.lines.collect { |pod| pod.strip }
50
+ stdout.lines.collect(&:strip)
31
51
  else
32
52
  []
33
53
  end
34
54
  end
35
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
+
36
70
  module HelmUpgradeLogs
37
71
  class Error < StandardError; end
38
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.1
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