Skip to content

Commit

Permalink
fix: check error first before accessing the result during translation (
Browse files Browse the repository at this point in the history
…#1927)

* fix: check error first before accessing the result

Signed-off-by: Ardika Bagus <me@ardikabs.com>

* chore: shouldn't halt the entire loop process, move it to else directive

Signed-off-by: Ardika Bagus <me@ardikabs.com>

* Revert "chore: shouldn't halt the entire loop process, move it to else directive"

This reverts commit 17452e7.

Signed-off-by: Ardika Bagus <me@ardikabs.com>

---------

Signed-off-by: Ardika Bagus <me@ardikabs.com>
  • Loading branch information
ardikabs authored Oct 6, 2023
1 parent 3c5e80c commit 5d3c1b4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/xds/translator/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) {
}

result, err := t.Translate(val)
if err != nil {
r.Logger.Error(err, "failed to translate xds ir")
return
}

// Publish EnvoyPatchPolicyStatus
for _, e := range result.EnvoyPatchPolicyStatuses {
Expand All @@ -90,12 +94,8 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) {
// Discard the EnvoyPatchPolicyStatuses to reduce memory footprint
result.EnvoyPatchPolicyStatuses = nil

if err != nil {
r.Logger.Error(err, "failed to translate xds ir")
} else {
// Publish
r.Xds.Store(key, result)
}
// Publish
r.Xds.Store(key, result)
}
},
)
Expand Down
87 changes: 87 additions & 0 deletions internal/xds/translator/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ package runner

import (
"context"
"fmt"
"testing"
"time"

listenerv3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"

"github.com/stretchr/testify/require"

"github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/extension/types"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/message"
)
Expand Down Expand Up @@ -93,3 +98,85 @@ func TestRunner(t *testing.T) {
}, time.Second*5, time.Millisecond*50)

}

func TestRunner_withExtensionManager(t *testing.T) {
// Setup
xdsIR := new(message.XdsIR)
xds := new(message.Xds)
cfg, err := config.New()
require.NoError(t, err)
r := New(&Config{
Server: *cfg,
XdsIR: xdsIR,
Xds: xds,
ExtensionManager: &extManagerMock{},
})

ctx := context.Background()
// Start
err = r.Start(ctx)
require.NoError(t, err)

// xDS is nil at start
require.Equal(t, map[string]*ir.Xds{}, xdsIR.LoadAll())

// test translation
path := "example"
res := ir.Xds{
HTTP: []*ir.HTTPListener{
{
Name: "test",
Address: "0.0.0.0",
Port: 80,
Hostnames: []string{"example.com"},
Routes: []*ir.HTTPRoute{
{
Name: "test-route",
PathMatch: &ir.StringMatch{
Exact: &path,
},
Destination: &ir.RouteDestination{
Name: "test-dest",
Settings: []*ir.DestinationSetting{
{
Endpoints: []*ir.DestinationEndpoint{
{
Host: "10.11.12.13",
Port: 8080,
},
},
},
},
},
},
},
},
},
}
xdsIR.Store("test", &res)
require.Eventually(t, func() bool {
out := xds.LoadAll()
// Ensure that xds has no key, value pairs
return len(out) == 0
}, time.Second*5, time.Millisecond*50)
}

type extManagerMock struct {
types.Manager
}

func (m *extManagerMock) GetPostXDSHookClient(xdsHookType v1alpha1.XDSTranslatorHook) types.XDSHookClient {
if xdsHookType == v1alpha1.XDSHTTPListener {
return &xdsHookClientMock{}
}

return nil
}

type xdsHookClientMock struct {
types.XDSHookClient
}

func (c *xdsHookClientMock) PostHTTPListenerModifyHook(*listenerv3.Listener) (*listenerv3.Listener, error) {
return nil, fmt.Errorf("assuming a network error during the call")
}

0 comments on commit 5d3c1b4

Please sign in to comment.