Skip to content

Commit

Permalink
fix: pick random port instead of failing
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeztk committed Jun 3, 2023
1 parent 01ec7dc commit deec9df
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions internal/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"os"
"strings"
"time"
Expand Down Expand Up @@ -93,6 +95,62 @@ func Validate(image string, configPath string, cmd *cobra.Command, debug bool) (

}

// checkPorts checks if all the ports are available, if not, for each busy port
// it tries with a higher port until a valid one is found or it times out.
// If successful, it updates the validator config with the new ports
func checkPorts(validatorConfig *canaryv1.Validator) error {
found := make(chan struct{})
// hold the ports that are already in use
usedPorts := make(map[int32]struct{}, len(validatorConfig.Ports))
// Generate a random port between 1024 and 65535
getRandomPort := func() int32 {
return rand.Int31n(65535-1024) + 1024
}

for i := range validatorConfig.Ports {
port := validatorConfig.Ports[i].Port

go func() {
for {
if _, ok := usedPorts[port]; ok {
port = getRandomPort()
continue
}

if _, err := net.DialTimeout("tcp", net.JoinHostPort("", fmt.Sprintf("%d", port)), time.Second); err != nil {
if port == validatorConfig.Ports[i].Port {
usedPorts[port] = struct{}{}
found <- struct{}{}
return
}

for j := range validatorConfig.Checks {
if validatorConfig.Checks[j].Probe.HTTPGet.Port == int(validatorConfig.Ports[i].Port) {
validatorConfig.Checks[j].Probe.HTTPGet.Port = int(port)
}
}
validatorConfig.Ports[i].Port = port

usedPorts[port] = struct{}{}
found <- struct{}{}
return
}

port = getRandomPort()
}
}()

// wait for 3 seconds to connect to a valid port, fail otherwise
select {
case <-time.After(time.Second * 3):
return fmt.Errorf("could not find a valid port for %d", validatorConfig.Ports[i].Port)
case <-found:
continue
}
}
return nil
}

func loadConfig(filePath string) tea.Cmd {
return func() tea.Msg {
var validatorConfig *canaryv1.Validator
Expand All @@ -116,6 +174,13 @@ func loadConfig(filePath string) tea.Cmd {
}
}

if err = checkPorts(validatorConfig); err != nil {
return configLoaded{
Config: nil,
Error: err,
}
}

return configLoaded{
Config: validatorConfig,
Error: nil,
Expand Down

0 comments on commit deec9df

Please sign in to comment.