fix(kube-ovn): fail the plunger when its status server cannot start

The status stream was started in a bare goroutine whose error was
dropped, and the runnable's registration error was discarded too, so a
port that could not be bound left the plunger running with no status
endpoint and nothing in its logs. The server now runs inside its
runnable: a listen error fails the manager, as a metrics server bind
failure does, and stopping the manager is not reported as an error.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-13 12:35:00 +03:00
1 parent 41ad82842a
commit bd998d9653
2 files changed
+75 -6

No files matched your search

@@ -3,8 +3,10 @@ package kubeovnplunger
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
@@ -153,12 +155,9 @@ func (r *KubeOVNPlunger) SetupWithManager(mgr ctrl.Manager, kubeOVNNamespace, ap
Complete(r); err != nil {
return err
}
_ = mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
go srv.ListenAndServe()
<-ctx.Done()
_ = srv.Shutdown(context.Background())
return nil
}))
if err := mgr.Add(serveStatus(srv)); err != nil {
return err
}
return mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
ticker := time.NewTicker(rescanInterval)
defer ticker.Stop()
@@ -180,6 +179,25 @@ func (r *KubeOVNPlunger) SetupWithManager(mgr ctrl.Manager, kubeOVNNamespace, ap
}))
}
// serveStatus runs the status stream for as long as the manager does. A server
// that cannot start fails the manager, as the metrics server does.
func serveStatus(s *sse.Server) manager.RunnableFunc {
return func(ctx context.Context) error {
served := make(chan error, 1)
go func() { served <- s.ListenAndServe() }()
select {
case err := <-served:
return err
case <-ctx.Done():
}
_ = s.Shutdown(context.Background())
if err := <-served; !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
}
type ExecOptions struct {
Namespace string
Pod string
@@ -0,0 +1,51 @@
package kubeovnplunger
import (
"context"
"io"
"log"
"net"
"testing"
"time"
"github.com/cozystack/cozystack/internal/sse"
)
func runStatusServer(ctx context.Context, t *testing.T, addr string) (bool, error) {
t.Helper()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
s := sse.New(sse.Options{Addr: addr, Logger: log.New(io.Discard, "", 0)})
done := make(chan error, 1)
go func() { done <- serveStatus(s)(ctx) }()
select {
case err := <-done:
return true, err
case <-time.After(2 * time.Second):
return false, nil
}
}
// A status server that cannot listen fails its runnable, and with it the
// manager, rather than leaving the plunger running without it.
func TestStatusServerReportsListenFailure(t *testing.T) {
taken, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = taken.Close() })
returned, err := runStatusServer(context.Background(), t, taken.Addr().String())
if !returned || err == nil {
t.Fatalf("serveStatus on an occupied address = %v (returned %v), want an error", err, returned)
}
}
// Stopping the manager stops the server, and that is not an error.
func TestStatusServerStopsWithoutError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(100*time.Millisecond, cancel)
returned, err := runStatusServer(ctx, t, "127.0.0.1:0")
if !returned || err != nil {
t.Fatalf("serveStatus after cancel = %v (returned %v), want nil", err, returned)
}
}