Skip to content

Commit

Permalink
Minor re-factor: move manager from pkg to it's own package (#18)
Browse files Browse the repository at this point in the history
* re-factor: move manager from common pkg to manager package

Signed-off-by: Matt Siwiec <[email protected]>

* sacrifice a lil' stutter for readability

Signed-off-by: Matt Siwiec <[email protected]>

---------

Signed-off-by: Matt Siwiec <[email protected]>
  • Loading branch information
rizzza authored Mar 31, 2023
1 parent f699f71 commit 390602f
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 80 deletions.
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"go.infratographer.com/loadbalancer-manager-haproxy/internal/dataplaneapi"
"go.infratographer.com/loadbalancer-manager-haproxy/internal/lbapi"
"go.infratographer.com/loadbalancer-manager-haproxy/internal/pkg"
"go.infratographer.com/loadbalancer-manager-haproxy/internal/manager"

"github.com/nats-io/nats.go"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -81,7 +81,7 @@ func run(cmdCtx context.Context, v *viper.Viper) error {
dpc := dataplaneapi.NewClient(viper.GetString("dataplane.url"))
lbc := lbapi.NewClient(viper.GetString("loadbalancerapi.url"))

mgr := &pkg.ManagerConfig{
mgr := &manager.Manager{
Context: ctx,
Logger: logger,
NatsConn: natsConn,
Expand Down
66 changes: 19 additions & 47 deletions internal/pkg/manager.go → internal/manager/manager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package manager

import (
"context"
Expand Down Expand Up @@ -37,36 +37,8 @@ type dataPlaneAPI interface {
ApiIsReady(ctx context.Context) bool
}

// TODO: move from pkg to internal/manager package
type Origin struct {
ID string
Name string
IPAddress string
Disabled bool
Port int64
}

type Pool struct {
ID string
Name string
Origins []Origin
}

type Port struct {
AddressFamily string
ID string
Name string
Port int64
Pools []Pool
}

type LoadBalancer struct {
ID string
Ports []Port
}

// ManagerConfig contains configuration and client connections
type ManagerConfig struct {
// Manager contains configuration and client connections
type Manager struct {
Context context.Context
Logger *zap.SugaredLogger
NatsConn *nats.Conn
Expand All @@ -79,7 +51,7 @@ type ManagerConfig struct {
}

// Run subscribes to a NATS subject and updates the haproxy config via dataplaneapi
func (m *ManagerConfig) Run() error {
func (m *Manager) Run() error {
// wait until the Data Plane API is running
if err := m.waitForDataPlaneReady(dataPlaneAPIRetryLimit, dataPlaneAPIRetrySleep); err != nil {
m.Logger.Fatal("unable to reach dataplaneapi. is it running?")
Expand Down Expand Up @@ -121,13 +93,13 @@ func (m *ManagerConfig) Run() error {

_ = m.processMsg(msg)

// TODO - @rizzza - √√ on this ack with Tyler. We ack everything? nack is fatal with this driver.
// TODO - @rizzza - For now, we ack everything. nack is fatal with this driver.
msg.Ack()
}
}

// processMsg message handler
func (m ManagerConfig) processMsg(msg *pubsub.Message) error {
func (m Manager) processMsg(msg *pubsub.Message) error {
pubsubMsg := pubsubx.Message{}
if err := json.Unmarshal(msg.Body, &pubsubMsg); err != nil {
m.Logger.Errorw("failed to process data in msg", zap.Error(err))
Expand All @@ -150,7 +122,7 @@ func (m ManagerConfig) processMsg(msg *pubsub.Message) error {
}

// updateConfigToLatest update the haproxy cfg to either baseline or one requested from lbapi with optional lbID param
func (m *ManagerConfig) updateConfigToLatest(lbID ...string) error {
func (m *Manager) updateConfigToLatest(lbID ...string) error {
if len(lbID) > 1 {
return fmt.Errorf("optional lbID param must be not set or set to a singular loadbalancer ID")
}
Expand All @@ -171,32 +143,32 @@ func (m *ManagerConfig) updateConfigToLatest(lbID ...string) error {
return err
}

lb := LoadBalancer{
lb := loadBalancer{
ID: lbResp.LoadBalancer.ID,
}

// translate responses, populate data structure
for i, port := range lbResp.LoadBalancer.Ports {
lb.Ports = append(lb.Ports, Port{
AddressFamily: port.AddressFamily,
ID: port.ID,
Name: port.Name,
Port: port.Port,
for i, p := range lbResp.LoadBalancer.Ports {
lb.Ports = append(lb.Ports, port{
AddressFamily: p.AddressFamily,
ID: p.ID,
Name: p.Name,
Port: p.Port,
})

for _, poolID := range port.Pools {
for _, poolID := range p.Pools {
poolResp, err := m.LBClient.GetPool(m.Context, poolID)
if err != nil {
return err
}

data := Pool{
data := pool{
ID: poolID,
Name: poolResp.Pool.Name,
}

for _, o := range poolResp.Pool.Origins {
data.Origins = append(data.Origins, Origin{
data.Origins = append(data.Origins, origin{
ID: o.ID,
Name: o.Name,
IPAddress: o.IPAddress,
Expand Down Expand Up @@ -227,7 +199,7 @@ func (m *ManagerConfig) updateConfigToLatest(lbID ...string) error {
return nil
}

func (m ManagerConfig) waitForDataPlaneReady(retries int, sleep time.Duration) error {
func (m Manager) waitForDataPlaneReady(retries int, sleep time.Duration) error {
for i := 0; i < retries; i++ {
if m.DataPlaneClient.ApiIsReady(m.Context) {
m.Logger.Info("dataplaneapi is ready")
Expand All @@ -242,7 +214,7 @@ func (m ManagerConfig) waitForDataPlaneReady(retries int, sleep time.Duration) e
}

// mergeConfig takes the response from lb api, merges with the base haproxy config and returns it
func mergeConfig(cfg parser.Parser, lb *LoadBalancer) (parser.Parser, error) {
func mergeConfig(cfg parser.Parser, lb *loadBalancer) (parser.Parser, error) {
for _, p := range lb.Ports {
// create port
if err := cfg.SectionsCreate(parser.Frontends, p.Name); err != nil {
Expand Down
58 changes: 29 additions & 29 deletions internal/pkg/manager_test.go → internal/manager/manager_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package manager

import (
"context"
Expand All @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.infratographer.com/loadbalancer-manager-haproxy/internal/lbapi"
"go.infratographer.com/loadbalancer-manager-haproxy/internal/pkg/mock"
"go.infratographer.com/loadbalancer-manager-haproxy/internal/manager/mock"
"go.uber.org/zap"
)

Expand All @@ -24,7 +24,7 @@ const (
func TestMergeConfig(t *testing.T) {
MergeConfigTests := []struct {
name string
testInput LoadBalancer
testInput loadBalancer
expectedCfgFilename string
}{
{"ssh service one pool", mergeTestData1, "lb-ex-1-exp.cfg"},
Expand Down Expand Up @@ -69,13 +69,13 @@ func TestUpdateConfigToLatest(t *testing.T) {
},
}

mgrCfg := ManagerConfig{
mgr := Manager{
Logger: logger,
LBClient: mockLBAPI,
BaseCfgPath: testBaseCfgPath,
}

err := mgrCfg.updateConfigToLatest("58622a8d-54a2-4b0c-8b5f-8de7dff29f6f")
err := mgr.updateConfigToLatest("58622a8d-54a2-4b0c-8b5f-8de7dff29f6f")
assert.NotNil(t, err)
})

Expand Down Expand Up @@ -105,13 +105,13 @@ func TestUpdateConfigToLatest(t *testing.T) {
},
}

mgrCfg := ManagerConfig{
mgr := Manager{
Logger: logger,
LBClient: mockLBAPI,
BaseCfgPath: testBaseCfgPath,
}

err := mgrCfg.updateConfigToLatest("58622a8d-54a2-4b0c-8b5f-8de7dff29f6f")
err := mgr.updateConfigToLatest("58622a8d-54a2-4b0c-8b5f-8de7dff29f6f")
assert.NotNil(t, err)
})

Expand All @@ -124,23 +124,23 @@ func TestUpdateConfigToLatest(t *testing.T) {
},
}

mgrCfg := ManagerConfig{
mgr := Manager{
Logger: logger,
DataPlaneClient: mockDataplaneAPI,
BaseCfgPath: testBaseCfgPath,
}

err := mgrCfg.updateConfigToLatest()
err := mgr.updateConfigToLatest()
require.Nil(t, err)

contents, err := os.ReadFile(testBaseCfgPath)
require.Nil(t, err)

// remove that 'unnamed_defaults_1' thing the haproxy parser library puts in the default section,
// even though the library is configured to not include default section labels
mgrCfg.currentConfig = strings.Replace(mgrCfg.currentConfig, " unnamed_defaults_1", "", -1)
mgr.currentConfig = strings.Replace(mgr.currentConfig, " unnamed_defaults_1", "", -1)

assert.Equal(t, strings.TrimSpace(string(contents)), strings.TrimSpace(mgrCfg.currentConfig))
assert.Equal(t, strings.TrimSpace(string(contents)), strings.TrimSpace(mgr.currentConfig))
})

t.Run("successfully queries lb api and merges changes with base config", func(t *testing.T) {
Expand Down Expand Up @@ -204,36 +204,36 @@ func TestUpdateConfigToLatest(t *testing.T) {
},
}

mgrCfg := ManagerConfig{
mgr := Manager{
Logger: logger,
LBClient: mockLBAPI,
DataPlaneClient: mockDataplaneAPI,
BaseCfgPath: testBaseCfgPath,
}

err := mgrCfg.updateConfigToLatest("58622a8d-54a2-4b0c-8b5f-8de7dff29f6f")
err := mgr.updateConfigToLatest("58622a8d-54a2-4b0c-8b5f-8de7dff29f6f")
require.Nil(t, err)

expCfg, err := os.ReadFile(fmt.Sprintf("%s/%s", testDataBaseDir, "lb-ex-1-exp.cfg"))
require.Nil(t, err)

assert.Equal(t, strings.TrimSpace(string(expCfg)), strings.TrimSpace(mgrCfg.currentConfig))
assert.Equal(t, strings.TrimSpace(string(expCfg)), strings.TrimSpace(mgr.currentConfig))
})
}

var mergeTestData1 = LoadBalancer{
var mergeTestData1 = loadBalancer{
ID: "58622a8d-54a2-4b0c-8b5f-8de7dff29f6f",
Ports: []Port{
Ports: []port{
{
Name: "ssh-service",
AddressFamily: "ipv4",
Port: 22,
ID: "16dd23d7-d3ab-42c8-a645-3169f2659a0b",
Pools: []Pool{
Pools: []pool{
{
ID: "49faa4a3-8d0b-4a7a-8bb9-7ed1b5995e49",
Name: "ssh-service-a",
Origins: []Origin{
Origins: []origin{
{
ID: "c0a80101-0000-0000-0000-000000000001",
Name: "svr1-2222",
Expand Down Expand Up @@ -262,19 +262,19 @@ var mergeTestData1 = LoadBalancer{
},
}

var mergeTestData2 = LoadBalancer{
var mergeTestData2 = loadBalancer{
ID: "58622a8d-54a2-4b0c-8b5f-8de7dff29f6f",
Ports: []Port{
Ports: []port{
{
Name: "ssh-service",
AddressFamily: "ipv4",
Port: 22,
ID: "16dd23d7-d3ab-42c8-a645-3169f2659a0b",
Pools: []Pool{
Pools: []pool{
{
ID: "49faa4a3-8d0b-4a7a-8bb9-7ed1b5995e49",
Name: "ssh-service-a",
Origins: []Origin{
Origins: []origin{
{
ID: "c0a80101-0000-0000-0000-000000000001",
Name: "svr1-2222",
Expand All @@ -301,7 +301,7 @@ var mergeTestData2 = LoadBalancer{
{
ID: "c9bd57ac-6d88-4786-849e-0b228c17d645",
Name: "ssh-service-b",
Origins: []Origin{
Origins: []origin{
{
ID: "b1982331-0000-0000-0000-000000000001",
Name: "svr1-2222",
Expand All @@ -316,19 +316,19 @@ var mergeTestData2 = LoadBalancer{
},
}

var mergeTestData3 = LoadBalancer{
var mergeTestData3 = loadBalancer{
ID: "a522bc95-2a74-4005-919d-6ae0a5be056d",
Ports: []Port{
Ports: []port{
{
Name: "http",
AddressFamily: "ipv4",
Port: 80,
ID: "16dd23d7-d3ab-42c8-a645-3169f2659a0b",
Pools: []Pool{
Pools: []pool{
{
ID: "49faa4a3-8d0b-4a7a-8bb9-7ed1b5995e49",
Name: "ssh-service-a",
Origins: []Origin{
Origins: []origin{
{
ID: "c0a80101-0000-0000-0000-000000000001",
Name: "svr1",
Expand All @@ -345,11 +345,11 @@ var mergeTestData3 = LoadBalancer{
AddressFamily: "ipv4",
Port: 443,
ID: "8ca812cc-9c3d-4fed-95be-40a773f7d876",
Pools: []Pool{
Pools: []pool{
{
ID: "d94ad98b-b074-4794-896f-d71ae3b7b0ac",
Name: "ssh-service-a",
Origins: []Origin{
Origins: []origin{
{
ID: "676a1536-0a17-4676-9296-ee957e5871c1",
Name: "svr1",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions internal/manager/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package manager

type origin struct {
ID string
Name string
IPAddress string
Disabled bool
Port int64
}

type pool struct {
ID string
Name string
Origins []origin
}

type port struct {
AddressFamily string
ID string
Name string
Port int64
Pools []pool
}

type loadBalancer struct {
ID string
Ports []port
}
2 changes: 0 additions & 2 deletions internal/pkg/doc.go

This file was deleted.

0 comments on commit 390602f

Please sign in to comment.