Skip to content

Commit 890fddb

Browse files
authored
Watch for Secrets (#807)
Problem: NKG doesn't watch for updates of TLS Secrets referenced by Gateway resource. Solution: - Move secrets processing into ChangeProcessor. - Introduce helper secretResolver component to resolve Secrets (includes validation) and capture resolved Secrets. - When building Gateway Listener, resolve Secrets using secretResolver. - When building Graph, add referenced Secrets by Gateway to the Graph, including the ones that don't exists. - When Upserting or Deleting a Secret to ChangeProccessor, use Graph to determine if the Secret is referenced by the Graph and thus changes the store. - When building Configuration, add all TLS Secrets to it referenced by _valid_ TLS Listeners. - Update NGINX file.Manager so that it can deal with multiple files of two types: regular and secret. - Remove SecretStore and SecretDiskMemoryManager components. Solves #553 Solves #441 Testing: - Update affected and add new unit tests - Manual testing - Conformance testing. Relevant tests pass: TestConformance/GatewayInvalidTLSConfiguration
1 parent 9ddf476 commit 890fddb

35 files changed

+1935
-1777
lines changed

internal/events/handler.go

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import (
55
"fmt"
66

77
"github.com/go-logr/logr"
8-
apiv1 "k8s.io/api/core/v1"
9-
discoveryV1 "k8s.io/api/discovery/v1"
10-
"sigs.k8s.io/gateway-api/apis/v1beta1"
118

129
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config"
1310
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file"
1411
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime"
1512
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
1613
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
1714
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver"
18-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets"
1915
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status"
2016
)
2117

@@ -32,10 +28,6 @@ type EventHandler interface {
3228
type EventHandlerConfig struct {
3329
// Processor is the state ChangeProcessor.
3430
Processor state.ChangeProcessor
35-
// SecretStore is the state SecretStore.
36-
SecretStore secrets.SecretStore
37-
// SecretMemoryManager is the state SecretMemoryManager.
38-
SecretMemoryManager secrets.SecretDiskMemoryManager
3931
// ServiceResolver resolves Services to Endpoints.
4032
ServiceResolver resolver.ServiceResolver
4133
// Generator is the nginx config Generator.
@@ -69,9 +61,9 @@ func (h *EventHandlerImpl) HandleEventBatch(ctx context.Context, batch EventBatc
6961
for _, event := range batch {
7062
switch e := event.(type) {
7163
case *UpsertEvent:
72-
h.propagateUpsert(e)
64+
h.cfg.Processor.CaptureUpsertChange(e.Resource)
7365
case *DeleteEvent:
74-
h.propagateDelete(e)
66+
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
7567
default:
7668
panic(fmt.Errorf("unknown event type %T", e))
7769
}
@@ -96,74 +88,15 @@ func (h *EventHandlerImpl) HandleEventBatch(ctx context.Context, batch EventBatc
9688
}
9789

9890
func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error {
99-
// Write all secrets (nuke and pave).
100-
// This will remove all secrets in the secrets directory before writing the requested secrets.
101-
// FIXME(kate-osborn): We may want to rethink this approach in the future and write and remove secrets individually.
102-
// https://github.com/nginxinc/nginx-kubernetes-gateway/issues/561
103-
err := h.cfg.SecretMemoryManager.WriteAllRequestedSecrets()
104-
if err != nil {
105-
return err
106-
}
91+
files := h.cfg.Generator.Generate(conf)
10792

108-
cfg := h.cfg.Generator.Generate(conf)
109-
110-
// For now, we keep all http servers and upstreams in one config file.
111-
// We might rethink that. For example, we can write each server to its file
112-
// or group servers in some way.
113-
err = h.cfg.NginxFileMgr.WriteHTTPConfig("http", cfg)
114-
if err != nil {
115-
return err
93+
if err := h.cfg.NginxFileMgr.ReplaceFiles(files); err != nil {
94+
return fmt.Errorf("failed to replace NGINX configuration files: %w", err)
11695
}
11796

118-
return h.cfg.NginxRuntimeMgr.Reload(ctx)
119-
}
120-
121-
func (h *EventHandlerImpl) propagateUpsert(e *UpsertEvent) {
122-
switch r := e.Resource.(type) {
123-
case *v1beta1.GatewayClass:
124-
h.cfg.Processor.CaptureUpsertChange(r)
125-
case *v1beta1.Gateway:
126-
h.cfg.Processor.CaptureUpsertChange(r)
127-
case *v1beta1.HTTPRoute:
128-
h.cfg.Processor.CaptureUpsertChange(r)
129-
case *v1beta1.ReferenceGrant:
130-
h.cfg.Processor.CaptureUpsertChange(r)
131-
case *apiv1.Service:
132-
h.cfg.Processor.CaptureUpsertChange(r)
133-
case *apiv1.Namespace:
134-
h.cfg.Processor.CaptureUpsertChange(r)
135-
case *apiv1.Secret:
136-
// FIXME(kate-osborn): need to handle certificate rotation
137-
// https://github.com/nginxinc/nginx-kubernetes-gateway/issues/553
138-
h.cfg.SecretStore.Upsert(r)
139-
case *discoveryV1.EndpointSlice:
140-
h.cfg.Processor.CaptureUpsertChange(r)
141-
default:
142-
panic(fmt.Errorf("unknown resource type %T", e.Resource))
97+
if err := h.cfg.NginxRuntimeMgr.Reload(ctx); err != nil {
98+
return fmt.Errorf("failed to reload NGINX: %w", err)
14399
}
144-
}
145100

146-
func (h *EventHandlerImpl) propagateDelete(e *DeleteEvent) {
147-
switch e.Type.(type) {
148-
case *v1beta1.GatewayClass:
149-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
150-
case *v1beta1.Gateway:
151-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
152-
case *v1beta1.HTTPRoute:
153-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
154-
case *v1beta1.ReferenceGrant:
155-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
156-
case *apiv1.Service:
157-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
158-
case *apiv1.Namespace:
159-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
160-
case *apiv1.Secret:
161-
// FIXME(kate-osborn): make sure that affected servers are updated
162-
// https://github.com/nginxinc/nginx-kubernetes-gateway/issues/553
163-
h.cfg.SecretStore.Delete(e.NamespacedName)
164-
case *discoveryV1.EndpointSlice:
165-
h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName)
166-
default:
167-
panic(fmt.Errorf("unknown resource type %T", e.Type))
168-
}
101+
return nil
169102
}

0 commit comments

Comments
 (0)