GCP: Only create debugd loadbalancer when debugCluster is set

This commit is contained in:
Nils Hanke 2022-09-06 10:28:00 +02:00 committed by Nils Hanke
parent d74c7a3769
commit 72d4456b3f
5 changed files with 54 additions and 22 deletions

View File

@ -20,7 +20,7 @@ type gcpclient interface {
CreateVPCs(ctx context.Context) error
CreateFirewall(ctx context.Context, input gcpcl.FirewallInput) error
CreateInstances(ctx context.Context, input gcpcl.CreateInstancesInput) error
CreateLoadBalancers(ctx context.Context) error
CreateLoadBalancers(ctx context.Context, isDebugCluster bool) error
TerminateFirewall(ctx context.Context) error
TerminateVPCs(context.Context) error
TerminateLoadBalancers(context.Context) error

View File

@ -284,7 +284,7 @@ func (c *fakeGcpClient) CreateInstances(ctx context.Context, input gcpcl.CreateI
return nil
}
func (c *fakeGcpClient) CreateLoadBalancers(ctx context.Context) error {
func (c *fakeGcpClient) CreateLoadBalancers(ctx context.Context, isDebugCluster bool) error {
c.loadbalancers = []string{"kube-lb", "boot-lb", "verify-lb"}
return nil
}
@ -361,7 +361,7 @@ func (c *stubGcpClient) CreateInstances(ctx context.Context, input gcpcl.CreateI
return c.createInstancesErr
}
func (c *stubGcpClient) CreateLoadBalancers(ctx context.Context) error {
func (c *stubGcpClient) CreateLoadBalancers(ctx context.Context, isDebugClient bool) error {
return c.createLoadBalancerErr
}

View File

@ -150,7 +150,7 @@ func (c *Creator) createGCP(ctx context.Context, cl gcpclient, config *config.Co
return state.ConstellationState{}, err
}
if err := cl.CreateLoadBalancers(ctx); err != nil {
if err := cl.CreateLoadBalancers(ctx, config.IsDebugCluster()); err != nil {
return state.ConstellationState{}, err
}

View File

@ -35,7 +35,7 @@ type loadBalancer struct {
}
// CreateLoadBalancers creates all necessary load balancers.
func (c *Client) CreateLoadBalancers(ctx context.Context) error {
func (c *Client) CreateLoadBalancers(ctx context.Context, isDebugCluster bool) error {
if err := c.createIPAddr(ctx); err != nil {
return fmt.Errorf("creating load balancer IP address: %w", err)
}
@ -69,6 +69,8 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error {
healthCheck: computepb.HealthCheck_TCP,
})
// Only create when the debug cluster flag is set in the Constellation config
if isDebugCluster {
c.loadbalancers = append(c.loadbalancers, &loadBalancer{
name: c.buildResourceName("debugd"),
ip: c.loadbalancerIPname,
@ -76,6 +78,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error {
backendPortName: "debugd",
healthCheck: computepb.HealthCheck_TCP,
})
}
// Load balancer creation.

View File

@ -9,6 +9,7 @@ package client
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
@ -28,6 +29,7 @@ func TestCreateLoadBalancers(t *testing.T) {
backendAPI backendServicesAPI
forwardAPI forwardingRulesAPI
opRegAPI operationRegionAPI
isDebugCluster bool
wantErr bool
}{
"successful create": {
@ -37,6 +39,14 @@ func TestCreateLoadBalancers(t *testing.T) {
forwardAPI: &stubForwardingRulesAPI{forwardingRule: forwardingRule},
opRegAPI: stubOperationRegionAPI{},
},
"successful create (debug cluster)": {
addrAPI: &stubAddressesAPI{getAddr: proto.String("192.0.2.1")},
healthAPI: &stubHealthChecksAPI{},
backendAPI: &stubBackendServicesAPI{},
forwardAPI: &stubForwardingRulesAPI{forwardingRule: forwardingRule},
opRegAPI: stubOperationRegionAPI{},
isDebugCluster: true,
},
"createIPAddr fails": {
addrAPI: &stubAddressesAPI{insertErr: someErr},
healthAPI: &stubHealthChecksAPI{},
@ -72,14 +82,33 @@ func TestCreateLoadBalancers(t *testing.T) {
operationRegionAPI: tc.opRegAPI,
}
err := client.CreateLoadBalancers(ctx)
err := client.CreateLoadBalancers(ctx, tc.isDebugCluster)
// In case we expect an error, check for the error and continue otherwise.
if tc.wantErr {
assert.Error(err)
} else {
return
}
// If we don't expect an error, check if the resources have been successfully created.
assert.NoError(err)
assert.NotEmpty(client.loadbalancerIPname)
var foundDebugdLB bool
for _, lb := range client.loadbalancers {
// Expect load balancer name to have the format of "name-serviceName-uid" which is what buildResourceName does currently.
if lb.name == fmt.Sprintf("%s-debugd-%s", client.name, client.uid) {
foundDebugdLB = true
break
}
}
if tc.isDebugCluster {
assert.Equal(4, len(client.loadbalancers))
assert.True(foundDebugdLB, "debugd loadbalancer not found in debug-mode")
} else {
assert.Equal(3, len(client.loadbalancers))
assert.False(foundDebugdLB, "debugd loadbalancer found in non-debug mode")
}
})
}