-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checks k8s-related port availability in PreInitChecks
PreInitChecks is called on bootstrap or when joining another Kubernetes cluster. Kubernetes and its services open up several ports; if they're already in use, we cannot progress. Adding these checks will make these error cases more explainable to the user, rather than a generic bootstrap / join error.
- Loading branch information
1 parent
946a94b
commit c391136
Showing
7 changed files
with
92 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"os" | ||
"strconv" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
// IsLocalPortOpen checks if the given local port is already open or not. | ||
func IsLocalPortOpen(port int) (bool, error) { | ||
if err := checkPort("localhost", port, 500*time.Millisecond); err == nil { | ||
return true, nil | ||
} else if errors.Is(err, os.ErrDeadlineExceeded) || errors.Is(err, syscall.ECONNREFUSED) { | ||
return false, nil | ||
} else { | ||
// could not open due to error, couldn't check. | ||
return false, err | ||
} | ||
} | ||
|
||
func checkPort(host string, port int, timeout time.Duration) error { | ||
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, strconv.Itoa(port)), timeout) | ||
if err != nil { | ||
return err | ||
} | ||
conn.Close() | ||
return nil | ||
} |