Skip to content

Commit c0b9f3a

Browse files
committed
Adding Gateway Controller
First basic version of gateway controller. Additional changes: - normalized gateway and gatewayclass implementations - added a config object to pass into implementations - added controller package to isolate Mgr startup - added Vim ignores - change flag to pflag for future use - added gateway yaml
1 parent 4cbd4aa commit c0b9f3a

File tree

15 files changed

+251
-81
lines changed

15 files changed

+251
-81
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616

17+
# Vim
18+
*.swp
19+
*.swo
20+
1721
# GoLand IDE
1822
.idea
1923

cmd/gateway/main.go

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
64
"os"
75

8-
"github.com/nginxinc/nginx-gateway-kubernetes/internal/implementation"
9-
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"
10-
"k8s.io/client-go/rest"
6+
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
7+
"github.com/nginxinc/nginx-gateway-kubernetes/internal/controller"
8+
9+
flag "github.com/spf13/pflag"
1110
"sigs.k8s.io/controller-runtime/pkg/log/zap"
12-
"sigs.k8s.io/controller-runtime/pkg/manager"
13-
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
14-
"sigs.k8s.io/gateway-api/apis/v1alpha2"
1511
)
1612

1713
var (
@@ -21,55 +17,31 @@ var (
2117
date string
2218

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

2723
func main() {
2824
flag.Parse()
2925

30-
if *gatewayClass == "" {
31-
fmt.Fprintln(os.Stderr, "-gatewayclass argument must be set")
26+
if *gatewayCtlrName == "" {
27+
flag.PrintDefaults()
3228
os.Exit(1)
3329
}
3430

3531
logger := zap.New()
32+
conf := config.Config{
33+
GatewayCtlrName: *gatewayCtlrName,
34+
Logger: logger,
35+
}
3636

3737
logger.Info("Starting NGINX Gateway",
3838
"version", version,
3939
"commit", commit,
4040
"date", date)
4141

42-
config, err := rest.InClusterConfig()
43-
if err != nil {
44-
logger.Error(err, "Failed to create InClusterConfig")
45-
os.Exit(1)
46-
}
47-
48-
mgr, err := manager.New(config, manager.Options{
49-
Logger: logger,
50-
})
51-
if err != nil {
52-
logger.Error(err, "Failed to create Manager")
53-
os.Exit(1)
54-
}
55-
56-
err = v1alpha2.AddToScheme(mgr.GetScheme())
57-
if err != nil {
58-
logger.Error(err, "Failed to add Gateway API scheme")
59-
os.Exit(1)
60-
}
61-
62-
err = sdk.RegisterGatewayClassController(mgr, implementation.NewGatewayClassImplementation(logger))
63-
if err != nil {
64-
logger.Error(err, "Failed to register GatewayClassController")
65-
os.Exit(1)
66-
}
67-
68-
logger.Info("Starting manager")
69-
70-
err = mgr.Start(signals.SetupSignalHandler())
42+
err := controller.Start(conf)
7143
if err != nil {
72-
logger.Error(err, "Failed to start Manager")
44+
logger.Error(err, "Failed to start control loop")
7345
os.Exit(1)
7446
}
7547
}

deploy/manifests/gateway.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: gateway.networking.k8s.io/v1alpha2
2+
kind: Gateway
3+
metadata:
4+
name: gateway
5+
namespace: nginx-gateway
6+
labels:
7+
domain: k8s-gateway.nginx.org
8+
spec:
9+
gatewayClassName: nginx
10+
listeners:
11+
- name: my-listener
12+
hostname: example.com
13+
port: 8080
14+
protocol: HTTP

deploy/manifests/gatewayclass.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ kind: GatewayClass
33
metadata:
44
name: nginx
55
spec:
6-
controllerName: k8s-gateway.nginx.org/gateway
6+
controllerName: k8s-gateway.nginx.org/nginx-gateway/gateway

deploy/manifests/nginx-gateway.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rules:
1818
- gateway.networking.k8s.io
1919
resources:
2020
- gatewayclasses
21+
- gateways
2122
verbs:
2223
- list
2324
- watch
@@ -66,7 +67,7 @@ spec:
6667
imagePullPolicy: IfNotPresent
6768
name: nginx-gateway
6869
args:
69-
- -gatewayclass=nginx
70+
- --gateway-ctlr-name=k8s-gateway.nginx.org/nginx-gateway/gateway
7071
- image: nginx:1.21.3
7172
imagePullPolicy: IfNotPresent
7273
name: nginx

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ go 1.17
44

55
require (
66
github.com/go-logr/logr v0.4.0
7-
go.uber.org/zap v1.19.1
87
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
98
k8s.io/apimachinery v0.22.2
10-
k8s.io/client-go v0.22.2
119
sigs.k8s.io/controller-runtime v0.10.2
1210
sigs.k8s.io/gateway-api v0.4.0
1311
)
@@ -39,8 +37,9 @@ require (
3937
github.com/spf13/pflag v1.0.5 // indirect
4038
go.uber.org/atomic v1.9.0 // indirect
4139
go.uber.org/multierr v1.7.0 // indirect
40+
go.uber.org/zap v1.19.1 // indirect
4241
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
43-
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2 // indirect
42+
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
4443
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
4544
golang.org/x/text v0.3.6 // indirect
4645
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
@@ -52,6 +51,7 @@ require (
5251
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
5352
k8s.io/api v0.22.2 // indirect
5453
k8s.io/apiextensions-apiserver v0.22.2 // indirect
54+
k8s.io/client-go v0.22.2 // indirect
5555
k8s.io/component-base v0.22.2 // indirect
5656
k8s.io/klog/v2 v2.10.0 // indirect
5757
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
8383
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
8484
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
8585
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
86+
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
8687
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
8788
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
8889
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
@@ -716,8 +717,9 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
716717
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
717718
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
718719
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
719-
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2 h1:c8PlLMqBbOHoqtjteWm5/kbe6rNY2pbRfbIMVnepueo=
720720
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
721+
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
722+
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
721723
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
722724
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
723725
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
)
6+
7+
type Config struct {
8+
GatewayCtlrName string
9+
Logger logr.Logger
10+
}

internal/controller/controller.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
7+
gw "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/gateway"
8+
gc "github.com/nginxinc/nginx-gateway-kubernetes/internal/implementations/gatewayclass"
9+
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"
10+
11+
"k8s.io/apimachinery/pkg/runtime"
12+
ctlr "sigs.k8s.io/controller-runtime"
13+
"sigs.k8s.io/controller-runtime/pkg/manager"
14+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
15+
)
16+
17+
var (
18+
scheme = runtime.NewScheme()
19+
)
20+
21+
func init() {
22+
_ = gatewayv1alpha2.AddToScheme(scheme)
23+
}
24+
25+
func Start(conf config.Config) error {
26+
logger := conf.Logger
27+
28+
options := manager.Options{
29+
Scheme: scheme,
30+
}
31+
32+
mgr, err := manager.New(ctlr.GetConfigOrDie(), options)
33+
if err != nil {
34+
return fmt.Errorf("cannot build runtime manager: %w", err)
35+
}
36+
37+
err = sdk.RegisterGatewayController(mgr, gw.NewGatewayImplementation(conf))
38+
if err != nil {
39+
return fmt.Errorf("cannot register gateway implementation: %w", err)
40+
}
41+
err = sdk.RegisterGatewayClassController(mgr, gc.NewGatewayClassImplementation(conf))
42+
if err != nil {
43+
return fmt.Errorf("cannot reguster gatewayclass implementation: %w", err)
44+
}
45+
46+
ctx := ctlr.SetupSignalHandler()
47+
48+
logger.Info("Starting manager")
49+
return mgr.Start(ctx)
50+
}

internal/implementation/gatewayclass.go

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package implementation
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
6+
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"
7+
8+
"sigs.k8s.io/gateway-api/apis/v1alpha2"
9+
)
10+
11+
type gatewayImplementation struct {
12+
conf config.Config
13+
}
14+
15+
func NewGatewayImplementation(conf config.Config) sdk.GatewayImpl {
16+
return &gatewayImplementation{
17+
conf: conf,
18+
}
19+
}
20+
21+
func (impl *gatewayImplementation) Logger() logr.Logger {
22+
return impl.conf.Logger
23+
}
24+
25+
func (impl *gatewayImplementation) ControllerName() string {
26+
return impl.conf.GatewayCtlrName
27+
}
28+
29+
func (impl *gatewayImplementation) Upsert(gw *v1alpha2.Gateway) {
30+
if gw.Name == impl.ControllerName() {
31+
impl.Logger().Info("Found correct Gateway resource",
32+
"name", gw.Name,
33+
)
34+
return
35+
}
36+
}
37+
38+
func (impl *gatewayImplementation) Remove(key string) {
39+
impl.Logger().Info("Gateway resource was removed",
40+
"name", key,
41+
)
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package implementation
2+
3+
import (
4+
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config"
5+
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"
6+
7+
"github.com/go-logr/logr"
8+
"sigs.k8s.io/gateway-api/apis/v1alpha2"
9+
)
10+
11+
type gatewayClassImplementation struct {
12+
conf config.Config
13+
}
14+
15+
func NewGatewayClassImplementation(conf config.Config) sdk.GatewayClassImpl {
16+
return &gatewayClassImplementation{
17+
conf: conf,
18+
}
19+
}
20+
21+
func (impl *gatewayClassImplementation) Logger() logr.Logger {
22+
return impl.conf.Logger
23+
}
24+
25+
func (impl *gatewayClassImplementation) ControllerName() string {
26+
return impl.conf.GatewayCtlrName
27+
}
28+
29+
func (impl *gatewayClassImplementation) Upsert(gc *v1alpha2.GatewayClass) {
30+
if string(gc.Spec.ControllerName) != impl.ControllerName() {
31+
impl.Logger().Info("Wrong ControllerName in the GatewayClass resource",
32+
"expected", impl.ControllerName(),
33+
"got", gc.Spec.ControllerName)
34+
return
35+
}
36+
37+
impl.Logger().Info("Processing GatewayClass resource",
38+
"name", gc.Name)
39+
}
40+
func (impl *gatewayClassImplementation) Remove(key string) {
41+
impl.Logger().Info("GatewayClass resource was removed",
42+
"name", key)
43+
}

0 commit comments

Comments
 (0)