Skip to content

Commit 4cbd4aa

Browse files
authored
Add GatewayClass Controller
1 parent 8462ae6 commit 4cbd4aa

File tree

8 files changed

+237
-53
lines changed

8 files changed

+237
-53
lines changed

build/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ COPY go.mod go.sum /go/src/github.com/nginxinc/nginx-gateway-kubernetes
99
RUN go mod download
1010

1111
COPY cmd /go/src/github.com/nginxinc/nginx-gateway-kubernetes/cmd
12+
COPY internal /go/src/github.com/nginxinc/nginx-gateway-kubernetes/internal
13+
COPY pkg /go/src/github.com/nginxinc/nginx-gateway-kubernetes/pkg
1214
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o gateway .
1315

1416
FROM scratch as common

cmd/gateway/main.go

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

33
import (
4-
"context"
54
"flag"
65
"fmt"
76
"os"
8-
"os/signal"
9-
"syscall"
10-
"time"
117

12-
"go.uber.org/zap"
13-
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"github.com/nginxinc/nginx-gateway-kubernetes/internal/implementation"
9+
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk"
1410
"k8s.io/client-go/rest"
15-
"sigs.k8s.io/gateway-api/pkg/client/clientset/gateway/versioned"
11+
"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"
1615
)
1716

1817
var (
@@ -33,58 +32,44 @@ func main() {
3332
os.Exit(1)
3433
}
3534

36-
logger, err := zap.NewProduction()
37-
if err != nil {
38-
fmt.Fprintf(os.Stderr, "Failed to initialize logging: %v\n", err)
39-
os.Exit(1)
40-
}
41-
sugar := logger.Sugar()
35+
logger := zap.New()
4236

43-
sugar.Infow("Starting NGINX Gateway",
37+
logger.Info("Starting NGINX Gateway",
4438
"version", version,
4539
"commit", commit,
4640
"date", date)
4741

4842
config, err := rest.InClusterConfig()
4943
if err != nil {
50-
sugar.Fatalw("Failed to create InClusterConfig",
51-
"error", err)
44+
logger.Error(err, "Failed to create InClusterConfig")
45+
os.Exit(1)
5246
}
5347

54-
gatewayClient, err := versioned.NewForConfig(config)
48+
mgr, err := manager.New(config, manager.Options{
49+
Logger: logger,
50+
})
5551
if err != nil {
56-
sugar.Fatalw("Failed to create a client for Gateway APIs",
57-
"error", err)
52+
logger.Error(err, "Failed to create Manager")
53+
os.Exit(1)
5854
}
5955

60-
gc, err := gatewayClient.GatewayV1alpha2().GatewayClasses().Get(context.TODO(), *gatewayClass, meta_v1.GetOptions{})
56+
err = v1alpha2.AddToScheme(mgr.GetScheme())
6157
if err != nil {
62-
sugar.Fatalw("Failed to get the GatewayClass",
63-
"name", *gatewayClass,
64-
"error", err)
58+
logger.Error(err, "Failed to add Gateway API scheme")
59+
os.Exit(1)
6560
}
6661

67-
if gc.Spec.ControllerName != "k8s-gateway.nginx.org/gateway" {
68-
sugar.Fatalw("Wrong ControllerName in the GatewayClass resource",
69-
"expected", "k8s-gateway.nginx.org/gateway",
70-
"got", "gc.Spec.ControllerName")
62+
err = sdk.RegisterGatewayClassController(mgr, implementation.NewGatewayClassImplementation(logger))
63+
if err != nil {
64+
logger.Error(err, "Failed to register GatewayClassController")
65+
os.Exit(1)
7166
}
7267

73-
sugar.Infow("Gateway class info",
74-
"name", gc.Name,
75-
"creation timestamp", gc.CreationTimestamp)
76-
77-
signalChan := make(chan os.Signal, 1)
78-
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
79-
go func() {
80-
sugar.Infow("Terminating because of the signal",
81-
"signal", <-signalChan)
68+
logger.Info("Starting manager")
8269

83-
os.Exit(0)
84-
}()
85-
86-
for {
87-
sugar.Infow("Gateway is running")
88-
time.Sleep(30 * time.Second)
70+
err = mgr.Start(signals.SetupSignalHandler())
71+
if err != nil {
72+
logger.Error(err, "Failed to start Manager")
73+
os.Exit(1)
8974
}
9075
}

deploy/manifests/nginx-gateway.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ rules:
1919
resources:
2020
- gatewayclasses
2121
verbs:
22-
- get
22+
- list
23+
- watch
2324
---
2425
kind: ClusterRoleBinding
2526
apiVersion: rbac.authorization.k8s.io/v1

go.mod

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,58 @@ module github.com/nginxinc/nginx-gateway-kubernetes
33
go 1.17
44

55
require (
6+
github.com/go-logr/logr v0.4.0
67
go.uber.org/zap v1.19.1
7-
k8s.io/apimachinery v0.22.1
8-
k8s.io/client-go v0.22.1
8+
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
9+
k8s.io/apimachinery v0.22.2
10+
k8s.io/client-go v0.22.2
11+
sigs.k8s.io/controller-runtime v0.10.2
912
sigs.k8s.io/gateway-api v0.4.0
1013
)
1114

1215
require (
16+
github.com/beorn7/perks v1.0.1 // indirect
17+
github.com/cespare/xxhash/v2 v2.1.1 // indirect
1318
github.com/davecgh/go-spew v1.1.1 // indirect
14-
github.com/go-logr/logr v0.4.0 // indirect
19+
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
20+
github.com/fsnotify/fsnotify v1.4.9 // indirect
21+
github.com/go-logr/zapr v0.4.0 // indirect
1522
github.com/gogo/protobuf v1.3.2 // indirect
23+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
1624
github.com/golang/protobuf v1.5.2 // indirect
1725
github.com/google/go-cmp v0.5.6 // indirect
1826
github.com/google/gofuzz v1.1.0 // indirect
27+
github.com/google/uuid v1.1.2 // indirect
1928
github.com/googleapis/gnostic v0.5.5 // indirect
29+
github.com/imdario/mergo v0.3.12 // indirect
2030
github.com/json-iterator/go v1.1.11 // indirect
31+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
2132
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2233
github.com/modern-go/reflect2 v1.0.1 // indirect
34+
github.com/pkg/errors v0.9.1 // indirect
35+
github.com/prometheus/client_golang v1.11.0 // indirect
36+
github.com/prometheus/client_model v0.2.0 // indirect
37+
github.com/prometheus/common v0.26.0 // indirect
38+
github.com/prometheus/procfs v0.6.0 // indirect
39+
github.com/spf13/pflag v1.0.5 // indirect
2340
go.uber.org/atomic v1.9.0 // indirect
2441
go.uber.org/multierr v1.7.0 // indirect
25-
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 // indirect
2642
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
27-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
43+
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2 // indirect
2844
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
2945
golang.org/x/text v0.3.6 // indirect
3046
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
47+
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
3148
google.golang.org/appengine v1.6.7 // indirect
3249
google.golang.org/protobuf v1.26.0 // indirect
3350
gopkg.in/inf.v0 v0.9.1 // indirect
3451
gopkg.in/yaml.v2 v2.4.0 // indirect
3552
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
36-
k8s.io/api v0.22.1 // indirect
53+
k8s.io/api v0.22.2 // indirect
54+
k8s.io/apiextensions-apiserver v0.22.2 // indirect
55+
k8s.io/component-base v0.22.2 // indirect
3756
k8s.io/klog/v2 v2.10.0 // indirect
57+
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
3858
k8s.io/utils v0.0.0-20210820185131-d34e5cb4466e // indirect
3959
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
4060
sigs.k8s.io/yaml v1.2.0 // indirect

0 commit comments

Comments
 (0)