helm_upgrade_logs 0.2.2 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa3f3a0b230ebece6f834e3f1d350f7dce7efdc38e9798f6e88e2c2ac2e4ebea
4
- data.tar.gz: '09b2dc417d34ceada26ea29976dceab275d035c69da9fac86afbf613b2f239b1'
3
+ metadata.gz: 05d454e1aeebb7122327e049f950b33f35161484018f71fbc2d49482e8830bf3
4
+ data.tar.gz: 3482c02fd2cc6c0a4ce397f8d08aba0a95b75a31c8d43ae595df6bb61bfce442
5
5
  SHA512:
6
- metadata.gz: 6b079a3c801b1cfc3ed7ebfe43b669e8845cf02bb2980e541e58fe1b049d4138e0cbb120a64099e97335bc95b0b98f2510a934299ff470251ebf56bf247cfbb3
7
- data.tar.gz: 40d106a0b06352621fc90cfb8653fbc3f279d8676d438eeb592e6fa94a39bc896f8a690df4a6633654d410f9bae5600605790fbcd1bdbf33c95aa114d9403053
6
+ metadata.gz: 19398249dbecf3c5e8b6d05eca07e33a9c950a6399cc954f4d9db103c25a0824abb770b2972c0fa9b113f6313650610edd5593eac5110a702e1133dc28371331
7
+ data.tar.gz: eb1efb17c3b164b4c4c6e49be16cb7db923d7c96321e2f0e9089ee7e06faf2f79699e9813d60d679de77dad266bbcaffd4ea4cffd2fed12095664761b00cea1a
@@ -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.5] 2022-04-21
2
+
3
+ - Return helm status code as script's status code
4
+
5
+ ## [0.2.4] 2022-04-21
6
+
7
+ - Handle `n`, `--namespace` from helm command
8
+ - Exit if there is failure on helm command while trying to get logs
9
+
10
+ ## [0.2.3] - 2022-04-20
11
+
12
+ - Configuration on initial wait for pods logs and subsequent wait limits
13
+
1
14
  ## [0.2.2] - 2022-04-20
2
15
 
3
16
  - Wait for logs on each pod having a process for logging each pod
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,16 @@
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
+ helm_status = $CHILD_STATUS.exitstatus
16
+ `kill #{log_pid}`
17
+ exit helm_status
@@ -1,42 +1,51 @@
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
45
+ helm_status = $CHILD_STATUS.exitstatus
36
46
  `kill #{event_pid}`
37
47
  `kill #{service_pid}`
38
- @pod_pids.each do |pod, pid|
48
+ @pod_pids.each do |_pod, pid|
39
49
  `kill #{pid}`
40
50
  end
41
-
42
-
51
+ exit helm_status
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HelmUpgradeLogs
4
- VERSION = "0.2.2"
4
+ # @return [String] Version of helm upgrade logs
5
+ VERSION = "0.2.5"
5
6
  end
@@ -1,51 +1,73 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'open3'
3
+ require "open3"
4
+ require 'English'
4
5
  require_relative "helm_upgrade_logs/version"
5
6
 
7
+ ENV["helm_upgrade_logs_log_start"] ||= "90"
8
+ ENV["helm_upgrade_logs_pod_start"] ||= "35"
9
+
6
10
  # Approach not ideal as it will for all containers to be ready and want to stream logs before that
7
11
  def wait_for_container_ready
8
12
  wait_pid = Process.spawn 'kubectl wait --for=condition=ContainersReady pod --selector "app.kubernetes.io/managed-by=Helm" --timeout=30s'
9
13
  Process.wait wait_pid
10
14
  end
11
15
 
12
- # Wait for pods with logs to be present
16
+ # Wait for very first pods with logs to be present
13
17
  # Not ideal due to https://github.com/kubernetes/kubernetes/issues/28746
14
18
  def wait_for_pod_to_log
15
- 90.times {
19
+ ENV["helm_upgrade_logs_log_start"].to_i.times do |i|
20
+ break unless Process.waitpid(@helm_pid, Process::WNOHANG).nil?
21
+
16
22
  sleep 1
17
- stdout, stderr, _ = Open3.capture3 "kubectl logs -lapp.kubernetes.io/managed-by=Helm,app.kubernetes.io/instance=#{$release_name}"
23
+ stdout, stderr, = Open3.capture3(add_ns("kubectl logs -lapp.kubernetes.io/instance=#{@release_name}"))
18
24
  if stderr.empty? && !stdout.strip.empty?
19
- puts 'Pods with logs found'
25
+ puts "[INFO] Pods with logs found"
20
26
  break
21
- else
22
- puts "Waiting for pod logs: #{stderr}"
27
+ elsif (i % 3).zero?
28
+ puts "[INFO] Waiting for pod logs: #{stderr}"
23
29
  end
24
- }
30
+ end
25
31
  end
26
32
 
33
+ # Wait for logs from a specific pod
27
34
  def wait_for_specific_pod_to_log(pod_name)
28
- 30.times {
35
+ ENV["helm_upgrade_logs_pod_start"].to_i.times do |i|
29
36
  sleep 1
30
- stdout, stderr, _ = Open3.capture3 "kubectl logs #{pod_name}"
37
+ stdout, stderr, = Open3.capture3(add_ns("kubectl logs #{pod_name}"))
31
38
  if stderr.empty? && !stdout.strip.empty?
32
- puts "Pod #{pod_name} with logs found"
39
+ puts "[INFO] Pod #{pod_name} with logs found"
33
40
  break
34
- else
35
- puts "Waiting for pod #{pod_name} logs: #{stderr}"
41
+ elsif i.even?
42
+ puts "[INFO] Waiting for pod #{pod_name} logs: #{stderr}"
36
43
  end
37
- }
44
+ end
38
45
  end
39
46
 
40
- def get_pods
41
- stdout, stderr, _ = Open3.capture3 "kubectl get pods -lapp.kubernetes.io/instance=#{$release_name} -o name"
47
+ # Get pods
48
+ def read_pods
49
+ stdout, stderr, = Open3.capture3(add_ns("kubectl get pods -lapp.kubernetes.io/instance=#{@release_name} -o name"))
42
50
  if stderr.empty?
43
- stdout.lines.collect { |pod| pod.strip }
51
+ stdout.lines.collect(&:strip)
44
52
  else
45
53
  []
46
54
  end
47
55
  end
48
56
 
57
+ # @param [Array] args
58
+ def namespace_from_args(args)
59
+ match_index = args.find_index { |arg| %w[-n --namespace].include?(arg) }
60
+ return nil unless match_index
61
+
62
+ args[match_index + 1]
63
+ end
64
+
65
+ # Add namespace to kube query
66
+ def add_ns(kube_query)
67
+ kube_query += " -n #{@namespace}" if @namespace
68
+ kube_query
69
+ end
70
+
49
71
  module HelmUpgradeLogs
50
72
  class Error < StandardError; end
51
73
  # 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.2
4
+ version: 0.2.5
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