Skip to content

Commit

Permalink
e2e: only add port-forwarders in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Apr 26, 2024
1 parent cb1d2fa commit f5aee08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
20 changes: 20 additions & 0 deletions e2e/internal/kuberesource/parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ func (p *PortForwarderConfig) WithForwardTarget(host string, port int32) *PortFo
return p
}

func AddPortForwarders(resources []any) []any {
var out []any
for _, resource := range resources {
switch obj := resource.(type) {
case *applycorev1.ServiceApplyConfiguration:
port := *obj.Spec.Ports[0].Port
namespace := ""
if obj.Namespace != nil {
namespace = *obj.Namespace
}
fwd := PortForwarder(*obj.Name, namespace).
WithListenPort(port).
WithForwardTarget(*obj.Name, port)
out = append(out, fwd.PodApplyConfiguration)
}
out = append(out, resource)
}
return out
}

// CoordinatorConfig wraps applyappsv1.DeploymentApplyConfiguration for a coordinator.
type CoordinatorConfig struct {
*applyappsv1.DeploymentApplyConfiguration
Expand Down
25 changes: 1 addition & 24 deletions e2e/internal/kuberesource/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Simple() ([]any, error) {
namespace := Namespace(ns)
coordinator := Coordinator(ns).DeploymentApplyConfiguration
coordinatorService := ServiceForDeployment(coordinator)
// TODO(burgerdev): forwarder should be added on demand by resourcegen
coordinatorForwarder := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
Expand Down Expand Up @@ -207,16 +208,6 @@ func OpenSSL() ([]any, error) {

frontendService := ServiceForDeployment(frontend)

portforwarderCoordinator := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
PodApplyConfiguration

portforwarderOpenSSLFrontend := PortForwarder("openssl-frontend", ns).
WithListenPort(443).
WithForwardTarget("openssl-frontend", 443).
PodApplyConfiguration

resources := []any{
namespace,
coordinator,
Expand All @@ -226,8 +217,6 @@ func OpenSSL() ([]any, error) {
client,
frontend,
frontendService,
portforwarderCoordinator,
portforwarderOpenSSLFrontend,
}

return resources, nil
Expand Down Expand Up @@ -562,22 +551,10 @@ func Emojivoto(smMode serviceMeshMode) ([]any, error) {
WithAPIVersion("v1").
WithKind("ServiceAccount")

portforwarderCoordinator := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
PodApplyConfiguration

portforwarderemojivotoWeb := PortForwarder("emojivoto-web", ns).
WithListenPort(8080).
WithForwardTarget("web-svc", 443).
PodApplyConfiguration

resources := []any{
emoji,
emojiService,
emojiserviceAccount,
portforwarderCoordinator,
portforwarderemojivotoWeb,
voteBot,
voting,
votingService,
Expand Down
1 change: 1 addition & 0 deletions e2e/openssl/openssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestOpenSSL(t *testing.T) {

resources, err := kuberesource.OpenSSL()
require.NoError(t, err)
resources = kuberesource.AddPortForwarders(resources)

ct.Init(t, resources)
require.True(t, t.Run("generate", ct.Generate), "contrast generate needs to succeed for subsequent tests")
Expand Down
4 changes: 3 additions & 1 deletion e2e/servicemesh/servicemesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func TestIngressEgress(t *testing.T) {
coordinatorService := kuberesource.ServiceForDeployment(coordinator)
resources = append(resources, coordinator, coordinatorService)

resources = kuberesource.AddPortForwarders(resources)

ct.Init(t, resources)

require.True(t, t.Run("generate", ct.Generate), "contrast generate needs to succeed for subsequent tests")
Expand Down Expand Up @@ -69,7 +71,7 @@ func TestIngressEgress(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

web, cancelPortForward, err := ct.Kubeclient.PortForwardPod(ctx, ct.Namespace, "port-forwarder-emojivoto-web", "8080")
web, cancelPortForward, err := ct.Kubeclient.PortForwardPod(ctx, ct.Namespace, "port-forwarder-web-svc", "443")
require.NoError(err)
t.Cleanup(cancelPortForward)

Expand Down

0 comments on commit f5aee08

Please sign in to comment.