Skip to content

Commit

Permalink
chore: debug message
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <lavacca.mattia@gmail.com>
  • Loading branch information
mlavacca committed Jul 5, 2023
1 parent 842fba5 commit d44c118
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
43 changes: 14 additions & 29 deletions internal/dataplane/kong_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,18 @@ func (c *KongClient) Update(ctx context.Context) error {

func (c *KongClient) FetchLastGoodConfiguration(ctx context.Context) error {
if c.lastValidKongState == nil {
var exists bool
gatewayClients := c.clientsProvider.GatewayClients()
c.logger.Debugf("sending configuration to %d gateway clients", len(gatewayClients))

type kongStateWithHash struct {
kongState *kongstate.KongState
hash string
rawState []byte
}

kongStates, err := iter.MapErr(gatewayClients, func(client **adminapi.Client) (*kongStateWithHash, error) {
state, err := c.configGetter.GetCurrentKongState(ctx, (*client).AdminAPIClient())
var goodKongState *kongStateWithHash
_, err := iter.MapErr(gatewayClients, func(client **adminapi.Client) (*kongStateWithHash, error) {
state, rawState, err := c.configGetter.GetCurrentKongState(ctx, (*client).AdminAPIClient())
if err != nil {
return nil, err
}
Expand All @@ -452,40 +453,24 @@ func (c *KongClient) FetchLastGoodConfiguration(ctx context.Context) error {
}
state.Version = *version
(*client).SetLastConfigSHA([]byte(status.ConfigurationHash))
kongStateWithHash := &kongStateWithHash{
kongState: state,
hash: status.ConfigurationHash,
rawState: rawState,
}
if status.ConfigurationHash != sendconfig.WellKnownInitialHash {
exists = true
// get the first good one as the one to be used
goodKongState = kongStateWithHash
}

return &kongStateWithHash{
kongState: state,
hash: status.ConfigurationHash,
}, nil
return kongStateWithHash, nil
})
if err != nil {
return err
}

// get the first good valid configuration...
var validKongState *kongstate.KongState
for _, ks := range kongStates {
if ks.hash != sendconfig.WellKnownInitialHash {
validKongState = ks.kongState
break
}
}
// then check that all the other configurations are coherent with the first good one.
var goodKongState *kongstate.KongState
for _, ks := range kongStates {
if ks.hash != sendconfig.WellKnownInitialHash {
if reflect.DeepEqual(*ks.kongState, *validKongState) {
goodKongState = ks.kongState
} else {
return errors.New("failed to resolve a unique last good configuration, different configurations coexist")
}
}
}
if exists {
c.lastValidKongState = goodKongState
if goodKongState != nil {
c.lastValidKongState = goodKongState.kongState
}
}
return nil
Expand Down
23 changes: 14 additions & 9 deletions internal/dataplane/kong_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ func (m mockConfigurationGetter) HasConfigurationChanged(
return m.hasConfigurationChanged, nil
}

func (m mockConfigurationGetter) GetCurrentKongState(context.Context, sendconfig.ConfigClient) (*kongstate.KongState, error) {
return m.kongState, nil
func (m mockConfigurationGetter) GetCurrentKongState(context.Context, sendconfig.ConfigClient) (*kongstate.KongState, []byte, error) {
return m.kongState, nil, nil
}

func (m mockConfigurationGetter) GetCurrentStatus(context.Context, sendconfig.StatusClient) (*gokong.Status, error) {
Expand Down Expand Up @@ -529,13 +529,16 @@ func TestKongClientUpdate_ConfigStatusIsAlwaysNotified(t *testing.T) {
}
}

func TestKongClientUpdate_PushLastValidConfig(t *testing.T) {
func TestKongClientUpdate_FetchAndPushLastValidConfig(t *testing.T) {
var (
ctx = context.Background()
testGatewayClient = mustSampleGatewayClient(t)
ctx = context.Background()
gatewayClients = []*adminapi.Client{
mustSampleGatewayClient(t),
mustSampleGatewayClient(t),
}

clientsProvider = mockGatewayClientsProvider{
gatewayClients: []*adminapi.Client{testGatewayClient},
gatewayClients: gatewayClients,
}

updateStrategyResolver = newMockUpdateStrategyResolver(t)
Expand Down Expand Up @@ -617,7 +620,7 @@ func TestKongClientUpdate_PushLastValidConfig(t *testing.T) {
lastValidConfighash: lastConfigHash,
expectedLastKongState: lastKongState,
gatewayFailure: true,
errorsSize: 2,
errorsSize: 3,
},
{
name: "no config fetched, success pushing",
Expand All @@ -642,15 +645,17 @@ func TestKongClientUpdate_PushLastValidConfig(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
configGetter.kongState = tc.lastValidKongState
configGetter.status.ConfigurationHash = tc.lastValidConfighash
updateStrategyResolver.returnErrorOnUpdate(testGatewayClient.BaseRootURL(), tc.gatewayFailure)
for _, client := range gatewayClients {
updateStrategyResolver.returnErrorOnUpdate(client.BaseRootURL(), tc.gatewayFailure)
}
updateStrategyResolver.singleError = tc.singleError
kongClient := setupTestKongClient(t, updateStrategyResolver, clientsProvider, configGetter, configBuilder)

err := kongClient.Update(ctx)
if tc.errorsSize > 0 {
// check if the error is joined with other errors. When there are multiple errors,
// they are separated by \n, hence we count the number of \n.
assert.Equal(t, strings.Count(err.Error(), "\n"), tc.errorsSize-1)
assert.Equal(t, tc.errorsSize, strings.Count(err.Error(), "\n"))
} else {
assert.NoError(t, err)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/dataplane/sendconfig/config_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ConfigurationGetter interface {
GetCurrentKongState(
ctx context.Context,
configClient ConfigClient,
) (*kongstate.KongState, error)
) (*kongstate.KongState, []byte, error)

// GetCurrentStatus retrieves the currently loaded Kong status by using
// the proxy's /status path.
Expand Down Expand Up @@ -122,16 +122,16 @@ func (d *DefaultConfigurationGetter) HasConfigurationChanged(
return false, nil
}

func (d *DefaultConfigurationGetter) GetCurrentKongState(ctx context.Context, configClient ConfigClient) (*kongstate.KongState, error) {
func (d *DefaultConfigurationGetter) GetCurrentKongState(ctx context.Context, configClient ConfigClient) (*kongstate.KongState, []byte, error) {
config, err := configClient.Config(ctx)
if err != nil {
return nil, err
return nil, nil, err
}
state := &kongstate.KongState{}
if err = yaml.Unmarshal(config, state); err != nil {
return nil, err
return nil, nil, err
}
return state, nil
return state, config, err
}

func (d *DefaultConfigurationGetter) GetCurrentStatus(ctx context.Context, statusClient StatusClient) (*kong.Status, error) {
Expand Down

0 comments on commit d44c118

Please sign in to comment.