Skip to content

Commit

Permalink
Move API outside of the Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jspdown committed Jun 2, 2020
1 parent 9e7b8b6 commit 617298e
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 263 deletions.
75 changes: 64 additions & 11 deletions cmd/maesh/maesh.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package main

import (
"context"
"fmt"
stdlog "log"
"os"
"sync"
"time"

"github.com/containous/maesh/cmd"
"github.com/containous/maesh/cmd/cleanup"
"github.com/containous/maesh/cmd/prepare"
"github.com/containous/maesh/cmd/proxy"
"github.com/containous/maesh/cmd/version"
"github.com/containous/maesh/pkg/api"
"github.com/containous/maesh/pkg/controller"
"github.com/containous/maesh/pkg/k8s"
preparepkg "github.com/containous/maesh/pkg/prepare"
"github.com/containous/maesh/pkg/signals"
"github.com/containous/traefik/v2/pkg/cli"
"github.com/sirupsen/logrus"
)

func main() {
Expand Down Expand Up @@ -62,6 +66,8 @@ func main() {
}

func maeshCommand(iConfig *cmd.MaeshConfiguration) error {
ctx := cmd.ContextWithSignal(context.Background())

log, err := cmd.BuildLogger(iConfig.LogFormat, iConfig.LogLevel, iConfig.Debug)
if err != nil {
return fmt.Errorf("could not build logger: %w", err)
Expand Down Expand Up @@ -92,34 +98,81 @@ func maeshCommand(iConfig *cmd.MaeshConfiguration) error {
}

aclEnabled := iConfig.ACL || iConfig.SMI

log.Debugf("ACL mode enabled: %t", aclEnabled)

// Create a new stop Channel
stopCh := signals.SetupSignalHandler()
// Create a new ctr.
apiServer, err := api.NewAPI(log, iConfig.APIPort, iConfig.APIHost, clients.KubernetesClient(), iConfig.Namespace)
if err != nil {
return fmt.Errorf("unable to create the API server: %w", err)
}

ctr, err := controller.NewMeshController(clients, controller.Config{
ACLEnabled: aclEnabled,
DefaultMode: iConfig.DefaultMode,
Namespace: iConfig.Namespace,
IgnoreNamespaces: iConfig.IgnoreNamespaces,
APIPort: iConfig.APIPort,
APIHost: iConfig.APIHost,
MinHTTPPort: minHTTPPort,
MaxHTTPPort: minHTTPPort + iConfig.LimitHTTPPort,
MinTCPPort: minTCPPort,
MaxTCPPort: minTCPPort + iConfig.LimitTCPPort,
MinUDPPort: minUDPPort,
MaxUDPPort: minUDPPort + iConfig.LimitUDPPort,
}, log)
}, apiServer, log)
if err != nil {
return fmt.Errorf("unable to create controller: %w", err)
}

// run the ctr loop to process items
if err := ctr.Run(stopCh); err != nil {
return fmt.Errorf("error during controller operation: %w", err)
var wg sync.WaitGroup

apiErrCh := make(chan error)
ctrlErrCh := make(chan error)
ctrlStopCh := make(chan struct{})

// Start the API server.
wg.Add(1)

go func() {
defer wg.Done()

if err := apiServer.ListenAndServe(); err != nil {
apiErrCh <- fmt.Errorf("API server has stopped unexpectedly: %w", err)
}
}()

// Start the Controller.
wg.Add(1)

go func() {
defer wg.Done()

if err := ctr.Run(ctrlStopCh); err != nil {
ctrlErrCh <- fmt.Errorf("controller has stopped unexpectedly: %w", err)
}
}()

// Wait for a stop event and shutdown servers.
select {
case <-ctx.Done():
ctrlStopCh <- struct{}{}

stopAPIServer(apiServer, log)
case err := <-apiErrCh:
log.Error(err)
ctrlStopCh <- struct{}{}
case err := <-ctrlErrCh:
log.Error(err)
stopAPIServer(apiServer, log)
}

wg.Wait()

return nil
}

func stopAPIServer(apiServer *api.API, log logrus.FieldLogger) {
stopCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

if err := apiServer.Shutdown(stopCtx); err != nil {
log.Errorf("Unable to stop the API server: %v", err)
}
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb h1:iKlO7ROJc6SttHKlxzwGytRtBUqX4VARrNTgP2YLX5M=
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down

0 comments on commit 617298e

Please sign in to comment.