Skip to content

Commit

Permalink
server: simplify server start
Browse files Browse the repository at this point in the history
Move server start code to a common function that both regular
and test code can use. Also shut down the server from the
testcases.

Signed-off-by: Dan Williams <[email protected]>
  • Loading branch information
dcbw committed Sep 8, 2023
1 parent be42a90 commit f6776b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
12 changes: 2 additions & 10 deletions cmd/multus-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"syscall"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
utilwait "k8s.io/apimachinery/pkg/util/wait"

"gopkg.in/k8snetworkplumbingwg/multus-cni.v4/pkg/logging"
Expand Down Expand Up @@ -207,15 +206,8 @@ func startMultusDaemon(ctx context.Context, daemonConfig *srv.ControllerNetConf)
return fmt.Errorf("failed to start the CNI server using socket %s. Reason: %+v", api.SocketPath(daemonConfig.SocketDir), err)
}

server.SetKeepAlivesEnabled(false)
go func() {
utilwait.UntilWithContext(ctx, func(ctx context.Context) {
logging.Debugf("open for business")
if err := server.Serve(l); err != nil {
utilruntime.HandleError(fmt.Errorf("CNI server Serve() failed: %v", err))
}
}, 0)
}()
server.Start(ctx, l)

go func() {
<-ctx.Done()
server.Shutdown(context.Background())
Expand Down
17 changes: 17 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package server

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -38,6 +39,9 @@ import (
"gopkg.in/k8snetworkplumbingwg/multus-cni.v4/pkg/server/api"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v4/pkg/server/config"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v4/pkg/types"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
utilwait "k8s.io/apimachinery/pkg/util/wait"
)

const (
Expand Down Expand Up @@ -180,6 +184,8 @@ func newCNIServer(rundir string, kubeClient *k8s.ClientInfo, exec invoke.Exec, s
),
},
}
s.SetKeepAlivesEnabled(false)

// register metrics
prometheus.MustRegister(s.metrics.requestCounter)

Expand Down Expand Up @@ -249,6 +255,17 @@ func newCNIServer(rundir string, kubeClient *k8s.ClientInfo, exec invoke.Exec, s
return s, nil
}

func (s *Server) Start(ctx context.Context, l net.Listener) {
go func() {
utilwait.UntilWithContext(ctx, func(ctx context.Context) {
logging.Debugf("open for business")
if err := s.Serve(l); err != nil {
utilruntime.HandleError(fmt.Errorf("CNI server Serve() failed: %v", err))
}
}, 0)
}()
}

func (s *Server) handleCNIRequest(r *http.Request) ([]byte, error) {
var cr api.Request
b, err := io.ReadAll(r.Body)
Expand Down
26 changes: 15 additions & 11 deletions pkg/server/thick_cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import (
"github.com/prometheus/client_golang/prometheus"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
utilwait "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/record"

Expand Down Expand Up @@ -102,14 +100,18 @@ var _ = Describe(suiteName, func() {
cniServer *Server
K8sClient *k8s.ClientInfo
netns ns.NetNS
ctx context.Context
cancel context.CancelFunc
)

BeforeEach(func() {
var err error
K8sClient = fakeK8sClient()

Expect(FilesystemPreRequirements(thickPluginRunDir)).To(Succeed())
cniServer, err = startCNIServer(thickPluginRunDir, K8sClient, nil)

ctx, cancel = context.WithCancel(context.TODO())
cniServer, err = startCNIServer(ctx, thickPluginRunDir, K8sClient, nil)
Expect(err).NotTo(HaveOccurred())

netns, err = testutils.NewNS()
Expand All @@ -121,6 +123,7 @@ var _ = Describe(suiteName, func() {
})

AfterEach(func() {
cancel()
unregisterMetrics(cniServer)
Expect(cniServer.Close()).To(Succeed())
Expect(teardownCNIEnv()).To(Succeed())
Expand Down Expand Up @@ -151,6 +154,8 @@ var _ = Describe(suiteName, func() {
cniServer *Server
K8sClient *k8s.ClientInfo
netns ns.NetNS
ctx context.Context
cancel context.CancelFunc
)

BeforeEach(func() {
Expand All @@ -163,7 +168,9 @@ var _ = Describe(suiteName, func() {
}`

Expect(FilesystemPreRequirements(thickPluginRunDir)).To(Succeed())
cniServer, err = startCNIServer(thickPluginRunDir, K8sClient, []byte(dummyServerConfig))

ctx, cancel = context.WithCancel(context.TODO())
cniServer, err = startCNIServer(ctx, thickPluginRunDir, K8sClient, []byte(dummyServerConfig))
Expect(err).NotTo(HaveOccurred())

netns, err = testutils.NewNS()
Expand All @@ -175,6 +182,7 @@ var _ = Describe(suiteName, func() {
})

AfterEach(func() {
cancel()
unregisterMetrics(cniServer)
Expect(cniServer.Close()).To(Succeed())
Expect(teardownCNIEnv()).To(Succeed())
Expand Down Expand Up @@ -245,7 +253,7 @@ func createFakePod(k8sClient *k8s.ClientInfo, podName string) error {
return err
}

func startCNIServer(runDir string, k8sClient *k8s.ClientInfo, servConfig []byte) (*Server, error) {
func startCNIServer(ctx context.Context, runDir string, k8sClient *k8s.ClientInfo, servConfig []byte) (*Server, error) {
const period = 0

cniServer, err := newCNIServer(runDir, k8sClient, &fakeExec{}, servConfig)
Expand All @@ -258,12 +266,8 @@ func startCNIServer(runDir string, k8sClient *k8s.ClientInfo, servConfig []byte)
return nil, fmt.Errorf("failed to start the CNI server using socket %s. Reason: %+v", api.SocketPath(runDir), err)
}

cniServer.SetKeepAlivesEnabled(false)
go utilwait.Forever(func() {
if err := cniServer.Serve(l); err != nil {
utilruntime.HandleError(fmt.Errorf("CNI server Serve() failed: %v", err))
}
}, period)
cniServer.Start(ctx, l)

return cniServer, nil
}

Expand Down

0 comments on commit f6776b5

Please sign in to comment.