Skip to content

Commit

Permalink
PathPrefix-based Routing (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rr3sp3dr0 authored Jun 15, 2023
1 parent fb17e77 commit d333a0e
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 143 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ This changelog keeps track of work items that have been completed and are ready

### New

- **General**: Add multi-host support to `HTTPScaledObject` ([#552](https://github.com/kedacore/http-add-on/issues/552))
- **Routing**: Add multi-host support to `HTTPScaledObject` ([#552](https://github.com/kedacore/http-add-on/issues/552))
- **Routing**: Support path-based routing ([#338](https://github.com/kedacore/http-add-on/issues/338))
- **General**: Log incoming requests using the Combined Log Format ([#669](https://github.com/kedacore/http-add-on/pull/669))

### Improvements

Expand All @@ -32,6 +34,7 @@ This changelog keeps track of work items that have been completed and are ready

- **Routing**: Lookup host without port ([#608](https://github.com/kedacore/http-add-on/issues/608))
- **Controller**: Use kedav1alpha1.ScaledObject default values ([#607](https://github.com/kedacore/http-add-on/issues/607))
- **General**: Changes to HTTPScaledObjects now take effect ([#605](https://github.com/kedacore/http-add-on/issues/605))

### Deprecations

Expand Down
26 changes: 19 additions & 7 deletions config/crd/bases/http.keda.sh_httpscaledobjects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,27 @@ spec:
properties:
host:
description: (optional) (deprecated) The host to route. All requests
with these hosts in the "Host" header will be routed to the Service
and Port specified in the scaleTargetRef. The host field is mutually
exclusive of the hosts field
which the "Host" header matches .spec.host and the Request Target
matches any .spec.pathPrefixes will be routed to the Service and
Port specified in the scaleTargetRef. The .spec.host field is mutually
exclusive with the .spec.hosts field.
type: string
hosts:
description: (optional) The hosts to route. All requests with these
hosts in the "Host" header will be routed to the Service and Port
specified in the scaleTargetRef. The hosts field is mutually exclusive
of the host field.
description: (optional) The hosts to route. All requests which the
"Host" header matches any .spec.hosts and the Request Target matches
any .spec.pathPrefixes will be routed to the Service and Port specified
in the scaleTargetRef. The .spec.hosts field is mutually exclusive
with the .spec.host field.
items:
type: string
type: array
pathPrefixes:
description: (optional) The paths to route. All requests which the
Request Target matches any .spec.pathPrefixes and the "Host" header
matches any .spec.hosts will be routed to the Service and Port specified
in the scaleTargetRef. The .spec.hosts field is mutually exclusive
with the .spec.host field. When this field is null, any path is
matched.
items:
type: string
type: array
Expand Down
21 changes: 17 additions & 4 deletions operator/apis/http/v1alpha1/httpscaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,27 @@ type ReplicaStruct struct {

// HTTPScaledObjectSpec defines the desired state of HTTPScaledObject
type HTTPScaledObjectSpec struct {
// (optional) (deprecated) The host to route. All requests with these hosts in the "Host" header will
// be routed to the Service and Port specified in the scaleTargetRef. The host field is mutually exclusive of the hosts field
// (optional) (deprecated) The host to route. All requests which the "Host"
// header matches .spec.host and the Request Target matches any
// .spec.pathPrefixes will be routed to the Service and Port specified in
// the scaleTargetRef. The .spec.host field is mutually exclusive with the
// .spec.hosts field.
// +optional
Host *string `json:"host,omitempty"`
// (optional) The hosts to route. All requests with these hosts in the "Host" header will
// be routed to the Service and Port specified in the scaleTargetRef. The hosts field is mutually exclusive of the host field.
// (optional) The hosts to route. All requests which the "Host" header
// matches any .spec.hosts and the Request Target matches any
// .spec.pathPrefixes will be routed to the Service and Port specified in
// the scaleTargetRef. The .spec.hosts field is mutually exclusive with the
// .spec.host field.
// +optional
Hosts []string `json:"hosts,omitempty"`
// (optional) The paths to route. All requests which the Request Target
// matches any .spec.pathPrefixes and the "Host" header matches any
// .spec.hosts will be routed to the Service and Port specified in the
// scaleTargetRef. The .spec.hosts field is mutually exclusive with the
// .spec.host field. When this field is null, any path is matched.
// +optional
PathPrefixes []string `json:"pathPrefixes,omitempty"`
// The name of the deployment to route HTTP requests to (and to autoscale).
ScaleTargetRef ScaleTargetRef `json:"scaleTargetRef"`
// (optional) Replica information
Expand Down
5 changes: 5 additions & 0 deletions operator/apis/http/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions operator/controllers/http/scaled_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ func createOrUpdateScaledObject(
httpso.Spec.ScaleTargetRef.Deployment,
externalScalerHostName,
httpso.Spec.Hosts,
// TODO(pedrotorres): delete this when we support path prefix
nil,
// TODO(pedrotorres): uncomment this when we support path prefix
// httpso.Spec.PathPrefixes,
httpso.Spec.PathPrefixes,
minReplicaCount,
maxReplicaCount,
httpso.Spec.CooldownPeriod,
Expand Down
28 changes: 18 additions & 10 deletions pkg/routing/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,25 @@ func NewKeysFromHTTPSO(httpso *httpv1alpha1.HTTPScaledObject) Keys {
}
spec := httpso.Spec

size := len(spec.Hosts)
keys := make([]Key, size)
for i := 0; i < size; i++ {
host := spec.Hosts[i]

// TODO(pedrotorres): delete this when we support path prefix
path := ""
// TODO(pedrotorres): uncomment this when we support path prefix
// path := spec.Paths[i]
hosts := spec.Hosts
if hosts == nil {
hosts = []string{""}
}
hostsSize := len(hosts)

keys[i] = NewKey(host, path)
pathPrefixes := spec.PathPrefixes
if pathPrefixes == nil {
pathPrefixes = []string{""}
}
pathPrefixesSize := len(pathPrefixes)

keysSize := hostsSize * pathPrefixesSize
keys := make([]Key, 0, keysSize)
for _, host := range hosts {
for _, pathPrefix := range pathPrefixes {
key := NewKey(host, pathPrefix)
keys = append(keys, key)
}
}

return keys
Expand Down
42 changes: 21 additions & 21 deletions pkg/routing/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,18 @@ var _ = Describe("Keys", func() {
It("returns expected key for HTTPSO", func() {
const (
host = "kubernetes.io"
// TODO(pedrotorres): delete this when we support path prefix
norm = "//kubernetes.io/"
// TODO(pedrotorres): uncomment this when we support path prefix
// path = "abc/def"
// norm = "//kubernetes.io/abc/def/"
path = "abc/def"
norm = "//kubernetes.io/abc/def/"
)

keys := NewKeysFromHTTPSO(&httpv1alpha1.HTTPScaledObject{
Spec: httpv1alpha1.HTTPScaledObjectSpec{
Hosts: []string{
host,
},
// TODO(pedrotorres): uncomment this when we support path prefix
// PathPrefix: path,
PathPrefixes: []string{
path,
},
},
})
Expect(keys).To(ConsistOf(Keys{
Expand All @@ -197,16 +195,14 @@ var _ = Describe("Keys", func() {

It("returns expected keys for HTTPSO", func() {
const (
host0 = "keda.sh"
host1 = "kubernetes.io"
// TODO(pedrotorres): delete this when we support path prefix
norm0 = "//keda.sh/"
norm1 = "//kubernetes.io/"
// TODO(pedrotorres): uncomment this when we support path prefix
// path0 = "abc/def"
// path1 = "123/456"
// norm0 = "//kubernetes.io/abc/def/"
// norm1 = "//keda.sh/123/456/"
host0 = "keda.sh"
host1 = "kubernetes.io"
path0 = "abc/def"
path1 = "123/456"
norm00 = "//kubernetes.io/abc/def/"
norm01 = "//kubernetes.io/123/456/"
norm10 = "//keda.sh/abc/def/"
norm11 = "//keda.sh/123/456/"
)

keys := NewKeysFromHTTPSO(&httpv1alpha1.HTTPScaledObject{
Expand All @@ -215,13 +211,17 @@ var _ = Describe("Keys", func() {
host0,
host1,
},
// TODO(pedrotorres): uncomment this when we support path prefix
// PathPrefix: path,
PathPrefixes: []string{
path0,
path1,
},
},
})
Expect(keys).To(ConsistOf(Keys{
Key(norm0),
Key(norm1),
Key(norm00),
Key(norm01),
Key(norm10),
Key(norm11),
}))
})

Expand Down
Loading

0 comments on commit d333a0e

Please sign in to comment.