Skip to content

Commit

Permalink
Add Stop method to the cable Engine
Browse files Browse the repository at this point in the history
This sets it in a non-running state so it appears to be passive with no
connections. The driver and other internal state is kept to maintain
continuity on restart.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Oct 25, 2023
1 parent 1536fac commit 2c5f817
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
30 changes: 26 additions & 4 deletions pkg/cableengine/cableengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
type Engine interface {
// StartEngine performs any general set up work needed independent of any remote connections.
StartEngine() error
Stop()
// InstallCable performs any set up work needed for connecting to given remote endpoint.
// Once InstallCable completes, it should be possible to connect to remote
// Pods or Services behind the given endpoint.
Expand All @@ -68,6 +69,7 @@ type Engine interface {
type engine struct {
sync.Mutex
driver cable.Driver
running bool
localCluster types.SubmarinerCluster
localEndpoint types.SubmarinerEndpoint
natDiscovery natdiscovery.Interface
Expand Down Expand Up @@ -101,12 +103,27 @@ func (i *engine) StartEngine() error {
return err
}

logger.Infof("CableEngine controller started, driver: %q", i.driver.GetName())
i.running = true

logger.Infof("CableEngine started with driver %q", i.driver.GetName())

return nil
}

func (i *engine) Stop() {
i.Lock()
defer i.Unlock()

i.running = false

logger.Info("CableEngine stopped")
}

func (i *engine) startDriver() error {
if i.driver != nil {
return nil
}

var err error

if i.driver, err = cable.NewDriver(&i.localEndpoint, &i.localCluster); err != nil {
Expand Down Expand Up @@ -145,6 +162,10 @@ func (i *engine) installCableWithNATInfo(rnat *natdiscovery.NATEndpointInfo) err
delete(i.natDiscoveryPending, rnat.Endpoint.Spec.CableName)
}

if !i.running {
return nil
}

activeConnections, err := i.driver.GetActiveConnections()
if err != nil {
return errors.Wrap(err, "error getting the active connections")
Expand Down Expand Up @@ -261,7 +282,7 @@ func (i *engine) GetHAStatus() v1.HAStatus {
i.Lock()
defer i.Unlock()

if i.driver == nil {
if !i.running {
return v1.HAStatusPassive
}

Expand All @@ -275,10 +296,11 @@ func (i *engine) ListCableConnections() ([]v1.Connection, error) {
i.Lock()
defer i.Unlock()

if i.driver != nil {
if i.running {
return i.driver.GetConnections() //nolint:wrapcheck // Let the caller wrap it
}
// if no driver, we can safely report that no connections exist.

// if not running, we can safely report that no connections exist.
return []v1.Connection{}, nil
}

Expand Down
24 changes: 23 additions & 1 deletion pkg/cableengine/cableengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,34 @@ var _ = Describe("Cable Engine", func() {
})
})

Context("and list cable connections", func() {
Context("and list of cable connections is queried", func() {
It("should return non-nil", func() {
Expect(engine.ListCableConnections()).ToNot(BeNil())
})
})
})

When("after Stop is called", func() {
BeforeEach(func() {
fakeDriver.Connections = []subv1.Connection{{Endpoint: remoteEndpoint.Spec}}
})

JustBeforeEach(func() {
engine.Stop()
})

Context("and the HA status is queried", func() {
It("should return passive", func() {
Expect(engine.GetHAStatus()).To(Equal(subv1.HAStatusPassive))
})
})

Context("and list of cable connections is queried", func() {
It("should return empty", func() {
Expect(engine.ListCableConnections()).To(BeEmpty())
})
})
})
})

func TestCableEngine(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cableengine/fake/cableengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (e *Engine) StartEngine() error {
return e.ErrOnStart
}

func (e *Engine) Stop() {
}

func (e *Engine) InstallCable(endpoint *v1.Endpoint) error {
err := e.ErrOnInstallCable
if err != nil {
Expand Down

0 comments on commit 2c5f817

Please sign in to comment.