-
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 (#846)
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
2e8fcb1
commit 046f45f
Showing
9 changed files
with
201 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package types | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
const ( | ||
// Default values for Kubernetes services. | ||
KubeControllerManagerPort = "10257" | ||
KubeSchedulerPort = "10259" | ||
KubeletPort = "10250" | ||
KubeletHealthzPort = "10248" | ||
KubeletReadOnlyPort = "10255" | ||
KubeProxyHealthzPort = "10256" | ||
KubeProxyMetricsPort = "10249" | ||
) | ||
|
||
type K8sServiceConfigs struct { | ||
ExtraNodeKubeControllerManagerArgs map[string]*string | ||
ExtraNodeKubeSchedulerArgs map[string]*string | ||
ExtraNodeKubeletArgs map[string]*string | ||
ExtraNodeKubeProxyArgs map[string]*string | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeControllerManagerPort() string { | ||
return getConfigOrDefault(s.ExtraNodeKubeControllerManagerArgs, "--secure-port", KubeControllerManagerPort) | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeSchedulerPort() string { | ||
return getConfigOrDefault(s.ExtraNodeKubeSchedulerArgs, "--secure-port", KubeSchedulerPort) | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeletPort() string { | ||
return getConfigOrDefault(s.ExtraNodeKubeletArgs, "--port", KubeletPort) | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeletHealthzPort() string { | ||
return getConfigOrDefault(s.ExtraNodeKubeletArgs, "--healthz-port", KubeletHealthzPort) | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeletReadOnlyPort() string { | ||
return getConfigOrDefault(s.ExtraNodeKubeletArgs, "--read-only-port", KubeletReadOnlyPort) | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeProxyHealthzPort() (string, error) { | ||
address := getConfigOrDefault(s.ExtraNodeKubeProxyArgs, "--healthz-bind-address", "") | ||
if address == "" { | ||
return KubeProxyHealthzPort, nil | ||
} | ||
_, port, err := net.SplitHostPort(address) | ||
return port, err | ||
} | ||
|
||
func (s *K8sServiceConfigs) GetKubeProxyMetricsPort() (string, error) { | ||
address := getConfigOrDefault(s.ExtraNodeKubeProxyArgs, "--metrics-bind-address", "") | ||
if address == "" { | ||
return KubeProxyMetricsPort, nil | ||
} | ||
_, port, err := net.SplitHostPort(address) | ||
return port, err | ||
} | ||
|
||
func getConfigOrDefault(serviceArgs map[string]*string, optionName, defaultValue string) string { | ||
if serviceArgs == nil { | ||
return defaultValue | ||
} else if val, ok := serviceArgs[optionName]; !ok || val == nil { | ||
return defaultValue | ||
} else { | ||
return *val | ||
} | ||
} |
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,59 @@ | ||
package checks | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/canonical/k8s/pkg/k8sd/types" | ||
"github.com/canonical/k8s/pkg/utils" | ||
) | ||
|
||
// CheckK8sServicePorts verifies that the Kubernetes-related ports are free to be used. | ||
// The ports checked depends on whether a node is a control plane node, or a worker node. | ||
func CheckK8sServicePorts(config types.ClusterConfig, serviceConfigs types.K8sServiceConfigs, isControlPlane bool) error { | ||
var allErrors []error | ||
ports := map[string]string{ | ||
// Default values from official Kubernetes documentation. | ||
"kubelet": serviceConfigs.GetKubeletPort(), | ||
"kubelet-healthz": serviceConfigs.GetKubeletHealthzPort(), | ||
"kubelet-read-only": serviceConfigs.GetKubeletReadOnlyPort(), | ||
"k8s-dqlite": strconv.Itoa(config.Datastore.GetK8sDqlitePort()), | ||
"loadbalancer": strconv.Itoa(config.LoadBalancer.GetBGPPeerPort()), | ||
} | ||
|
||
if port, err := serviceConfigs.GetKubeProxyHealthzPort(); err != nil { | ||
allErrors = append(allErrors, err) | ||
} else { | ||
ports["kube-proxy-healhz"] = port | ||
} | ||
|
||
if port, err := serviceConfigs.GetKubeProxyMetricsPort(); err != nil { | ||
allErrors = append(allErrors, err) | ||
} else { | ||
ports["kube-proxy-metrics"] = port | ||
} | ||
|
||
if isControlPlane { | ||
ports["kube-apiserver"] = strconv.Itoa(config.APIServer.GetSecurePort()) | ||
ports["kube-scheduler"] = serviceConfigs.GetKubeSchedulerPort() | ||
ports["kube-controller-manager"] = serviceConfigs.GetKubeControllerManagerPort() | ||
} else { | ||
ports["kube-apiserver-proxy"] = strconv.Itoa(config.APIServer.GetSecurePort()) | ||
} | ||
|
||
for service, port := range ports { | ||
if port == "0" { | ||
// Some ports may be set to 0 in order to disable them. No need to check. | ||
continue | ||
} | ||
if open, err := utils.IsLocalPortOpen(port); err != nil { | ||
// Could not open port due to error. | ||
allErrors = append(allErrors, fmt.Errorf("could not check port %s (needed by: %s): %w", port, service, err)) | ||
} else if !open { | ||
allErrors = append(allErrors, fmt.Errorf("port %s (needed by: %s) is already in use.", port, service)) | ||
} | ||
} | ||
|
||
return errors.Join(allErrors...) | ||
} |
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,21 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"syscall" | ||
) | ||
|
||
// IsLocalPortOpen checks if the given local port is already open or not. | ||
func IsLocalPortOpen(port string) (bool, error) { | ||
// Without an address, Listen will listen on all addresses. | ||
if l, err := net.Listen("tcp", fmt.Sprintf(":%s", port)); errors.Is(err, syscall.EADDRINUSE) { | ||
return false, nil | ||
} else if err != nil { | ||
return false, err | ||
} else { | ||
l.Close() | ||
return true, nil | ||
} | ||
} |