Skip to content

Commit 1466637

Browse files
author
Etienne Tremel
committed
Add timeout to command, fix command path during install
1 parent 252e91b commit 1466637

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

cmd/monitor_elasticsearch.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ query string.
2525
2626
Usage with Lucene query:
2727
28-
$ helm monitor elasticsearch frontend 'status:500 AND kubernetes.labels.app:app'
28+
$ helm monitor elasticsearch frontend 'status:500 AND kubernetes.labels.app:app AND version:2.0.0'
2929
3030
Usage with query DSL file:
3131
@@ -85,7 +85,7 @@ func newMonitorElasticSearchCmd(client helm.Interface, out io.Writer) *cobra.Com
8585
f.BoolVar(&elasticSearchMonitor.dryRun, "dry-run", false, "simulate a monitoring")
8686
f.Int64Var(&elasticSearchMonitor.timeout, "timeout", 300, "time in seconds to wait before assuming a monitoring action is successfull")
8787
f.Int64Var(&elasticSearchMonitor.rollbackTimeout, "rollback-timeout", 300, "time in seconds to wait for any individual Kubernetes operation during the rollback (like Jobs for hooks)")
88-
f.Int64Var(&elasticSearchMonitor.interval, "interval", 10, "time in seconds between each PromQL query")
88+
f.Int64Var(&elasticSearchMonitor.interval, "interval", 10, "time in seconds between each query")
8989
f.BoolVar(&elasticSearchMonitor.wait, "wait", false, "if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment are in a ready state before marking a rollback as successful. It will wait for as long as --rollback-timeout")
9090
f.BoolVar(&elasticSearchMonitor.force, "force", false, "force resource update through delete/recreate if needed")
9191
f.BoolVar(&elasticSearchMonitor.disableHooks, "no-hooks", false, "prevent hooks from running during rollback")
@@ -129,6 +129,12 @@ func (m *monitorElasticSearchCmd) run() error {
129129

130130
ticker := time.NewTicker(time.Second * time.Duration(m.interval))
131131

132+
go func() {
133+
time.Sleep(time.Second * time.Duration(m.timeout))
134+
fmt.Fprintf(m.out, "No results after %d second(s)\n", m.timeout)
135+
close(quit)
136+
}()
137+
132138
for {
133139
select {
134140
case <-ticker.C:
@@ -154,7 +160,10 @@ func (m *monitorElasticSearchCmd) run() error {
154160
}
155161

156162
if response.Count > 0 {
157-
fmt.Fprintf(m.out, "Rolling back\n")
163+
ticker.Stop()
164+
165+
fmt.Fprintf(m.out, "Failure detected, rolling back...\n")
166+
158167
_, err := m.client.RollbackRelease(
159168
m.name,
160169
helm.RollbackDryRun(m.dryRun),
@@ -170,12 +179,11 @@ func (m *monitorElasticSearchCmd) run() error {
170179
}
171180

172181
fmt.Fprintf(m.out, "Successfully rolled back to previous revision!\n")
173-
break
182+
return nil
174183
}
175184

176185
case <-quit:
177186
ticker.Stop()
178-
fmt.Fprintf(m.out, "No results after %d...\n", m.timeout)
179187
return nil
180188
}
181189
}

cmd/monitor_prometheus.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func newMonitorPrometheusCmd(client helm.Interface, out io.Writer) *cobra.Comman
8080
f.BoolVar(&prometheusMonitor.dryRun, "dry-run", false, "simulate a monitoring")
8181
f.Int64Var(&prometheusMonitor.timeout, "timeout", 300, "time in seconds to wait before assuming a monitoring action is successfull")
8282
f.Int64Var(&prometheusMonitor.rollbackTimeout, "rollback-timeout", 300, "time in seconds to wait for any individual Kubernetes operation during the rollback (like Jobs for hooks)")
83-
f.Int64Var(&prometheusMonitor.interval, "interval", 10, "time in seconds between each PromQL query")
83+
f.Int64Var(&prometheusMonitor.interval, "interval", 10, "time in seconds between each query")
8484
f.BoolVar(&prometheusMonitor.wait, "wait", false, "if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment are in a ready state before marking a rollback as successful. It will wait for as long as --rollback-timeout")
8585
f.BoolVar(&prometheusMonitor.force, "force", false, "force resource update through delete/recreate if needed")
8686
f.BoolVar(&prometheusMonitor.disableHooks, "no-hooks", false, "prevent hooks from running during rollback")
@@ -113,6 +113,12 @@ func (m *monitorPrometheusCmd) run() error {
113113

114114
ticker := time.NewTicker(time.Second * time.Duration(m.interval))
115115

116+
go func() {
117+
time.Sleep(time.Second * time.Duration(m.timeout))
118+
fmt.Fprintf(m.out, "No results after %d second(s)\n", m.timeout)
119+
close(quit)
120+
}()
121+
116122
for {
117123
select {
118124
case <-ticker.C:
@@ -136,7 +142,10 @@ func (m *monitorPrometheusCmd) run() error {
136142
}
137143

138144
if len(response.Data.Result) > 0 {
139-
fmt.Fprintf(m.out, "Rolling back\n")
145+
ticker.Stop()
146+
147+
fmt.Fprintf(m.out, "Failure detected, rolling back...\n")
148+
140149
_, err := m.client.RollbackRelease(
141150
m.name,
142151
helm.RollbackDryRun(m.dryRun),
@@ -152,12 +161,11 @@ func (m *monitorPrometheusCmd) run() error {
152161
}
153162

154163
fmt.Fprintf(m.out, "Successfully rolled back to previous revision!\n")
155-
break
164+
return nil
156165
}
157166

158167
case <-quit:
159168
ticker.Stop()
160-
fmt.Fprintf(m.out, "No results after %d...\n", m.timeout)
161169
return nil
162170
}
163171
}

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
current_version=$(sed -n -e 's/version:[ "]*\([^"]*\).*/\1/p' plugin.yaml)
5+
current_version=$(sed -n -e 's/version:[ "]*\([^"]*\).*/\1/p' $(dirname $0)/plugin.yaml)
66
HELM_MONITOR_VERSION=${HELM_MONITOR_VERSION:-$current_version}
77

88
dir=${HELM_PLUGIN_DIR:-"$(helm home)/plugins/helm-monitor"}

plugin.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
name: "monitor"
33
version: "0.1.0"
4-
usage: "monitor and rollback based on metrics"
4+
usage: "monitor and rollback in case of failure based on metrics or logs"
55
description: |-
6-
Monitor a release, rollback to a previous version based on a query
6+
Query at a given interval a Prometheus or ElasticSearch instance, a rollback
7+
of the release is initiated if the number of item from the result is positive.
78
ignoreFlags: false
89
useTunnel: true
910
command: "$HELM_PLUGIN_DIR/helm-monitor"

0 commit comments

Comments
 (0)