Skip to content

Commit

Permalink
Refactor internal package organization (#839)
Browse files Browse the repository at this point in the history
Problem: After the merge of the provisioner-mode code into 
the main branch, our codebase now consists of three distinct 
components or groups: provisioner-mode, static-mode, and framework. 
However, our current internal package structure does not align with 
these components.

Solution: Align the internal package structure with our three main 
components by introducing the following internal packages:
- framework: code shared between provisioner and static modes
- mode/provisioner: all code related to provisioner-mode
- mode/static: all code related to static mode

In addition, some packages were refactored/split so the framework
package would not have any dependencies on the mode/static package.
  • Loading branch information
kate-osborn authored Jul 11, 2023
1 parent d650268 commit e065469
Show file tree
Hide file tree
Showing 168 changed files with 915 additions and 828 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ dist/
node_modules/

# JS test coverage
internal/nginx/modules/coverage
internal/mode/static/nginx/modules/coverage
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
8 changes: 4 additions & 4 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion conformance/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
102 changes: 0 additions & 102 deletions internal/events/handler.go

This file was deleted.

48 changes: 48 additions & 0 deletions internal/framework/conditions/conditions.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Reconciler Suite")
RunSpecs(t, "Controller Suite")
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions internal/framework/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package framework contains all the packages that are shared by the provisioner and static modes of the project.
*/
package framework
File renamed without changes.
File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions internal/framework/events/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
File renamed without changes.
Loading

0 comments on commit e065469

Please sign in to comment.