Skip to content

Commit

Permalink
feat: support sni based tls route
Browse files Browse the repository at this point in the history
Signed-off-by: mango <xu.weiKyrie@foxmail.com>
  • Loading branch information
mangoGoForward committed May 30, 2022
1 parent 795be22 commit b2dd8b1
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/apisix/stream_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (r *streamRouteClient) Create(ctx context.Context, obj *v1.StreamRoute) (*v
zap.Int32("server_port", obj.ServerPort),
zap.String("cluster", "default"),
zap.String("url", r.url),
zap.String("sni", obj.SNI),
)

if err := r.cluster.HasSynced(ctx); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/apisix/stream_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,21 @@ func TestStreamRouteClient(t *testing.T) {
ID: "1",
ServerPort: 8001,
UpstreamId: "1",
SNI: "a.test.com",
})
assert.Nil(t, err)
assert.Equal(t, obj.ID, "1")
assert.Equal(t, obj.SNI, "a.test.com")

obj, err = cli.Create(context.Background(), &v1.StreamRoute{
ID: "2",
ServerPort: 8002,
UpstreamId: "1",
SNI: "*.test.com",
})
assert.Nil(t, err)
assert.Equal(t, obj.ID, "2")
assert.Equal(t, obj.SNI, "*.test.com")

// List
objs, err := cli.List(context.Background())
Expand All @@ -200,4 +204,6 @@ func TestStreamRouteClient(t *testing.T) {
assert.Nil(t, err)
assert.Len(t, objs, 1)
assert.Equal(t, "2", objs[0].ID)
assert.Equal(t, "112", objs[0].UpstreamId)
assert.Equal(t, "", objs[0].SNI)
}
5 changes: 5 additions & 0 deletions pkg/ingress/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestDiffStreamRoutes(t *testing.T) {
{
ID: "3",
ServerPort: 8080,
SNI: "a.test.com",
},
}
added, updated, deleted := diffStreamRoutes(nil, news)
Expand All @@ -92,6 +93,7 @@ func TestDiffStreamRoutes(t *testing.T) {
assert.Equal(t, "1", added[0].ID)
assert.Equal(t, "3", added[1].ID)
assert.Equal(t, int32(8080), added[1].ServerPort)
assert.Equal(t, "a.test.com", added[1].SNI)

olds := []*apisixv1.StreamRoute{
{
Expand All @@ -100,6 +102,7 @@ func TestDiffStreamRoutes(t *testing.T) {
{
ID: "3",
ServerPort: 8081,
SNI: "a.test.com",
},
}
added, updated, deleted = diffStreamRoutes(olds, nil)
Expand All @@ -109,13 +112,15 @@ func TestDiffStreamRoutes(t *testing.T) {
assert.Equal(t, "2", deleted[0].ID)
assert.Equal(t, "3", deleted[1].ID)
assert.Equal(t, int32(8081), deleted[1].ServerPort)
assert.Equal(t, "a.test.com", deleted[1].SNI)

added, updated, deleted = diffStreamRoutes(olds, news)
assert.Len(t, added, 1)
assert.Equal(t, "1", added[0].ID)
assert.Len(t, updated, 1)
assert.Equal(t, "3", updated[0].ID)
assert.Equal(t, int32(8080), updated[0].ServerPort)
assert.Equal(t, "a.test.com", updated[0].SNI)
assert.Len(t, deleted, 1)
assert.Equal(t, "2", deleted[0].ID)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kube/apisix/apis/config/v2beta3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ type ApisixRouteStream struct {
type ApisixRouteStreamMatch struct {
// IngressPort represents the port listening on the Ingress proxy server.
// It should be pre-defined as APISIX doesn't support dynamic listening.
IngressPort int32 `json:"ingressPort" yaml:"ingressPort"`
IngressPort int32 `json:"ingressPort" yaml:"ingressPort"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
}

// ApisixRouteStreamBackend represents a TCP backend (a Kubernetes Service).
Expand Down
1 change: 1 addition & 0 deletions pkg/kube/translation/apisix_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ func (t *translator) translateStreamRouteV2beta3(ctx *TranslateContext, ar *conf
name := apisixv1.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name)
sr.ID = id.GenID(name)
sr.ServerPort = part.Match.IngressPort
sr.SNI = part.Match.Host
ups, err := t.translateUpstream(ar.Namespace, backend.ServiceName, backend.Subset, backend.ResolveGranularity, svcClusterIP, svcPort)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/types/apisix/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ type StreamRoute struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
ServerPort int32 `json:"server_port,omitempty" yaml:"server_port,omitempty"`
SNI string `json:"sni,omitempty" yaml:"sni,omitempty"`
UpstreamId string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"`
Upstream *Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
}
Expand Down
12 changes: 10 additions & 2 deletions samples/deploy/crd/v1/ApisixRoute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ spec:
match:
type: object
required:
- paths
- paths
properties:
paths:
type: array
Expand Down Expand Up @@ -134,7 +134,7 @@ spec:
type: string
minLength: 1
required:
- scope
- scope
op:
type: string
enum:
Expand Down Expand Up @@ -230,6 +230,8 @@ spec:
match:
type: object
properties:
host:
type: string
ingressPort:
type: integer
minimum: 1
Expand Down Expand Up @@ -290,6 +292,10 @@ spec:
name: Target Service(HTTP)
type: string
priority: 1
- jsonPath: .spec.tcp[].match.host
name: Ingress Server Host
type: string
priority: 0
- jsonPath: .spec.tcp[].match.ingressPort
name: Ingress Server Port(TCP)
type: integer
Expand Down Expand Up @@ -476,6 +482,8 @@ spec:
match:
type: object
properties:
host:
type: string
ingressPort:
type: integer
minimum: 1
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/suite-features/global_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"net/http"
"time"

"github.com/apache/apisix-ingress-controller/pkg/id"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"

"github.com/apache/apisix-ingress-controller/pkg/id"
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
)

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/suite-ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"net/http"
"time"

"github.com/apache/apisix-ingress-controller/pkg/id"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"

"github.com/apache/apisix-ingress-controller/pkg/id"
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
)

Expand Down
4 changes: 4 additions & 0 deletions test/e2e/suite-ingress/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ spec:
protocol: TCP
match:
ingressPort: 9100
host: a.test.com
backend:
serviceName: %s
servicePort: %d
Expand All @@ -64,6 +65,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), sr, 1)
assert.Equal(ginkgo.GinkgoT(), sr[0].ServerPort, int32(9100))
assert.Equal(ginkgo.GinkgoT(), sr[0].SNI, "a.test.com")

resp := s.NewAPISIXClientWithTCPProxy().GET("/ip").Expect()
resp.Body().Contains("origin")
Expand Down Expand Up @@ -133,6 +135,7 @@ spec:
protocol: UDP
match:
ingressPort: 9200
host: a.test.com
backend:
serviceName: coredns
servicePort: 53
Expand All @@ -151,6 +154,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), sr, 1)
assert.Equal(ginkgo.GinkgoT(), sr[0].ServerPort, int32(9200))
assert.Equal(ginkgo.GinkgoT(), sr[0].SNI, "a.test.com")
// test dns query
r := s.DNSResolver()
host := "httpbin.org"
Expand Down

0 comments on commit b2dd8b1

Please sign in to comment.