Skip to content

Adding Gateway Controller #5

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 1 commit into from
Nov 12, 2021
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
# Dependency directories (remove the comment below to include it)
# vendor/

# Vim
*.swp
*.swo

# GoLand IDE
.idea

Expand Down
54 changes: 13 additions & 41 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/nginxinc/nginx-gateway-kubernetes/internal/implementation"
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"
"k8s.io/client-go/rest"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/controller"

flag "github.com/spf13/pflag"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
)

var (
Expand All @@ -21,55 +17,31 @@ var (
date string

// Command-line flags
gatewayClass = flag.String("gatewayclass", "", "Tha name of the GatewayClass resource")
gatewayCtlrName = flag.String("gateway-ctlr-name", "", "The name of the Gateway controller")
)

func main() {
flag.Parse()

if *gatewayClass == "" {
fmt.Fprintln(os.Stderr, "-gatewayclass argument must be set")
if *gatewayCtlrName == "" {
flag.PrintDefaults()
os.Exit(1)
}

logger := zap.New()
conf := config.Config{
GatewayCtlrName: *gatewayCtlrName,
Logger: logger,
}

logger.Info("Starting NGINX Gateway",
"version", version,
"commit", commit,
"date", date)

config, err := rest.InClusterConfig()
if err != nil {
logger.Error(err, "Failed to create InClusterConfig")
os.Exit(1)
}

mgr, err := manager.New(config, manager.Options{
Logger: logger,
})
if err != nil {
logger.Error(err, "Failed to create Manager")
os.Exit(1)
}

err = v1alpha2.AddToScheme(mgr.GetScheme())
if err != nil {
logger.Error(err, "Failed to add Gateway API scheme")
os.Exit(1)
}

err = sdk.RegisterGatewayClassController(mgr, implementation.NewGatewayClassImplementation(logger))
if err != nil {
logger.Error(err, "Failed to register GatewayClassController")
os.Exit(1)
}

logger.Info("Starting manager")

err = mgr.Start(signals.SetupSignalHandler())
err := controller.Start(conf)
if err != nil {
logger.Error(err, "Failed to start Manager")
logger.Error(err, "Failed to start control loop")
os.Exit(1)
}
}
14 changes: 14 additions & 0 deletions deploy/manifests/gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: Gateway
metadata:
name: gateway
namespace: nginx-gateway
labels:
domain: k8s-gateway.nginx.org
spec:
gatewayClassName: nginx
listeners:
- name: my-listener
hostname: example.com
port: 8080
protocol: HTTP
2 changes: 1 addition & 1 deletion deploy/manifests/gatewayclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: GatewayClass
metadata:
name: nginx
spec:
controllerName: k8s-gateway.nginx.org/gateway
controllerName: k8s-gateway.nginx.org/nginx-gateway/gateway
3 changes: 2 additions & 1 deletion deploy/manifests/nginx-gateway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rules:
- gateway.networking.k8s.io
resources:
- gatewayclasses
- gateways
verbs:
- list
- watch
Expand Down Expand Up @@ -66,7 +67,7 @@ spec:
imagePullPolicy: IfNotPresent
name: nginx-gateway
args:
- -gatewayclass=nginx
- --gateway-ctlr-name=k8s-gateway.nginx.org/nginx-gateway/gateway
- image: nginx:1.21.3
imagePullPolicy: IfNotPresent
name: nginx
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ go 1.17

require (
github.com/go-logr/logr v0.4.0
go.uber.org/zap v1.19.1
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
sigs.k8s.io/controller-runtime v0.10.2
sigs.k8s.io/gateway-api v0.4.0
)
Expand Down Expand Up @@ -39,8 +37,9 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2 // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
Expand All @@ -52,6 +51,7 @@ require (
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.22.2 // indirect
k8s.io/apiextensions-apiserver v0.22.2 // indirect
k8s.io/client-go v0.22.2 // indirect
k8s.io/component-base v0.22.2 // indirect
k8s.io/klog/v2 v2.10.0 // indirect
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -716,8 +717,9 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2 h1:c8PlLMqBbOHoqtjteWm5/kbe6rNY2pbRfbIMVnepueo=
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

import (
"github.com/go-logr/logr"
)

type Config struct {
GatewayCtlrName string
Logger logr.Logger
}
50 changes: 50 additions & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controller

import (
"fmt"

"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
gw "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/gateway"
gc "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/gatewayclass"
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"

"k8s.io/apimachinery/pkg/runtime"
ctlr "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

var (
scheme = runtime.NewScheme()
)

func init() {
_ = gatewayv1alpha2.AddToScheme(scheme)
}

func Start(conf config.Config) error {
logger := conf.Logger

options := manager.Options{
Scheme: scheme,
}

mgr, err := manager.New(ctlr.GetConfigOrDie(), options)
if err != nil {
return fmt.Errorf("cannot build runtime manager: %w", err)
}

err = sdk.RegisterGatewayController(mgr, gw.NewGatewayImplementation(conf))
if err != nil {
return fmt.Errorf("cannot register gateway implementation: %w", err)
}
err = sdk.RegisterGatewayClassController(mgr, gc.NewGatewayClassImplementation(conf))
if err != nil {
return fmt.Errorf("cannot reguster gatewayclass implementation: %w", err)
}

ctx := ctlr.SetupSignalHandler()

logger.Info("Starting manager")
return mgr.Start(ctx)
}
31 changes: 0 additions & 31 deletions internal/implementation/gatewayclass.go

This file was deleted.

42 changes: 42 additions & 0 deletions internal/implementations/gateway/gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package implementation

import (
"github.com/go-logr/logr"
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"

"sigs.k8s.io/gateway-api/apis/v1alpha2"
)

type gatewayImplementation struct {
conf config.Config
}

func NewGatewayImplementation(conf config.Config) sdk.GatewayImpl {
return &gatewayImplementation{
conf: conf,
}
}

func (impl *gatewayImplementation) Logger() logr.Logger {
return impl.conf.Logger
}

func (impl *gatewayImplementation) ControllerName() string {
return impl.conf.GatewayCtlrName
}

func (impl *gatewayImplementation) Upsert(gw *v1alpha2.Gateway) {
if gw.Name == impl.ControllerName() {
impl.Logger().Info("Found correct Gateway resource",
"name", gw.Name,
)
return
}
}

func (impl *gatewayImplementation) Remove(key string) {
impl.Logger().Info("Gateway resource was removed",
"name", key,
)
}
43 changes: 43 additions & 0 deletions internal/implementations/gatewayclass/gatewayclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package implementation

import (
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"

"github.com/go-logr/logr"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
)

type gatewayClassImplementation struct {
conf config.Config
}

func NewGatewayClassImplementation(conf config.Config) sdk.GatewayClassImpl {
return &gatewayClassImplementation{
conf: conf,
}
}

func (impl *gatewayClassImplementation) Logger() logr.Logger {
return impl.conf.Logger
}

func (impl *gatewayClassImplementation) ControllerName() string {
return impl.conf.GatewayCtlrName
}

func (impl *gatewayClassImplementation) Upsert(gc *v1alpha2.GatewayClass) {
if string(gc.Spec.ControllerName) != impl.ControllerName() {
impl.Logger().Info("Wrong ControllerName in the GatewayClass resource",
"expected", impl.ControllerName(),
"got", gc.Spec.ControllerName)
return
}

impl.Logger().Info("Processing GatewayClass resource",
"name", gc.Name)
}
func (impl *gatewayClassImplementation) Remove(key string) {
impl.Logger().Info("GatewayClass resource was removed",
"name", key)
}
Loading