diff options
-rw-r--r-- | internal/serving/serverless/director.go | 8 | ||||
-rw-r--r-- | internal/serving/serverless/function.go | 18 | ||||
-rw-r--r-- | internal/serving/serverless/function_test.go | 17 | ||||
-rw-r--r-- | internal/serving/serverless/serverless.go | 10 | ||||
-rw-r--r-- | internal/serving/serverless/serverless_test.go | 24 | ||||
-rw-r--r-- | internal/source/gitlab/api/lookup_path.go | 15 | ||||
-rw-r--r-- | internal/source/gitlab/factory/serving_test.go | 44 |
7 files changed, 60 insertions, 76 deletions
diff --git a/internal/serving/serverless/director.go b/internal/serving/serverless/director.go index 4478b104..3f1bc99a 100644 --- a/internal/serving/serverless/director.go +++ b/internal/serving/serverless/director.go @@ -8,12 +8,10 @@ import ( // NewDirectorFunc returns a director function capable of configuring a proxy // request -func NewDirectorFunc(function Function) func(*http.Request) { +func NewDirectorFunc(service string) func(*http.Request) { return func(request *http.Request) { - host := function.Host() - - request.Host = host - request.URL.Host = host + request.Host = service + request.URL.Host = service request.URL.Scheme = "https" request.Header.Set("User-Agent", "GitLab Pages Daemon") request.Header.Set("X-Forwarded-For", realip.FromRequest(request)) diff --git a/internal/serving/serverless/function.go b/internal/serving/serverless/function.go deleted file mode 100644 index c5d3d179..00000000 --- a/internal/serving/serverless/function.go +++ /dev/null @@ -1,18 +0,0 @@ -package serverless - -import "strings" - -// Function represents a Knative service that is going to be invoked by the -// proxied request -type Function struct { - Name string // Name is a function name, it includes a "service name" component too - Domain string // Domain is a cluster base domain, used to route requests to apropriate service - Namespace string // Namespace is a kubernetes namespace this function has been deployed to -} - -// Host returns a function address that we are going to expose in the `Host:` -// header to make it possible to route a proxied request to appropriate service -// in a Knative cluster -func (f Function) Host() string { - return strings.Join([]string{f.Name, f.Namespace, f.Domain}, ".") -} diff --git a/internal/serving/serverless/function_test.go b/internal/serving/serverless/function_test.go deleted file mode 100644 index 39f86025..00000000 --- a/internal/serving/serverless/function_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package serverless - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestFunctionHost(t *testing.T) { - function := Function{ - Name: "my-func", - Domain: "knative.example.com", - Namespace: "my-namespace-123", - } - - require.Equal(t, "my-func.my-namespace-123.knative.example.com", function.Host()) -} diff --git a/internal/serving/serverless/serverless.go b/internal/serving/serverless/serverless.go index bec6db16..e1881362 100644 --- a/internal/serving/serverless/serverless.go +++ b/internal/serving/serverless/serverless.go @@ -19,9 +19,7 @@ type Serverless struct { // NewFromAPISource returns a serverless serving instance built from GitLab API // response func NewFromAPISource(config api.Serverless) (serving.Serving, error) { - function := Function(config.Function) - - if len(function.Name) == 0 { + if len(config.Service) == 0 { return nil, errors.New("incomplete serverless serving config") } @@ -40,13 +38,13 @@ func NewFromAPISource(config api.Serverless) (serving.Serving, error) { Certs: certs, } - return New(function, cluster), nil + return New(config.Service, cluster), nil } // New returns a new serving instance -func New(function Function, cluster Cluster) serving.Serving { +func New(service string, cluster Cluster) serving.Serving { proxy := httputil.ReverseProxy{ - Director: NewDirectorFunc(function), + Director: NewDirectorFunc(service), Transport: NewTransport(cluster), ErrorHandler: NewErrorHandler(), } diff --git a/internal/serving/serverless/serverless_test.go b/internal/serving/serverless/serverless_test.go index 2dd933cc..ebd14343 100644 --- a/internal/serving/serverless/serverless_test.go +++ b/internal/serving/serverless/serverless_test.go @@ -39,11 +39,7 @@ func TestServeFileHTTP(t *testing.T) { t.Run("when proxying simple request to a cluster", func(t *testing.T) { withTestCluster(t, fixture.Certificate, fixture.Key, func(mux *http.ServeMux, server *url.URL, certs *Certs) { serverless := New( - Function{ - Name: "my-func", - Namespace: "my-namespace-123", - Domain: "knative.example.com", - }, + "my-func.my-namespace-123.knative.example.com", Cluster{ Name: "knative.gitlab-example.com", Address: server.Hostname(), @@ -75,11 +71,7 @@ func TestServeFileHTTP(t *testing.T) { t.Run("when proxying request with invalid hostname", func(t *testing.T) { withTestCluster(t, fixture.Certificate, fixture.Key, func(mux *http.ServeMux, server *url.URL, certs *Certs) { serverless := New( - Function{ - Name: "my-func", - Namespace: "my-namespace-123", - Domain: "knative.example.com", - }, + "my-func.my-namespace-123.knative.example.com", Cluster{ Name: "knative.invalid-gitlab-example.com", Address: server.Hostname(), @@ -110,11 +102,7 @@ func TestServeFileHTTP(t *testing.T) { t.Run("when a cluster responds with an error", func(t *testing.T) { withTestCluster(t, fixture.Certificate, fixture.Key, func(mux *http.ServeMux, server *url.URL, certs *Certs) { serverless := New( - Function{ - Name: "my-func", - Namespace: "my-namespace-123", - Domain: "knative.example.com", - }, + "my-func.my-namespace-123.knative.example.com", Cluster{ Name: "knative.gitlab-example.com", Address: server.Hostname(), @@ -146,11 +134,7 @@ func TestServeFileHTTP(t *testing.T) { t.Run("when a cluster responds correctly", func(t *testing.T) { withTestCluster(t, fixture.Certificate, fixture.Key, func(mux *http.ServeMux, server *url.URL, certs *Certs) { serverless := New( - Function{ - Name: "my-func", - Namespace: "my-namespace-123", - Domain: "knative.example.com", - }, + "my-func.my-namespace-123.knative.example.com", Cluster{ Name: "knative.gitlab-example.com", Address: server.Hostname(), diff --git a/internal/source/gitlab/api/lookup_path.go b/internal/source/gitlab/api/lookup_path.go index de200e37..77b264ff 100644 --- a/internal/source/gitlab/api/lookup_path.go +++ b/internal/source/gitlab/api/lookup_path.go @@ -16,19 +16,14 @@ type Source struct { Serverless Serverless `json:"serverless,omitempty"` } -// Serverless describeg serverless serving configuration +// Serverless describes serverless serving configuration type Serverless struct { - Function function `json:"function,omitempty"` - Cluster cluster `json:"cluster,omitempty"` + Service string `json:"service,omitempty"` + Cluster Cluster `json:"cluster,omitempty"` } -type function struct { - Name string `json:"name,omitempty"` - Domain string `json:"domain,omitempty"` - Namespace string `json:"namespace,omitempty"` -} - -type cluster struct { +// Cluster describes serverless cluster configuration +type Cluster struct { Address string `json:"address,omitempty"` Port string `json:"port,omitempty"` Hostname string `json:"hostname,omitempty"` diff --git a/internal/source/gitlab/factory/serving_test.go b/internal/source/gitlab/factory/serving_test.go new file mode 100644 index 00000000..6d4a3787 --- /dev/null +++ b/internal/source/gitlab/factory/serving_test.go @@ -0,0 +1,44 @@ +package factory + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "gitlab.com/gitlab-org/gitlab-pages/internal/fixture" + "gitlab.com/gitlab-org/gitlab-pages/internal/serving/disk" + "gitlab.com/gitlab-org/gitlab-pages/internal/serving/serverless" + "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" +) + +func TestServing(t *testing.T) { + t.Run("when lookup path requires disk serving", func(t *testing.T) { + lookup := api.LookupPath{ + Prefix: "/", + Source: api.Source{Type: "file"}, + } + + require.IsType(t, &disk.Disk{}, Serving(lookup)) + }) + + t.Run("when lookup path requires serverless serving", func(t *testing.T) { + lookup := api.LookupPath{ + Prefix: "/", + Source: api.Source{ + Type: "serverless", + Serverless: api.Serverless{ + Service: "my-func.knative.example.com", + Cluster: api.Cluster{ + Address: "127.0.0.10", + Port: "443", + Hostname: "my-cluster.example.com", + CertificateCert: fixture.Certificate, + CertificateKey: fixture.Key, + }, + }, + }, + } + + require.IsType(t, &serverless.Serverless{}, Serving(lookup)) + }) +} |