diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 48ff2d805..6f05279a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -87,7 +87,7 @@ jobs: uses: actions/setup-node@e33196f7422957bea03ed53f6fbb155025ffc7b8 # v3.7.0 with: node-version: 18 - - run: npm --prefix ${{ github.workspace }}/internal/nginx/modules install-ci-test + - run: npm --prefix ${{ github.workspace }}/internal/mode/static/nginx/modules install-ci-test release: name: Release diff --git a/.gitignore b/.gitignore index 596e42848..fdea6e239 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,4 @@ dist/ node_modules/ # JS test coverage -internal/nginx/modules/coverage +internal/mode/static/nginx/modules/coverage diff --git a/Makefile b/Makefile index 0571eb471..fe4172d37 100644 --- a/Makefile +++ b/Makefile @@ -80,7 +80,7 @@ unit-test: ## Run unit tests for the go code njs-unit-test: ## Run unit tests for the njs httpmatches module docker run --rm -w /modules \ - -v $(PWD)/internal/nginx/modules:/modules/ \ + -v $(PWD)/internal/mode/static/nginx/modules:/modules/ \ node:18 \ /bin/bash -c "npm install && npm test && npm run clean" diff --git a/cmd/gateway/commands.go b/cmd/gateway/commands.go index ff6a2c055..fa0f9423c 100644 --- a/cmd/gateway/commands.go +++ b/cmd/gateway/commands.go @@ -9,9 +9,9 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/config" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/provisioner" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/provisioner" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/config" ) const ( @@ -149,7 +149,7 @@ func createStaticModeCommand() *cobra.Command { UpdateGatewayClassStatus: updateGCStatus, } - if err := manager.Start(conf); err != nil { + if err := static.StartManager(conf); err != nil { return fmt.Errorf("failed to start control loop: %w", err) } diff --git a/conformance/Makefile b/conformance/Makefile index c797171d7..e256a9445 100644 --- a/conformance/Makefile +++ b/conformance/Makefile @@ -45,7 +45,7 @@ prepare-nkg-dependencies: ## Install NKG dependencies on configured kind cluster kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v0.7.1/standard-install.yaml kubectl wait --for=condition=available --timeout=60s deployment gateway-api-admission-server -n gateway-system kubectl apply -f ../deploy/manifests/namespace.yaml - kubectl create configmap njs-modules --from-file=../internal/nginx/modules/src/httpmatches.js -n nginx-gateway + kubectl create configmap njs-modules --from-file=../internal/mode/static/nginx/modules/src/httpmatches.js -n nginx-gateway kubectl apply -f ../deploy/manifests/nginx-conf.yaml kubectl apply -f ../deploy/manifests/rbac.yaml kubectl apply -f ../deploy/manifests/gatewayclass.yaml diff --git a/docs/installation.md b/docs/installation.md index ed719329b..5e32f797b 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -32,7 +32,7 @@ This guide walks you through how to install NGINX Kubernetes Gateway on a generi 1. Create the njs-modules ConfigMap: ``` - kubectl create configmap njs-modules --from-file=internal/nginx/modules/src/httpmatches.js -n nginx-gateway + kubectl create configmap njs-modules --from-file=internal/mode/static/nginx/modules/src/httpmatches.js -n nginx-gateway ``` 1. Create the ConfigMap with the main NGINX configuration file: diff --git a/internal/events/handler.go b/internal/events/handler.go deleted file mode 100644 index af07d5c9e..000000000 --- a/internal/events/handler.go +++ /dev/null @@ -1,102 +0,0 @@ -package events - -import ( - "context" - "fmt" - - "github.com/go-logr/logr" - - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" -) - -//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . EventHandler - -// EventHandler handle events. -type EventHandler interface { - // HandleEventBatch handles a batch of events. - // EventBatch can include duplicated events. - HandleEventBatch(ctx context.Context, batch EventBatch) -} - -// EventHandlerConfig holds configuration parameters for EventHandlerImpl. -type EventHandlerConfig struct { - // Processor is the state ChangeProcessor. - Processor state.ChangeProcessor - // ServiceResolver resolves Services to Endpoints. - ServiceResolver resolver.ServiceResolver - // Generator is the nginx config Generator. - Generator config.Generator - // NginxFileMgr is the file Manager for nginx. - NginxFileMgr file.Manager - // NginxRuntimeMgr manages nginx runtime. - NginxRuntimeMgr runtime.Manager - // StatusUpdater updates statuses on Kubernetes resources. - StatusUpdater status.Updater - // Logger is the logger to be used by the EventHandler. - Logger logr.Logger -} - -// EventHandlerImpl implements EventHandler. -// EventHandlerImpl is responsible for: -// (1) Reconciling the Gateway API and Kubernetes built-in resources with the NGINX configuration. -// (2) Keeping the statuses of the Gateway API resources updated. -type EventHandlerImpl struct { - cfg EventHandlerConfig -} - -// NewEventHandlerImpl creates a new EventHandlerImpl. -func NewEventHandlerImpl(cfg EventHandlerConfig) *EventHandlerImpl { - return &EventHandlerImpl{ - cfg: cfg, - } -} - -func (h *EventHandlerImpl) HandleEventBatch(ctx context.Context, batch EventBatch) { - for _, event := range batch { - switch e := event.(type) { - case *UpsertEvent: - h.cfg.Processor.CaptureUpsertChange(e.Resource) - case *DeleteEvent: - h.cfg.Processor.CaptureDeleteChange(e.Type, e.NamespacedName) - default: - panic(fmt.Errorf("unknown event type %T", e)) - } - } - - changed, graph := h.cfg.Processor.Process() - if !changed { - h.cfg.Logger.Info("Handling events didn't result into NGINX configuration changes") - return - } - - var nginxReloadRes status.NginxReloadResult - err := h.updateNginx(ctx, dataplane.BuildConfiguration(ctx, graph, h.cfg.ServiceResolver)) - if err != nil { - h.cfg.Logger.Error(err, "Failed to update NGINX configuration") - nginxReloadRes.Error = err - } else { - h.cfg.Logger.Info("NGINX configuration was successfully updated") - } - - h.cfg.StatusUpdater.Update(ctx, status.BuildStatuses(graph, nginxReloadRes)) -} - -func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error { - files := h.cfg.Generator.Generate(conf) - - if err := h.cfg.NginxFileMgr.ReplaceFiles(files); err != nil { - return fmt.Errorf("failed to replace NGINX configuration files: %w", err) - } - - if err := h.cfg.NginxRuntimeMgr.Reload(ctx); err != nil { - return fmt.Errorf("failed to reload NGINX: %w", err) - } - - return nil -} diff --git a/internal/framework/conditions/conditions.go b/internal/framework/conditions/conditions.go new file mode 100644 index 000000000..8e37e2061 --- /dev/null +++ b/internal/framework/conditions/conditions.go @@ -0,0 +1,48 @@ +package conditions + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/gateway-api/apis/v1beta1" +) + +const ( + // GatewayClassReasonGatewayClassConflict indicates there are multiple GatewayClass resources + // that reference this controller, and we ignored the resource in question and picked the + // GatewayClass that is referenced in the command-line argument. + // This reason is used with GatewayClassConditionAccepted (false). + GatewayClassReasonGatewayClassConflict v1beta1.GatewayClassConditionReason = "GatewayClassConflict" + + // GatewayClassMessageGatewayClassConflict is a message that describes GatewayClassReasonGatewayClassConflict. + GatewayClassMessageGatewayClassConflict = "The resource is ignored due to a conflicting GatewayClass resource" +) + +// Condition defines a condition to be reported in the status of resources. +type Condition struct { + Type string + Status metav1.ConditionStatus + Reason string + Message string +} + +// NewDefaultGatewayClassConditions returns the default Conditions that must be present in the status of a GatewayClass. +func NewDefaultGatewayClassConditions() []Condition { + return []Condition{ + { + Type: string(v1beta1.GatewayClassConditionStatusAccepted), + Status: metav1.ConditionTrue, + Reason: string(v1beta1.GatewayClassReasonAccepted), + Message: "GatewayClass is accepted", + }, + } +} + +// NewGatewayClassConflict returns a Condition that indicates that the GatewayClass is not accepted +// due to a conflict with another GatewayClass. +func NewGatewayClassConflict() Condition { + return Condition{ + Type: string(v1beta1.GatewayClassConditionStatusAccepted), + Status: metav1.ConditionFalse, + Reason: string(GatewayClassReasonGatewayClassConflict), + Message: GatewayClassMessageGatewayClassConflict, + } +} diff --git a/internal/controller/controller_suite_test.go b/internal/framework/controller/controller_suite_test.go similarity index 84% rename from internal/controller/controller_suite_test.go rename to internal/framework/controller/controller_suite_test.go index f5b8541ed..2a0ef5ee0 100644 --- a/internal/controller/controller_suite_test.go +++ b/internal/framework/controller/controller_suite_test.go @@ -9,5 +9,5 @@ import ( func TestControllers(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "Reconciler Suite") + RunSpecs(t, "Controller Suite") } diff --git a/internal/controller/controllerfakes/fake_field_indexer.go b/internal/framework/controller/controllerfakes/fake_field_indexer.go similarity index 100% rename from internal/controller/controllerfakes/fake_field_indexer.go rename to internal/framework/controller/controllerfakes/fake_field_indexer.go diff --git a/internal/controller/controllerfakes/fake_getter.go b/internal/framework/controller/controllerfakes/fake_getter.go similarity index 97% rename from internal/controller/controllerfakes/fake_getter.go rename to internal/framework/controller/controllerfakes/fake_getter.go index 0a7862a66..466622e1e 100644 --- a/internal/controller/controllerfakes/fake_getter.go +++ b/internal/framework/controller/controllerfakes/fake_getter.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) diff --git a/internal/controller/controllerfakes/fake_manager.go b/internal/framework/controller/controllerfakes/fake_manager.go similarity index 100% rename from internal/controller/controllerfakes/fake_manager.go rename to internal/framework/controller/controllerfakes/fake_manager.go diff --git a/internal/controller/doc.go b/internal/framework/controller/doc.go similarity index 100% rename from internal/controller/doc.go rename to internal/framework/controller/doc.go diff --git a/internal/controller/fakes.go b/internal/framework/controller/fakes.go similarity index 100% rename from internal/controller/fakes.go rename to internal/framework/controller/fakes.go diff --git a/internal/manager/filter/filter.go b/internal/framework/controller/filter/filter.go similarity index 89% rename from internal/manager/filter/filter.go rename to internal/framework/controller/filter/filter.go index 12002ca17..86901fd80 100644 --- a/internal/manager/filter/filter.go +++ b/internal/framework/controller/filter/filter.go @@ -5,7 +5,7 @@ import ( "k8s.io/apimachinery/pkg/types" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller" ) // CreateSingleResourceFilter creates a filter function that filters out all resources except the one diff --git a/internal/manager/filter/filter_test.go b/internal/framework/controller/filter/filter_test.go similarity index 100% rename from internal/manager/filter/filter_test.go rename to internal/framework/controller/filter/filter_test.go diff --git a/internal/controller/getter.go b/internal/framework/controller/getter.go similarity index 100% rename from internal/controller/getter.go rename to internal/framework/controller/getter.go diff --git a/internal/manager/index/endpointslice.go b/internal/framework/controller/index/endpointslice.go similarity index 100% rename from internal/manager/index/endpointslice.go rename to internal/framework/controller/index/endpointslice.go diff --git a/internal/manager/index/endpointslice_test.go b/internal/framework/controller/index/endpointslice_test.go similarity index 100% rename from internal/manager/index/endpointslice_test.go rename to internal/framework/controller/index/endpointslice_test.go diff --git a/internal/manager/index/index.go b/internal/framework/controller/index/index.go similarity index 100% rename from internal/manager/index/index.go rename to internal/framework/controller/index/index.go diff --git a/internal/manager/predicate/gatewayclass.go b/internal/framework/controller/predicate/gatewayclass.go similarity index 100% rename from internal/manager/predicate/gatewayclass.go rename to internal/framework/controller/predicate/gatewayclass.go diff --git a/internal/manager/predicate/gatewayclass_test.go b/internal/framework/controller/predicate/gatewayclass_test.go similarity index 100% rename from internal/manager/predicate/gatewayclass_test.go rename to internal/framework/controller/predicate/gatewayclass_test.go diff --git a/internal/manager/predicate/service.go b/internal/framework/controller/predicate/service.go similarity index 100% rename from internal/manager/predicate/service.go rename to internal/framework/controller/predicate/service.go diff --git a/internal/manager/predicate/service_test.go b/internal/framework/controller/predicate/service_test.go similarity index 100% rename from internal/manager/predicate/service_test.go rename to internal/framework/controller/predicate/service_test.go diff --git a/internal/controller/reconciler.go b/internal/framework/controller/reconciler.go similarity index 97% rename from internal/controller/reconciler.go rename to internal/framework/controller/reconciler.go index b2a87997e..5766721c7 100644 --- a/internal/controller/reconciler.go +++ b/internal/framework/controller/reconciler.go @@ -11,7 +11,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" ) // NamespacedNameFilterFunc is a function that returns true if the resource should be processed by the reconciler. diff --git a/internal/controller/reconciler_test.go b/internal/framework/controller/reconciler_test.go similarity index 96% rename from internal/controller/reconciler_test.go rename to internal/framework/controller/reconciler_test.go index 206a99ff5..103abd7a7 100644 --- a/internal/controller/reconciler_test.go +++ b/internal/framework/controller/reconciler_test.go @@ -14,9 +14,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller/controllerfakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/controllerfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" ) type getFunc func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error diff --git a/internal/controller/register.go b/internal/framework/controller/register.go similarity index 97% rename from internal/controller/register.go rename to internal/framework/controller/register.go index ac7f04803..e52e348e4 100644 --- a/internal/controller/register.go +++ b/internal/framework/controller/register.go @@ -10,7 +10,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/predicate" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" ) const ( diff --git a/internal/controller/register_test.go b/internal/framework/controller/register_test.go similarity index 91% rename from internal/controller/register_test.go rename to internal/framework/controller/register_test.go index 0a60fef04..942c8dfe3 100644 --- a/internal/controller/register_test.go +++ b/internal/framework/controller/register_test.go @@ -16,10 +16,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller/controllerfakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/predicate" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/controllerfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/predicate" ) func TestRegister(t *testing.T) { diff --git a/internal/framework/doc.go b/internal/framework/doc.go new file mode 100644 index 000000000..221cf0744 --- /dev/null +++ b/internal/framework/doc.go @@ -0,0 +1,4 @@ +/* +Package framework contains all the packages that are shared by the provisioner and static modes of the project. +*/ +package framework diff --git a/internal/events/event.go b/internal/framework/events/event.go similarity index 100% rename from internal/events/event.go rename to internal/framework/events/event.go diff --git a/internal/events/events_suit_test.go b/internal/framework/events/events_suite_test.go similarity index 100% rename from internal/events/events_suit_test.go rename to internal/framework/events/events_suite_test.go diff --git a/internal/events/events_test.go b/internal/framework/events/events_test.go similarity index 100% rename from internal/events/events_test.go rename to internal/framework/events/events_test.go diff --git a/internal/events/eventsfakes/fake_event_handler.go b/internal/framework/events/eventsfakes/fake_event_handler.go similarity index 97% rename from internal/events/eventsfakes/fake_event_handler.go rename to internal/framework/events/eventsfakes/fake_event_handler.go index 06a20b3a1..bfb6ab896 100644 --- a/internal/events/eventsfakes/fake_event_handler.go +++ b/internal/framework/events/eventsfakes/fake_event_handler.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" ) type FakeEventHandler struct { diff --git a/internal/events/eventsfakes/fake_first_event_batch_preparer.go b/internal/framework/events/eventsfakes/fake_first_event_batch_preparer.go similarity index 97% rename from internal/events/eventsfakes/fake_first_event_batch_preparer.go rename to internal/framework/events/eventsfakes/fake_first_event_batch_preparer.go index e35585dda..874543c64 100644 --- a/internal/events/eventsfakes/fake_first_event_batch_preparer.go +++ b/internal/framework/events/eventsfakes/fake_first_event_batch_preparer.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" ) type FakeFirstEventBatchPreparer struct { diff --git a/internal/events/eventsfakes/fake_reader.go b/internal/framework/events/eventsfakes/fake_reader.go similarity index 98% rename from internal/events/eventsfakes/fake_reader.go rename to internal/framework/events/eventsfakes/fake_reader.go index f0ff663f0..1674faf3c 100644 --- a/internal/events/eventsfakes/fake_reader.go +++ b/internal/framework/events/eventsfakes/fake_reader.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) diff --git a/internal/events/first_eventbatch_preparer.go b/internal/framework/events/first_eventbatch_preparer.go similarity index 100% rename from internal/events/first_eventbatch_preparer.go rename to internal/framework/events/first_eventbatch_preparer.go diff --git a/internal/events/first_eventbatch_preparer_test.go b/internal/framework/events/first_eventbatch_preparer_test.go similarity index 96% rename from internal/events/first_eventbatch_preparer_test.go rename to internal/framework/events/first_eventbatch_preparer_test.go index 02b5aa6d3..69972d810 100644 --- a/internal/events/first_eventbatch_preparer_test.go +++ b/internal/framework/events/first_eventbatch_preparer_test.go @@ -16,8 +16,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events/eventsfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events/eventsfakes" ) var _ = Describe("FirstEventBatchPreparer", func() { diff --git a/internal/framework/events/handler.go b/internal/framework/events/handler.go new file mode 100644 index 000000000..8c2cff413 --- /dev/null +++ b/internal/framework/events/handler.go @@ -0,0 +1,14 @@ +package events + +import ( + "context" +) + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . EventHandler + +// EventHandler handles events. +type EventHandler interface { + // HandleEventBatch handles a batch of events. + // EventBatch can include duplicated events. + HandleEventBatch(ctx context.Context, batch EventBatch) +} diff --git a/internal/events/loop.go b/internal/framework/events/loop.go similarity index 100% rename from internal/events/loop.go rename to internal/framework/events/loop.go diff --git a/internal/events/loop_test.go b/internal/framework/events/loop_test.go similarity index 96% rename from internal/events/loop_test.go rename to internal/framework/events/loop_test.go index b6ae87fb9..5fbdf7277 100644 --- a/internal/events/loop_test.go +++ b/internal/framework/events/loop_test.go @@ -8,8 +8,8 @@ import ( . "github.com/onsi/gomega" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events/eventsfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events/eventsfakes" ) var _ = Describe("EventLoop", func() { diff --git a/internal/helpers/helpers.go b/internal/framework/helpers/helpers.go similarity index 100% rename from internal/helpers/helpers.go rename to internal/framework/helpers/helpers.go diff --git a/internal/status/clock.go b/internal/framework/status/clock.go similarity index 90% rename from internal/status/clock.go rename to internal/framework/status/clock.go index 718ba0fdd..67d22daa8 100644 --- a/internal/status/clock.go +++ b/internal/framework/status/clock.go @@ -11,7 +11,7 @@ type Clock interface { Now() metav1.Time } -// Real clock returns the current local time. +// RealClock returns the current local time. type RealClock struct{} // NewRealClock creates a new RealClock. diff --git a/internal/status/conditions.go b/internal/framework/status/conditions.go similarity index 88% rename from internal/status/conditions.go rename to internal/framework/status/conditions.go index c9ce4c21b..ba958bc63 100644 --- a/internal/status/conditions.go +++ b/internal/framework/status/conditions.go @@ -3,7 +3,7 @@ package status import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" ) func convertConditions( diff --git a/internal/status/conditions_test.go b/internal/framework/status/conditions_test.go similarity index 90% rename from internal/status/conditions_test.go rename to internal/framework/status/conditions_test.go index 8b52896ff..de4fd02c2 100644 --- a/internal/status/conditions_test.go +++ b/internal/framework/status/conditions_test.go @@ -7,8 +7,8 @@ import ( . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func CreateTestConditions(condType string) []conditions.Condition { diff --git a/internal/status/gateway.go b/internal/framework/status/gateway.go similarity index 100% rename from internal/status/gateway.go rename to internal/framework/status/gateway.go diff --git a/internal/status/gateway_test.go b/internal/framework/status/gateway_test.go similarity index 94% rename from internal/status/gateway_test.go rename to internal/framework/status/gateway_test.go index a3e6d5a1f..30f3cbd5d 100644 --- a/internal/status/gateway_test.go +++ b/internal/framework/status/gateway_test.go @@ -8,7 +8,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func TestPrepareGatewayStatus(t *testing.T) { diff --git a/internal/status/gatewayclass.go b/internal/framework/status/gatewayclass.go similarity index 100% rename from internal/status/gatewayclass.go rename to internal/framework/status/gatewayclass.go diff --git a/internal/status/gatewayclass_test.go b/internal/framework/status/gatewayclass_test.go similarity index 89% rename from internal/status/gatewayclass_test.go rename to internal/framework/status/gatewayclass_test.go index c49ce2d74..530d0566d 100644 --- a/internal/status/gatewayclass_test.go +++ b/internal/framework/status/gatewayclass_test.go @@ -8,7 +8,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func TestPrepareGatewayClassStatus(t *testing.T) { diff --git a/internal/status/httproute.go b/internal/framework/status/httproute.go similarity index 100% rename from internal/status/httproute.go rename to internal/framework/status/httproute.go diff --git a/internal/status/httproute_test.go b/internal/framework/status/httproute_test.go similarity index 96% rename from internal/status/httproute_test.go rename to internal/framework/status/httproute_test.go index e3bcf8b5d..92a276d94 100644 --- a/internal/status/httproute_test.go +++ b/internal/framework/status/httproute_test.go @@ -9,7 +9,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func TestPrepareHTTPRouteStatus(t *testing.T) { diff --git a/internal/status/status_suit_test.go b/internal/framework/status/status_suite_test.go similarity index 100% rename from internal/status/status_suit_test.go rename to internal/framework/status/status_suite_test.go diff --git a/internal/framework/status/statuses.go b/internal/framework/status/statuses.go new file mode 100644 index 000000000..9def44228 --- /dev/null +++ b/internal/framework/status/statuses.go @@ -0,0 +1,71 @@ +package status + +import ( + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/gateway-api/apis/v1beta1" + + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" +) + +// ListenerStatuses holds the statuses of listeners where the key is the name of a listener in the Gateway resource. +type ListenerStatuses map[string]ListenerStatus + +// HTTPRouteStatuses holds the statuses of HTTPRoutes where the key is the namespaced name of an HTTPRoute. +type HTTPRouteStatuses map[types.NamespacedName]HTTPRouteStatus + +// GatewayStatuses holds the statuses of Gateways where the key is the namespaced name of a Gateway. +type GatewayStatuses map[types.NamespacedName]GatewayStatus + +// GatewayClassStatuses holds the statuses of GatewayClasses where the key is the namespaced name of a GatewayClass. +type GatewayClassStatuses map[types.NamespacedName]GatewayClassStatus + +// Statuses holds the status-related information about Gateway API resources. +type Statuses struct { + GatewayClassStatuses GatewayClassStatuses + GatewayStatuses GatewayStatuses + HTTPRouteStatuses HTTPRouteStatuses +} + +// GatewayStatus holds the status of the winning Gateway resource. +type GatewayStatus struct { + // ListenerStatuses holds the statuses of listeners defined on the Gateway. + ListenerStatuses ListenerStatuses + // Conditions is the list of conditions for this Gateway. + Conditions []conditions.Condition + // ObservedGeneration is the generation of the resource that was processed. + ObservedGeneration int64 +} + +// ListenerStatus holds the status-related information about a listener in the Gateway resource. +type ListenerStatus struct { + // Conditions is the list of conditions for this listener. + Conditions []conditions.Condition + // SupportedKinds is the list of SupportedKinds for this listener. + SupportedKinds []v1beta1.RouteGroupKind + // AttachedRoutes is the number of routes attached to the listener. + AttachedRoutes int32 +} + +// HTTPRouteStatus holds the status-related information about an HTTPRoute resource. +type HTTPRouteStatus struct { + // ParentStatuses holds the statuses for parentRefs of the HTTPRoute. + ParentStatuses []ParentStatus + // ObservedGeneration is the generation of the resource that was processed. + ObservedGeneration int64 +} + +// ParentStatus holds status-related information related to how the HTTPRoute binds to a specific parentRef. +type ParentStatus struct { + // GatewayNsName is the Namespaced name of the Gateway, which the parentRef references. + GatewayNsName types.NamespacedName + // SectionName is the SectionName of the parentRef. + SectionName *v1beta1.SectionName + // Conditions is the list of conditions that are relevant to the parentRef. + Conditions []conditions.Condition +} + +// GatewayClassStatus holds status-related information about the GatewayClass resource. +type GatewayClassStatus struct { + Conditions []conditions.Condition + ObservedGeneration int64 +} diff --git a/internal/status/statusfakes/fake_clock.go b/internal/framework/status/statusfakes/fake_clock.go similarity index 96% rename from internal/status/statusfakes/fake_clock.go rename to internal/framework/status/statusfakes/fake_clock.go index f02f382c0..a1a33166c 100644 --- a/internal/status/statusfakes/fake_clock.go +++ b/internal/framework/status/statusfakes/fake_clock.go @@ -4,7 +4,7 @@ package statusfakes import ( "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/internal/status/statusfakes/fake_updater.go b/internal/framework/status/statusfakes/fake_updater.go similarity index 96% rename from internal/status/statusfakes/fake_updater.go rename to internal/framework/status/statusfakes/fake_updater.go index 206db177d..53c2002b0 100644 --- a/internal/status/statusfakes/fake_updater.go +++ b/internal/framework/status/statusfakes/fake_updater.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" ) type FakeUpdater struct { diff --git a/internal/status/updater.go b/internal/framework/status/updater.go similarity index 100% rename from internal/status/updater.go rename to internal/framework/status/updater.go diff --git a/internal/status/updater_test.go b/internal/framework/status/updater_test.go similarity index 95% rename from internal/status/updater_test.go rename to internal/framework/status/updater_test.go index 4732ff85a..7791b1ff3 100644 --- a/internal/status/updater_test.go +++ b/internal/framework/status/updater_test.go @@ -14,10 +14,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status/statusfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status/statusfakes" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) var _ = Describe("Updater", func() { @@ -89,7 +89,7 @@ var _ = Describe("Updater", func() { ObservedGeneration: gens.gateways, }, {Namespace: "test", Name: "ignored-gateway"}: { - Conditions: conditions.NewGatewayConflict(), + Conditions: staticConds.NewGatewayConflict(), ObservedGeneration: 1, }, }, @@ -165,16 +165,16 @@ var _ = Describe("Updater", func() { Status: metav1.ConditionFalse, ObservedGeneration: 1, LastTransitionTime: fakeClockTime, - Reason: string(conditions.GatewayReasonGatewayConflict), - Message: conditions.GatewayMessageGatewayConflict, + Reason: string(staticConds.GatewayReasonGatewayConflict), + Message: staticConds.GatewayMessageGatewayConflict, }, { Type: string(v1beta1.GatewayConditionProgrammed), Status: metav1.ConditionFalse, ObservedGeneration: 1, LastTransitionTime: fakeClockTime, - Reason: string(conditions.GatewayReasonGatewayConflict), - Message: conditions.GatewayMessageGatewayConflict, + Reason: string(staticConds.GatewayReasonGatewayConflict), + Message: staticConds.GatewayMessageGatewayConflict, }, }, Addresses: []v1beta1.GatewayAddress{addr}, diff --git a/internal/provisioner/deployment.go b/internal/mode/provisioner/deployment.go similarity index 100% rename from internal/provisioner/deployment.go rename to internal/mode/provisioner/deployment.go diff --git a/internal/mode/provisioner/doc.go b/internal/mode/provisioner/doc.go new file mode 100644 index 000000000..689924d87 --- /dev/null +++ b/internal/mode/provisioner/doc.go @@ -0,0 +1,6 @@ +/* +Package provisioner contains all the packages that relate to the provisioner-mode implementation of NKG. +Provisioner-mode implements data plane provisioning for NGINX Kubernetes Gateway (NKG): it creates an NKG static mode +Deployment for each Gateway that belongs to the provisioner GatewayClass. +*/ +package provisioner diff --git a/internal/provisioner/handler.go b/internal/mode/provisioner/handler.go similarity index 94% rename from internal/provisioner/handler.go rename to internal/mode/provisioner/handler.go index 16f7c8a8d..3e760608e 100644 --- a/internal/provisioner/handler.go +++ b/internal/mode/provisioner/handler.go @@ -9,9 +9,9 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" ) // eventHandler ensures each Gateway for the specific GatewayClass has a corresponding Deployment diff --git a/internal/provisioner/handler_test.go b/internal/mode/provisioner/handler_test.go similarity index 96% rename from internal/provisioner/handler_test.go rename to internal/mode/provisioner/handler_test.go index ad47f3873..bf6549f63 100644 --- a/internal/provisioner/handler_test.go +++ b/internal/mode/provisioner/handler_test.go @@ -17,11 +17,11 @@ import ( . "github.com/onsi/gomega" embeddedfiles "github.com/nginxinc/nginx-kubernetes-gateway" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status/statusfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status/statusfakes" ) var _ = Describe("handler", func() { diff --git a/internal/provisioner/manager.go b/internal/mode/provisioner/manager.go similarity index 91% rename from internal/provisioner/manager.go rename to internal/mode/provisioner/manager.go index 0337c7b49..91c074b41 100644 --- a/internal/provisioner/manager.go +++ b/internal/mode/provisioner/manager.go @@ -14,10 +14,10 @@ import ( gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" embeddedfiles "github.com/nginxinc/nginx-kubernetes-gateway" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/predicate" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/predicate" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" ) // Config is configuration for the provisioner mode. diff --git a/internal/provisioner/provisioner_suite_test.go b/internal/mode/provisioner/provisioner_suite_test.go similarity index 100% rename from internal/provisioner/provisioner_suite_test.go rename to internal/mode/provisioner/provisioner_suite_test.go diff --git a/internal/provisioner/store.go b/internal/mode/provisioner/store.go similarity index 94% rename from internal/provisioner/store.go rename to internal/mode/provisioner/store.go index f13e57b16..c1a861165 100644 --- a/internal/provisioner/store.go +++ b/internal/mode/provisioner/store.go @@ -7,7 +7,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" ) // store stores the cluster state needed by the provisioner and allows to update it from the events. diff --git a/internal/mode/static/build_statuses.go b/internal/mode/static/build_statuses.go new file mode 100644 index 000000000..4b8c7aae7 --- /dev/null +++ b/internal/mode/static/build_statuses.go @@ -0,0 +1,180 @@ +package static + +import ( + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/gateway-api/apis/v1beta1" + + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" +) + +type nginxReloadResult struct { + error error +} + +// buildStatuses builds status.Statuses from a Graph. +func buildStatuses(graph *graph.Graph, nginxReloadRes nginxReloadResult) status.Statuses { + statuses := status.Statuses{ + HTTPRouteStatuses: make(status.HTTPRouteStatuses), + } + + statuses.GatewayClassStatuses = buildGatewayClassStatuses(graph.GatewayClass, graph.IgnoredGatewayClasses) + + statuses.GatewayStatuses = buildGatewayStatuses(graph.Gateway, graph.IgnoredGateways, nginxReloadRes) + + for nsname, r := range graph.Routes { + parentStatuses := make([]status.ParentStatus, 0, len(r.ParentRefs)) + + defaultConds := staticConds.NewDefaultRouteConditions() + + for _, ref := range r.ParentRefs { + failedAttachmentCondCount := 0 + if ref.Attachment != nil && !ref.Attachment.Attached { + failedAttachmentCondCount = 1 + } + allConds := make([]conditions.Condition, 0, len(r.Conditions)+len(defaultConds)+failedAttachmentCondCount) + + // We add defaultConds first, so that any additional conditions will override them, which is + // ensured by DeduplicateConditions. + allConds = append(allConds, defaultConds...) + allConds = append(allConds, r.Conditions...) + if failedAttachmentCondCount == 1 { + allConds = append(allConds, ref.Attachment.FailedCondition) + } + + if nginxReloadRes.error != nil { + allConds = append( + allConds, + staticConds.NewRouteGatewayNotProgrammed(staticConds.RouteMessageFailedNginxReload), + ) + } + + routeRef := r.Source.Spec.ParentRefs[ref.Idx] + + parentStatuses = append(parentStatuses, status.ParentStatus{ + GatewayNsName: ref.Gateway, + SectionName: routeRef.SectionName, + Conditions: staticConds.DeduplicateConditions(allConds), + }) + } + + statuses.HTTPRouteStatuses[nsname] = status.HTTPRouteStatus{ + ObservedGeneration: r.Source.Generation, + ParentStatuses: parentStatuses, + } + } + + return statuses +} + +func buildGatewayClassStatuses( + gc *graph.GatewayClass, + ignoredGwClasses map[types.NamespacedName]*v1beta1.GatewayClass, +) status.GatewayClassStatuses { + statuses := make(status.GatewayClassStatuses) + + if gc != nil { + defaultConds := conditions.NewDefaultGatewayClassConditions() + + conds := make([]conditions.Condition, 0, len(gc.Conditions)+len(defaultConds)) + + // We add default conds first, so that any additional conditions will override them, which is + // ensured by DeduplicateConditions. + conds = append(conds, defaultConds...) + conds = append(conds, gc.Conditions...) + + statuses[client.ObjectKeyFromObject(gc.Source)] = status.GatewayClassStatus{ + Conditions: staticConds.DeduplicateConditions(conds), + ObservedGeneration: gc.Source.Generation, + } + } + + for nsname, gwClass := range ignoredGwClasses { + statuses[nsname] = status.GatewayClassStatus{ + Conditions: []conditions.Condition{conditions.NewGatewayClassConflict()}, + ObservedGeneration: gwClass.Generation, + } + } + + return statuses +} + +func buildGatewayStatuses( + gateway *graph.Gateway, + ignoredGateways map[types.NamespacedName]*v1beta1.Gateway, + nginxReloadRes nginxReloadResult, +) status.GatewayStatuses { + statuses := make(status.GatewayStatuses) + + if gateway != nil { + statuses[client.ObjectKeyFromObject(gateway.Source)] = buildGatewayStatus(gateway, nginxReloadRes) + } + + for nsname, gw := range ignoredGateways { + statuses[nsname] = status.GatewayStatus{ + Conditions: staticConds.NewGatewayConflict(), + ObservedGeneration: gw.Generation, + } + } + + return statuses +} + +func buildGatewayStatus(gateway *graph.Gateway, nginxReloadRes nginxReloadResult) status.GatewayStatus { + if !gateway.Valid { + return status.GatewayStatus{ + Conditions: staticConds.DeduplicateConditions(gateway.Conditions), + ObservedGeneration: gateway.Source.Generation, + } + } + + listenerStatuses := make(map[string]status.ListenerStatus) + + validListenerCount := 0 + for name, l := range gateway.Listeners { + var conds []conditions.Condition + + if l.Valid { + conds = staticConds.NewDefaultListenerConditions() + validListenerCount++ + } else { + conds = l.Conditions + } + + if nginxReloadRes.error != nil { + conds = append( + conds, + staticConds.NewListenerNotProgrammedInvalid(staticConds.ListenerMessageFailedNginxReload), + ) + } + + listenerStatuses[name] = status.ListenerStatus{ + AttachedRoutes: int32(len(l.Routes)), + Conditions: staticConds.DeduplicateConditions(conds), + SupportedKinds: l.SupportedKinds, + } + } + + gwConds := staticConds.NewDefaultGatewayConditions() + if validListenerCount == 0 { + gwConds = append(gwConds, staticConds.NewGatewayNotAcceptedListenersNotValid()...) + } else if validListenerCount < len(gateway.Listeners) { + gwConds = append(gwConds, staticConds.NewGatewayAcceptedListenersNotValid()) + } + + if nginxReloadRes.error != nil { + gwConds = append( + gwConds, + staticConds.NewGatewayNotProgrammedInvalid(staticConds.GatewayMessageFailedNginxReload), + ) + } + + return status.GatewayStatus{ + Conditions: staticConds.DeduplicateConditions(gwConds), + ListenerStatuses: listenerStatuses, + ObservedGeneration: gateway.Source.Generation, + } +} diff --git a/internal/status/statuses_test.go b/internal/mode/static/build_statuses_test.go similarity index 71% rename from internal/status/statuses_test.go rename to internal/mode/static/build_statuses_test.go index b151658bd..f2feb13a0 100644 --- a/internal/status/statuses_test.go +++ b/internal/mode/static/build_statuses_test.go @@ -1,4 +1,4 @@ -package status +package static import ( "errors" @@ -10,9 +10,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" ) var ( @@ -133,43 +135,43 @@ func TestBuildStatuses(t *testing.T) { Routes: routes, } - expected := Statuses{ - GatewayClassStatuses: GatewayClassStatuses{ + expected := status.Statuses{ + GatewayClassStatuses: status.GatewayClassStatuses{ {Name: ""}: { ObservedGeneration: 1, Conditions: conditions.NewDefaultGatewayClassConditions(), }, }, - GatewayStatuses: GatewayStatuses{ + GatewayStatuses: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { - Conditions: conditions.NewDefaultGatewayConditions(), - ListenerStatuses: map[string]ListenerStatus{ + Conditions: staticConds.NewDefaultGatewayConditions(), + ListenerStatuses: map[string]status.ListenerStatus{ "listener-80-1": { AttachedRoutes: 1, - Conditions: conditions.NewDefaultListenerConditions(), + Conditions: staticConds.NewDefaultListenerConditions(), }, }, ObservedGeneration: 2, }, {Namespace: "test", Name: "ignored-gateway"}: { - Conditions: conditions.NewGatewayConflict(), + Conditions: staticConds.NewGatewayConflict(), ObservedGeneration: 1, }, }, - HTTPRouteStatuses: HTTPRouteStatuses{ + HTTPRouteStatuses: status.HTTPRouteStatuses{ {Namespace: "test", Name: "hr-valid"}: { ObservedGeneration: 3, - ParentStatuses: []ParentStatus{ + ParentStatuses: []status.ParentStatus{ { GatewayNsName: client.ObjectKeyFromObject(gw), SectionName: helpers.GetPointer[v1beta1.SectionName]("listener-80-1"), - Conditions: conditions.NewDefaultRouteConditions(), + Conditions: staticConds.NewDefaultRouteConditions(), }, { GatewayNsName: client.ObjectKeyFromObject(gw), SectionName: helpers.GetPointer[v1beta1.SectionName]("listener-80-2"), Conditions: append( - conditions.NewDefaultRouteConditions(), + staticConds.NewDefaultRouteConditions(), invalidAttachmentCondition, ), }, @@ -177,12 +179,12 @@ func TestBuildStatuses(t *testing.T) { }, {Namespace: "test", Name: "hr-invalid"}: { ObservedGeneration: 3, - ParentStatuses: []ParentStatus{ + ParentStatuses: []status.ParentStatus{ { GatewayNsName: client.ObjectKeyFromObject(gw), SectionName: helpers.GetPointer[v1beta1.SectionName]("listener-80-1"), Conditions: append( - conditions.NewDefaultRouteConditions(), + staticConds.NewDefaultRouteConditions(), invalidRouteCondition, ), }, @@ -193,8 +195,8 @@ func TestBuildStatuses(t *testing.T) { g := NewGomegaWithT(t) - var nginxReloadRes NginxReloadResult - result := BuildStatuses(graph, nginxReloadRes) + var nginxReloadRes nginxReloadResult + result := buildStatuses(graph, nginxReloadRes) g.Expect(helpers.Diff(expected, result)).To(BeEmpty()) } @@ -244,38 +246,38 @@ func TestBuildStatusesNginxErr(t *testing.T) { Routes: routes, } - expected := Statuses{ - GatewayClassStatuses: GatewayClassStatuses{}, - GatewayStatuses: GatewayStatuses{ + expected := status.Statuses{ + GatewayClassStatuses: status.GatewayClassStatuses{}, + GatewayStatuses: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { Conditions: []conditions.Condition{ - conditions.NewGatewayAccepted(), - conditions.NewGatewayNotProgrammedInvalid(conditions.GatewayMessageFailedNginxReload), + staticConds.NewGatewayAccepted(), + staticConds.NewGatewayNotProgrammedInvalid(staticConds.GatewayMessageFailedNginxReload), }, - ListenerStatuses: map[string]ListenerStatus{ + ListenerStatuses: map[string]status.ListenerStatus{ "listener-80-1": { AttachedRoutes: 1, Conditions: []conditions.Condition{ - conditions.NewListenerAccepted(), - conditions.NewListenerResolvedRefs(), - conditions.NewListenerNoConflicts(), - conditions.NewListenerNotProgrammedInvalid(conditions.ListenerMessageFailedNginxReload), + staticConds.NewListenerAccepted(), + staticConds.NewListenerResolvedRefs(), + staticConds.NewListenerNoConflicts(), + staticConds.NewListenerNotProgrammedInvalid(staticConds.ListenerMessageFailedNginxReload), }, }, }, ObservedGeneration: 2, }, }, - HTTPRouteStatuses: HTTPRouteStatuses{ + HTTPRouteStatuses: status.HTTPRouteStatuses{ {Namespace: "test", Name: "hr-valid"}: { ObservedGeneration: 3, - ParentStatuses: []ParentStatus{ + ParentStatuses: []status.ParentStatus{ { GatewayNsName: client.ObjectKeyFromObject(gw), SectionName: helpers.GetPointer[v1beta1.SectionName]("listener-80-1"), Conditions: []conditions.Condition{ - conditions.NewRouteResolvedRefs(), - conditions.NewRouteGatewayNotProgrammed(conditions.RouteMessageFailedNginxReload), + staticConds.NewRouteResolvedRefs(), + staticConds.NewRouteGatewayNotProgrammed(staticConds.RouteMessageFailedNginxReload), }, }, }, @@ -285,8 +287,8 @@ func TestBuildStatusesNginxErr(t *testing.T) { g := NewGomegaWithT(t) - nginxReloadRes := NginxReloadResult{Error: errors.New("test error")} - result := BuildStatuses(graph, nginxReloadRes) + nginxReloadRes := nginxReloadResult{error: errors.New("test error")} + result := buildStatuses(graph, nginxReloadRes) g.Expect(helpers.Diff(expected, result)).To(BeEmpty()) } @@ -294,12 +296,12 @@ func TestBuildGatewayClassStatuses(t *testing.T) { tests := []struct { gc *graph.GatewayClass ignoredClasses map[types.NamespacedName]*v1beta1.GatewayClass - expected GatewayClassStatuses + expected status.GatewayClassStatuses name string }{ { name: "nil gatewayclass and no ignored gatewayclasses", - expected: GatewayClassStatuses{}, + expected: status.GatewayClassStatuses{}, }, { name: "nil gatewayclass and ignored gatewayclasses", @@ -315,7 +317,7 @@ func TestBuildGatewayClassStatuses(t *testing.T) { }, }, }, - expected: GatewayClassStatuses{ + expected: status.GatewayClassStatuses{ {Name: "ignored-1"}: { Conditions: []conditions.Condition{conditions.NewGatewayClassConflict()}, ObservedGeneration: 1, @@ -336,7 +338,7 @@ func TestBuildGatewayClassStatuses(t *testing.T) { }, }, }, - expected: GatewayClassStatuses{ + expected: status.GatewayClassStatuses{ {Name: "valid-gc"}: { Conditions: conditions.NewDefaultGatewayClassConditions(), ObservedGeneration: 1, @@ -357,15 +359,15 @@ func TestBuildGatewayClassStatuses(t *testing.T) { func TestBuildGatewayStatuses(t *testing.T) { tests := []struct { - nginxReloadRes NginxReloadResult + nginxReloadRes nginxReloadResult gateway *graph.Gateway ignoredGateways map[types.NamespacedName]*v1beta1.Gateway - expected GatewayStatuses + expected status.GatewayStatuses name string }{ { name: "nil gateway and no ignored gateways", - expected: GatewayStatuses{}, + expected: status.GatewayStatuses{}, }, { name: "nil gateway and ignored gateways", @@ -381,13 +383,13 @@ func TestBuildGatewayStatuses(t *testing.T) { }, }, }, - expected: GatewayStatuses{ + expected: status.GatewayStatuses{ {Namespace: "test", Name: "ignored-1"}: { - Conditions: conditions.NewGatewayConflict(), + Conditions: staticConds.NewGatewayConflict(), ObservedGeneration: 1, }, {Namespace: "test", Name: "ignored-2"}: { - Conditions: conditions.NewGatewayConflict(), + Conditions: staticConds.NewGatewayConflict(), ObservedGeneration: 2, }, }, @@ -412,17 +414,17 @@ func TestBuildGatewayStatuses(t *testing.T) { }, Valid: true, }, - expected: GatewayStatuses{ + expected: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { - Conditions: conditions.NewDefaultGatewayConditions(), - ListenerStatuses: map[string]ListenerStatus{ + Conditions: staticConds.NewDefaultGatewayConditions(), + ListenerStatuses: map[string]status.ListenerStatus{ "listener-valid-1": { AttachedRoutes: 1, - Conditions: conditions.NewDefaultListenerConditions(), + Conditions: staticConds.NewDefaultListenerConditions(), }, "listener-valid-2": { AttachedRoutes: 1, - Conditions: conditions.NewDefaultListenerConditions(), + Conditions: staticConds.NewDefaultListenerConditions(), }, }, ObservedGeneration: 2, @@ -442,24 +444,24 @@ func TestBuildGatewayStatuses(t *testing.T) { }, "listener-invalid": { Valid: false, - Conditions: conditions.NewListenerUnsupportedValue("unsupported value"), + Conditions: staticConds.NewListenerUnsupportedValue("unsupported value"), }, }, Valid: true, }, - expected: GatewayStatuses{ + expected: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { Conditions: []conditions.Condition{ - conditions.NewGatewayProgrammed(), - conditions.NewGatewayAcceptedListenersNotValid(), + staticConds.NewGatewayProgrammed(), + staticConds.NewGatewayAcceptedListenersNotValid(), }, - ListenerStatuses: map[string]ListenerStatus{ + ListenerStatuses: map[string]status.ListenerStatus{ "listener-valid": { AttachedRoutes: 1, - Conditions: conditions.NewDefaultListenerConditions(), + Conditions: staticConds.NewDefaultListenerConditions(), }, "listener-invalid": { - Conditions: conditions.NewListenerUnsupportedValue("unsupported value"), + Conditions: staticConds.NewListenerUnsupportedValue("unsupported value"), }, }, ObservedGeneration: 2, @@ -473,24 +475,24 @@ func TestBuildGatewayStatuses(t *testing.T) { Listeners: map[string]*graph.Listener{ "listener-invalid-1": { Valid: false, - Conditions: conditions.NewListenerUnsupportedProtocol("unsupported protocol"), + Conditions: staticConds.NewListenerUnsupportedProtocol("unsupported protocol"), }, "listener-invalid-2": { Valid: false, - Conditions: conditions.NewListenerUnsupportedValue("unsupported value"), + Conditions: staticConds.NewListenerUnsupportedValue("unsupported value"), }, }, Valid: true, }, - expected: GatewayStatuses{ + expected: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { - Conditions: conditions.NewGatewayNotAcceptedListenersNotValid(), - ListenerStatuses: map[string]ListenerStatus{ + Conditions: staticConds.NewGatewayNotAcceptedListenersNotValid(), + ListenerStatuses: map[string]status.ListenerStatus{ "listener-invalid-1": { - Conditions: conditions.NewListenerUnsupportedProtocol("unsupported protocol"), + Conditions: staticConds.NewListenerUnsupportedProtocol("unsupported protocol"), }, "listener-invalid-2": { - Conditions: conditions.NewListenerUnsupportedValue("unsupported value"), + Conditions: staticConds.NewListenerUnsupportedValue("unsupported value"), }, }, ObservedGeneration: 2, @@ -502,11 +504,11 @@ func TestBuildGatewayStatuses(t *testing.T) { gateway: &graph.Gateway{ Source: gw, Valid: false, - Conditions: conditions.NewGatewayInvalid("no gateway class"), + Conditions: staticConds.NewGatewayInvalid("no gateway class"), }, - expected: GatewayStatuses{ + expected: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { - Conditions: conditions.NewGatewayInvalid("no gateway class"), + Conditions: staticConds.NewGatewayInvalid("no gateway class"), ObservedGeneration: 2, }, }, @@ -516,7 +518,7 @@ func TestBuildGatewayStatuses(t *testing.T) { gateway: &graph.Gateway{ Source: gw, Valid: true, - Conditions: conditions.NewDefaultGatewayConditions(), + Conditions: staticConds.NewDefaultGatewayConditions(), Listeners: map[string]*graph.Listener{ "listener-valid": { Valid: true, @@ -526,27 +528,29 @@ func TestBuildGatewayStatuses(t *testing.T) { }, }, }, - expected: GatewayStatuses{ + expected: status.GatewayStatuses{ {Namespace: "test", Name: "gateway"}: { Conditions: []conditions.Condition{ - conditions.NewGatewayAccepted(), - conditions.NewGatewayNotProgrammedInvalid(conditions.GatewayMessageFailedNginxReload), + staticConds.NewGatewayAccepted(), + staticConds.NewGatewayNotProgrammedInvalid(staticConds.GatewayMessageFailedNginxReload), }, - ListenerStatuses: map[string]ListenerStatus{ + ListenerStatuses: map[string]status.ListenerStatus{ "listener-valid": { AttachedRoutes: 1, Conditions: []conditions.Condition{ - conditions.NewListenerAccepted(), - conditions.NewListenerResolvedRefs(), - conditions.NewListenerNoConflicts(), - conditions.NewListenerNotProgrammedInvalid(conditions.ListenerMessageFailedNginxReload), + staticConds.NewListenerAccepted(), + staticConds.NewListenerResolvedRefs(), + staticConds.NewListenerNoConflicts(), + staticConds.NewListenerNotProgrammedInvalid( + staticConds.ListenerMessageFailedNginxReload, + ), }, }, }, ObservedGeneration: 2, }, }, - nginxReloadRes: NginxReloadResult{Error: errors.New("test error")}, + nginxReloadRes: nginxReloadResult{error: errors.New("test error")}, }, } diff --git a/internal/config/config.go b/internal/mode/static/config/config.go similarity index 100% rename from internal/config/config.go rename to internal/mode/static/config/config.go diff --git a/internal/mode/static/doc.go b/internal/mode/static/doc.go new file mode 100644 index 000000000..5dcf35b75 --- /dev/null +++ b/internal/mode/static/doc.go @@ -0,0 +1,5 @@ +/* +Package static contains all the packages that relate to the static-mode implementation of NKG. +Static-mode configures NGINX in the scope of a single Gateway resource. +*/ +package static diff --git a/internal/mode/static/handler.go b/internal/mode/static/handler.go new file mode 100644 index 000000000..2fea22e0b --- /dev/null +++ b/internal/mode/static/handler.go @@ -0,0 +1,94 @@ +package static + +import ( + "context" + "fmt" + + "github.com/go-logr/logr" + + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/runtime" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" +) + +// eventHandlerConfig holds configuration parameters for eventHandlerImpl. +type eventHandlerConfig struct { + // processor is the state ChangeProcessor. + processor state.ChangeProcessor + // serviceResolver resolves Services to Endpoints. + serviceResolver resolver.ServiceResolver + // generator is the nginx config generator. + generator config.Generator + // nginxFileMgr is the file Manager for nginx. + nginxFileMgr file.Manager + // nginxRuntimeMgr manages nginx runtime. + nginxRuntimeMgr runtime.Manager + // statusUpdater updates statuses on Kubernetes resources. + statusUpdater status.Updater + // logger is the logger to be used by the EventHandler. + logger logr.Logger +} + +// eventHandlerImpl implements EventHandler. +// eventHandlerImpl is responsible for: +// (1) Reconciling the Gateway API and Kubernetes built-in resources with the NGINX configuration. +// (2) Keeping the statuses of the Gateway API resources updated. +type eventHandlerImpl struct { + cfg eventHandlerConfig +} + +// newEventHandlerImpl creates a new eventHandlerImpl. +func newEventHandlerImpl(cfg eventHandlerConfig) *eventHandlerImpl { + return &eventHandlerImpl{ + cfg: cfg, + } +} + +func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, batch events.EventBatch) { + for _, event := range batch { + switch e := event.(type) { + case *events.UpsertEvent: + h.cfg.processor.CaptureUpsertChange(e.Resource) + case *events.DeleteEvent: + h.cfg.processor.CaptureDeleteChange(e.Type, e.NamespacedName) + default: + panic(fmt.Errorf("unknown event type %T", e)) + } + } + + changed, graph := h.cfg.processor.Process() + if !changed { + h.cfg.logger.Info("Handling events didn't result into NGINX configuration changes") + return + } + + var nginxReloadRes nginxReloadResult + err := h.updateNginx(ctx, dataplane.BuildConfiguration(ctx, graph, h.cfg.serviceResolver)) + if err != nil { + h.cfg.logger.Error(err, "Failed to update NGINX configuration") + nginxReloadRes.error = err + } else { + h.cfg.logger.Info("NGINX configuration was successfully updated") + } + + h.cfg.statusUpdater.Update(ctx, buildStatuses(graph, nginxReloadRes)) +} + +func (h *eventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error { + files := h.cfg.generator.Generate(conf) + + if err := h.cfg.nginxFileMgr.ReplaceFiles(files); err != nil { + return fmt.Errorf("failed to replace NGINX configuration files: %w", err) + } + + if err := h.cfg.nginxRuntimeMgr.Reload(ctx); err != nil { + return fmt.Errorf("failed to reload NGINX: %w", err) + } + + return nil +} diff --git a/internal/events/handler_test.go b/internal/mode/static/handler_test.go similarity index 76% rename from internal/events/handler_test.go rename to internal/mode/static/handler_test.go index 7c0e309fe..3d937262e 100644 --- a/internal/events/handler_test.go +++ b/internal/mode/static/handler_test.go @@ -1,4 +1,4 @@ -package events_test +package static import ( "context" @@ -9,20 +9,20 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/configfakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file/filefakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime/runtimefakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/statefakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status/statusfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status/statusfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/configfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file/filefakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/runtime/runtimefakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/statefakes" ) -var _ = Describe("EventHandler", func() { +var _ = Describe("eventHandler", func() { var ( - handler *events.EventHandlerImpl + handler *eventHandlerImpl fakeProcessor *statefakes.FakeChangeProcessor fakeGenerator *configfakes.FakeGenerator fakeNginxFileMgr *filefakes.FakeManager @@ -52,13 +52,13 @@ var _ = Describe("EventHandler", func() { fakeNginxRuntimeMgr = &runtimefakes.FakeManager{} fakeStatusUpdater = &statusfakes.FakeUpdater{} - handler = events.NewEventHandlerImpl(events.EventHandlerConfig{ - Processor: fakeProcessor, - Generator: fakeGenerator, - Logger: zap.New(), - NginxFileMgr: fakeNginxFileMgr, - NginxRuntimeMgr: fakeNginxRuntimeMgr, - StatusUpdater: fakeStatusUpdater, + handler = newEventHandlerImpl(eventHandlerConfig{ + processor: fakeProcessor, + generator: fakeGenerator, + logger: zap.New(), + nginxFileMgr: fakeNginxFileMgr, + nginxRuntimeMgr: fakeNginxRuntimeMgr, + statusUpdater: fakeStatusUpdater, }) }) diff --git a/internal/manager/manager.go b/internal/mode/static/manager.go similarity index 79% rename from internal/manager/manager.go rename to internal/mode/static/manager.go index 3454ab26d..50cd5b078 100644 --- a/internal/manager/manager.go +++ b/internal/mode/static/manager.go @@ -1,4 +1,4 @@ -package manager +package static import ( "fmt" @@ -16,21 +16,21 @@ import ( k8spredicate "sigs.k8s.io/controller-runtime/pkg/predicate" gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/config" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/controller" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/events" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/filter" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/predicate" - ngxcfg "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config" - ngxvalidation "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/validation" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - ngxruntime "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/filter" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/predicate" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/events" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/status" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/config" + ngxcfg "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config" + ngxvalidation "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + ngxruntime "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/runtime" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" ) const ( @@ -46,7 +46,7 @@ func init() { utilruntime.Must(discoveryV1.AddToScheme(scheme)) } -func Start(cfg config.Config) error { +func StartManager(cfg config.Config) error { logger := cfg.Logger options := manager.Options{ @@ -168,14 +168,14 @@ func Start(cfg config.Config) error { UpdateGatewayClassStatus: cfg.UpdateGatewayClassStatus, }) - eventHandler := events.NewEventHandlerImpl(events.EventHandlerConfig{ - Processor: processor, - ServiceResolver: resolver.NewServiceResolverImpl(mgr.GetClient()), - Generator: configGenerator, - Logger: cfg.Logger.WithName("eventHandler"), - NginxFileMgr: nginxFileMgr, - NginxRuntimeMgr: nginxRuntimeMgr, - StatusUpdater: statusUpdater, + eventHandler := newEventHandlerImpl(eventHandlerConfig{ + processor: processor, + serviceResolver: resolver.NewServiceResolverImpl(mgr.GetClient()), + generator: configGenerator, + logger: cfg.Logger.WithName("eventHandler"), + nginxFileMgr: nginxFileMgr, + nginxRuntimeMgr: nginxRuntimeMgr, + statusUpdater: statusUpdater, }) objects, objectLists := prepareFirstEventBatchPreparerArgs(cfg.GatewayClassName, cfg.GatewayNsName) diff --git a/internal/manager/manager_test.go b/internal/mode/static/manager_test.go similarity index 99% rename from internal/manager/manager_test.go rename to internal/mode/static/manager_test.go index 71554bc5f..4fbe4c9f5 100644 --- a/internal/manager/manager_test.go +++ b/internal/mode/static/manager_test.go @@ -1,4 +1,4 @@ -package manager +package static import ( "testing" diff --git a/internal/nginx/config/configfakes/fake_generator.go b/internal/mode/static/nginx/config/configfakes/fake_generator.go similarity index 92% rename from internal/nginx/config/configfakes/fake_generator.go rename to internal/mode/static/nginx/config/configfakes/fake_generator.go index 7628a7978..4b058a355 100644 --- a/internal/nginx/config/configfakes/fake_generator.go +++ b/internal/mode/static/nginx/config/configfakes/fake_generator.go @@ -4,9 +4,9 @@ package configfakes import ( "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) type FakeGenerator struct { diff --git a/internal/nginx/config/execute.go b/internal/mode/static/nginx/config/execute.go similarity index 100% rename from internal/nginx/config/execute.go rename to internal/mode/static/nginx/config/execute.go diff --git a/internal/nginx/config/execute_test.go b/internal/mode/static/nginx/config/execute_test.go similarity index 85% rename from internal/nginx/config/execute_test.go rename to internal/mode/static/nginx/config/execute_test.go index 618fa438e..9b3a4b5f2 100644 --- a/internal/nginx/config/execute_test.go +++ b/internal/mode/static/nginx/config/execute_test.go @@ -3,7 +3,7 @@ package config import ( "testing" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" ) func TestExecute(t *testing.T) { diff --git a/internal/nginx/config/generator.go b/internal/mode/static/nginx/config/generator.go similarity index 95% rename from internal/nginx/config/generator.go rename to internal/mode/static/nginx/config/generator.go index 732c09b72..1956a0366 100644 --- a/internal/nginx/config/generator.go +++ b/internal/mode/static/nginx/config/generator.go @@ -3,8 +3,8 @@ package config import ( "path/filepath" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Generator diff --git a/internal/nginx/config/generator_test.go b/internal/mode/static/nginx/config/generator_test.go similarity index 89% rename from internal/nginx/config/generator_test.go rename to internal/mode/static/nginx/config/generator_test.go index cc30a4e77..3d64174da 100644 --- a/internal/nginx/config/generator_test.go +++ b/internal/mode/static/nginx/config/generator_test.go @@ -6,9 +6,9 @@ import ( . "github.com/onsi/gomega" "k8s.io/apimachinery/pkg/types" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) func TestGenerate(t *testing.T) { diff --git a/internal/nginx/config/http/config.go b/internal/mode/static/nginx/config/http/config.go similarity index 100% rename from internal/nginx/config/http/config.go rename to internal/mode/static/nginx/config/http/config.go diff --git a/internal/nginx/config/maps.go b/internal/mode/static/nginx/config/maps.go similarity index 91% rename from internal/nginx/config/maps.go rename to internal/mode/static/nginx/config/maps.go index 4d1aa3a1b..b5cc695f4 100644 --- a/internal/nginx/config/maps.go +++ b/internal/mode/static/nginx/config/maps.go @@ -4,8 +4,8 @@ import ( "strings" gotemplate "text/template" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) var mapsTemplate = gotemplate.Must(gotemplate.New("maps").Parse(mapsTemplateText)) diff --git a/internal/nginx/config/maps_template.go b/internal/mode/static/nginx/config/maps_template.go similarity index 100% rename from internal/nginx/config/maps_template.go rename to internal/mode/static/nginx/config/maps_template.go diff --git a/internal/nginx/config/maps_test.go b/internal/mode/static/nginx/config/maps_test.go similarity index 95% rename from internal/nginx/config/maps_test.go rename to internal/mode/static/nginx/config/maps_test.go index 233ff90c6..3e7529655 100644 --- a/internal/nginx/config/maps_test.go +++ b/internal/mode/static/nginx/config/maps_test.go @@ -6,8 +6,8 @@ import ( . "github.com/onsi/gomega" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) func TestExecuteMaps(t *testing.T) { diff --git a/internal/nginx/config/servers.go b/internal/mode/static/nginx/config/servers.go similarity index 98% rename from internal/nginx/config/servers.go rename to internal/mode/static/nginx/config/servers.go index f2efec53b..aa604ab1e 100644 --- a/internal/nginx/config/servers.go +++ b/internal/mode/static/nginx/config/servers.go @@ -8,8 +8,8 @@ import ( "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) var serversTemplate = gotemplate.Must(gotemplate.New("servers").Parse(serversTemplateText)) diff --git a/internal/nginx/config/servers_template.go b/internal/mode/static/nginx/config/servers_template.go similarity index 100% rename from internal/nginx/config/servers_template.go rename to internal/mode/static/nginx/config/servers_template.go diff --git a/internal/nginx/config/servers_test.go b/internal/mode/static/nginx/config/servers_test.go similarity index 99% rename from internal/nginx/config/servers_test.go rename to internal/mode/static/nginx/config/servers_test.go index e0a5d9a81..eb45a9a61 100644 --- a/internal/nginx/config/servers_test.go +++ b/internal/mode/static/nginx/config/servers_test.go @@ -12,9 +12,9 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) func TestExecuteServers(t *testing.T) { diff --git a/internal/nginx/config/split_clients.go b/internal/mode/static/nginx/config/split_clients.go similarity index 95% rename from internal/nginx/config/split_clients.go rename to internal/mode/static/nginx/config/split_clients.go index a77e176f1..7728c5230 100644 --- a/internal/nginx/config/split_clients.go +++ b/internal/mode/static/nginx/config/split_clients.go @@ -5,8 +5,8 @@ import ( "math" gotemplate "text/template" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) var splitClientsTemplate = gotemplate.Must(gotemplate.New("split_clients").Parse(splitClientsTemplateText)) diff --git a/internal/nginx/config/split_clients_template.go b/internal/mode/static/nginx/config/split_clients_template.go similarity index 100% rename from internal/nginx/config/split_clients_template.go rename to internal/mode/static/nginx/config/split_clients_template.go diff --git a/internal/nginx/config/split_clients_test.go b/internal/mode/static/nginx/config/split_clients_test.go similarity index 98% rename from internal/nginx/config/split_clients_test.go rename to internal/mode/static/nginx/config/split_clients_test.go index ac190448e..0396c5932 100644 --- a/internal/nginx/config/split_clients_test.go +++ b/internal/mode/static/nginx/config/split_clients_test.go @@ -7,8 +7,8 @@ import ( "github.com/google/go-cmp/cmp" "k8s.io/apimachinery/pkg/types" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) func TestExecuteSplitClients(t *testing.T) { diff --git a/internal/nginx/config/upstreams.go b/internal/mode/static/nginx/config/upstreams.go similarity index 91% rename from internal/nginx/config/upstreams.go rename to internal/mode/static/nginx/config/upstreams.go index 927745ed5..3410f01c1 100644 --- a/internal/nginx/config/upstreams.go +++ b/internal/mode/static/nginx/config/upstreams.go @@ -4,8 +4,8 @@ import ( "fmt" gotemplate "text/template" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" ) var upstreamsTemplate = gotemplate.Must(gotemplate.New("upstreams").Parse(upstreamsTemplateText)) diff --git a/internal/nginx/config/upstreams_template.go b/internal/mode/static/nginx/config/upstreams_template.go similarity index 100% rename from internal/nginx/config/upstreams_template.go rename to internal/mode/static/nginx/config/upstreams_template.go diff --git a/internal/nginx/config/upstreams_test.go b/internal/mode/static/nginx/config/upstreams_test.go similarity index 93% rename from internal/nginx/config/upstreams_test.go rename to internal/mode/static/nginx/config/upstreams_test.go index 95cb9bfe6..79cc381f8 100644 --- a/internal/nginx/config/upstreams_test.go +++ b/internal/mode/static/nginx/config/upstreams_test.go @@ -6,9 +6,9 @@ import ( "github.com/google/go-cmp/cmp" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" ) func TestExecuteUpstreams(t *testing.T) { diff --git a/internal/nginx/config/validation/common.go b/internal/mode/static/nginx/config/validation/common.go similarity index 100% rename from internal/nginx/config/validation/common.go rename to internal/mode/static/nginx/config/validation/common.go diff --git a/internal/nginx/config/validation/common_test.go b/internal/mode/static/nginx/config/validation/common_test.go similarity index 100% rename from internal/nginx/config/validation/common_test.go rename to internal/mode/static/nginx/config/validation/common_test.go diff --git a/internal/nginx/config/validation/doc.go b/internal/mode/static/nginx/config/validation/doc.go similarity index 100% rename from internal/nginx/config/validation/doc.go rename to internal/mode/static/nginx/config/validation/doc.go diff --git a/internal/nginx/config/validation/framework.go b/internal/mode/static/nginx/config/validation/framework.go similarity index 100% rename from internal/nginx/config/validation/framework.go rename to internal/mode/static/nginx/config/validation/framework.go diff --git a/internal/nginx/config/validation/framework_test.go b/internal/mode/static/nginx/config/validation/framework_test.go similarity index 100% rename from internal/nginx/config/validation/framework_test.go rename to internal/mode/static/nginx/config/validation/framework_test.go diff --git a/internal/nginx/config/validation/http_filters.go b/internal/mode/static/nginx/config/validation/http_filters.go similarity index 100% rename from internal/nginx/config/validation/http_filters.go rename to internal/mode/static/nginx/config/validation/http_filters.go diff --git a/internal/nginx/config/validation/http_filters_test.go b/internal/mode/static/nginx/config/validation/http_filters_test.go similarity index 100% rename from internal/nginx/config/validation/http_filters_test.go rename to internal/mode/static/nginx/config/validation/http_filters_test.go diff --git a/internal/nginx/config/validation/http_njs_match.go b/internal/mode/static/nginx/config/validation/http_njs_match.go similarity index 97% rename from internal/nginx/config/validation/http_njs_match.go rename to internal/mode/static/nginx/config/validation/http_njs_match.go index eb4ec9308..6675f049f 100644 --- a/internal/nginx/config/validation/http_njs_match.go +++ b/internal/mode/static/nginx/config/validation/http_njs_match.go @@ -8,7 +8,7 @@ import ( k8svalidation "k8s.io/apimachinery/pkg/util/validation" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config" ) // HTTPNJSMatchValidator validates values used for matching a request. diff --git a/internal/nginx/config/validation/http_njs_match_test.go b/internal/mode/static/nginx/config/validation/http_njs_match_test.go similarity index 100% rename from internal/nginx/config/validation/http_njs_match_test.go rename to internal/mode/static/nginx/config/validation/http_njs_match_test.go diff --git a/internal/nginx/config/validation/http_validator.go b/internal/mode/static/nginx/config/validation/http_validator.go similarity index 85% rename from internal/nginx/config/validation/http_validator.go rename to internal/mode/static/nginx/config/validation/http_validator.go index a206bb114..55af7e336 100644 --- a/internal/nginx/config/validation/http_validator.go +++ b/internal/mode/static/nginx/config/validation/http_validator.go @@ -1,7 +1,7 @@ package validation import ( - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" ) // HTTPValidator validates values that will propagate into the NGINX configuration http context. diff --git a/internal/nginx/config/variable_names.go b/internal/mode/static/nginx/config/variable_names.go similarity index 100% rename from internal/nginx/config/variable_names.go rename to internal/mode/static/nginx/config/variable_names.go diff --git a/internal/nginx/config/variable_names_test.go b/internal/mode/static/nginx/config/variable_names_test.go similarity index 100% rename from internal/nginx/config/variable_names_test.go rename to internal/mode/static/nginx/config/variable_names_test.go diff --git a/internal/nginx/file/file_suite_test.go b/internal/mode/static/nginx/file/file_suite_test.go similarity index 100% rename from internal/nginx/file/file_suite_test.go rename to internal/mode/static/nginx/file/file_suite_test.go diff --git a/internal/nginx/file/filefakes/fake_clear_folders_osfile_manager.go b/internal/mode/static/nginx/file/filefakes/fake_clear_folders_osfile_manager.go similarity index 98% rename from internal/nginx/file/filefakes/fake_clear_folders_osfile_manager.go rename to internal/mode/static/nginx/file/filefakes/fake_clear_folders_osfile_manager.go index 349f88997..dc4595e44 100644 --- a/internal/nginx/file/filefakes/fake_clear_folders_osfile_manager.go +++ b/internal/mode/static/nginx/file/filefakes/fake_clear_folders_osfile_manager.go @@ -5,7 +5,7 @@ import ( "io/fs" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" ) type FakeClearFoldersOSFileManager struct { diff --git a/internal/nginx/file/filefakes/fake_dir_entry.go b/internal/mode/static/nginx/file/filefakes/fake_dir_entry.go similarity index 100% rename from internal/nginx/file/filefakes/fake_dir_entry.go rename to internal/mode/static/nginx/file/filefakes/fake_dir_entry.go diff --git a/internal/nginx/file/filefakes/fake_manager.go b/internal/mode/static/nginx/file/filefakes/fake_manager.go similarity index 97% rename from internal/nginx/file/filefakes/fake_manager.go rename to internal/mode/static/nginx/file/filefakes/fake_manager.go index 644e5a3c0..1e03abfef 100644 --- a/internal/nginx/file/filefakes/fake_manager.go +++ b/internal/mode/static/nginx/file/filefakes/fake_manager.go @@ -4,7 +4,7 @@ package filefakes import ( "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" ) type FakeManager struct { diff --git a/internal/nginx/file/filefakes/fake_osfile_manager.go b/internal/mode/static/nginx/file/filefakes/fake_osfile_manager.go similarity index 99% rename from internal/nginx/file/filefakes/fake_osfile_manager.go rename to internal/mode/static/nginx/file/filefakes/fake_osfile_manager.go index bf03b2080..edcac47d2 100644 --- a/internal/nginx/file/filefakes/fake_osfile_manager.go +++ b/internal/mode/static/nginx/file/filefakes/fake_osfile_manager.go @@ -6,7 +6,7 @@ import ( "os" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" ) type FakeOSFileManager struct { diff --git a/internal/nginx/file/folders.go b/internal/mode/static/nginx/file/folders.go similarity index 100% rename from internal/nginx/file/folders.go rename to internal/mode/static/nginx/file/folders.go diff --git a/internal/nginx/file/folders_test.go b/internal/mode/static/nginx/file/folders_test.go similarity index 91% rename from internal/nginx/file/folders_test.go rename to internal/mode/static/nginx/file/folders_test.go index d9ba1a03a..a04beba58 100644 --- a/internal/nginx/file/folders_test.go +++ b/internal/mode/static/nginx/file/folders_test.go @@ -8,8 +8,8 @@ import ( . "github.com/onsi/gomega" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file/filefakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file/filefakes" ) func writeFile(t *testing.T, name string, data []byte) { diff --git a/internal/nginx/file/manager.go b/internal/mode/static/nginx/file/manager.go similarity index 100% rename from internal/nginx/file/manager.go rename to internal/mode/static/nginx/file/manager.go diff --git a/internal/nginx/file/manager_test.go b/internal/mode/static/nginx/file/manager_test.go similarity index 96% rename from internal/nginx/file/manager_test.go rename to internal/mode/static/nginx/file/manager_test.go index 12252ec52..4ef6d1aae 100644 --- a/internal/nginx/file/manager_test.go +++ b/internal/mode/static/nginx/file/manager_test.go @@ -9,8 +9,8 @@ import ( . "github.com/onsi/gomega" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file/filefakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/file/filefakes" ) var _ = Describe("EventHandler", func() { diff --git a/internal/nginx/file/os_filemanager.go b/internal/mode/static/nginx/file/os_filemanager.go similarity index 100% rename from internal/nginx/file/os_filemanager.go rename to internal/mode/static/nginx/file/os_filemanager.go diff --git a/internal/nginx/modules/.prettierrc b/internal/mode/static/nginx/modules/.prettierrc similarity index 100% rename from internal/nginx/modules/.prettierrc rename to internal/mode/static/nginx/modules/.prettierrc diff --git a/internal/nginx/modules/README.md b/internal/mode/static/nginx/modules/README.md similarity index 100% rename from internal/nginx/modules/README.md rename to internal/mode/static/nginx/modules/README.md diff --git a/internal/nginx/modules/package-lock.json b/internal/mode/static/nginx/modules/package-lock.json similarity index 100% rename from internal/nginx/modules/package-lock.json rename to internal/mode/static/nginx/modules/package-lock.json diff --git a/internal/nginx/modules/package.json b/internal/mode/static/nginx/modules/package.json similarity index 100% rename from internal/nginx/modules/package.json rename to internal/mode/static/nginx/modules/package.json diff --git a/internal/nginx/modules/src/httpmatches.js b/internal/mode/static/nginx/modules/src/httpmatches.js similarity index 100% rename from internal/nginx/modules/src/httpmatches.js rename to internal/mode/static/nginx/modules/src/httpmatches.js diff --git a/internal/nginx/modules/test/httpmatches.test.js b/internal/mode/static/nginx/modules/test/httpmatches.test.js similarity index 100% rename from internal/nginx/modules/test/httpmatches.test.js rename to internal/mode/static/nginx/modules/test/httpmatches.test.js diff --git a/internal/nginx/runtime/manager.go b/internal/mode/static/nginx/runtime/manager.go similarity index 100% rename from internal/nginx/runtime/manager.go rename to internal/mode/static/nginx/runtime/manager.go diff --git a/internal/nginx/runtime/manager_test.go b/internal/mode/static/nginx/runtime/manager_test.go similarity index 100% rename from internal/nginx/runtime/manager_test.go rename to internal/mode/static/nginx/runtime/manager_test.go diff --git a/internal/nginx/runtime/runtimefakes/fake_manager.go b/internal/mode/static/nginx/runtime/runtimefakes/fake_manager.go similarity index 97% rename from internal/nginx/runtime/runtimefakes/fake_manager.go rename to internal/mode/static/nginx/runtime/runtimefakes/fake_manager.go index 73593d09a..93b65360a 100644 --- a/internal/nginx/runtime/runtimefakes/fake_manager.go +++ b/internal/mode/static/nginx/runtime/runtimefakes/fake_manager.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/runtime" ) type FakeManager struct { diff --git a/internal/sort/doc.go b/internal/mode/static/sort/doc.go similarity index 100% rename from internal/sort/doc.go rename to internal/mode/static/sort/doc.go diff --git a/internal/sort/sort.go b/internal/mode/static/sort/sort.go similarity index 100% rename from internal/sort/sort.go rename to internal/mode/static/sort/sort.go diff --git a/internal/sort/sort_test.go b/internal/mode/static/sort/sort_test.go similarity index 100% rename from internal/sort/sort_test.go rename to internal/mode/static/sort/sort_test.go diff --git a/internal/state/change_processor.go b/internal/mode/static/state/change_processor.go similarity index 97% rename from internal/state/change_processor.go rename to internal/mode/static/state/change_processor.go index 6ec5269ec..075efb8e2 100644 --- a/internal/state/change_processor.go +++ b/internal/mode/static/state/change_processor.go @@ -17,9 +17,9 @@ import ( gwapivalidation "sigs.k8s.io/gateway-api/apis/v1beta1/validation" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" ) const ( diff --git a/internal/state/change_processor_test.go b/internal/mode/static/state/change_processor_test.go similarity index 97% rename from internal/state/change_processor_test.go rename to internal/mode/static/state/change_processor_test.go index 6c26c0763..a22088b7a 100644 --- a/internal/state/change_processor_test.go +++ b/internal/mode/static/state/change_processor_test.go @@ -16,15 +16,16 @@ import ( "sigs.k8s.io/gateway-api/apis/v1alpha2" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship/relationshipfakes" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation/validationfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship/relationshipfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation/validationfakes" ) const ( @@ -406,7 +407,7 @@ var _ = Describe("ChangeProcessor", func() { }, Valid: true, Conditions: []conditions.Condition{ - conditions.NewRouteBackendRefRefBackendNotFound( + staticConds.NewRouteBackendRefRefBackendNotFound( "spec.rules[0].backendRefs[0].name: Not found: \"service\"", ), }, @@ -506,23 +507,23 @@ var _ = Describe("ChangeProcessor", func() { expGraph.GatewayClass = nil - expGraph.Gateway.Conditions = conditions.NewGatewayInvalid("GatewayClass doesn't exist") + expGraph.Gateway.Conditions = staticConds.NewGatewayInvalid("GatewayClass doesn't exist") expGraph.Gateway.Valid = false expGraph.Gateway.Listeners = nil // no ref grant exists yet for hr1 expGraph.Routes[hr1Name].Conditions = []conditions.Condition{ - conditions.NewRouteBackendRefRefNotPermitted( + staticConds.NewRouteBackendRefRefNotPermitted( "Backend ref to Service service-ns/service not permitted by any ReferenceGrant", ), } expGraph.Routes[hr1Name].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{ AcceptedHostnames: map[string][]string{}, - FailedCondition: conditions.NewRouteInvalidGateway(), + FailedCondition: staticConds.NewRouteInvalidGateway(), } expGraph.Routes[hr1Name].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{ AcceptedHostnames: map[string][]string{}, - FailedCondition: conditions.NewRouteInvalidGateway(), + FailedCondition: staticConds.NewRouteInvalidGateway(), } expGraph.ReferencedSecrets = nil @@ -543,7 +544,7 @@ var _ = Describe("ChangeProcessor", func() { Source: gw1.Spec.Listeners[1], Valid: false, Routes: map[types.NamespacedName]*graph.Route{}, - Conditions: conditions.NewListenerRefNotPermitted( + Conditions: staticConds.NewListenerRefNotPermitted( "Certificate ref to secret cert-ns/different-ns-tls-secret not permitted by any ReferenceGrant", ), SupportedKinds: []v1beta1.RouteGroupKind{{Kind: "HTTPRoute"}}, @@ -551,7 +552,7 @@ var _ = Describe("ChangeProcessor", func() { expAttachment := &graph.ParentRefAttachmentStatus{ AcceptedHostnames: map[string][]string{}, - FailedCondition: conditions.NewRouteInvalidListener(), + FailedCondition: staticConds.NewRouteInvalidListener(), Attached: false, } @@ -560,7 +561,7 @@ var _ = Describe("ChangeProcessor", func() { // no ref grant exists yet for hr1 expGraph.Routes[hr1Name].ParentRefs[1].Attachment = expAttachment expGraph.Routes[hr1Name].Conditions = []conditions.Condition{ - conditions.NewRouteBackendRefRefNotPermitted( + staticConds.NewRouteBackendRefRefNotPermitted( "Backend ref to Service service-ns/service not permitted by any ReferenceGrant", ), } @@ -578,7 +579,7 @@ var _ = Describe("ChangeProcessor", func() { // no ref grant exists yet for hr1 expGraph.Routes[hr1Name].Conditions = []conditions.Condition{ - conditions.NewRouteBackendRefRefNotPermitted( + staticConds.NewRouteBackendRefRefNotPermitted( "Backend ref to Service service-ns/service not permitted by any ReferenceGrant", ), } @@ -739,11 +740,11 @@ var _ = Describe("ChangeProcessor", func() { expGraph.Routes[hr2Name] = expRouteHR2 expGraph.Routes[hr2Name].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{ AcceptedHostnames: map[string][]string{}, - FailedCondition: conditions.NewTODO("Gateway is ignored"), + FailedCondition: staticConds.NewTODO("Gateway is ignored"), } expGraph.Routes[hr2Name].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{ AcceptedHostnames: map[string][]string{}, - FailedCondition: conditions.NewTODO("Gateway is ignored"), + FailedCondition: staticConds.NewTODO("Gateway is ignored"), } expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{ Source: diffNsTLSSecret, @@ -819,7 +820,7 @@ var _ = Describe("ChangeProcessor", func() { expGraph.GatewayClass = nil expGraph.Gateway = &graph.Gateway{ Source: gw2, - Conditions: conditions.NewGatewayInvalid("GatewayClass doesn't exist"), + Conditions: staticConds.NewGatewayInvalid("GatewayClass doesn't exist"), } expGraph.Routes = map[types.NamespacedName]*graph.Route{} expGraph.ReferencedSecrets = nil diff --git a/internal/state/conditions/conditions.go b/internal/mode/static/state/conditions/conditions.go similarity index 77% rename from internal/state/conditions/conditions.go rename to internal/mode/static/state/conditions/conditions.go index 929731a8b..72f95a884 100644 --- a/internal/state/conditions/conditions.go +++ b/internal/mode/static/state/conditions/conditions.go @@ -5,18 +5,11 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/gateway-api/apis/v1beta1" + + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" ) const ( - // GatewayClassReasonGatewayClassConflict indicates there are multiple GatewayClass resources - // that reference this controller, and we ignored the resource in question and picked the - // GatewayClass that is referenced in the command-line argument. - // This reason is used with GatewayClassConditionAccepted (false). - GatewayClassReasonGatewayClassConflict v1beta1.GatewayClassConditionReason = "GatewayClassConflict" - - // GatewayClassMessageGatewayClassConflict is a message that describes GatewayClassReasonGatewayClassConflict. - GatewayClassMessageGatewayClassConflict = "The resource is ignored due to a conflicting GatewayClass resource" - // ListenerReasonUnsupportedValue is used with the "Accepted" condition when a value of a field in a Listener // is invalid or not supported. ListenerReasonUnsupportedValue v1beta1.ListenerConditionReason = "UnsupportedValue" @@ -65,19 +58,11 @@ const ( "is programmed again" ) -// Condition defines a condition to be reported in the status of resources. -type Condition struct { - Type string - Status metav1.ConditionStatus - Reason string - Message string -} - // DeduplicateConditions removes duplicate conditions based on the condition type. // The last condition wins. The order of conditions is preserved. -func DeduplicateConditions(conds []Condition) []Condition { +func DeduplicateConditions(conds []conditions.Condition) []conditions.Condition { type elem struct { - cond Condition + cond conditions.Condition reverseIdx int } @@ -96,7 +81,7 @@ func DeduplicateConditions(conds []Condition) []Condition { idx++ } - result := make([]Condition, len(uniqueElems)) + result := make([]conditions.Condition, len(uniqueElems)) for _, el := range uniqueElems { result[len(result)-el.reverseIdx-1] = el.cond @@ -106,8 +91,8 @@ func DeduplicateConditions(conds []Condition) []Condition { } // NewTODO returns a Condition that can be used as a placeholder for a condition that is not yet implemented. -func NewTODO(msg string) Condition { - return Condition{ +func NewTODO(msg string) conditions.Condition { + return conditions.Condition{ Type: "TODO", Status: metav1.ConditionTrue, Reason: "TODO", @@ -116,8 +101,8 @@ func NewTODO(msg string) Condition { } // NewDefaultRouteConditions returns the default conditions that must be present in the status of an HTTPRoute. -func NewDefaultRouteConditions() []Condition { - return []Condition{ +func NewDefaultRouteConditions() []conditions.Condition { + return []conditions.Condition{ NewRouteAccepted(), NewRouteResolvedRefs(), } @@ -125,8 +110,8 @@ func NewDefaultRouteConditions() []Condition { // NewRouteNotAllowedByListeners returns a Condition that indicates that the HTTPRoute is not allowed by // any listener. -func NewRouteNotAllowedByListeners() Condition { - return Condition{ +func NewRouteNotAllowedByListeners() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonNotAllowedByListeners), @@ -136,8 +121,8 @@ func NewRouteNotAllowedByListeners() Condition { // NewRouteNoMatchingListenerHostname returns a Condition that indicates that the hostname of the listener // does not match the hostnames of the HTTPRoute. -func NewRouteNoMatchingListenerHostname() Condition { - return Condition{ +func NewRouteNoMatchingListenerHostname() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonNoMatchingListenerHostname), @@ -146,8 +131,8 @@ func NewRouteNoMatchingListenerHostname() Condition { } // NewRouteAccepted returns a Condition that indicates that the HTTPRoute is accepted. -func NewRouteAccepted() Condition { - return Condition{ +func NewRouteAccepted() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionTrue, Reason: string(v1beta1.RouteReasonAccepted), @@ -156,8 +141,8 @@ func NewRouteAccepted() Condition { } // NewRouteUnsupportedValue returns a Condition that indicates that the HTTPRoute includes an unsupported value. -func NewRouteUnsupportedValue(msg string) Condition { - return Condition{ +func NewRouteUnsupportedValue(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonUnsupportedValue), @@ -167,8 +152,8 @@ func NewRouteUnsupportedValue(msg string) Condition { // NewRouteInvalidListener returns a Condition that indicates that the HTTPRoute is not accepted because of an // invalid listener. -func NewRouteInvalidListener() Condition { - return Condition{ +func NewRouteInvalidListener() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: string(RouteReasonInvalidListener), @@ -177,8 +162,8 @@ func NewRouteInvalidListener() Condition { } // NewRouteResolvedRefs returns a Condition that indicates that all the references on the Route are resolved. -func NewRouteResolvedRefs() Condition { - return Condition{ +func NewRouteResolvedRefs() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionResolvedRefs), Status: metav1.ConditionTrue, Reason: string(v1beta1.RouteReasonResolvedRefs), @@ -188,8 +173,8 @@ func NewRouteResolvedRefs() Condition { // NewRouteBackendRefInvalidKind returns a Condition that indicates that the Route has a backendRef with an // invalid kind. -func NewRouteBackendRefInvalidKind(msg string) Condition { - return Condition{ +func NewRouteBackendRefInvalidKind(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionResolvedRefs), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonInvalidKind), @@ -199,8 +184,8 @@ func NewRouteBackendRefInvalidKind(msg string) Condition { // NewRouteBackendRefRefNotPermitted returns a Condition that indicates that the Route has a backendRef that // is not permitted. -func NewRouteBackendRefRefNotPermitted(msg string) Condition { - return Condition{ +func NewRouteBackendRefRefNotPermitted(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionResolvedRefs), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonRefNotPermitted), @@ -210,8 +195,8 @@ func NewRouteBackendRefRefNotPermitted(msg string) Condition { // NewRouteBackendRefRefBackendNotFound returns a Condition that indicates that the Route has a backendRef that // points to non-existing backend. -func NewRouteBackendRefRefBackendNotFound(msg string) Condition { - return Condition{ +func NewRouteBackendRefRefBackendNotFound(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionResolvedRefs), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonBackendNotFound), @@ -221,8 +206,8 @@ func NewRouteBackendRefRefBackendNotFound(msg string) Condition { // NewRouteBackendRefUnsupportedValue returns a Condition that indicates that the Route has a backendRef with // an unsupported value. -func NewRouteBackendRefUnsupportedValue(msg string) Condition { - return Condition{ +func NewRouteBackendRefUnsupportedValue(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionResolvedRefs), Status: metav1.ConditionFalse, Reason: RouteReasonBackendRefUnsupportedValue, @@ -232,8 +217,8 @@ func NewRouteBackendRefUnsupportedValue(msg string) Condition { // NewRouteInvalidGateway returns a Condition that indicates that the Route is not Accepted because the Gateway it // references is invalid. -func NewRouteInvalidGateway() Condition { - return Condition{ +func NewRouteInvalidGateway() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: RouteReasonInvalidGateway, @@ -243,8 +228,8 @@ func NewRouteInvalidGateway() Condition { // NewRouteNoMatchingParent returns a Condition that indicates that the Route is not Accepted because // it specifies a Port and/or SectionName that does not match any Listeners in the Gateway. -func NewRouteNoMatchingParent() Condition { - return Condition{ +func NewRouteNoMatchingParent() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: string(v1beta1.RouteReasonNoMatchingParent), @@ -254,8 +239,8 @@ func NewRouteNoMatchingParent() Condition { // NewRouteGatewayNotProgrammed returns a Condition that indicates that the Gateway it references is not programmed, // which does not guarantee that the HTTPRoute has been configured. -func NewRouteGatewayNotProgrammed(msg string) Condition { - return Condition{ +func NewRouteGatewayNotProgrammed(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.RouteConditionAccepted), Status: metav1.ConditionFalse, Reason: string(RouteReasonGatewayNotProgrammed), @@ -264,8 +249,8 @@ func NewRouteGatewayNotProgrammed(msg string) Condition { } // NewDefaultListenerConditions returns the default Conditions that must be present in the status of a Listener. -func NewDefaultListenerConditions() []Condition { - return []Condition{ +func NewDefaultListenerConditions() []conditions.Condition { + return []conditions.Condition{ NewListenerAccepted(), NewListenerProgrammed(), NewListenerResolvedRefs(), @@ -274,8 +259,8 @@ func NewDefaultListenerConditions() []Condition { } // NewListenerAccepted returns a Condition that indicates that the Listener is accepted. -func NewListenerAccepted() Condition { - return Condition{ +func NewListenerAccepted() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.ListenerConditionAccepted), Status: metav1.ConditionTrue, Reason: string(v1beta1.ListenerReasonAccepted), @@ -284,8 +269,8 @@ func NewListenerAccepted() Condition { } // NewListenerProgrammed returns a Condition that indicates the Listener is programmed. -func NewListenerProgrammed() Condition { - return Condition{ +func NewListenerProgrammed() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.ListenerConditionProgrammed), Status: metav1.ConditionTrue, Reason: string(v1beta1.ListenerReasonProgrammed), @@ -294,8 +279,8 @@ func NewListenerProgrammed() Condition { } // NewListenerResolvedRefs returns a Condition that indicates that all references in a Listener are resolved. -func NewListenerResolvedRefs() Condition { - return Condition{ +func NewListenerResolvedRefs() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.ListenerConditionResolvedRefs), Status: metav1.ConditionTrue, Reason: string(v1beta1.ListenerReasonResolvedRefs), @@ -304,8 +289,8 @@ func NewListenerResolvedRefs() Condition { } // NewListenerNoConflicts returns a Condition that indicates that there are no conflicts in a Listener. -func NewListenerNoConflicts() Condition { - return Condition{ +func NewListenerNoConflicts() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.ListenerConditionConflicted), Status: metav1.ConditionFalse, Reason: string(v1beta1.ListenerReasonNoConflicts), @@ -315,8 +300,8 @@ func NewListenerNoConflicts() Condition { // NewListenerNotProgrammedInvalid returns a Condition that indicates the Listener is not programmed because it is // semantically or syntactically invalid. The provided message contains the details of why the Listener is invalid. -func NewListenerNotProgrammedInvalid(msg string) Condition { - return Condition{ +func NewListenerNotProgrammedInvalid(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.ListenerConditionProgrammed), Status: metav1.ConditionFalse, Reason: string(v1beta1.ListenerReasonInvalid), @@ -326,8 +311,8 @@ func NewListenerNotProgrammedInvalid(msg string) Condition { // NewListenerUnsupportedValue returns Conditions that indicate that a field of a Listener has an unsupported value. // Unsupported means that the value is not supported by the implementation or invalid. -func NewListenerUnsupportedValue(msg string) []Condition { - return []Condition{ +func NewListenerUnsupportedValue(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.ListenerConditionAccepted), Status: metav1.ConditionFalse, @@ -339,8 +324,8 @@ func NewListenerUnsupportedValue(msg string) []Condition { } // NewListenerInvalidCertificateRef returns Conditions that indicate that a CertificateRef of a Listener is invalid. -func NewListenerInvalidCertificateRef(msg string) []Condition { - return []Condition{ +func NewListenerInvalidCertificateRef(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.ListenerConditionAccepted), Status: metav1.ConditionFalse, @@ -359,8 +344,8 @@ func NewListenerInvalidCertificateRef(msg string) []Condition { // NewListenerInvalidRouteKinds returns Conditions that indicate that an invalid or unsupported Route kind is // specified by the Listener. -func NewListenerInvalidRouteKinds(msg string) []Condition { - return []Condition{ +func NewListenerInvalidRouteKinds(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.ListenerReasonResolvedRefs), Status: metav1.ConditionFalse, @@ -373,8 +358,8 @@ func NewListenerInvalidRouteKinds(msg string) []Condition { // NewListenerProtocolConflict returns Conditions that indicate multiple Listeners are specified with the same // Listener port number, but have conflicting protocol specifications. -func NewListenerProtocolConflict(msg string) []Condition { - return []Condition{ +func NewListenerProtocolConflict(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.ListenerConditionAccepted), Status: metav1.ConditionFalse, @@ -392,8 +377,8 @@ func NewListenerProtocolConflict(msg string) []Condition { } // NewListenerUnsupportedProtocol returns Conditions that indicate that the protocol of a Listener is unsupported. -func NewListenerUnsupportedProtocol(msg string) []Condition { - return []Condition{ +func NewListenerUnsupportedProtocol(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.ListenerConditionAccepted), Status: metav1.ConditionFalse, @@ -406,8 +391,8 @@ func NewListenerUnsupportedProtocol(msg string) []Condition { // NewListenerRefNotPermitted returns Conditions that indicates that the Listener references a TLS secret that is not // permitted by a ReferenceGrant. -func NewListenerRefNotPermitted(msg string) []Condition { - return []Condition{ +func NewListenerRefNotPermitted(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.ListenerConditionAccepted), Status: metav1.ConditionFalse, @@ -424,32 +409,9 @@ func NewListenerRefNotPermitted(msg string) []Condition { } } -// NewDefaultGatewayClassConditions returns the default Conditions that must be present in the status of a GatewayClass. -func NewDefaultGatewayClassConditions() []Condition { - return []Condition{ - { - Type: string(v1beta1.GatewayClassConditionStatusAccepted), - Status: metav1.ConditionTrue, - Reason: string(v1beta1.GatewayClassReasonAccepted), - Message: "GatewayClass is accepted", - }, - } -} - -// NewGatewayClassConflict returns a Condition that indicates that the GatewayClass is not accepted -// due to a conflict with another GatewayClass. -func NewGatewayClassConflict() Condition { - return Condition{ - Type: string(v1beta1.GatewayClassConditionStatusAccepted), - Status: metav1.ConditionFalse, - Reason: string(GatewayClassReasonGatewayClassConflict), - Message: GatewayClassMessageGatewayClassConflict, - } -} - // NewGatewayClassInvalidParameters returns a Condition that indicates that the GatewayClass has invalid parameters. -func NewGatewayClassInvalidParameters(msg string) Condition { - return Condition{ +func NewGatewayClassInvalidParameters(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.GatewayClassConditionStatusAccepted), Status: metav1.ConditionFalse, Reason: string(v1beta1.GatewayClassReasonInvalidParameters), @@ -458,16 +420,16 @@ func NewGatewayClassInvalidParameters(msg string) Condition { } // NewDefaultGatewayConditions returns the default Conditions that must be present in the status of a Gateway. -func NewDefaultGatewayConditions() []Condition { - return []Condition{ +func NewDefaultGatewayConditions() []conditions.Condition { + return []conditions.Condition{ NewGatewayAccepted(), NewGatewayProgrammed(), } } // NewGatewayAccepted returns a Condition that indicates the Gateway is accepted. -func NewGatewayAccepted() Condition { - return Condition{ +func NewGatewayAccepted() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.GatewayConditionAccepted), Status: metav1.ConditionTrue, Reason: string(v1beta1.GatewayReasonAccepted), @@ -476,8 +438,8 @@ func NewGatewayAccepted() Condition { } // NewGatewayConflict returns Conditions that indicate the Gateway has a conflict with another Gateway. -func NewGatewayConflict() []Condition { - return []Condition{ +func NewGatewayConflict() []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.GatewayConditionAccepted), Status: metav1.ConditionFalse, @@ -490,8 +452,8 @@ func NewGatewayConflict() []Condition { // NewGatewayAcceptedListenersNotValid returns a Condition that indicates the Gateway is accepted, // but has at least one listener that is invalid. -func NewGatewayAcceptedListenersNotValid() Condition { - return Condition{ +func NewGatewayAcceptedListenersNotValid() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.GatewayConditionAccepted), Status: metav1.ConditionTrue, Reason: string(v1beta1.GatewayReasonListenersNotValid), @@ -501,9 +463,9 @@ func NewGatewayAcceptedListenersNotValid() Condition { // NewGatewayNotAcceptedListenersNotValid returns Conditions that indicate the Gateway is not accepted, // because all listeners are invalid. -func NewGatewayNotAcceptedListenersNotValid() []Condition { +func NewGatewayNotAcceptedListenersNotValid() []conditions.Condition { msg := "Gateway has no valid listeners" - return []Condition{ + return []conditions.Condition{ { Type: string(v1beta1.GatewayConditionAccepted), Status: metav1.ConditionFalse, @@ -516,8 +478,8 @@ func NewGatewayNotAcceptedListenersNotValid() []Condition { // NewGatewayInvalid returns Conditions that indicate the Gateway is not accepted and programmed because it is // semantically or syntactically invalid. The provided message contains the details of why the Gateway is invalid. -func NewGatewayInvalid(msg string) []Condition { - return []Condition{ +func NewGatewayInvalid(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.GatewayConditionAccepted), Status: metav1.ConditionFalse, @@ -530,8 +492,8 @@ func NewGatewayInvalid(msg string) []Condition { // NewGatewayUnsupportedValue returns Conditions that indicate that a field of the Gateway has an unsupported value. // Unsupported means that the value is not supported by the implementation or invalid. -func NewGatewayUnsupportedValue(msg string) []Condition { - return []Condition{ +func NewGatewayUnsupportedValue(msg string) []conditions.Condition { + return []conditions.Condition{ { Type: string(v1beta1.GatewayConditionAccepted), Status: metav1.ConditionFalse, @@ -548,8 +510,8 @@ func NewGatewayUnsupportedValue(msg string) []Condition { } // NewGatewayProgrammed returns a Condition that indicates the Gateway is programmed. -func NewGatewayProgrammed() Condition { - return Condition{ +func NewGatewayProgrammed() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.GatewayConditionProgrammed), Status: metav1.ConditionTrue, Reason: string(v1beta1.GatewayReasonProgrammed), @@ -557,10 +519,11 @@ func NewGatewayProgrammed() Condition { } } -// NewGatewayInvalid returns a Condition that indicates the Gateway is not programmed because it is -// semantically or syntactically invalid. The provided message contains the details of why the Gateway is invalid. -func NewGatewayNotProgrammedInvalid(msg string) Condition { - return Condition{ +// NewGatewayNotProgrammedInvalid returns a Condition that indicates the Gateway is not programmed +// because it is semantically or syntactically invalid. The provided message contains the details of +// why the Gateway is invalid. +func NewGatewayNotProgrammedInvalid(msg string) conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.GatewayConditionProgrammed), Status: metav1.ConditionFalse, Reason: string(v1beta1.GatewayReasonInvalid), @@ -570,8 +533,8 @@ func NewGatewayNotProgrammedInvalid(msg string) Condition { // NewGatewayConflictNotProgrammed returns a custom Programmed Condition that indicates the Gateway has a // conflict with another Gateway. -func NewGatewayConflictNotProgrammed() Condition { - return Condition{ +func NewGatewayConflictNotProgrammed() conditions.Condition { + return conditions.Condition{ Type: string(v1beta1.GatewayConditionProgrammed), Status: metav1.ConditionFalse, Reason: string(GatewayReasonGatewayConflict), diff --git a/internal/state/conditions/conditions_test.go b/internal/mode/static/state/conditions/conditions_test.go similarity index 86% rename from internal/state/conditions/conditions_test.go rename to internal/mode/static/state/conditions/conditions_test.go index b0510dc66..f1067899c 100644 --- a/internal/state/conditions/conditions_test.go +++ b/internal/mode/static/state/conditions/conditions_test.go @@ -5,12 +5,14 @@ import ( . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" ) func TestDeduplicateConditions(t *testing.T) { g := NewGomegaWithT(t) - conds := []Condition{ + conds := []conditions.Condition{ { Type: "Type1", Status: metav1.ConditionTrue, @@ -38,7 +40,7 @@ func TestDeduplicateConditions(t *testing.T) { }, } - expected := []Condition{ + expected := []conditions.Condition{ { Type: "Type1", Status: metav1.ConditionFalse, diff --git a/internal/state/dataplane/configuration.go b/internal/mode/static/state/dataplane/configuration.go similarity index 99% rename from internal/state/dataplane/configuration.go rename to internal/mode/static/state/dataplane/configuration.go index d3124fb7c..0205db641 100644 --- a/internal/state/dataplane/configuration.go +++ b/internal/mode/static/state/dataplane/configuration.go @@ -9,8 +9,8 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" ) type PathType string diff --git a/internal/state/dataplane/configuration_test.go b/internal/mode/static/state/dataplane/configuration_test.go similarity index 99% rename from internal/state/dataplane/configuration_test.go rename to internal/mode/static/state/dataplane/configuration_test.go index 553a5776e..0a4b04951 100644 --- a/internal/state/dataplane/configuration_test.go +++ b/internal/mode/static/state/dataplane/configuration_test.go @@ -14,10 +14,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver/resolverfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver/resolverfakes" ) func TestBuildConfiguration(t *testing.T) { diff --git a/internal/state/dataplane/doc.go b/internal/mode/static/state/dataplane/doc.go similarity index 100% rename from internal/state/dataplane/doc.go rename to internal/mode/static/state/dataplane/doc.go diff --git a/internal/state/dataplane/sort.go b/internal/mode/static/state/dataplane/sort.go similarity index 99% rename from internal/state/dataplane/sort.go rename to internal/mode/static/state/dataplane/sort.go index 9237114c3..7227fb384 100644 --- a/internal/state/dataplane/sort.go +++ b/internal/mode/static/state/dataplane/sort.go @@ -3,7 +3,7 @@ package dataplane import ( "sort" - nkgsort "github.com/nginxinc/nginx-kubernetes-gateway/internal/sort" + nkgsort "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/sort" ) func sortMatchRules(matchRules []MatchRule) { diff --git a/internal/state/dataplane/sort_test.go b/internal/mode/static/state/dataplane/sort_test.go similarity index 98% rename from internal/state/dataplane/sort_test.go rename to internal/mode/static/state/dataplane/sort_test.go index 42e7fa5cf..9a2c3d417 100644 --- a/internal/state/dataplane/sort_test.go +++ b/internal/mode/static/state/dataplane/sort_test.go @@ -8,7 +8,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func TestSort(t *testing.T) { diff --git a/internal/state/graph/backend_refs.go b/internal/mode/static/state/graph/backend_refs.go similarity index 90% rename from internal/state/graph/backend_refs.go rename to internal/mode/static/state/graph/backend_refs.go index b5e7e3399..a5a9b2b42 100644 --- a/internal/state/graph/backend_refs.go +++ b/internal/mode/static/state/graph/backend_refs.go @@ -8,7 +8,8 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) // BackendRef is an internal representation of a backendRef in an HTTPRoute. @@ -122,7 +123,7 @@ func createBackendRef( Valid: false, } - cond := conditions.NewRouteBackendRefRefBackendNotFound(err.Error()) + cond := staticConds.NewRouteBackendRefRefBackendNotFound(err.Error()) return backendRef, &cond } @@ -168,7 +169,7 @@ func validateHTTPBackendRef( if len(ref.Filters) > 0 { valErr := field.TooMany(path.Child("filters"), len(ref.Filters), 0) - return false, conditions.NewRouteBackendRefUnsupportedValue(valErr.Error()) + return false, staticConds.NewRouteBackendRefUnsupportedValue(valErr.Error()) } return validateBackendRef(ref.BackendRef, routeNs, refGrantResolver, path) @@ -184,12 +185,12 @@ func validateBackendRef( if ref.Group != nil && !(*ref.Group == "core" || *ref.Group == "") { valErr := field.NotSupported(path.Child("group"), *ref.Group, []string{"core", ""}) - return false, conditions.NewRouteBackendRefInvalidKind(valErr.Error()) + return false, staticConds.NewRouteBackendRefInvalidKind(valErr.Error()) } if ref.Kind != nil && *ref.Kind != "Service" { valErr := field.NotSupported(path.Child("kind"), *ref.Kind, []string{"Service"}) - return false, conditions.NewRouteBackendRefInvalidKind(valErr.Error()) + return false, staticConds.NewRouteBackendRefInvalidKind(valErr.Error()) } // no need to validate ref.Name @@ -200,7 +201,7 @@ func validateBackendRef( if !refGrantResolver.refAllowed(toService(refNsName), fromHTTPRoute(routeNs)) { msg := fmt.Sprintf("Backend ref to Service %s not permitted by any ReferenceGrant", refNsName) - return false, conditions.NewRouteBackendRefRefNotPermitted(msg) + return false, staticConds.NewRouteBackendRefRefNotPermitted(msg) } } @@ -213,7 +214,7 @@ func validateBackendRef( if ref.Weight != nil { if err := validateWeight(*ref.Weight); err != nil { valErr := field.Invalid(path.Child("weight"), *ref.Weight, err.Error()) - return false, conditions.NewRouteBackendRefUnsupportedValue(valErr.Error()) + return false, staticConds.NewRouteBackendRefUnsupportedValue(valErr.Error()) } } diff --git a/internal/state/graph/backend_refs_test.go b/internal/mode/static/state/graph/backend_refs_test.go similarity index 94% rename from internal/state/graph/backend_refs_test.go rename to internal/mode/static/state/graph/backend_refs_test.go index 81eb66655..d4fa16b4c 100644 --- a/internal/state/graph/backend_refs_test.go +++ b/internal/mode/static/state/graph/backend_refs_test.go @@ -11,8 +11,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) func getNormalRef() v1beta1.BackendRef { @@ -57,7 +58,7 @@ func TestValidateHTTPBackendRef(t *testing.T) { }, }, expectedValid: false, - expectedCondition: conditions.NewRouteBackendRefUnsupportedValue( + expectedCondition: staticConds.NewRouteBackendRefUnsupportedValue( "test.filters: Too many: 1: must have at most 0 items", ), }, @@ -70,7 +71,7 @@ func TestValidateHTTPBackendRef(t *testing.T) { }), }, expectedValid: false, - expectedCondition: conditions.NewRouteBackendRefInvalidKind( + expectedCondition: staticConds.NewRouteBackendRefInvalidKind( `test.kind: Unsupported value: "NotService": supported values: "Service"`, ), }, @@ -168,7 +169,7 @@ func TestValidateBackendRef(t *testing.T) { return backend }), expectedValid: false, - expectedCondition: conditions.NewRouteBackendRefInvalidKind( + expectedCondition: staticConds.NewRouteBackendRefInvalidKind( `test.group: Unsupported value: "invalid": supported values: "core", ""`, ), }, @@ -179,7 +180,7 @@ func TestValidateBackendRef(t *testing.T) { return backend }), expectedValid: false, - expectedCondition: conditions.NewRouteBackendRefInvalidKind( + expectedCondition: staticConds.NewRouteBackendRefInvalidKind( `test.kind: Unsupported value: "NotService": supported values: "Service"`, ), }, @@ -190,7 +191,7 @@ func TestValidateBackendRef(t *testing.T) { return backend }), expectedValid: false, - expectedCondition: conditions.NewRouteBackendRefRefNotPermitted( + expectedCondition: staticConds.NewRouteBackendRefRefNotPermitted( "Backend ref to Service invalid/service1 not permitted by any ReferenceGrant", ), }, @@ -201,7 +202,7 @@ func TestValidateBackendRef(t *testing.T) { return backend }), expectedValid: false, - expectedCondition: conditions.NewRouteBackendRefUnsupportedValue( + expectedCondition: staticConds.NewRouteBackendRefUnsupportedValue( "test.weight: Invalid value: -1: must be in the range [0, 1000000]", ), }, @@ -466,7 +467,7 @@ func TestAddBackendRefsToRulesTest(t *testing.T) { }, }, expectedConditions: []conditions.Condition{ - conditions.NewRouteBackendRefInvalidKind( + staticConds.NewRouteBackendRefInvalidKind( `spec.rules[0].backendRefs[0].kind: Unsupported value: "NotService": supported values: "Service"`, ), }, @@ -558,7 +559,7 @@ func TestCreateBackend(t *testing.T) { }, expectedServicePortReference: "", expectedCondition: helpers.GetPointer( - conditions.NewRouteBackendRefUnsupportedValue( + staticConds.NewRouteBackendRefUnsupportedValue( "test.weight: Invalid value: -1: must be in the range [0, 1000000]", ), ), @@ -579,7 +580,7 @@ func TestCreateBackend(t *testing.T) { }, expectedServicePortReference: "", expectedCondition: helpers.GetPointer( - conditions.NewRouteBackendRefInvalidKind( + staticConds.NewRouteBackendRefInvalidKind( `test.kind: Unsupported value: "NotService": supported values: "Service"`, ), ), @@ -600,7 +601,7 @@ func TestCreateBackend(t *testing.T) { }, expectedServicePortReference: "", expectedCondition: helpers.GetPointer( - conditions.NewRouteBackendRefRefBackendNotFound(`test.name: Not found: "not-exist"`), + staticConds.NewRouteBackendRefRefBackendNotFound(`test.name: Not found: "not-exist"`), ), name: "service doesn't exist", }, diff --git a/internal/state/graph/doc.go b/internal/mode/static/state/graph/doc.go similarity index 100% rename from internal/state/graph/doc.go rename to internal/mode/static/state/graph/doc.go diff --git a/internal/state/graph/gateway.go b/internal/mode/static/state/graph/gateway.go similarity index 87% rename from internal/state/graph/gateway.go rename to internal/mode/static/state/graph/gateway.go index 50f2fdb0f..278cc996a 100644 --- a/internal/state/graph/gateway.go +++ b/internal/mode/static/state/graph/gateway.go @@ -8,8 +8,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - nkgsort "github.com/nginxinc/nginx-kubernetes-gateway/internal/sort" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + nkgsort "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/sort" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) // Gateway represents the winning Gateway resource. @@ -120,16 +121,16 @@ func validateGateway(gw *v1beta1.Gateway, gc *GatewayClass) []conditions.Conditi var conds []conditions.Condition if gc == nil { - conds = append(conds, conditions.NewGatewayInvalid("GatewayClass doesn't exist")...) + conds = append(conds, staticConds.NewGatewayInvalid("GatewayClass doesn't exist")...) } else if !gc.Valid { - conds = append(conds, conditions.NewGatewayInvalid("GatewayClass is invalid")...) + conds = append(conds, staticConds.NewGatewayInvalid("GatewayClass is invalid")...) } if len(gw.Spec.Addresses) > 0 { path := field.NewPath("spec", "addresses") valErr := field.Forbidden(path, "addresses are not supported") - conds = append(conds, conditions.NewGatewayUnsupportedValue(valErr.Error())...) + conds = append(conds, staticConds.NewGatewayUnsupportedValue(valErr.Error())...) } return conds diff --git a/internal/state/graph/gateway_listener.go b/internal/mode/static/state/graph/gateway_listener.go similarity index 90% rename from internal/state/graph/gateway_listener.go rename to internal/mode/static/state/graph/gateway_listener.go index 31119d7ab..71e66a0f7 100644 --- a/internal/state/graph/gateway_listener.go +++ b/internal/mode/static/state/graph/gateway_listener.go @@ -10,7 +10,8 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) // Listener represents a Listener of the Gateway resource. @@ -83,7 +84,7 @@ func newListenerConfiguratorFactory( listener.Protocol, []string{string(v1beta1.HTTPProtocolType), string(v1beta1.HTTPSProtocolType)}, ) - return conditions.NewListenerUnsupportedProtocol(valErr.Error()) + return staticConds.NewListenerUnsupportedProtocol(valErr.Error()) }, }, }, @@ -156,7 +157,7 @@ func (c *listenerConfigurator) configure(listener v1beta1.Listener) *Listener { allowedRouteSelector, err = metav1.LabelSelectorAsSelector(selector) if err != nil { msg := fmt.Sprintf("invalid label selector: %s", err.Error()) - conds = append(conds, conditions.NewListenerUnsupportedValue(msg)...) + conds = append(conds, staticConds.NewListenerUnsupportedValue(msg)...) } } @@ -207,7 +208,7 @@ func validateListenerHostname(listener v1beta1.Listener) []conditions.Condition if err != nil { path := field.NewPath("hostname") valErr := field.Invalid(path, listener.Hostname, err.Error()) - return conditions.NewListenerUnsupportedValue(valErr.Error()) + return staticConds.NewListenerUnsupportedValue(valErr.Error()) } return nil } @@ -242,7 +243,7 @@ func getAndValidateListenerSupportedKinds(listener v1beta1.Listener) ( for _, kind := range listener.AllowedRoutes.Kinds { if !validHTTPRouteKind(kind) { msg := fmt.Sprintf("Unsupported route kind \"%s/%s\"", *kind.Group, kind.Kind) - conds = append(conds, conditions.NewListenerInvalidRouteKinds(msg)...) + conds = append(conds, staticConds.NewListenerInvalidRouteKinds(msg)...) continue } supportedKinds = append(supportedKinds, kind) @@ -268,7 +269,7 @@ func validateListenerLabelSelector(listener v1beta1.Listener) []conditions.Condi *listener.AllowedRoutes.Namespaces.From == v1beta1.NamespacesFromSelector && listener.AllowedRoutes.Namespaces.Selector == nil { msg := "Listener's AllowedRoutes Selector must be set when From is set to type Selector" - return conditions.NewListenerUnsupportedValue(msg) + return staticConds.NewListenerUnsupportedValue(msg) } return nil @@ -278,7 +279,7 @@ func validateHTTPListener(listener v1beta1.Listener) []conditions.Condition { if err := validateListenerPort(listener.Port); err != nil { path := field.NewPath("port") valErr := field.Invalid(path, listener.Port, err.Error()) - return conditions.NewListenerUnsupportedValue(valErr.Error()) + return staticConds.NewListenerUnsupportedValue(valErr.Error()) } if listener.TLS != nil { @@ -303,7 +304,7 @@ func createHTTPSListenerValidator() listenerValidator { if err := validateListenerPort(listener.Port); err != nil { path := field.NewPath("port") valErr := field.Invalid(path, listener.Port, err.Error()) - conds = append(conds, conditions.NewListenerUnsupportedValue(valErr.Error())...) + conds = append(conds, staticConds.NewListenerUnsupportedValue(valErr.Error())...) } if listener.TLS == nil { @@ -318,13 +319,13 @@ func createHTTPSListenerValidator() listenerValidator { *listener.TLS.Mode, []string{string(v1beta1.TLSModeTerminate)}, ) - conds = append(conds, conditions.NewListenerUnsupportedValue(valErr.Error())...) + conds = append(conds, staticConds.NewListenerUnsupportedValue(valErr.Error())...) } if len(listener.TLS.Options) > 0 { path := tlsPath.Child("options") valErr := field.Forbidden(path, "options are not supported") - conds = append(conds, conditions.NewListenerUnsupportedValue(valErr.Error())...) + conds = append(conds, staticConds.NewListenerUnsupportedValue(valErr.Error())...) } if len(listener.TLS.CertificateRefs) == 0 { @@ -338,20 +339,20 @@ func createHTTPSListenerValidator() listenerValidator { if certRef.Kind != nil && *certRef.Kind != "Secret" { path := certRefPath.Child("kind") valErr := field.NotSupported(path, *certRef.Kind, []string{"Secret"}) - conds = append(conds, conditions.NewListenerInvalidCertificateRef(valErr.Error())...) + conds = append(conds, staticConds.NewListenerInvalidCertificateRef(valErr.Error())...) } // for Kind Secret, certRef.Group must be nil or empty if certRef.Group != nil && *certRef.Group != "" { path := certRefPath.Child("group") valErr := field.NotSupported(path, *certRef.Group, []string{""}) - conds = append(conds, conditions.NewListenerInvalidCertificateRef(valErr.Error())...) + conds = append(conds, staticConds.NewListenerInvalidCertificateRef(valErr.Error())...) } if l := len(listener.TLS.CertificateRefs); l > 1 { path := tlsPath.Child("certificateRefs") valErr := field.TooMany(path, l, 1) - conds = append(conds, conditions.NewListenerUnsupportedValue(valErr.Error())...) + conds = append(conds, staticConds.NewListenerUnsupportedValue(valErr.Error())...) } return conds @@ -373,7 +374,7 @@ func createPortConflictResolver() listenerConflictResolver { if conflictedPorts[port] { l.Valid = false - conflictedConds := conditions.NewListenerProtocolConflict(fmt.Sprintf(format, port)) + conflictedConds := staticConds.NewListenerProtocolConflict(fmt.Sprintf(format, port)) l.Conditions = append(l.Conditions, conflictedConds...) return } @@ -395,7 +396,7 @@ func createPortConflictResolver() listenerConflictResolver { conflictedPorts[port] = true for _, l := range listenersByPort[port] { l.Valid = false - conflictedConds := conditions.NewListenerProtocolConflict(fmt.Sprintf(format, port)) + conflictedConds := staticConds.NewListenerProtocolConflict(fmt.Sprintf(format, port)) l.Conditions = append(l.Conditions, conflictedConds...) } } @@ -424,7 +425,7 @@ func createExternalReferencesForTLSSecretsResolver( if !refGrantResolver.refAllowed(toSecret(certRefNsName), fromGateway(gwNs)) { msg := fmt.Sprintf("Certificate ref to secret %s not permitted by any ReferenceGrant", certRefNsName) - l.Conditions = append(l.Conditions, conditions.NewListenerRefNotPermitted(msg)...) + l.Conditions = append(l.Conditions, staticConds.NewListenerRefNotPermitted(msg)...) l.Valid = false return } @@ -435,7 +436,7 @@ func createExternalReferencesForTLSSecretsResolver( // field.NotFound could be better, but it doesn't allow us to set the error message. valErr := field.Invalid(path, certRefNsName, err.Error()) - l.Conditions = append(l.Conditions, conditions.NewListenerInvalidCertificateRef(valErr.Error())...) + l.Conditions = append(l.Conditions, staticConds.NewListenerInvalidCertificateRef(valErr.Error())...) l.Valid = false } else { l.ResolvedSecret = &certRefNsName diff --git a/internal/state/graph/gateway_listener_test.go b/internal/mode/static/state/graph/gateway_listener_test.go similarity index 92% rename from internal/state/graph/gateway_listener_test.go rename to internal/mode/static/state/graph/gateway_listener_test.go index bbc7c66a8..191244047 100644 --- a/internal/state/graph/gateway_listener_test.go +++ b/internal/mode/static/state/graph/gateway_listener_test.go @@ -8,8 +8,9 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) func TestValidateHTTPListener(t *testing.T) { @@ -29,7 +30,7 @@ func TestValidateHTTPListener(t *testing.T) { l: v1beta1.Listener{ Port: 0, }, - expected: conditions.NewListenerUnsupportedValue(`port: Invalid value: 0: port must be between 1-65535`), + expected: staticConds.NewListenerUnsupportedValue(`port: Invalid value: 0: port must be between 1-65535`), name: "invalid port", }, } @@ -90,7 +91,7 @@ func TestValidateHTTPSListener(t *testing.T) { CertificateRefs: []v1beta1.SecretObjectReference{validSecretRef}, }, }, - expected: conditions.NewListenerUnsupportedValue(`port: Invalid value: 0: port must be between 1-65535`), + expected: staticConds.NewListenerUnsupportedValue(`port: Invalid value: 0: port must be between 1-65535`), name: "invalid port", }, { @@ -102,7 +103,7 @@ func TestValidateHTTPSListener(t *testing.T) { Options: map[v1beta1.AnnotationKey]v1beta1.AnnotationValue{"key": "val"}, }, }, - expected: conditions.NewListenerUnsupportedValue("tls.options: Forbidden: options are not supported"), + expected: staticConds.NewListenerUnsupportedValue("tls.options: Forbidden: options are not supported"), name: "invalid options", }, { @@ -113,7 +114,7 @@ func TestValidateHTTPSListener(t *testing.T) { CertificateRefs: []v1beta1.SecretObjectReference{validSecretRef}, }, }, - expected: conditions.NewListenerUnsupportedValue( + expected: staticConds.NewListenerUnsupportedValue( `tls.mode: Unsupported value: "Passthrough": supported values: "Terminate"`, ), name: "invalid tls mode", @@ -126,7 +127,7 @@ func TestValidateHTTPSListener(t *testing.T) { CertificateRefs: []v1beta1.SecretObjectReference{invalidSecretRefGroup}, }, }, - expected: conditions.NewListenerInvalidCertificateRef( + expected: staticConds.NewListenerInvalidCertificateRef( `tls.certificateRefs[0].group: Unsupported value: "some-group": supported values: ""`, ), name: "invalid cert ref group", @@ -139,7 +140,7 @@ func TestValidateHTTPSListener(t *testing.T) { CertificateRefs: []v1beta1.SecretObjectReference{invalidSecretRefKind}, }, }, - expected: conditions.NewListenerInvalidCertificateRef( + expected: staticConds.NewListenerInvalidCertificateRef( `tls.certificateRefs[0].kind: Unsupported value: "ConfigMap": supported values: "Secret"`, ), name: "invalid cert ref kind", @@ -152,7 +153,7 @@ func TestValidateHTTPSListener(t *testing.T) { CertificateRefs: []v1beta1.SecretObjectReference{validSecretRef, validSecretRef}, }, }, - expected: conditions.NewListenerUnsupportedValue( + expected: staticConds.NewListenerUnsupportedValue( "tls.certificateRefs: Too many: 2: must have at most 1 items", ), name: "too many cert refs", diff --git a/internal/state/graph/gateway_test.go b/internal/mode/static/state/graph/gateway_test.go similarity index 94% rename from internal/state/graph/gateway_test.go rename to internal/mode/static/state/graph/gateway_test.go index 8738d7d3e..e5fdb85d4 100644 --- a/internal/state/graph/gateway_test.go +++ b/internal/mode/static/state/graph/gateway_test.go @@ -12,8 +12,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) func TestProcessedGatewaysGetAllNsNames(t *testing.T) { @@ -475,7 +475,7 @@ func TestBuildGateway(t *testing.T) { "listener-cross-ns-secret": { Source: crossNamespaceSecretListener, Valid: false, - Conditions: conditions.NewListenerRefNotPermitted( + Conditions: staticConds.NewListenerRefNotPermitted( `Certificate ref to secret diff-ns/secret not permitted by any ReferenceGrant`, ), Routes: map[types.NamespacedName]*Route{}, @@ -497,7 +497,7 @@ func TestBuildGateway(t *testing.T) { "listener-with-invalid-selector": { Source: listenerInvalidSelector, Valid: false, - Conditions: conditions.NewListenerUnsupportedValue( + Conditions: staticConds.NewListenerUnsupportedValue( `invalid label selector: "invalid" is not a valid label selector operator`, ), SupportedKinds: []v1beta1.RouteGroupKind{ @@ -518,7 +518,7 @@ func TestBuildGateway(t *testing.T) { "invalid-protocol": { Source: invalidProtocolListener, Valid: false, - Conditions: conditions.NewListenerUnsupportedProtocol( + Conditions: staticConds.NewListenerUnsupportedProtocol( `protocol: Unsupported value: "TCP": supported values: "HTTP", "HTTPS"`, ), SupportedKinds: []v1beta1.RouteGroupKind{ @@ -541,7 +541,7 @@ func TestBuildGateway(t *testing.T) { "invalid-port": { Source: invalidPortListener, Valid: false, - Conditions: conditions.NewListenerUnsupportedValue( + Conditions: staticConds.NewListenerUnsupportedValue( `port: Invalid value: 0: port must be between 1-65535`, ), SupportedKinds: []v1beta1.RouteGroupKind{ @@ -551,7 +551,7 @@ func TestBuildGateway(t *testing.T) { "invalid-https-port": { Source: invalidHTTPSPortListener, Valid: false, - Conditions: conditions.NewListenerUnsupportedValue( + Conditions: staticConds.NewListenerUnsupportedValue( `port: Invalid value: 0: port must be between 1-65535`, ), SupportedKinds: []v1beta1.RouteGroupKind{ @@ -574,7 +574,7 @@ func TestBuildGateway(t *testing.T) { "invalid-hostname": { Source: invalidHostnameListener, Valid: false, - Conditions: conditions.NewListenerUnsupportedValue(invalidHostnameMsg), + Conditions: staticConds.NewListenerUnsupportedValue(invalidHostnameMsg), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, }, @@ -582,7 +582,7 @@ func TestBuildGateway(t *testing.T) { "invalid-https-hostname": { Source: invalidHTTPSHostnameListener, Valid: false, - Conditions: conditions.NewListenerUnsupportedValue(invalidHostnameMsg), + Conditions: staticConds.NewListenerUnsupportedValue(invalidHostnameMsg), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, }, @@ -602,7 +602,7 @@ func TestBuildGateway(t *testing.T) { Source: invalidTLSConfigListener, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerInvalidCertificateRef( + Conditions: staticConds.NewListenerInvalidCertificateRef( `tls.certificateRefs[0]: Invalid value: test/does-not-exist: secret does not exist`, ), SupportedKinds: []v1beta1.RouteGroupKind{ @@ -727,7 +727,7 @@ func TestBuildGateway(t *testing.T) { Source: foo80Listener1, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerProtocolConflict(conflict80PortMsg), + Conditions: staticConds.NewListenerProtocolConflict(conflict80PortMsg), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, }, @@ -736,7 +736,7 @@ func TestBuildGateway(t *testing.T) { Source: bar80Listener, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerProtocolConflict(conflict80PortMsg), + Conditions: staticConds.NewListenerProtocolConflict(conflict80PortMsg), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, }, @@ -745,7 +745,7 @@ func TestBuildGateway(t *testing.T) { Source: foo443Listener, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerProtocolConflict(conflict443PortMsg), + Conditions: staticConds.NewListenerProtocolConflict(conflict443PortMsg), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, }, @@ -754,7 +754,7 @@ func TestBuildGateway(t *testing.T) { Source: foo80HTTPSListener, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerProtocolConflict(conflict80PortMsg), + Conditions: staticConds.NewListenerProtocolConflict(conflict80PortMsg), ResolvedSecret: helpers.GetPointer(client.ObjectKeyFromObject(secretSameNs)), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, @@ -764,7 +764,7 @@ func TestBuildGateway(t *testing.T) { Source: foo443HTTPSListener1, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerProtocolConflict(conflict443PortMsg), + Conditions: staticConds.NewListenerProtocolConflict(conflict443PortMsg), ResolvedSecret: helpers.GetPointer(client.ObjectKeyFromObject(secretSameNs)), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, @@ -774,7 +774,7 @@ func TestBuildGateway(t *testing.T) { Source: bar443HTTPSListener, Valid: false, Routes: map[types.NamespacedName]*Route{}, - Conditions: conditions.NewListenerProtocolConflict(conflict443PortMsg), + Conditions: staticConds.NewListenerProtocolConflict(conflict443PortMsg), ResolvedSecret: helpers.GetPointer(client.ObjectKeyFromObject(secretSameNs)), SupportedKinds: []v1beta1.RouteGroupKind{ {Kind: "HTTPRoute"}, @@ -796,7 +796,7 @@ func TestBuildGateway(t *testing.T) { expected: &Gateway{ Source: getLastCreatedGetaway(), Valid: false, - Conditions: conditions.NewGatewayUnsupportedValue("spec." + + Conditions: staticConds.NewGatewayUnsupportedValue("spec." + "addresses: Forbidden: addresses are not supported", ), }, @@ -815,7 +815,7 @@ func TestBuildGateway(t *testing.T) { expected: &Gateway{ Source: getLastCreatedGetaway(), Valid: false, - Conditions: conditions.NewGatewayInvalid("GatewayClass is invalid"), + Conditions: staticConds.NewGatewayInvalid("GatewayClass is invalid"), }, name: "invalid gatewayclass", }, @@ -827,7 +827,7 @@ func TestBuildGateway(t *testing.T) { expected: &Gateway{ Source: getLastCreatedGetaway(), Valid: false, - Conditions: conditions.NewGatewayInvalid("GatewayClass doesn't exist"), + Conditions: staticConds.NewGatewayInvalid("GatewayClass doesn't exist"), }, name: "nil gatewayclass", }, diff --git a/internal/state/graph/gatewayclass.go b/internal/mode/static/state/graph/gatewayclass.go similarity index 89% rename from internal/state/graph/gatewayclass.go rename to internal/mode/static/state/graph/gatewayclass.go index 8c932939c..feb0e7989 100644 --- a/internal/state/graph/gatewayclass.go +++ b/internal/mode/static/state/graph/gatewayclass.go @@ -6,7 +6,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) // GatewayClass represents the GatewayClass resource. @@ -64,7 +65,7 @@ func buildGatewayClass(gc *v1beta1.GatewayClass) *GatewayClass { valErr := validateGatewayClass(gc) if valErr != nil { - conds = append(conds, conditions.NewGatewayClassInvalidParameters(valErr.Error())) + conds = append(conds, staticConds.NewGatewayClassInvalidParameters(valErr.Error())) } return &GatewayClass{ diff --git a/internal/state/graph/gatewayclass_test.go b/internal/mode/static/state/graph/gatewayclass_test.go similarity index 91% rename from internal/state/graph/gatewayclass_test.go rename to internal/mode/static/state/graph/gatewayclass_test.go index 178f2d04b..789cbe100 100644 --- a/internal/state/graph/gatewayclass_test.go +++ b/internal/mode/static/state/graph/gatewayclass_test.go @@ -9,8 +9,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" ) func TestProcessGatewayClasses(t *testing.T) { @@ -150,7 +151,9 @@ func TestBuildGatewayClass(t *testing.T) { Source: invalidGC, Valid: false, Conditions: []conditions.Condition{ - conditions.NewGatewayClassInvalidParameters("spec.parametersRef: Forbidden: parametersRef is not supported"), + staticConds.NewGatewayClassInvalidParameters( + "spec.parametersRef: Forbidden: parametersRef is not supported", + ), }, }, name: "invalid gatewayclass", diff --git a/internal/state/graph/graph.go b/internal/mode/static/state/graph/graph.go similarity index 97% rename from internal/state/graph/graph.go rename to internal/mode/static/state/graph/graph.go index b63571802..8581c35c5 100644 --- a/internal/state/graph/graph.go +++ b/internal/mode/static/state/graph/graph.go @@ -6,7 +6,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" ) // ClusterState includes cluster resources necessary to build the Graph. diff --git a/internal/state/graph/graph_test.go b/internal/mode/static/state/graph/graph_test.go similarity index 97% rename from internal/state/graph/graph_test.go rename to internal/mode/static/state/graph/graph_test.go index 3bc5bd584..c9a57d88f 100644 --- a/internal/state/graph/graph_test.go +++ b/internal/mode/static/state/graph/graph_test.go @@ -11,9 +11,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation/validationfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation/validationfakes" ) func TestBuildGraph(t *testing.T) { diff --git a/internal/state/graph/httproute.go b/internal/mode/static/state/graph/httproute.go similarity index 95% rename from internal/state/graph/httproute.go rename to internal/mode/static/state/graph/httproute.go index 470863bee..f0b6d3802 100644 --- a/internal/state/graph/httproute.go +++ b/internal/mode/static/state/graph/httproute.go @@ -12,8 +12,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" ) const wildcardHostname = "~^" @@ -182,7 +183,7 @@ func buildRoute( err := validateHostnames(ghr.Spec.Hostnames, field.NewPath("spec").Child("hostnames")) if err != nil { r.Valid = false - r.Conditions = append(r.Conditions, conditions.NewRouteUnsupportedValue(err.Error())) + r.Conditions = append(r.Conditions, staticConds.NewRouteUnsupportedValue(err.Error())) return r } @@ -233,10 +234,10 @@ func buildRoute( // FIXME(pleshakov): Partial validity for HTTPRoute rules is not defined in the Gateway API spec yet. // See https://github.com/nginxinc/nginx-kubernetes-gateway/issues/485 msg = "Some rules are invalid: " + msg - r.Conditions = append(r.Conditions, conditions.NewTODO(msg)) + r.Conditions = append(r.Conditions, staticConds.NewTODO(msg)) } else { msg = "All rules are invalid: " + msg - r.Conditions = append(r.Conditions, conditions.NewRouteUnsupportedValue(msg)) + r.Conditions = append(r.Conditions, staticConds.NewRouteUnsupportedValue(msg)) r.Valid = false } @@ -279,7 +280,7 @@ func bindRouteToListeners(r *Route, gw *Gateway, namespaces map[types.Namespaced if routeRef.Port != nil { valErr := field.Forbidden(path.Child("port"), "cannot be set") - attachment.FailedCondition = conditions.NewRouteUnsupportedValue(valErr.Error()) + attachment.FailedCondition = staticConds.NewRouteUnsupportedValue(valErr.Error()) continue } @@ -288,14 +289,14 @@ func bindRouteToListeners(r *Route, gw *Gateway, namespaces map[types.Namespaced referencesWinningGw := ref.Gateway.Namespace == gw.Source.Namespace && ref.Gateway.Name == gw.Source.Name if !referencesWinningGw { - attachment.FailedCondition = conditions.NewTODO("Gateway is ignored") + attachment.FailedCondition = staticConds.NewTODO("Gateway is ignored") continue } // Case 3: Attachment is not possible because Gateway is invalid if !gw.Valid { - attachment.FailedCondition = conditions.NewRouteInvalidGateway() + attachment.FailedCondition = staticConds.NewRouteInvalidGateway() continue } @@ -325,11 +326,11 @@ func tryToAttachRouteToListeners( validListeners, listenerExists := findValidListeners(getSectionName(sectionName), gw.Listeners) if !listenerExists { - return conditions.NewRouteNoMatchingParent(), false + return staticConds.NewRouteNoMatchingParent(), false } if len(validListeners) == 0 { - return conditions.NewRouteInvalidListener(), false + return staticConds.NewRouteInvalidListener(), false } bind := func(l *Listener) (allowed, attached bool) { @@ -357,9 +358,9 @@ func tryToAttachRouteToListeners( if !attached { if !allowed { - return conditions.NewRouteNotAllowedByListeners(), false + return staticConds.NewRouteNotAllowedByListeners(), false } - return conditions.NewRouteNoMatchingListenerHostname(), false + return staticConds.NewRouteNoMatchingListenerHostname(), false } return conditions.Condition{}, true diff --git a/internal/state/graph/httproute_test.go b/internal/mode/static/state/graph/httproute_test.go similarity index 97% rename from internal/state/graph/httproute_test.go rename to internal/mode/static/state/graph/httproute_test.go index bd83a0876..2f954f64d 100644 --- a/internal/state/graph/httproute_test.go +++ b/internal/mode/static/state/graph/httproute_test.go @@ -14,9 +14,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation/validationfakes" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + staticConds "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/conditions" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation/validationfakes" ) const ( @@ -422,7 +423,9 @@ func TestBuildRoute(t *testing.T) { }, }, Conditions: []conditions.Condition{ - conditions.NewRouteUnsupportedValue(`spec.hostnames[0]: Invalid value: "": cannot be empty string`), + staticConds.NewRouteUnsupportedValue( + `spec.hostnames[0]: Invalid value: "": cannot be empty string`, + ), }, }, name: "invalid hostname", @@ -440,7 +443,7 @@ func TestBuildRoute(t *testing.T) { }, }, Conditions: []conditions.Condition{ - conditions.NewRouteUnsupportedValue( + staticConds.NewRouteUnsupportedValue( `All rules are invalid: spec.rules[0].matches[0].path.value: Invalid value: "/invalid": invalid path`, ), }, @@ -466,7 +469,7 @@ func TestBuildRoute(t *testing.T) { }, }, Conditions: []conditions.Condition{ - conditions.NewRouteUnsupportedValue( + staticConds.NewRouteUnsupportedValue( `All rules are invalid: spec.rules[0].filters[0].requestRedirect.hostname: ` + `Invalid value: "invalid.example.com": invalid hostname`), }, @@ -492,7 +495,7 @@ func TestBuildRoute(t *testing.T) { }, }, Conditions: []conditions.Condition{ - conditions.NewTODO( + staticConds.NewTODO( `Some rules are invalid: ` + `[spec.rules[0].matches[0].path.value: Invalid value: "/invalid": invalid path, ` + `spec.rules[1].filters[0].requestRedirect.hostname: Invalid value: ` + @@ -804,7 +807,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteInvalidListener(), + FailedCondition: staticConds.NewRouteInvalidListener(), AcceptedHostnames: map[string][]string{}, }, }, @@ -829,7 +832,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteUnsupportedValue( + FailedCondition: staticConds.NewRouteUnsupportedValue( `spec.parentRefs[0].port: Forbidden: cannot be set`, ), AcceptedHostnames: map[string][]string{}, @@ -856,7 +859,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteNoMatchingParent(), + FailedCondition: staticConds.NewRouteNoMatchingParent(), AcceptedHostnames: map[string][]string{}, }, }, @@ -881,7 +884,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteInvalidListener(), + FailedCondition: staticConds.NewRouteInvalidListener(), AcceptedHostnames: map[string][]string{}, }, }, @@ -906,7 +909,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteNoMatchingListenerHostname(), + FailedCondition: staticConds.NewRouteNoMatchingListenerHostname(), AcceptedHostnames: map[string][]string{}, }, }, @@ -931,7 +934,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: ignoredGwNsName, Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewTODO("Gateway is ignored"), + FailedCondition: staticConds.NewTODO("Gateway is ignored"), AcceptedHostnames: map[string][]string{}, }, }, @@ -977,7 +980,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteInvalidGateway(), + FailedCondition: staticConds.NewRouteInvalidGateway(), AcceptedHostnames: map[string][]string{}, }, }, @@ -1010,7 +1013,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gw), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteNotAllowedByListeners(), + FailedCondition: staticConds.NewRouteNotAllowedByListeners(), AcceptedHostnames: map[string][]string{}, }, }, @@ -1094,7 +1097,7 @@ func TestBindRouteToListeners(t *testing.T) { Gateway: client.ObjectKeyFromObject(gwDiffNamespace), Attachment: &ParentRefAttachmentStatus{ Attached: false, - FailedCondition: conditions.NewRouteNotAllowedByListeners(), + FailedCondition: staticConds.NewRouteNotAllowedByListeners(), AcceptedHostnames: map[string][]string{}, }, }, diff --git a/internal/state/graph/reference_grant.go b/internal/mode/static/state/graph/reference_grant.go similarity index 100% rename from internal/state/graph/reference_grant.go rename to internal/mode/static/state/graph/reference_grant.go diff --git a/internal/state/graph/reference_grant_test.go b/internal/mode/static/state/graph/reference_grant_test.go similarity index 98% rename from internal/state/graph/reference_grant_test.go rename to internal/mode/static/state/graph/reference_grant_test.go index 3dfe4547e..7e65ae7b1 100644 --- a/internal/state/graph/reference_grant_test.go +++ b/internal/mode/static/state/graph/reference_grant_test.go @@ -7,7 +7,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func TestReferenceGrantResolver(t *testing.T) { diff --git a/internal/state/graph/secret.go b/internal/mode/static/state/graph/secret.go similarity index 100% rename from internal/state/graph/secret.go rename to internal/mode/static/state/graph/secret.go diff --git a/internal/state/graph/secret_test.go b/internal/mode/static/state/graph/secret_test.go similarity index 100% rename from internal/state/graph/secret_test.go rename to internal/mode/static/state/graph/secret_test.go diff --git a/internal/state/graph/validation.go b/internal/mode/static/state/graph/validation.go similarity index 100% rename from internal/state/graph/validation.go rename to internal/mode/static/state/graph/validation.go diff --git a/internal/state/graph/validation_test.go b/internal/mode/static/state/graph/validation_test.go similarity index 100% rename from internal/state/graph/validation_test.go rename to internal/mode/static/state/graph/validation_test.go diff --git a/internal/state/relationship/capturer.go b/internal/mode/static/state/relationship/capturer.go similarity index 97% rename from internal/state/relationship/capturer.go rename to internal/mode/static/state/relationship/capturer.go index 6767e287f..ed2a784ea 100644 --- a/internal/state/relationship/capturer.go +++ b/internal/mode/static/state/relationship/capturer.go @@ -9,8 +9,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" ) //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Capturer diff --git a/internal/state/relationship/capturer_suite_test.go b/internal/mode/static/state/relationship/capturer_suite_test.go similarity index 100% rename from internal/state/relationship/capturer_suite_test.go rename to internal/mode/static/state/relationship/capturer_suite_test.go diff --git a/internal/state/relationship/capturer_test.go b/internal/mode/static/state/relationship/capturer_test.go similarity index 98% rename from internal/state/relationship/capturer_test.go rename to internal/mode/static/state/relationship/capturer_test.go index eb0b7fce8..6faa5de8e 100644 --- a/internal/state/relationship/capturer_test.go +++ b/internal/mode/static/state/relationship/capturer_test.go @@ -10,9 +10,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship" ) func createBackendRefs(backendNames ...v1beta1.ObjectName) []v1beta1.HTTPBackendRef { diff --git a/internal/state/relationship/relationshipfakes/fake_capturer.go b/internal/mode/static/state/relationship/relationshipfakes/fake_capturer.go similarity index 98% rename from internal/state/relationship/relationshipfakes/fake_capturer.go rename to internal/mode/static/state/relationship/relationshipfakes/fake_capturer.go index 4307105ed..7f85ab6f8 100644 --- a/internal/state/relationship/relationshipfakes/fake_capturer.go +++ b/internal/mode/static/state/relationship/relationshipfakes/fake_capturer.go @@ -4,7 +4,7 @@ package relationshipfakes import ( "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) diff --git a/internal/state/relationship/relationships_test.go b/internal/mode/static/state/relationship/relationships_test.go similarity index 98% rename from internal/state/relationship/relationships_test.go rename to internal/mode/static/state/relationship/relationships_test.go index 843ea9a44..ce15a03cf 100644 --- a/internal/state/relationship/relationships_test.go +++ b/internal/mode/static/state/relationship/relationships_test.go @@ -8,7 +8,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) func TestGetBackendServiceNamesFromRoute(t *testing.T) { diff --git a/internal/state/resolver/resolver.go b/internal/mode/static/state/resolver/resolver.go similarity index 98% rename from internal/state/resolver/resolver.go rename to internal/mode/static/state/resolver/resolver.go index 7df65b690..149edcbb0 100644 --- a/internal/state/resolver/resolver.go +++ b/internal/mode/static/state/resolver/resolver.go @@ -10,7 +10,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" ) //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ServiceResolver diff --git a/internal/state/resolver/resolver_test.go b/internal/mode/static/state/resolver/resolver_test.go similarity index 99% rename from internal/state/resolver/resolver_test.go rename to internal/mode/static/state/resolver/resolver_test.go index d6844a0a5..bfaa51c7d 100644 --- a/internal/state/resolver/resolver_test.go +++ b/internal/mode/static/state/resolver/resolver_test.go @@ -10,7 +10,7 @@ import ( discoveryV1 "k8s.io/api/discovery/v1" "k8s.io/apimachinery/pkg/util/intstr" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" ) var ( diff --git a/internal/state/resolver/resolverfakes/fake_service_resolver.go b/internal/mode/static/state/resolver/resolverfakes/fake_service_resolver.go similarity index 97% rename from internal/state/resolver/resolverfakes/fake_service_resolver.go rename to internal/mode/static/state/resolver/resolverfakes/fake_service_resolver.go index f069266fa..06f223d39 100644 --- a/internal/state/resolver/resolverfakes/fake_service_resolver.go +++ b/internal/mode/static/state/resolver/resolverfakes/fake_service_resolver.go @@ -5,7 +5,7 @@ import ( "context" "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" v1 "k8s.io/api/core/v1" ) diff --git a/internal/state/resolver/service_resolver_suite_test.go b/internal/mode/static/state/resolver/service_resolver_suite_test.go similarity index 100% rename from internal/state/resolver/service_resolver_suite_test.go rename to internal/mode/static/state/resolver/service_resolver_suite_test.go diff --git a/internal/state/resolver/service_resolver_test.go b/internal/mode/static/state/resolver/service_resolver_test.go similarity index 96% rename from internal/state/resolver/service_resolver_test.go rename to internal/mode/static/state/resolver/service_resolver_test.go index 5cd8f84e2..a525d4dd5 100644 --- a/internal/state/resolver/service_resolver_test.go +++ b/internal/mode/static/state/resolver/service_resolver_test.go @@ -13,9 +13,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/controller/index" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/framework/helpers" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/resolver" ) func createSlice( diff --git a/internal/state/state_suite_test.go b/internal/mode/static/state/state_suite_test.go similarity index 100% rename from internal/state/state_suite_test.go rename to internal/mode/static/state/state_suite_test.go diff --git a/internal/state/statefakes/fake_change_processor.go b/internal/mode/static/state/statefakes/fake_change_processor.go similarity index 97% rename from internal/state/statefakes/fake_change_processor.go rename to internal/mode/static/state/statefakes/fake_change_processor.go index 43f7aa3a6..b24b78404 100644 --- a/internal/state/statefakes/fake_change_processor.go +++ b/internal/mode/static/state/statefakes/fake_change_processor.go @@ -4,8 +4,8 @@ package statefakes import ( "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/graph" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) diff --git a/internal/state/store.go b/internal/mode/static/state/store.go similarity index 99% rename from internal/state/store.go rename to internal/mode/static/state/store.go index 289e7ce95..40663ce45 100644 --- a/internal/state/store.go +++ b/internal/mode/static/state/store.go @@ -9,7 +9,7 @@ import ( "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/relationship" ) // Updater updates the cluster state. diff --git a/internal/state/validation/validationfakes/fake_httpfields_validator.go b/internal/mode/static/state/validation/validationfakes/fake_httpfields_validator.go similarity index 99% rename from internal/state/validation/validationfakes/fake_httpfields_validator.go rename to internal/mode/static/state/validation/validationfakes/fake_httpfields_validator.go index b26ab548f..f19519a32 100644 --- a/internal/state/validation/validationfakes/fake_httpfields_validator.go +++ b/internal/mode/static/state/validation/validationfakes/fake_httpfields_validator.go @@ -4,7 +4,7 @@ package validationfakes import ( "sync" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/validation" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/validation" ) type FakeHTTPFieldsValidator struct { diff --git a/internal/state/validation/validator.go b/internal/mode/static/state/validation/validator.go similarity index 100% rename from internal/state/validation/validator.go rename to internal/mode/static/state/validation/validator.go diff --git a/internal/mode/static/static_suite_test.go b/internal/mode/static/static_suite_test.go new file mode 100644 index 000000000..95fafaeec --- /dev/null +++ b/internal/mode/static/static_suite_test.go @@ -0,0 +1,13 @@ +package static_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestStatic(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Static Suite") +} diff --git a/internal/status/statuses.go b/internal/status/statuses.go deleted file mode 100644 index f7c4a8d46..000000000 --- a/internal/status/statuses.go +++ /dev/null @@ -1,229 +0,0 @@ -package status - -import ( - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/gateway-api/apis/v1beta1" - - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions" - "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph" -) - -// ListenerStatuses holds the statuses of listeners where the key is the name of a listener in the Gateway resource. -type ListenerStatuses map[string]ListenerStatus - -// HTTPRouteStatuses holds the statuses of HTTPRoutes where the key is the namespaced name of an HTTPRoute. -type HTTPRouteStatuses map[types.NamespacedName]HTTPRouteStatus - -// GatewayStatuses holds the statuses of Gateways where the key is the namespaced name of a Gateway. -type GatewayStatuses map[types.NamespacedName]GatewayStatus - -// GatewayClassStatuses holds the statuses of GatewayClasses where the key is the namespaced name of a GatewayClass. -type GatewayClassStatuses map[types.NamespacedName]GatewayClassStatus - -// Statuses holds the status-related information about Gateway API resources. -type Statuses struct { - GatewayClassStatuses GatewayClassStatuses - GatewayStatuses GatewayStatuses - HTTPRouteStatuses HTTPRouteStatuses -} - -// GatewayStatus holds the status of the winning Gateway resource. -type GatewayStatus struct { - // ListenerStatuses holds the statuses of listeners defined on the Gateway. - ListenerStatuses ListenerStatuses - // Conditions is the list of conditions for this Gateway. - Conditions []conditions.Condition - // ObservedGeneration is the generation of the resource that was processed. - ObservedGeneration int64 -} - -// ListenerStatus holds the status-related information about a listener in the Gateway resource. -type ListenerStatus struct { - Conditions []conditions.Condition - SupportedKinds []v1beta1.RouteGroupKind - AttachedRoutes int32 -} - -// HTTPRouteStatus holds the status-related information about an HTTPRoute resource. -type HTTPRouteStatus struct { - // ParentStatuses holds the statuses for parentRefs of the HTTPRoute. - ParentStatuses []ParentStatus - // ObservedGeneration is the generation of the resource that was processed. - ObservedGeneration int64 -} - -// ParentStatus holds status-related information related to how the HTTPRoute binds to a specific parentRef. -type ParentStatus struct { - // GatewayNsName is the Namespaced name of the Gateway, which the parentRef references. - GatewayNsName types.NamespacedName - // SectionName is the SectionName of the parentRef. - SectionName *v1beta1.SectionName - // Conditions is the list of conditions that are relevant to the parentRef. - Conditions []conditions.Condition -} - -// GatewayClassStatus holds status-related information about the GatewayClass resource. -type GatewayClassStatus struct { - Conditions []conditions.Condition - ObservedGeneration int64 -} - -type NginxReloadResult struct { - Error error -} - -// BuildStatuses builds statuses from a Graph. -func BuildStatuses(graph *graph.Graph, nginxReloadRes NginxReloadResult) Statuses { - statuses := Statuses{ - HTTPRouteStatuses: make(HTTPRouteStatuses), - } - - statuses.GatewayClassStatuses = buildGatewayClassStatuses(graph.GatewayClass, graph.IgnoredGatewayClasses) - - statuses.GatewayStatuses = buildGatewayStatuses(graph.Gateway, graph.IgnoredGateways, nginxReloadRes) - - for nsname, r := range graph.Routes { - parentStatuses := make([]ParentStatus, 0, len(r.ParentRefs)) - - defaultConds := conditions.NewDefaultRouteConditions() - - for _, ref := range r.ParentRefs { - failedAttachmentCondCount := 0 - if ref.Attachment != nil && !ref.Attachment.Attached { - failedAttachmentCondCount = 1 - } - allConds := make([]conditions.Condition, 0, len(r.Conditions)+len(defaultConds)+failedAttachmentCondCount) - - // We add defaultConds first, so that any additional conditions will override them, which is - // ensured by DeduplicateConditions. - allConds = append(allConds, defaultConds...) - allConds = append(allConds, r.Conditions...) - if failedAttachmentCondCount == 1 { - allConds = append(allConds, ref.Attachment.FailedCondition) - } - - if nginxReloadRes.Error != nil { - allConds = append(allConds, conditions.NewRouteGatewayNotProgrammed(conditions.RouteMessageFailedNginxReload)) - } - - routeRef := r.Source.Spec.ParentRefs[ref.Idx] - - parentStatuses = append(parentStatuses, ParentStatus{ - GatewayNsName: ref.Gateway, - SectionName: routeRef.SectionName, - Conditions: conditions.DeduplicateConditions(allConds), - }) - } - - statuses.HTTPRouteStatuses[nsname] = HTTPRouteStatus{ - ObservedGeneration: r.Source.Generation, - ParentStatuses: parentStatuses, - } - } - - return statuses -} - -func buildGatewayClassStatuses( - gc *graph.GatewayClass, - ignoredGwClasses map[types.NamespacedName]*v1beta1.GatewayClass, -) GatewayClassStatuses { - statuses := make(GatewayClassStatuses) - - if gc != nil { - defaultConds := conditions.NewDefaultGatewayClassConditions() - - conds := make([]conditions.Condition, 0, len(gc.Conditions)+len(defaultConds)) - - // We add default conds first, so that any additional conditions will override them, which is - // ensured by DeduplicateConditions. - conds = append(conds, defaultConds...) - conds = append(conds, gc.Conditions...) - - statuses[client.ObjectKeyFromObject(gc.Source)] = GatewayClassStatus{ - Conditions: conditions.DeduplicateConditions(conds), - ObservedGeneration: gc.Source.Generation, - } - } - - for nsname, gwClass := range ignoredGwClasses { - statuses[nsname] = GatewayClassStatus{ - Conditions: []conditions.Condition{conditions.NewGatewayClassConflict()}, - ObservedGeneration: gwClass.Generation, - } - } - - return statuses -} - -func buildGatewayStatuses( - gateway *graph.Gateway, - ignoredGateways map[types.NamespacedName]*v1beta1.Gateway, - nginxReloadRes NginxReloadResult, -) GatewayStatuses { - statuses := make(GatewayStatuses) - - if gateway != nil { - statuses[client.ObjectKeyFromObject(gateway.Source)] = buildGatewayStatus(gateway, nginxReloadRes) - } - - for nsname, gw := range ignoredGateways { - statuses[nsname] = GatewayStatus{ - Conditions: conditions.NewGatewayConflict(), - ObservedGeneration: gw.Generation, - } - } - - return statuses -} - -func buildGatewayStatus(gateway *graph.Gateway, nginxReloadRes NginxReloadResult) GatewayStatus { - if !gateway.Valid { - return GatewayStatus{ - Conditions: conditions.DeduplicateConditions(gateway.Conditions), - ObservedGeneration: gateway.Source.Generation, - } - } - - listenerStatuses := make(map[string]ListenerStatus) - - validListenerCount := 0 - for name, l := range gateway.Listeners { - var conds []conditions.Condition - - if l.Valid { - conds = conditions.NewDefaultListenerConditions() - validListenerCount++ - } else { - conds = l.Conditions - } - - if nginxReloadRes.Error != nil { - conds = append(conds, conditions.NewListenerNotProgrammedInvalid(conditions.ListenerMessageFailedNginxReload)) - } - - listenerStatuses[name] = ListenerStatus{ - AttachedRoutes: int32(len(l.Routes)), - Conditions: conditions.DeduplicateConditions(conds), - SupportedKinds: l.SupportedKinds, - } - } - - gwConds := conditions.NewDefaultGatewayConditions() - if validListenerCount == 0 { - gwConds = append(gwConds, conditions.NewGatewayNotAcceptedListenersNotValid()...) - } else if validListenerCount < len(gateway.Listeners) { - gwConds = append(gwConds, conditions.NewGatewayAcceptedListenersNotValid()) - } - - if nginxReloadRes.Error != nil { - gwConds = append(gwConds, conditions.NewGatewayNotProgrammedInvalid(conditions.GatewayMessageFailedNginxReload)) - } - - return GatewayStatus{ - Conditions: conditions.DeduplicateConditions(gwConds), - ListenerStatuses: listenerStatuses, - ObservedGeneration: gateway.Source.Generation, - } -}