Skip to content

Support Config Generation #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions internal/events/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@ import (
"fmt"

"github.com/go-logr/logr"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/nginx/config"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/state"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/status"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// EventLoop is the main event loop of the Gateway.
type EventLoop struct {
conf state.Configuration
serviceStore state.ServiceStore
generator config.Generator
eventCh <-chan interface{}
logger logr.Logger
statusUpdater status.Updater
}

// NewEventLoop creates a new EventLoop.
func NewEventLoop(conf state.Configuration, serviceStore state.ServiceStore, eventCh <-chan interface{},
statusUpdater status.Updater, logger logr.Logger) *EventLoop {
func NewEventLoop(
conf state.Configuration,
serviceStore state.ServiceStore,
generator config.Generator,
eventCh <-chan interface{},
statusUpdater status.Updater,
logger logr.Logger,
) *EventLoop {
return &EventLoop{
conf: conf,
serviceStore: serviceStore,
generator: generator,
eventCh: eventCh,
statusUpdater: statusUpdater,
logger: logger.WithName("eventLoop"),
Expand Down Expand Up @@ -114,38 +122,19 @@ func (el *EventLoop) processChangesAndStatusUpdates(ctx context.Context, changes
fmt.Printf("%+v\n", c)

if c.Op == state.Upsert {
// The code below resolves service backend refs into their cluster IPs
// TO-DO: this code will be removed once we have the component that generates NGINX config and
// uses the ServiceStore to resolve services.
for _, g := range c.Host.PathRouteGroups {
for _, r := range g.Routes {
for _, b := range r.Source.Spec.Rules[r.RuleIdx].BackendRefs {
if b.BackendRef.Kind == nil || *b.BackendRef.Kind == "Service" {
ns := r.Source.Namespace
if b.BackendRef.Namespace != nil {
ns = string(*b.BackendRef.Namespace)
}

address, err := el.serviceStore.Resolve(types.NamespacedName{
Namespace: ns,
Name: string(b.BackendRef.Name),
})

if err != nil {
fmt.Printf("Service %s/%s error: %v\n", ns, b.BackendRef.Name, err)
continue
}

var port int32 = 80
if b.BackendRef.Port != nil {
port = int32(*b.BackendRef.Port)
}

address = fmt.Sprintf("%s:%d", address, port)

fmt.Printf("Service %s/%s: %s\n", ns, b.BackendRef.Name, address)
}
}
cfg, warnings := el.generator.GenerateForHost(c.Host)
// TO-DO: for now, we only print the generated config, without writing it on the file system
// and reloading NGINX.
fmt.Println(string(cfg))

for obj, objWarnings := range warnings {
for _, w := range objWarnings {
// TO-DO: report warnings via Object status
el.logger.Info("got warning while generating config",
"kind", obj.GetObjectKind().GroupVersionKind().Kind,
"namespace", obj.GetNamespace(),
"name", obj.GetName(),
"warning", w)
}
}
}
Expand Down
36 changes: 27 additions & 9 deletions internal/events/loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/nginxinc/nginx-gateway-kubernetes/internal/events"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/nginx/config/configfakes"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/state"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/state/statefakes"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/status/statusfakes"
Expand Down Expand Up @@ -35,6 +36,7 @@ var _ = Describe("EventLoop", func() {
var fakeConf *statefakes.FakeConfiguration
var fakeUpdater *statusfakes.FakeUpdater
var fakeServiceStore *statefakes.FakeServiceStore
var fakeGenerator *configfakes.FakeGenerator
var cancel context.CancelFunc
var eventCh chan interface{}
var errorCh chan error
Expand All @@ -44,7 +46,8 @@ var _ = Describe("EventLoop", func() {
eventCh = make(chan interface{})
fakeUpdater = &statusfakes.FakeUpdater{}
fakeServiceStore = &statefakes.FakeServiceStore{}
ctrl = events.NewEventLoop(fakeConf, fakeServiceStore, eventCh, fakeUpdater, zap.New())
fakeGenerator = &configfakes.FakeGenerator{}
ctrl = events.NewEventLoop(fakeConf, fakeServiceStore, fakeGenerator, eventCh, fakeUpdater, zap.New())

var ctx context.Context

Expand All @@ -66,15 +69,19 @@ var _ = Describe("EventLoop", func() {
})

It("should process upsert event", func() {
fakeChanges := []state.Change{
{
Op: state.Upsert,
Host: state.Host{},
},
}
fakeStatusUpdates := []state.StatusUpdate{
{
NamespacedName: types.NamespacedName{},
Status: nil,
},
}
// for now, we pass nil, because we don't need to test how EventLoop processes changes yet. We will start
// testing once we have NGINX Configuration Manager component.
fakeConf.UpsertHTTPRouteReturns(nil, fakeStatusUpdates)
fakeConf.UpsertHTTPRouteReturns(fakeChanges, fakeStatusUpdates)

hr := &v1alpha2.HTTPRoute{}

Expand All @@ -87,23 +94,30 @@ var _ = Describe("EventLoop", func() {
return fakeConf.UpsertHTTPRouteArgsForCall(0)
}).Should(Equal(hr))

Eventually(fakeUpdater.ProcessStatusUpdatesCallCount()).Should(Equal(1))
Eventually(fakeUpdater.ProcessStatusUpdatesCallCount).Should(Equal(1))
Eventually(func() []state.StatusUpdate {
_, updates := fakeUpdater.ProcessStatusUpdatesArgsForCall(0)
return updates
}).Should(Equal(fakeStatusUpdates))

Eventually(fakeGenerator.GenerateForHostCallCount).Should(Equal(1))
Expect(fakeGenerator.GenerateForHostArgsForCall(0)).Should(Equal(fakeChanges[0].Host))
})

It("should process delete event", func() {
fakeChanges := []state.Change{
{
Op: state.Delete,
Host: state.Host{},
},
}
fakeStatusUpdates := []state.StatusUpdate{
{
NamespacedName: types.NamespacedName{},
Status: nil,
},
}
// for now, we pass nil, because we don't need to test how EventLoop processes changes yet. We will start
// testing once we have NGINX Configuration Manager component.
fakeConf.DeleteHTTPRouteReturns(nil, fakeStatusUpdates)
fakeConf.DeleteHTTPRouteReturns(fakeChanges, fakeStatusUpdates)

nsname := types.NamespacedName{Namespace: "test", Name: "route"}

Expand All @@ -117,11 +131,15 @@ var _ = Describe("EventLoop", func() {
return fakeConf.DeleteHTTPRouteArgsForCall(0)
}).Should(Equal(nsname))

Eventually(fakeUpdater.ProcessStatusUpdatesCallCount()).Should(Equal(1))
Eventually(fakeUpdater.ProcessStatusUpdatesCallCount).Should(Equal(1))
Eventually(func() []state.StatusUpdate {
_, updates := fakeUpdater.ProcessStatusUpdatesArgsForCall(0)
return updates
}).Should(Equal(fakeStatusUpdates))

// TO-DO:
// once we have a component that processes host deletion, ensure that
// its corresponding method is eventually called
})
})

Expand Down
10 changes: 10 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ func Diff(x, y interface{}) string {
}
return r
}

// GetStringPointer takes a string and returns a pointer to it. Useful in unit tests when initializing structs.
func GetStringPointer(s string) *string {
return &s
}

// GetInt32Pointer takes an int32 and returns a pointer to it. Useful in unit tests when initializing structs.
func GetInt32Pointer(i int32) *int32 {
return &i
}
4 changes: 3 additions & 1 deletion internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
gcfg "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/gatewayconfig"
hr "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/httproute"
svc "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/service"
ngxcfg "github.com/nginxinc/nginx-gateway-kubernetes/internal/nginx/config"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/state"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/status"
nginxgwv1alpha1 "github.com/nginxinc/nginx-gateway-kubernetes/pkg/apis/gateway/v1alpha1"
Expand Down Expand Up @@ -76,7 +77,8 @@ func Start(cfg config.Config) error {
conf := state.NewConfiguration(cfg.GatewayCtlrName, state.NewRealClock())
serviceStore := state.NewServiceStore()
reporter := status.NewUpdater(mgr.GetClient(), cfg.Logger)
eventLoop := events.NewEventLoop(conf, serviceStore, eventCh, reporter, cfg.Logger)
configGenerator := ngxcfg.NewGeneratorImpl(serviceStore)
eventLoop := events.NewEventLoop(conf, serviceStore, configGenerator, eventCh, reporter, cfg.Logger)

err = mgr.Add(eventLoop)
if err != nil {
Expand Down
117 changes: 117 additions & 0 deletions internal/nginx/config/configfakes/fake_generator.go

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

Loading