Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header route modules support #121

Merged
merged 7 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/v1alpha1/httpsedge_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type HTTPSEdgeRouteSpec struct {

// IPRestriction is an IPRestriction to apply to this route
IPRestriction *EndpointIPPolicy `json:"ipRestriction,omitempty"`

// Headers are request/response headers to apply to this route
Headers *EndpointHeaders `json:"headers,omitempty"`
}

// HTTPSEdgeSpec defines the desired state of HTTPSEdge
Expand Down
32 changes: 30 additions & 2 deletions api/v1alpha1/ngrok_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,38 @@ type ngrokAPICommon struct {

type EndpointCompression struct {
// Enabled is whether or not to enable compression for this endpoint
Enabled *bool `json:"enabled,omitempty"`
Enabled bool `json:"enabled,omitempty"`
}

type EndpointIPPolicy struct {
Enabled *bool `json:"enabled,omitempty"`
IPPolicyIDs []string `json:"policyIDs,omitempty"`
}

// EndpointRequestHeaders is the configuration for a HTTPSEdgeRoute's request headers
// to be added or removed from the request before it is sent to the backend service.
type EndpointRequestHeaders struct {
// a map of header key to header value that will be injected into the HTTP Request
// before being sent to the upstream application server
Add map[string]string `json:"add,omitempty"`
// a list of header names that will be removed from the HTTP Request before being
// sent to the upstream application server
Remove []string `json:"remove,omitempty"`
}

// EndpointResponseHeaders is the configuration for a HTTPSEdgeRoute's response headers
// to be added or removed from the response before it is sent to the client.
type EndpointResponseHeaders struct {
// a map of header key to header value that will be injected into the HTTP Response
// returned to the HTTP client
Add map[string]string `json:"add,omitempty"`
// a list of header names that will be removed from the HTTP Response returned to
// the HTTP client
Remove []string `json:"remove,omitempty"`
}

type EndpointHeaders struct {
// Request headers are the request headers module configuration or null
Request *EndpointRequestHeaders `json:"request,omitempty"`
// Response headers are the response headers module configuration or null
Response *EndpointResponseHeaders `json:"response,omitempty"`
}
96 changes: 85 additions & 11 deletions api/v1alpha1/zz_generated.deepcopy.go

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

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

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

3 changes: 3 additions & 0 deletions internal/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/imdario/mergo"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/compression"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/headers"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/ip_policies"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/parser"
"github.com/ngrok/kubernetes-ingress-controller/internal/errors"
Expand All @@ -32,6 +33,7 @@ const DeniedKeyName = "Denied"

type RouteModules struct {
Compression *ingressv1alpha1.EndpointCompression
Headers *ingressv1alpha1.EndpointHeaders
IPRestriction *ingressv1alpha1.EndpointIPPolicy
}

Expand All @@ -43,6 +45,7 @@ func NewAnnotationsExtractor() Extractor {
return Extractor{
annotations: map[string]parser.IngressAnnotation{
"Compression": compression.NewParser(),
"Headers": headers.NewParser(),
"IPRestriction": ip_policies.NewParser(),
},
}
Expand Down
28 changes: 8 additions & 20 deletions internal/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"testing"

ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/parser"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/testutil"
"github.com/stretchr/testify/assert"
networking "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

func newIngressWithAnnotations(annotations map[string]string) *networking.Ingress {
Expand All @@ -18,29 +19,16 @@ func newIngressWithAnnotations(annotations map[string]string) *networking.Ingres
}
}

func TestCompression(t *testing.T) {
func TestParsesIPPolicies(t *testing.T) {
e := NewAnnotationsExtractor()
modules := e.Extract(newIngressWithAnnotations(map[string]string{
"k8s.ngrok.com/https-compression": "false",
}))
assert.False(t, *modules.Compression.Enabled)
ing := testutil.NewIngress()
ing.SetAnnotations(map[string]string{
parser.GetAnnotationWithPrefix("ip-policy-ids"): "abc123,def456",
})

modules = e.Extract(newIngressWithAnnotations(map[string]string{
"k8s.ngrok.com/https-compression": "true",
}))
assert.True(t, *modules.Compression.Enabled)
modules := e.Extract(ing)

modules = e.Extract(newIngressWithAnnotations(map[string]string{}))
assert.Nil(t, modules.Compression)
}

func TestIPPolicies(t *testing.T) {
e := NewAnnotationsExtractor()
modules := e.Extract(newIngressWithAnnotations(map[string]string{
"k8s.ngrok.com/ip-policy-ids": "abc123,def456",
}))
assert.Equal(t, &ingressv1alpha1.EndpointIPPolicy{
Enabled: pointer.Bool(true),
IPPolicyIDs: []string{
"abc123",
"def456",
Expand Down
6 changes: 4 additions & 2 deletions internal/annotations/compression/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/parser"
networking "k8s.io/api/networking/v1"
"k8s.io/utils/pointer"
)

type compression struct{}
Expand All @@ -13,13 +12,16 @@ func NewParser() parser.IngressAnnotation {
return compression{}
}

// Parse parses the annotations contained in the ingress and returns a
// compression configuration or an error. If no compression annotations are
// found, the returned error an errors.ErrMissingAnnotations.
func (c compression) Parse(ing *networking.Ingress) (interface{}, error) {
v, err := parser.GetBoolAnnotation("https-compression", ing)
if err != nil {
return nil, err
}

return &ingressv1alpha1.EndpointCompression{
Enabled: pointer.Bool(v),
Enabled: v,
}, nil
}
53 changes: 53 additions & 0 deletions internal/annotations/compression/compression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package compression

import (
"testing"

ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/parser"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/testutil"
"github.com/ngrok/kubernetes-ingress-controller/internal/errors"
"github.com/stretchr/testify/assert"
)

func TestCompressionWhenNotSupplied(t *testing.T) {
ing := testutil.NewIngress()
ing.SetAnnotations(map[string]string{})
parsed, err := NewParser().Parse(ing)

assert.Nil(t, parsed)
assert.Error(t, err)
assert.True(t, errors.IsMissingAnnotations(err))
}

func TestCompressionWhenSuppliedAndTrue(t *testing.T) {
ing := testutil.NewIngress()
annotations := map[string]string{}
annotations[parser.GetAnnotationWithPrefix("https-compression")] = "true"
ing.SetAnnotations(annotations)

parsed, err := NewParser().Parse(ing)
assert.NoError(t, err)

compression, ok := parsed.(*ingressv1alpha1.EndpointCompression)
if !ok {
t.Fatalf("expected *ingressv1alpha1.EndpointCompression, got %T", parsed)
}
assert.Equal(t, true, compression.Enabled)
}

func TestCompressionWhenSuppliedAndFalse(t *testing.T) {
ing := testutil.NewIngress()
annotations := map[string]string{}
annotations[parser.GetAnnotationWithPrefix("https-compression")] = "false"
ing.SetAnnotations(annotations)

parsed, err := NewParser().Parse(ing)
assert.NoError(t, err)

compression, ok := parsed.(*ingressv1alpha1.EndpointCompression)
if !ok {
t.Fatalf("expected *ingressv1alpha1.EndpointCompression, got %T", parsed)
}
assert.Equal(t, false, compression.Enabled)
}
Loading