diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2020-02-12 14:47:36 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2020-02-12 14:47:36 +0300 |
commit | efc9707333679aecf36b38f6f53dd0a0e52698eb (patch) | |
tree | e9d8df60ac0672b50f78aa050a0d50ef33dedafb /internal/serving | |
parent | e3901d4a4e33ce530e4fd85cc2d7af52e5fec7a6 (diff) |
Make it possible to fabricate a serverless serving
Diffstat (limited to 'internal/serving')
-rw-r--r-- | internal/serving/serverless/function.go | 8 | ||||
-rw-r--r-- | internal/serving/serverless/function_test.go | 6 | ||||
-rw-r--r-- | internal/serving/serverless/serverless.go | 29 | ||||
-rw-r--r-- | internal/serving/serverless/serverless_test.go | 24 |
4 files changed, 48 insertions, 19 deletions
diff --git a/internal/serving/serverless/function.go b/internal/serving/serverless/function.go index 20d4ec2c..c5d3d179 100644 --- a/internal/serving/serverless/function.go +++ b/internal/serving/serverless/function.go @@ -5,14 +5,14 @@ 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 - Namespace string // Namespace is a kubernetes namespace this function has been deployed to - BaseDomain string // BaseDomain is a cluster base domain, used to route requests to apropriate service + 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.BaseDomain}, ".") + 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 index 65d84eb7..39f86025 100644 --- a/internal/serving/serverless/function_test.go +++ b/internal/serving/serverless/function_test.go @@ -8,9 +8,9 @@ import ( func TestFunctionHost(t *testing.T) { function := Function{ - Name: "my-func", - Namespace: "my-namespace-123", - BaseDomain: "knative.example.com", + 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 a8d090da..3c360054 100644 --- a/internal/serving/serverless/serverless.go +++ b/internal/serving/serverless/serverless.go @@ -1,10 +1,12 @@ package serverless import ( + "errors" "net/http/httputil" "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors" "gitlab.com/gitlab-org/gitlab-pages/internal/serving" + "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" ) // Serverless is a servering used to proxy requests between a client and @@ -13,6 +15,33 @@ type Serverless struct { proxy *httputil.ReverseProxy } +// 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 { + return nil, errors.New("incomplete serverless serving config") + } + + certs, err := NewClusterCerts( + config.Cluster.CertificateCert, + config.Cluster.CertificateKey, + ) + if err != nil { + return nil, err + } + + cluster := Cluster{ + Name: config.Cluster.Hostname, + Address: config.Cluster.Address, + Port: config.Cluster.Port, + Certs: certs, + } + + return New(function, cluster), nil +} + // New returns a new serving instance func New(function Function, cluster Cluster) serving.Serving { proxy := httputil.ReverseProxy{ diff --git a/internal/serving/serverless/serverless_test.go b/internal/serving/serverless/serverless_test.go index c330cbda..2dd933cc 100644 --- a/internal/serving/serverless/serverless_test.go +++ b/internal/serving/serverless/serverless_test.go @@ -40,9 +40,9 @@ func TestServeFileHTTP(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", - BaseDomain: "knative.example.com", + Name: "my-func", + Namespace: "my-namespace-123", + Domain: "knative.example.com", }, Cluster{ Name: "knative.gitlab-example.com", @@ -76,9 +76,9 @@ func TestServeFileHTTP(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", - BaseDomain: "knative.example.com", + Name: "my-func", + Namespace: "my-namespace-123", + Domain: "knative.example.com", }, Cluster{ Name: "knative.invalid-gitlab-example.com", @@ -111,9 +111,9 @@ func TestServeFileHTTP(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", - BaseDomain: "knative.example.com", + Name: "my-func", + Namespace: "my-namespace-123", + Domain: "knative.example.com", }, Cluster{ Name: "knative.gitlab-example.com", @@ -147,9 +147,9 @@ func TestServeFileHTTP(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", - BaseDomain: "knative.example.com", + Name: "my-func", + Namespace: "my-namespace-123", + Domain: "knative.example.com", }, Cluster{ Name: "knative.gitlab-example.com", |