Skip to content

Commit

Permalink
fix: crash if targetContainer does not exist
Browse files Browse the repository at this point in the history
Signed-off-by: hoyhbx <hoyhbx@gmail.com>
  • Loading branch information
hoyhbx committed Jun 22, 2022
1 parent 44a4c25 commit 12adfd5
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions k8sutils/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ func executeCommand(cr *redisv1beta1.RedisCluster, cmd []string, podName string)
config, err := generateK8sConfig()
if err != nil {
logger.Error(err, "Could not find pod to execute")
return
}
targetContainer, pod := getContainerID(cr, podName)
if targetContainer < 0 {
logger.Error(err, "Could not find pod to execute")
return
}

req := generateK8sClient().CoreV1().RESTClient().Post().Resource("pods").Name(podName).Namespace(cr.Namespace).SubResource("exec")
Expand All @@ -290,6 +292,7 @@ func executeCommand(cr *redisv1beta1.RedisCluster, cmd []string, podName string)
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
logger.Error(err, "Failed to init executor")
return
}

err = exec.Stream(remotecommand.StreamOptions{
Expand All @@ -299,6 +302,7 @@ func executeCommand(cr *redisv1beta1.RedisCluster, cmd []string, podName string)
})
if err != nil {
logger.Error(err, "Could not execute command", "Command", cmd, "Output", execOut.String(), "Error", execErr.String())
return
}
logger.Info("Successfully executed the command", "Command", cmd, "Output", execOut.String())
}
Expand Down

0 comments on commit 12adfd5

Please sign in to comment.