Skip to content

Commit

Permalink
fix(connect): set initial_fetch_time to wait indefinitely
Browse files Browse the repository at this point in the history
  • Loading branch information
DanStough committed Jul 5, 2023
1 parent 548829a commit 8a2f60d
Show file tree
Hide file tree
Showing 46 changed files with 134 additions and 21 deletions.
20 changes: 15 additions & 5 deletions agent/proxycfg/api_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package proxycfg
import (
"context"
"fmt"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/leafcert"
"github.com/hashicorp/consul/agent/proxycfg/internal/watch"
Expand Down Expand Up @@ -48,13 +49,13 @@ func (h *handlerAPIGateway) initialize(ctx context.Context) (ConfigSnapshot, err
}

// Watch the api-gateway's config entry
err = h.subscribeToConfigEntry(ctx, structs.APIGateway, h.service, h.proxyID.EnterpriseMeta, gatewayConfigWatchID)
err = h.subscribeToConfigEntry(ctx, structs.APIGateway, h.service, h.proxyID.EnterpriseMeta, apiGatewayConfigWatchID)
if err != nil {
return snap, err
}

// Watch the bound-api-gateway's config entry
err = h.subscribeToConfigEntry(ctx, structs.BoundAPIGateway, h.service, h.proxyID.EnterpriseMeta, gatewayConfigWatchID)
err = h.subscribeToConfigEntry(ctx, structs.BoundAPIGateway, h.service, h.proxyID.EnterpriseMeta, boundGatewayConfigWatchID)
if err != nil {
return snap, err
}
Expand Down Expand Up @@ -108,9 +109,9 @@ func (h *handlerAPIGateway) handleUpdate(ctx context.Context, u UpdateEvent, sna
if err := h.handleRootCAUpdate(u, snap); err != nil {
return err
}
case u.CorrelationID == gatewayConfigWatchID:
case u.CorrelationID == apiGatewayConfigWatchID || u.CorrelationID == boundGatewayConfigWatchID:
// Handle change in the api-gateway or bound-api-gateway config entry
if err := h.handleGatewayConfigUpdate(ctx, u, snap); err != nil {
if err := h.handleGatewayConfigUpdate(ctx, u, snap, u.CorrelationID); err != nil {
return err
}
case u.CorrelationID == inlineCertificateConfigWatchID:
Expand Down Expand Up @@ -146,11 +147,20 @@ func (h *handlerAPIGateway) handleRootCAUpdate(u UpdateEvent, snap *ConfigSnapsh
// In particular, we want to make sure that we're subscribing to any attached resources such
// as routes and certificates. These additional subscriptions will enable us to update the
// config snapshot appropriately for any route or certificate changes.
func (h *handlerAPIGateway) handleGatewayConfigUpdate(ctx context.Context, u UpdateEvent, snap *ConfigSnapshot) error {
func (h *handlerAPIGateway) handleGatewayConfigUpdate(ctx context.Context, u UpdateEvent, snap *ConfigSnapshot, correlationID string) error {
resp, ok := u.Result.(*structs.ConfigEntryResponse)
if !ok {
return fmt.Errorf("invalid type for response: %T", u.Result)
} else if resp.Entry == nil {
// A nil response indicates that we have the watch configured and that we are done with further changes
// until a new response comes in. By setting these earlier we allow a minimal xDS snapshot to configure the
// gateway.
if correlationID == apiGatewayConfigWatchID {
snap.APIGateway.BoundGatewayConfigLoaded = true
}
if correlationID == boundGatewayConfigWatchID {
snap.APIGateway.GatewayConfigLoaded = true
}
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion agent/proxycfg/ingress_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (s *handlerIngressGateway) handleUpdate(ctx context.Context, u UpdateEvent,
if !ok {
return fmt.Errorf("invalid type for response: %T", u.Result)
}

// We set this even if the response is empty so that we know the watch is set,
// but we don't block if the ingress config entry is unset for this gateway
snap.IngressGateway.GatewayConfigLoaded = true

if resp.Entry == nil {
return nil
}
Expand All @@ -103,7 +108,6 @@ func (s *handlerIngressGateway) handleUpdate(ctx context.Context, u UpdateEvent,
return fmt.Errorf("invalid type for config entry: %T", resp.Entry)
}

snap.IngressGateway.GatewayConfigLoaded = true
snap.IngressGateway.TLSConfig = gatewayConf.TLS
if gatewayConf.Defaults != nil {
snap.IngressGateway.Defaults = *gatewayConf.Defaults
Expand Down
31 changes: 26 additions & 5 deletions agent/proxycfg/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,18 @@ DOMAIN_LOOP:
return services, upstreams, compiled, err
}

// valid tests for two valid api gateway snapshot states:
// 1. waiting: the watch on api and bound gateway entries is set, but none were received
// 2. loaded: both the valid config entries AND the leaf certs are set
func (c *configSnapshotAPIGateway) valid() bool {
waiting := c.GatewayConfigLoaded && len(c.Upstreams) == 0 && c.BoundGatewayConfigLoaded && c.Leaf == nil

// If we have a leaf, it implies we successfully watched parent resources
loaded := c.GatewayConfigLoaded && c.BoundGatewayConfigLoaded && c.Leaf != nil

return waiting || loaded
}

type configSnapshotIngressGateway struct {
ConfigSnapshotUpstreams

Expand Down Expand Up @@ -872,6 +884,18 @@ func (c *configSnapshotIngressGateway) isEmpty() bool {
!c.MeshConfigSet
}

// valid tests for two valid ingress snapshot states:
// 1. waiting: the watch on ingress config entries is set, but none were received
// 2. loaded: both the ingress config entry AND the leaf cert are set
func (c *configSnapshotIngressGateway) valid() bool {
waiting := c.GatewayConfigLoaded && len(c.Upstreams) == 0 && c.Leaf == nil

// If we have a leaf, it implies we successfully watched parent resources
loaded := c.GatewayConfigLoaded && c.Leaf != nil

return waiting || loaded
}

type APIGatewayListenerKey = IngressListenerKey

func APIGatewayListenerKeyFromListener(l structs.APIGatewayListener) APIGatewayListenerKey {
Expand Down Expand Up @@ -965,17 +989,14 @@ func (s *ConfigSnapshot) Valid() bool {

case structs.ServiceKindIngressGateway:
return s.Roots != nil &&
s.IngressGateway.Leaf != nil &&
s.IngressGateway.GatewayConfigLoaded &&
s.IngressGateway.valid() &&
s.IngressGateway.HostsSet &&
s.IngressGateway.MeshConfigSet

case structs.ServiceKindAPIGateway:
// TODO Is this the proper set of things to validate?
return s.Roots != nil &&
s.APIGateway.Leaf != nil &&
s.APIGateway.GatewayConfigLoaded &&
s.APIGateway.BoundGatewayConfigLoaded &&
s.APIGateway.valid() &&
s.APIGateway.MeshConfigSet
default:
return false
Expand Down
2 changes: 2 additions & 0 deletions agent/proxycfg/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
serviceResolversWatchID = "service-resolvers"
gatewayServicesWatchID = "gateway-services"
gatewayConfigWatchID = "gateway-config"
apiGatewayConfigWatchID = "api-gateway-config"
boundGatewayConfigWatchID = "bound-gateway-config"
inlineCertificateConfigWatchID = "inline-certificate-config"
routeConfigWatchID = "route-config"
externalServiceIDPrefix = "external-service:"
Expand Down
11 changes: 6 additions & 5 deletions agent/proxycfg/testing_api_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package proxycfg
import (
"fmt"

"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/consul/discoverychain"
"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/consul/agent/configentry"
"github.com/hashicorp/consul/agent/structs"
Expand Down Expand Up @@ -49,13 +50,13 @@ func TestConfigSnapshotAPIGateway(
Result: placeholderLeaf,
},
{
CorrelationID: gatewayConfigWatchID,
CorrelationID: apiGatewayConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: entry,
},
},
{
CorrelationID: gatewayConfigWatchID,
CorrelationID: boundGatewayConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: boundEntry,
},
Expand Down Expand Up @@ -141,13 +142,13 @@ func TestConfigSnapshotAPIGateway_NilConfigEntry(
Result: roots,
},
{
CorrelationID: gatewayConfigWatchID,
CorrelationID: apiGatewayConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: nil, // The first watch on a config entry will return nil if the config entry doesn't exist.
},
},
{
CorrelationID: gatewayConfigWatchID,
CorrelationID: boundGatewayConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: nil, // The first watch on a config entry will return nil if the config entry doesn't exist.
},
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/bootstrap_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ const bootstrapTemplate = `{
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/access-log-path.golden
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/access-logs-enabled.golden
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/acl-enabled-and-token.golden
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/defaults-nodemeta.golden
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/defaults.golden
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/envoy-readiness-probe.golden
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/existing-ca-file.golden
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
2 changes: 2 additions & 0 deletions command/connect/envoy/testdata/existing-ca-path.golden
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@
"dynamic_resources": {
"lds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"cds_config": {
"ads": {},
"initial_fetch_timeout": "0s",
"resource_api_version": "V3"
},
"ads_config": {
Expand Down
Loading

0 comments on commit 8a2f60d

Please sign in to comment.