Skip to content

Commit b1b39cf

Browse files
authored
feat: support gateway/gatewayclass (#42)
1 parent 70361c2 commit b1b39cf

File tree

38 files changed

+1892
-532
lines changed

38 files changed

+1892
-532
lines changed

.github/workflows/e2e-test.yml

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ jobs:
2828
with:
2929
go-version: "1.22"
3030

31-
- name: Build images
32-
env:
33-
TAG: dev
34-
ARCH: amd64
35-
ENABLE_PROXY: "false"
36-
BASE_IMAGE_TAG: "debug"
37-
USE_BUILDKIT: 0
38-
run: |
39-
echo "building images..."
40-
make docker-build
41-
4231
- name: Install kind
4332
run: |
4433
go install sigs.k8s.io/[email protected]
@@ -65,9 +54,31 @@ jobs:
6554
username: ${{ secrets.PRIVATE_DOCKER_USERNAME }}
6655
password: ${{ secrets.PRIVATE_DOCKER_PASSWORD }}
6756

57+
- name: Build images
58+
env:
59+
TAG: dev
60+
ARCH: amd64
61+
ENABLE_PROXY: "false"
62+
BASE_IMAGE_TAG: "debug"
63+
run: |
64+
echo "building images..."
65+
make build-image
66+
67+
- name: Launch Kind Cluster
68+
run: |
69+
make kind-up
70+
71+
- name: Install Gateway API
72+
run: |
73+
make install-gateway-api
74+
75+
- name: Loading Docker Image to Kind Cluster
76+
run: |
77+
make kind-load-images
78+
6879
- name: Run E2E test suite
6980
shell: bash
7081
env:
7182
API7_EE_LICENSE: ${{ secrets.API7_EE_LICENSE }}
7283
run: |
73-
make kind-e2e-test
84+
make e2e-test

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ go.work
2525
*.swp
2626
*.swo
2727
*~
28+
29+
dist
30+
.tmp
31+
api7-ingress-controller

Dockerfile

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
1-
# Build the manager binary
2-
FROM golang:1.22 AS builder
3-
ARG TARGETOS
4-
ARG TARGETARCH
1+
ARG ENABLE_PROXY=false
2+
ARG BASE_IMAGE_TAG=nonroot
53

4+
FROM golang:1.22 AS builder
65
WORKDIR /workspace
7-
# Copy the Go Modules manifests
8-
COPY go.mod go.mod
9-
COPY go.sum go.sum
10-
# cache deps before building and copying source so that we don't need to re-download as much
11-
# and so that source changes don't invalidate our downloaded layer
12-
RUN go mod download
6+
COPY go.* ./
7+
8+
RUN if [ "$ENABLE_PROXY" = "true" ] ; then go env -w GOPROXY=https://goproxy.cn,direct ; fi \
9+
&& go mod download
10+
11+
COPY . .
1312

14-
# Copy the go source
15-
COPY cmd/main.go cmd/main.go
16-
COPY api/ api/
17-
COPY internal/controller/ internal/controller/
13+
RUN --mount=type=cache,target=/root/.cache/go-build make build && mv bin/api7-ingress-controller /bin && rm -rf /workspace
1814

19-
# Build
20-
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21-
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22-
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23-
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
15+
FROM gcr.io/distroless/static-debian12:${BASE_IMAGE_TAG}
16+
WORKDIR /app
2517

26-
# Use distroless as minimal base image to package the manager binary
27-
# Refer to https://github.com/GoogleContainerTools/distroless for more details
28-
FROM gcr.io/distroless/static:nonroot
29-
WORKDIR /
30-
COPY --from=builder /workspace/manager .
31-
USER 65532:65532
18+
COPY --from=builder /bin/api7-ingress-controller .
19+
COPY conf/config.yaml ./conf/config.yaml
3220

33-
ENTRYPOINT ["/manager"]
21+
ENTRYPOINT ["/app/api7-ingress-controller"]
22+
CMD ["-c", "/app/conf/config.yaml"]

Makefile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Image URL to use all building/pushing image targets
2-
IMG ?= controller:latest
2+
33
VERSION ?= 2.0.0
4+
5+
6+
IMAGE_TAG ?= dev
7+
8+
IMG ?= api7/api7-ingress-controller:$(IMAGE_TAG)
49
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
510
ENVTEST_K8S_VERSION = 1.30.0
611

@@ -9,6 +14,12 @@ GATEAY_API_VERSION ?= v1.1.0
914

1015
DASHBOARD_VERSION ?= v3.2.14.2
1116

17+
# go
18+
VERSYM="github.com/api7/api7-ingress-controller/internal/version._buildVersion"
19+
GITSHASYM="github.com/api7/api7-ingress-controller/internal/version._buildGitRevision"
20+
BUILDOSSYM="github.com/api7/api7-ingress-controller/internal/version._buildOS"
21+
GO_LDFLAGS ?= "-X=$(VERSYM)=$(VERSION) -X=$(GITSHASYM)=$(GITSHA) -X=$(BUILDOSSYM)=$(OSNAME)/$(OSARCH)"
22+
1223
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
1324
ifeq (,$(shell go env GOBIN))
1425
GOBIN=$(shell go env GOPATH)/bin
@@ -70,7 +81,7 @@ test: manifests generate fmt vet envtest ## Run tests.
7081
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
7182

7283
.PHONY: kind-e2e-test
73-
kind-e2e-test: kind-up kind-load-images e2e-test
84+
kind-e2e-test: kind-up build-image kind-load-images e2e-test
7485

7586
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
7687
.PHONY: e2e-test
@@ -104,7 +115,9 @@ kind-load-images: pull-infra-images
104115
@kind load docker-image hkccr.ccs.tencentyun.com/api7-dev/api7-ee-dp-manager:$(DASHBOARD_VERSION) --name $(KIND_NAME)
105116
@kind load docker-image hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-integrated:$(DASHBOARD_VERSION) --name $(KIND_NAME)
106117
@kind load docker-image kennethreitz/httpbin:latest --name $(KIND_NAME)
118+
@kind load docker-image $(IMG) --name $(KIND_NAME)
107119

120+
.PHONY: pull-infra-images
108121
pull-infra-images:
109122
@docker pull hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-gateway:dev
110123
@docker pull hkccr.ccs.tencentyun.com/api7-dev/api7-ee-dp-manager:$(DASHBOARD_VERSION)
@@ -115,7 +128,11 @@ pull-infra-images:
115128

116129
.PHONY: build
117130
build: manifests generate fmt vet ## Build manager binary.
118-
go build -o bin/manager cmd/main.go
131+
CGO_ENABLED=0 go build -o bin/api7-ingress-controller -ldflags $(GO_LDFLAGS) cmd/main.go
132+
133+
134+
.PHONY: build-image
135+
build-image: docker-build
119136

120137
.PHONY: run
121138
run: manifests generate fmt vet ## Run a controller from your host.

cmd/main.go

Lines changed: 3 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -17,154 +17,10 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"crypto/tls"
21-
"flag"
22-
"os"
23-
24-
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
25-
// to ensure that exec-entrypoint and run can make use of them.
26-
_ "k8s.io/client-go/plugin/pkg/client/auth"
27-
28-
"k8s.io/apimachinery/pkg/runtime"
29-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
30-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
31-
ctrl "sigs.k8s.io/controller-runtime"
32-
"sigs.k8s.io/controller-runtime/pkg/healthz"
33-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34-
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
35-
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
36-
"sigs.k8s.io/controller-runtime/pkg/webhook"
37-
38-
gatewayapisixiov1alpha1 "github.com/api7/api7-ingress-controller/api/v1alpha1"
39-
"github.com/api7/api7-ingress-controller/internal/controller"
40-
// +kubebuilder:scaffold:imports
41-
)
42-
43-
var (
44-
scheme = runtime.NewScheme()
45-
setupLog = ctrl.Log.WithName("setup")
20+
"github.com/api7/api7-ingress-controller/cmd/root"
21+
"github.com/spf13/cobra"
4622
)
4723

48-
func init() {
49-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
50-
51-
utilruntime.Must(gatewayapisixiov1alpha1.AddToScheme(scheme))
52-
// +kubebuilder:scaffold:scheme
53-
}
54-
5524
func main() {
56-
var metricsAddr string
57-
var enableLeaderElection bool
58-
var probeAddr string
59-
var secureMetrics bool
60-
var enableHTTP2 bool
61-
var tlsOpts []func(*tls.Config)
62-
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
63-
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
64-
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
65-
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
66-
"Enable leader election for controller manager. "+
67-
"Enabling this will ensure there is only one active controller manager.")
68-
flag.BoolVar(&secureMetrics, "metrics-secure", true,
69-
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
70-
flag.BoolVar(&enableHTTP2, "enable-http2", false,
71-
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
72-
opts := zap.Options{
73-
Development: true,
74-
}
75-
opts.BindFlags(flag.CommandLine)
76-
flag.Parse()
77-
78-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
79-
80-
// if the enable-http2 flag is false (the default), http/2 should be disabled
81-
// due to its vulnerabilities. More specifically, disabling http/2 will
82-
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
83-
// Rapid Reset CVEs. For more information see:
84-
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
85-
// - https://github.com/advisories/GHSA-4374-p667-p6c8
86-
disableHTTP2 := func(c *tls.Config) {
87-
setupLog.Info("disabling http/2")
88-
c.NextProtos = []string{"http/1.1"}
89-
}
90-
91-
if !enableHTTP2 {
92-
tlsOpts = append(tlsOpts, disableHTTP2)
93-
}
94-
95-
webhookServer := webhook.NewServer(webhook.Options{
96-
TLSOpts: tlsOpts,
97-
})
98-
99-
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
100-
// More info:
101-
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server
102-
// - https://book.kubebuilder.io/reference/metrics.html
103-
metricsServerOptions := metricsserver.Options{
104-
BindAddress: metricsAddr,
105-
SecureServing: secureMetrics,
106-
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
107-
// not provided, self-signed certificates will be generated by default. This option is not recommended for
108-
// production environments as self-signed certificates do not offer the same level of trust and security
109-
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
110-
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
111-
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
112-
TLSOpts: tlsOpts,
113-
}
114-
115-
if secureMetrics {
116-
// FilterProvider is used to protect the metrics endpoint with authn/authz.
117-
// These configurations ensure that only authorized users and service accounts
118-
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
119-
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
120-
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
121-
}
122-
123-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
124-
Scheme: scheme,
125-
Metrics: metricsServerOptions,
126-
WebhookServer: webhookServer,
127-
HealthProbeBindAddress: probeAddr,
128-
LeaderElection: enableLeaderElection,
129-
LeaderElectionID: "b2fc8523.github.com",
130-
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
131-
// when the Manager ends. This requires the binary to immediately end when the
132-
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
133-
// speeds up voluntary leader transitions as the new leader don't have to wait
134-
// LeaseDuration time first.
135-
//
136-
// In the default scaffold provided, the program ends immediately after
137-
// the manager stops, so would be fine to enable this option. However,
138-
// if you are doing or is intended to do any operation such as perform cleanups
139-
// after the manager stops then its usage might be unsafe.
140-
// LeaderElectionReleaseOnCancel: true,
141-
})
142-
if err != nil {
143-
setupLog.Error(err, "unable to start manager")
144-
os.Exit(1)
145-
}
146-
147-
if err = (&controller.GuestbookReconciler{
148-
Client: mgr.GetClient(),
149-
Scheme: mgr.GetScheme(),
150-
}).SetupWithManager(mgr); err != nil {
151-
setupLog.Error(err, "unable to create controller", "controller", "Guestbook")
152-
os.Exit(1)
153-
}
154-
// +kubebuilder:scaffold:builder
155-
156-
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
157-
setupLog.Error(err, "unable to set up health check")
158-
os.Exit(1)
159-
}
160-
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
161-
setupLog.Error(err, "unable to set up ready check")
162-
os.Exit(1)
163-
}
164-
165-
setupLog.Info("starting manager")
166-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
167-
setupLog.Error(err, "problem running manager")
168-
os.Exit(1)
169-
}
25+
cobra.CheckErr(root.NewRootCmd().Execute())
17026
}

0 commit comments

Comments
 (0)