Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
core: add possibility to route traffic to service running in differen…
Browse files Browse the repository at this point in the history
…t namespace
  • Loading branch information
h3adex committed Dec 9, 2023
1 parent fcf7fac commit 4be7cfb
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 64 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test-go-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ jobs:
with:
go-version: '1.21.5'

- name: Setup Kubernetes hosts for tests
run: |
sudo echo "127.0.0.1 127.0.0.1.default.svc.cluster.local 127.0.0.1.test.svc.cluster.local" | sudo tee -a /etc/hosts
- name: Install Dependencies
run: go get -v -t -d ./...

Expand Down
15 changes: 15 additions & 0 deletions build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ kubectl edit ingress whoami
This will allow you to modify and test various annotations associated
with the whoami ingress.

### Go Tests
Before running any Go tests, ensure you've added specific entries to your /etc/hosts file:
```txt
127.0.0.1 localhost 127.0.0.1.default.svc.cluster.local 127.0.0.1.test.svc.cluster.local
```
These entries are necessary for server_test.go to function correctly.
Regrettably, this approach is the current requirement to mimic a
Kubernetes environment for our tests. If you have alternative solutions,
I'm open to suggestions.

To execute the Go tests, utilize the following command:
```sh
go test -cover ./pkg/...
```

### Test HPA Functionality
To test the [HPA](../k8s/guardgress-deployment-hpa.yaml) functionality, use the following command:
```sh
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ require (
github.com/caarlos0/env v3.5.0+incompatible
github.com/gin-gonic/gin v1.9.1
github.com/gospider007/ja3 v0.0.0-20231029025157-38fc2f8f2d91
github.com/gospider007/net v0.0.0-20231028084010-313c148cf0a1
github.com/gospider007/requests v0.0.0-20231107034155-de1c366b1e46
github.com/h3adex/fp v0.0.0-20231121110146-224beb073d4b
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/ulule/limiter/v3 v3.11.2
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
Expand Down Expand Up @@ -49,7 +52,6 @@ require (
github.com/gospider007/gson v0.0.0-20231024092648-c97546a0287d // indirect
github.com/gospider007/gtls v0.0.0-20231024092712-01193b9f0404 // indirect
github.com/gospider007/kinds v0.0.0-20231024093643-7a4424f2d30e // indirect
github.com/gospider007/net v0.0.0-20231028084010-313c148cf0a1 // indirect
github.com/gospider007/re v0.0.0-20231024115818-adfd03636256 // indirect
github.com/gospider007/tools v0.0.0-20231104124710-69ac32876ccc // indirect
github.com/gospider007/websocket v0.0.0-20231104023155-0d1a787293d3 // indirect
Expand All @@ -67,20 +69,17 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/quic-go v0.40.0 // indirect
github.com/refraction-networking/utls v1.5.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ulule/limiter/v3 v3.11.2 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/didip/tollbooth v4.0.2+incompatible h1:fVSa33JzSz0hoh2NxpwZtksAzAgd7zjmGO20HCZtF4M=
github.com/didip/tollbooth v4.0.2+incompatible/go.mod h1:A9b0665CE6l1KmzpDws2++elm/CsuWBMa5Jv4WY0PEY=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
Expand Down Expand Up @@ -154,8 +152,6 @@ github.com/onsi/ginkgo/v2 v2.12.1 h1:uHNEO1RP2SpuZApSkel9nEh1/Mu+hmQe7Q+Pepg5OYA
github.com/onsi/ginkgo/v2 v2.12.1/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
33 changes: 13 additions & 20 deletions pkg/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,7 @@ var pathTypeImplementationSpecific = v1.PathTypeImplementationSpecific
func IngressExactPathTypeMock() v1.Ingress {
return v1.Ingress{
ObjectMeta: v12.ObjectMeta{
Name: "",
GenerateName: "",
Namespace: "",
UID: "",
ResourceVersion: "",
Generation: 0,
CreationTimestamp: v12.Time{},
DeletionTimestamp: nil,
DeletionGracePeriodSeconds: nil,
Labels: nil,
Annotations: map[string]string{},
OwnerReferences: nil,
Finalizers: nil,
ManagedFields: nil,
Namespace: "default",
},
Spec: v1.IngressSpec{
IngressClassName: nil,
Expand Down Expand Up @@ -71,8 +58,10 @@ func IngressExactPathTypeMock() v1.Ingress {

func IngressPathTypePrefixMock() v1.Ingress {
return v1.Ingress{
TypeMeta: v12.TypeMeta{},
ObjectMeta: v12.ObjectMeta{},
TypeMeta: v12.TypeMeta{},
ObjectMeta: v12.ObjectMeta{
Namespace: "default",
},
Spec: v1.IngressSpec{
IngressClassName: nil,
DefaultBackend: nil,
Expand Down Expand Up @@ -109,8 +98,10 @@ func IngressPathTypePrefixMock() v1.Ingress {

func IngressPathTypeImplementationSpecificTypeMock() v1.Ingress {
return v1.Ingress{
TypeMeta: v12.TypeMeta{},
ObjectMeta: v12.ObjectMeta{},
TypeMeta: v12.TypeMeta{},
ObjectMeta: v12.ObjectMeta{
Namespace: "default",
},
Spec: v1.IngressSpec{
IngressClassName: nil,
DefaultBackend: nil,
Expand Down Expand Up @@ -147,8 +138,10 @@ func IngressPathTypeImplementationSpecificTypeMock() v1.Ingress {

func IngressNoPathTypeMock() v1.Ingress {
return v1.Ingress{
TypeMeta: v12.TypeMeta{},
ObjectMeta: v12.ObjectMeta{},
TypeMeta: v12.TypeMeta{},
ObjectMeta: v12.ObjectMeta{
Namespace: "default",
},
Spec: v1.IngressSpec{
IngressClassName: nil,
DefaultBackend: nil,
Expand Down
6 changes: 4 additions & 2 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func (r *RoutingTable) GetBackend(host, uri, ip string) (*url.URL, map[string]st

return &url.URL{
Host: fmt.Sprintf(
"%s:%d",
"%s.%s.svc.cluster.local:%d",
path.Backend.Service.Name,
ingress.Namespace,
path.Backend.Service.Port.Number,
),
Path: path.Path,
Expand Down Expand Up @@ -97,8 +98,9 @@ func (r *RoutingTable) GetBackend(host, uri, ip string) (*url.URL, map[string]st

return &url.URL{
Host: fmt.Sprintf(
"%s:%d",
"%s.%s.svc.cluster.local:%d",
path.Backend.Service.Name,
ingress.Namespace,
path.Backend.Service.Port.Number,
),
Path: path.Path,
Expand Down
Loading

0 comments on commit 4be7cfb

Please sign in to comment.