Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e: add hook to debug OIDC fail #3914

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 3 additions & 75 deletions internal/cmd/egctl/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
"syscall"
"time"

troubleshootv1b2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
tbcollect "github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/convert"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

"github.com/envoyproxy/gateway/internal/cmd/options"
"github.com/envoyproxy/gateway/internal/troubleshoot/collect"
tb "github.com/envoyproxy/gateway/internal/troubleshoot"
)

type collectOptions struct {
Expand Down Expand Up @@ -65,16 +63,6 @@
defer cancel()
go waitForSignal(ctx, cancel)

progressChan := make(chan interface{})
go func() {
select {
case <-ctx.Done():
close(progressChan)
case msg := <-progressChan:
fmt.Printf("Collecting support bundle: %v\n", msg)
}
}()

tmpDir, err := os.MkdirTemp("", "envoy-gateway-support-bundle")
if err != nil {
return fmt.Errorf("create temp dir: %w", err)
Expand All @@ -100,68 +88,8 @@
return fmt.Errorf("create bundle dir: %w", err)
}

var result tbcollect.CollectorResult
collectors := []tbcollect.Collector{
// Collect the custom resources from Gateway API and EG
collect.CustomResource{
ClientConfig: restConfig,
BundlePath: bundlePath,
IncludeGroups: []string{
"gateway.envoyproxy.io",
"gateway.networking.k8s.io",
},
},
// Collect resources from EnvoyGateway system namespace
collect.EnvoyGatewayResource{
ClientConfig: restConfig,
BundlePath: bundlePath,
Namespace: collectOpts.envoyGatewayNamespace,
},
// Collect logs from EnvoyGateway system namespace
&tbcollect.CollectLogs{
Collector: &troubleshootv1b2.Logs{
Name: "pod-logs",
Namespace: collectOpts.envoyGatewayNamespace,
},
ClientConfig: restConfig,
BundlePath: bundlePath,
Context: ctx,
},
// Collect prometheus metrics from EnvoyGateway system namespace
collect.PrometheusMetric{
BundlePath: bundlePath,
ClientConfig: restConfig,
Namespace: collectOpts.envoyGatewayNamespace,
},
// Collect config dump from EnvoyGateway system namespace
collect.ConfigDump{
BundlePath: bundlePath,
ClientConfig: restConfig,
Namespace: collectOpts.envoyGatewayNamespace,
},
}
total := len(collectors)
allCollectedData := make(map[string][]byte)
for i, collector := range collectors {
res, err := collector.Collect(progressChan)
if err != nil {
progressChan <- fmt.Errorf("failed to run collector: %s: %w", collector.Title(), err)
progressChan <- tbcollect.CollectProgress{
CurrentName: collector.Title(),
CurrentStatus: "failed",
CompletedCount: i + 1,
TotalCount: total,
}
continue
}
for k, v := range res {
allCollectedData[k] = v
}
}
result = allCollectedData

filename := fmt.Sprintf("%s.tar.gz", basename)
return result.ArchiveSupportBundle(bundlePath, filename)
result := tb.CollectResult(ctx, restConfig, bundlePath, collectOpts.envoyGatewayNamespace)
return result.ArchiveSupportBundle(bundlePath, fmt.Sprintf("%s.tar.gz", basename))

Check warning on line 92 in internal/cmd/egctl/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/collect.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}

func waitForSignal(c context.Context, cancel context.CancelFunc) {
Expand Down
92 changes: 92 additions & 0 deletions internal/troubleshoot/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package troubleshoot

import (
"context"
"fmt"

troubleshootv1b2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
tbcollect "github.com/replicatedhq/troubleshoot/pkg/collect"
"k8s.io/client-go/rest"

"github.com/envoyproxy/gateway/internal/troubleshoot/collect"
)

func CollectResult(ctx context.Context, restConfig *rest.Config, bundlePath string, egNamespace string) tbcollect.CollectorResult {
var result tbcollect.CollectorResult

Check warning on line 20 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L19-L20

Added lines #L19 - L20 were not covered by tests

progressChan := make(chan interface{})
go func() {
select {
case <-ctx.Done():
close(progressChan)
case msg := <-progressChan:
fmt.Printf("Collecting support bundle: %v\n", msg)

Check warning on line 28 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L22-L28

Added lines #L22 - L28 were not covered by tests
}
}()

collectors := []tbcollect.Collector{

Check warning on line 32 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L32

Added line #L32 was not covered by tests
// Collect the custom resources from Gateway API and EG
collect.CustomResource{
ClientConfig: restConfig,
BundlePath: bundlePath,
IncludeGroups: []string{
"gateway.envoyproxy.io",
"gateway.networking.k8s.io",
},
},

Check warning on line 41 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L34-L41

Added lines #L34 - L41 were not covered by tests
// Collect resources from EnvoyGateway system namespace
collect.EnvoyGatewayResource{
ClientConfig: restConfig,
BundlePath: bundlePath,
Namespace: egNamespace,
},

Check warning on line 47 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L43-L47

Added lines #L43 - L47 were not covered by tests
// Collect logs from EnvoyGateway system namespace
&tbcollect.CollectLogs{
Collector: &troubleshootv1b2.Logs{
Name: "pod-logs",
Namespace: egNamespace,
},
ClientConfig: restConfig,
BundlePath: bundlePath,
Context: ctx,
},

Check warning on line 57 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L49-L57

Added lines #L49 - L57 were not covered by tests
// Collect prometheus metrics from EnvoyGateway system namespace
collect.PrometheusMetric{
BundlePath: bundlePath,
ClientConfig: restConfig,
Namespace: egNamespace,
},

Check warning on line 63 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L59-L63

Added lines #L59 - L63 were not covered by tests
// Collect config dump from EnvoyGateway system namespace
collect.ConfigDump{
BundlePath: bundlePath,
ClientConfig: restConfig,
Namespace: egNamespace,
},

Check warning on line 69 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L65-L69

Added lines #L65 - L69 were not covered by tests
}
total := len(collectors)
allCollectedData := make(map[string][]byte)
for i, collector := range collectors {
res, err := collector.Collect(progressChan)
if err != nil {
progressChan <- fmt.Errorf("failed to run collector: %s: %w", collector.Title(), err)
progressChan <- tbcollect.CollectProgress{
CurrentName: collector.Title(),
CurrentStatus: "failed",
CompletedCount: i + 1,
TotalCount: total,

Check warning on line 81 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L71-L81

Added lines #L71 - L81 were not covered by tests
}
continue

Check warning on line 83 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L83

Added line #L83 was not covered by tests
}
for k, v := range res {
allCollectedData[k] = v

Check warning on line 86 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}
}
result = allCollectedData

Check warning on line 89 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L89

Added line #L89 was not covered by tests

return result

Check warning on line 91 in internal/troubleshoot/collect.go

View check run for this annotation

Codecov / codecov/patch

internal/troubleshoot/collect.go#L91

Added line #L91 was not covered by tests
}
2 changes: 1 addition & 1 deletion test/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestBenchmark(t *testing.T) {
cli := kubetest.NewClient(t)
cli, _ := kubetest.NewClient(t)

// Parse benchmark options.
flag.Parse()
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
func TestE2E(t *testing.T) {
flag.Parse()

c := kubetest.NewClient(t)
c, cfg := kubetest.NewClient(t)

if flags.RunTest != nil && *flags.RunTest != "" {
tlog.Logf(t, "Running E2E test %s with %s GatewayClass\n cleanup: %t\n debug: %t",
Expand All @@ -38,6 +38,7 @@ func TestE2E(t *testing.T) {

cSuite, err := suite.NewConformanceTestSuite(suite.ConformanceOptions{
Client: c,
RestConfig: cfg,
GatewayClassName: *flags.GatewayClassName,
Debug: *flags.ShowDebug,
CleanupBaseResources: *flags.CleanupBaseResources,
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/merge_gateways/merge_gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func TestMergeGateways(t *testing.T) {
flag.Parse()

c := kubetest.NewClient(t)
c, cfg := kubetest.NewClient(t)

if flags.RunTest != nil && *flags.RunTest != "" {
tlog.Logf(t, "Running E2E test %s with %s GatewayClass\n cleanup: %t\n debug: %t",
Expand All @@ -40,6 +40,7 @@ func TestMergeGateways(t *testing.T) {

cSuite, err := suite.NewConformanceTestSuite(suite.ConformanceOptions{
Client: c,
RestConfig: cfg,
GatewayClassName: *flags.GatewayClassName,
Debug: *flags.ShowDebug,
CleanupBaseResources: *flags.CleanupBaseResources,
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/multiple_gc/multiple_gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func TestMultipleGC(t *testing.T) {
flag.Parse()

c := kubetest.NewClient(t)
c, cfg := kubetest.NewClient(t)

if flags.RunTest != nil && *flags.RunTest != "" {
tlog.Logf(t, "Running E2E test %s with %s GatewayClass\n cleanup: %t\n debug: %t",
Expand All @@ -43,6 +43,7 @@ func TestMultipleGC(t *testing.T) {
internetGatewaySuiteGatewayClassName := "internet"
internetGatewaySuite, err := suite.NewConformanceTestSuite(suite.ConformanceOptions{
Client: c,
RestConfig: cfg,
GatewayClassName: internetGatewaySuiteGatewayClassName,
Debug: *flags.ShowDebug,
CleanupBaseResources: *flags.CleanupBaseResources,
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/tests/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ var OIDCTest = suite.ConformanceTest{
Manifests: []string{"testdata/oidc-keycloak.yaml", "testdata/oidc-securitypolicy.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
t.Run("http route with oidc authentication", func(t *testing.T) {
// Add a function to dump current cluster status
t.Cleanup(func() {
CollectAndDump(t, suite.RestConfig)
})
ns := "gateway-conformance-infra"
routeNN := types.NamespacedName{Name: "http-with-oidc", Namespace: ns}
gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/conformance/utils/config"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
"sigs.k8s.io/gateway-api/conformance/utils/tlog"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
tb "github.com/envoyproxy/gateway/internal/troubleshoot"
)

const defaultServiceStartupTimeout = 5 * time.Minute
Expand Down Expand Up @@ -569,3 +571,13 @@ func createTagsQueryParam(tags map[string]string) (string, error) {
}
return tagsBuilder.String(), nil
}

// CollectAndDump collects and dumps the cluster data for troubleshooting and log.
// This function should be call within t.Cleanup.
func CollectAndDump(t *testing.T, rest *rest.Config) {
result := tb.CollectResult(context.TODO(), rest, "", "envoy-gateway")
for r, data := range result {
tlog.Logf(t, "filename: %s", r)
tlog.Logf(t, "data: \n%s", data)
}
}
3 changes: 2 additions & 1 deletion test/e2e/upgrade/eg_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func TestEGUpgrade(t *testing.T) {
flag.Parse()

c := kubetest.NewClient(t)
c, cfg := kubetest.NewClient(t)

if flags.RunTest != nil && *flags.RunTest != "" {
tlog.Logf(t, "Running E2E test %s with %s GatewayClass\n cleanup: %t\n debug: %t",
Expand All @@ -39,6 +39,7 @@ func TestEGUpgrade(t *testing.T) {

cSuite, err := suite.NewConformanceTestSuite(suite.ConformanceOptions{
Client: c,
RestConfig: cfg,
GatewayClassName: *flags.GatewayClassName,
Debug: *flags.ShowDebug,
CleanupBaseResources: *flags.CleanupBaseResources,
Expand Down
5 changes: 3 additions & 2 deletions test/utils/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/require"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
Expand All @@ -20,7 +21,7 @@ import (
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
)

func NewClient(t *testing.T) client.Client {
func NewClient(t *testing.T) (client.Client, *rest.Config) {
cfg, err := config.GetConfig()
require.NoError(t, err)

Expand All @@ -30,7 +31,7 @@ func NewClient(t *testing.T) client.Client {
// Install all the scheme to kubernetes client.
CheckInstallScheme(t, c)

return c
return c, cfg
}

func CheckInstallScheme(t *testing.T, c client.Client) {
Expand Down
Loading