From 359aab76c4a0ee6c8039097313135bfc8d1669ee Mon Sep 17 00:00:00 2001 From: Etienne Audet-Cobello Date: Tue, 19 Nov 2024 17:42:18 -0500 Subject: [PATCH 1/2] validate ports in PreInitChecks this check happens before any of the k8s binaries are started, it's the right place to validate that the api-server, kubelet, etc. ports are available. --- src/k8s/pkg/snap/snap.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/k8s/pkg/snap/snap.go b/src/k8s/pkg/snap/snap.go index 8645fe408..c3487adb2 100644 --- a/src/k8s/pkg/snap/snap.go +++ b/src/k8s/pkg/snap/snap.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "net" "os" "os/exec" "path/filepath" @@ -329,7 +330,26 @@ func (s *snap) SnapctlSet(ctx context.Context, args ...string) error { } func (s *snap) PreInitChecks(ctx context.Context, config types.ClusterConfig) error { - // TODO: check for available ports for k8s-dqlite, apiserver, containerd, etc + // TODO(eac): Make these configurable or part of a Config struct + ports := []int{ + config.APIServer.GetSecurePort(), // kubeAPIServerPort + config.Datastore.GetK8sDqlitePort(), // k8sDqlitePort + 10248, // kubeletHealthzPort + 10250, // kubeletPort + 10256, // kubeProxyHealthzPort + 10257, // kubeControllerManagerPort + 10259, // kubeSchedulerPort + } + for _, port := range ports { + for _, address := range []string{"127.0.0.1", "0.0.0.0"} { + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port)) + if err != nil { + listener.Close() + return fmt.Errorf("couldn't bind to required address %s. Is it in use?", address) + } + listener.Close() + } + } // NOTE(neoaggelos): in some environments the Kubernetes might hang when running for the first time // This works around the issue by running them once during the install hook From 44899237a8b593a4f1fbbdda2046418fefb5f139 Mon Sep 17 00:00:00 2001 From: Etienne Audet-Cobello Date: Wed, 20 Nov 2024 14:35:48 -0500 Subject: [PATCH 2/2] fix nil pointer dereference --- src/k8s/pkg/snap/snap.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/k8s/pkg/snap/snap.go b/src/k8s/pkg/snap/snap.go index c3487adb2..05722bac2 100644 --- a/src/k8s/pkg/snap/snap.go +++ b/src/k8s/pkg/snap/snap.go @@ -344,8 +344,7 @@ func (s *snap) PreInitChecks(ctx context.Context, config types.ClusterConfig) er for _, address := range []string{"127.0.0.1", "0.0.0.0"} { listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port)) if err != nil { - listener.Close() - return fmt.Errorf("couldn't bind to required address %s. Is it in use?", address) + return fmt.Errorf("couldn't bind to required address %s:%d. Is it in use?", address, port) } listener.Close() }