Skip to content

Commit

Permalink
Create port additionalSubjects not populated in all cases (#308)
Browse files Browse the repository at this point in the history
* ignore go work files

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* oidc=false

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* ensure additionalSubjects are present in port create when no pool ids associated

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

---------

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>
  • Loading branch information
rizzza authored Jan 9, 2024
1 parent b4721c0 commit d9fefb0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ debugConfig.yaml
.devcontainer/nats/resolver.conf
.devcontainer/nsc

# go work
go.work
go.work.sum

# helm test
test-output/

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ generate: background-run .generate kill-running ## Generates code
go-run: ## Runs the app
@echo --- Running binary...
@date --rfc-3339=seconds
@go run main.go serve --dev
@go run main.go serve --dev --oidc=false

background-run: ## Runs in the app in the background
@date --rfc-3339=seconds
Expand Down
34 changes: 15 additions & 19 deletions internal/manualhooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,30 +1007,26 @@ func PortHooks() []ent.Hook {
}

// Ensure we have additional relevant subjects in the event msg
addSubjPools, err := m.Client().Pool.Query().WithPorts(func(q *generated.PortQuery) {
q.WithLoadBalancer()
}).Where(pool.HasPortsWith(port.IDEQ(objID))).All(ctx)
addSubjPort, err := m.Client().Port.Query().WithPools().WithLoadBalancer().Where(port.IDEQ(objID)).Only(ctx)
if err == nil {
for _, pool := range addSubjPools {
for _, port := range pool.Edges.Ports {
if !slices.Contains(msg.AdditionalSubjectIDs, port.LoadBalancerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.LoadBalancerID)
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.LoadBalancerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.LoadBalancerID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.LocationID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.LocationID)
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.LocationID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.LocationID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.ProviderID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.ProviderID)
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.OwnerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.OwnerID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.OwnerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.OwnerID)
}
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.ProviderID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.ProviderID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, pool.ID) && objID != pool.ID {
for _, pool := range addSubjPort.Edges.Pools {
if !slices.Contains(msg.AdditionalSubjectIDs, pool.ID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, pool.ID)
}

Expand Down
59 changes: 47 additions & 12 deletions internal/manualhooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,58 @@ func Test_PortCreateHook(t *testing.T) {
changesChannel, err := testutils.EventsConn.SubscribeChanges(ctx, "create.load-balancer-port")
require.NoError(t, err, "failed to subscribe to changes")

lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
pool := (&testutils.PoolBuilder{}).MustNew(ctx)

testutils.EntClient.Port.Use(manualhooks.PortHooks()...)

// Act
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{pool.ID}, LoadBalancerID: lb.ID}).MustNew(ctx)
t.Run("with pool", func(t *testing.T) {
// Act
lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
pool := (&testutils.PoolBuilder{OwnerID: lb.OwnerID}).MustNew(ctx)
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{pool.ID}, LoadBalancerID: lb.ID}).MustNew(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)
msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)

// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{pool.ID, pool.OwnerID, lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs
// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{pool.ID, lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs

assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
})

t.Run("with multiple pools", func(t *testing.T) {
// Act
lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
pool := (&testutils.PoolBuilder{OwnerID: lb.OwnerID}).MustNew(ctx)
pool2 := (&testutils.PoolBuilder{OwnerID: lb.OwnerID}).MustNew(ctx)
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{pool.ID, pool2.ID}, LoadBalancerID: lb.ID}).MustNew(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)

// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{pool.ID, pool2.ID, lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs

assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
})

t.Run("with no pool", func(t *testing.T) {
// Act
lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{}, LoadBalancerID: lb.ID}).MustNew(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)

// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs

assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
})
}

func Test_PortUpdateHook(t *testing.T) {
Expand Down

0 comments on commit d9fefb0

Please sign in to comment.