Skip to content

Commit

Permalink
Allow restarting and terminating all gateways (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
npepinpe authored Dec 7, 2023
2 parents 2668464 + 40ccd8b commit ba69b8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
11 changes: 9 additions & 2 deletions go-chaos/cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ func AddRestartCmd(rootCmd *cobra.Command, flags *Flags) {
Run: func(cmd *cobra.Command, args []string) {
k8Client, err := createK8ClientWithFlags(flags)
ensureNoError(err)
gatewayPod := restartGateway(k8Client, nil)
internal.LogInfo("Restarted %s", gatewayPod)

if flags.all {
restartGateways(k8Client, "restart", nil)
} else {
gatewayPod := restartGateway(k8Client, nil)
internal.LogInfo("Restarted %s", gatewayPod)
}
},
}

Expand All @@ -76,6 +81,8 @@ func AddRestartCmd(rootCmd *cobra.Command, flags *Flags) {
restartBrokerCmd.MarkFlagsMutuallyExclusive("role", "all")

restartCmd.AddCommand(restartGatewayCmd)
restartGatewayCmd.Flags().BoolVar(&flags.all, "all", false, "Specify whether all gateways should be restarted")

restartCmd.AddCommand(restartWorkerCmd)
restartWorkerCmd.Flags().BoolVar(&flags.all, "all", false, "Specify whether all workers should be restarted")
}
27 changes: 25 additions & 2 deletions go-chaos/cmd/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ func AddTerminateCommand(rootCmd *cobra.Command, flags *Flags) {
k8Client, err := createK8ClientWithFlags(flags)
ensureNoError(err)
gracePeriodSec := int64(0)
gatewayPod := restartGateway(k8Client, &gracePeriodSec)
internal.LogInfo("Terminated %s", gatewayPod)

if flags.all {
restartGateways(k8Client, "terminate", &gracePeriodSec)
} else {
gatewayPod := restartGateway(k8Client, &gracePeriodSec)
internal.LogInfo("Restarted %s", gatewayPod)
}
},
}

Expand All @@ -83,6 +88,7 @@ func AddTerminateCommand(rootCmd *cobra.Command, flags *Flags) {
terminateBrokerCmd.MarkFlagsMutuallyExclusive("role", "all")

terminateCmd.AddCommand(terminateGatewayCmd)
terminateGatewayCmd.Flags().BoolVar(&flags.all, "all", false, "Specify whether all gateways should be terminated")

terminateCmd.AddCommand(terminateWorkerCmd)
terminateWorkerCmd.Flags().BoolVar(&flags.all, "all", false, "Specify whether all workers should be terminated")
Expand Down Expand Up @@ -142,6 +148,23 @@ func restartGateway(k8Client internal.K8Client, gracePeriod *int64) string {
return gatewayPod
}

// Restarts all gateways in the current namespace.
// GracePeriod (in second) can be nil, which would mean using K8 default.
func restartGateways(k8Client internal.K8Client, actionName string, gracePeriod *int64) {
gatewayPodNames, err := k8Client.GetGatewayPodNames()
ensureNoError(err)

if len(gatewayPodNames) <= 0 {
panic(errors.New(fmt.Sprintf("Expected to find a Zeebe gateways in namespace %s, but none found", k8Client.GetCurrentNamespace())))
}

for _, gatewayPodName := range gatewayPodNames {
err = k8Client.RestartPodWithGracePeriod(gatewayPodName, gracePeriod)
ensureNoError(err)
internal.LogInfo("%s %s", actionName, gatewayPodName)
}
}

// Restart a worker pod. The pod is the first from a list of existing pods, if all is not specified.
// GracePeriod (in second) can be nil, which would mean using K8 default.
// The actionName specifies whether it was restarted or terminated to log the right thing.
Expand Down

0 comments on commit ba69b8f

Please sign in to comment.