Created
September 7, 2020 12:43
-
-
Save michaljemala/df1d14804375033df1dcd4577dbba268 to your computer and use it in GitHub Desktop.
Zero-downtime deployment tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"time" | |
"golang.org/x/sync/errgroup" | |
) | |
const ( | |
NumConcurrentSenders = 50 | |
MaxRequestPerSecond = 500 | |
Duration = 2 * time.Minute | |
DeploymentName = "todod" | |
Endpoint = "http://192.168.64.2:32197/todo" | |
) | |
func main() { | |
var g errgroup.Group | |
g.Go(func() error { | |
return runFortio(fortioConfig{ | |
C: NumConcurrentSenders, | |
QPS: MaxRequestPerSecond, | |
T: Duration, | |
URL: Endpoint, | |
}) | |
}) | |
g.Go(func() error { | |
return chaosDeployment(chaosConfig{ | |
Deployment: DeploymentName, | |
T: Duration, | |
}) | |
}) | |
if err := g.Wait(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
type chaosConfig struct { | |
Deployment string | |
T time.Duration | |
} | |
func chaosDeployment(cfg chaosConfig) error { | |
if _, err := exec.LookPath("kubectl"); err != nil { | |
return fmt.Errorf("kubectl executable not found: %v", err) | |
} | |
tickCh := time.Tick(cfg.T / 5) | |
doneCh := time.After(cfg.T) | |
loop: | |
for { | |
select { | |
case <-tickCh: | |
log.Print("chaos: restarting deployment") | |
cmd := exec.Command("kubectl", | |
"rollout", "restart", "deployment", cfg.Deployment) | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
if err := cmd.Run(); err != nil { | |
return fmt.Errorf("command execution failed: %v", err) | |
} | |
case <-doneCh: | |
break loop | |
} | |
} | |
log.Print("chaos: finished") | |
return nil | |
} | |
type fortioConfig struct { | |
C int | |
QPS float64 | |
T time.Duration | |
URL string | |
} | |
func runFortio(cfg fortioConfig) error { | |
if _, err := exec.LookPath("fortio"); err != nil { | |
return fmt.Errorf("fortio executable not found: %v", err) | |
} | |
cmd := exec.Command("fortio", | |
"load", | |
"-c", fmt.Sprintf("%d", cfg.C), | |
"-qps", fmt.Sprintf("%f", cfg.QPS), | |
"-t", fmt.Sprintf("%s", cfg.T.String()), | |
cfg.URL, | |
) | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
if err := cmd.Run(); err != nil { | |
return fmt.Errorf("command execution failed: %v", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment