Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeistel <6742251-feistel@users.noreply.gitlab.com>2021-06-15 14:16:04 +0300
committerfeistel <6742251-feistel@users.noreply.gitlab.com>2021-08-17 14:47:01 +0300
commitd922473a13483bee223217c08a3b4914a910b636 (patch)
tree70a739b2ffd721f22b01a5c75f168b0dacb892fc
parentd79892fe182e3c28cd055bd34669d82c1ab5294b (diff)
refactor: remove GitLab API internal polling
-rw-r--r--internal/source/gitlab/api/client.go3
-rw-r--r--internal/source/gitlab/api/resolver.go3
-rw-r--r--internal/source/gitlab/cache/cache.go5
-rw-r--r--internal/source/gitlab/client/client.go16
-rw-r--r--internal/source/gitlab/client/client_test.go84
-rw-r--r--internal/source/gitlab/gitlab.go3
-rw-r--r--internal/source/gitlab/gitlab_poll.go45
-rw-r--r--internal/source/gitlab/gitlab_poll_test.go79
-rw-r--r--test/acceptance/helpers_test.go18
-rw-r--r--test/acceptance/serving_test.go32
10 files changed, 2 insertions, 286 deletions
diff --git a/internal/source/gitlab/api/client.go b/internal/source/gitlab/api/client.go
index 181c580b..923f7c03 100644
--- a/internal/source/gitlab/api/client.go
+++ b/internal/source/gitlab/api/client.go
@@ -8,7 +8,4 @@ import (
type Client interface {
// Resolve retrieves an VirtualDomain from the GitLab API and wraps it into a Lookup
GetLookup(ctx context.Context, domain string) Lookup
-
- // Status checks the connectivity with the GitLab API
- Status() error
}
diff --git a/internal/source/gitlab/api/resolver.go b/internal/source/gitlab/api/resolver.go
index 738278e2..b1e9404f 100644
--- a/internal/source/gitlab/api/resolver.go
+++ b/internal/source/gitlab/api/resolver.go
@@ -9,7 +9,4 @@ import (
type Resolver interface {
// Resolve retrieves an VirtualDomain from the GitLab API and wraps it into a Lookup
Resolve(ctx context.Context, domain string) *Lookup
-
- // Status checks the connectivity with the GitLab API
- Status() error
}
diff --git a/internal/source/gitlab/cache/cache.go b/internal/source/gitlab/cache/cache.go
index 71b8e745..cb12c088 100644
--- a/internal/source/gitlab/cache/cache.go
+++ b/internal/source/gitlab/cache/cache.go
@@ -89,8 +89,3 @@ func (c *Cache) Resolve(ctx context.Context, domain string) *api.Lookup {
metrics.DomainsSourceCacheMiss.Inc()
return entry.Retrieve(ctx)
}
-
-// Status calls the client Status to check connectivity with the API
-func (c *Cache) Status() error {
- return c.client.Status()
-}
diff --git a/internal/source/gitlab/client/client.go b/internal/source/gitlab/client/client.go
index 31071fa8..30185143 100644
--- a/internal/source/gitlab/client/client.go
+++ b/internal/source/gitlab/client/client.go
@@ -128,22 +128,6 @@ func (gc *Client) GetLookup(ctx context.Context, host string) api.Lookup {
return lookup
}
-// Status checks that Pages can reach the rails internal Pages API
-// for source domain configuration.
-// Timeout is the same as -gitlab-client-http-timeout
-func (gc *Client) Status() error {
- res, err := gc.get(context.Background(), "/api/v4/internal/pages/status", url.Values{})
- if err != nil {
- return fmt.Errorf("%s: %v", ConnectionErrorMsg, err)
- }
-
- if res != nil && res.Body != nil {
- res.Body.Close()
- }
-
- return nil
-}
-
func (gc *Client) get(ctx context.Context, path string, params url.Values) (*http.Response, error) {
endpoint, err := gc.endpoint(path, params)
if err != nil {
diff --git a/internal/source/gitlab/client/client_test.go b/internal/source/gitlab/client/client_test.go
index 12bc026e..131ddd9d 100644
--- a/internal/source/gitlab/client/client_test.go
+++ b/internal/source/gitlab/client/client_test.go
@@ -234,90 +234,6 @@ func TestGetVirtualDomainAuthenticatedRequest(t *testing.T) {
require.Equal(t, "mygroup/myproject/public/", lookupPath.Source.Path)
}
-func TestClientStatus(t *testing.T) {
- tests := []struct {
- name string
- status int
- wantErr bool
- }{
- {
- name: "api_enabled",
- status: http.StatusNoContent,
- },
- {
- name: "api_unauthorized",
- status: http.StatusUnauthorized,
- wantErr: true,
- },
- {
- name: "server_error",
- status: http.StatusInternalServerError,
- wantErr: true,
- },
- {
- name: "gateway_timeout",
- status: http.StatusGatewayTimeout,
- wantErr: true,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- mux := http.NewServeMux()
- mux.HandleFunc("/api/v4/internal/pages/status", func(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(tt.status)
- })
-
- server := httptest.NewServer(mux)
- defer server.Close()
-
- client := defaultClient(t, server.URL)
-
- err := client.Status()
- if tt.wantErr {
- require.Error(t, err)
- require.Contains(t, err.Error(), ConnectionErrorMsg)
- return
- }
-
- require.NoError(t, err)
- })
- }
-}
-
-func TestClientStatusClientTimeout(t *testing.T) {
- timeout := 20 * time.Millisecond
-
- mux := http.NewServeMux()
- mux.HandleFunc("/api/v4/internal/pages/status", func(w http.ResponseWriter, r *http.Request) {
- time.Sleep(timeout * 3)
-
- w.WriteHeader(http.StatusOK)
- })
-
- server := httptest.NewServer(mux)
- defer server.Close()
-
- client := defaultClient(t, server.URL)
- client.httpClient.Timeout = timeout
-
- err := client.Status()
- require.Error(t, err)
- // we can receive any of these messages
- // - context deadline exceeded (Client.Timeout exceeded while awaiting headers)
- // - net/http: request canceled (Client.Timeout exceeded while awaiting headers)
- // - context deadline exceeded
- require.Contains(t, err.Error(), "exceeded")
-}
-
-func TestClientStatusConnectionRefused(t *testing.T) {
- client := defaultClient(t, "http://127.0.0.1:1234")
-
- err := client.Status()
- require.Error(t, err)
- require.Contains(t, err.Error(), "connection refused")
-}
-
func validateToken(t *testing.T, tokenString string) {
t.Helper()
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go
index a295b753..3a372072 100644
--- a/internal/source/gitlab/gitlab.go
+++ b/internal/source/gitlab/gitlab.go
@@ -9,7 +9,6 @@ import (
"strings"
"sync"
- "github.com/cenkalti/backoff/v4"
"gitlab.com/gitlab-org/labkit/log"
"gitlab.com/gitlab-org/gitlab-pages/internal/config"
@@ -42,7 +41,7 @@ func New(cfg *config.GitLab) (*Gitlab, error) {
enableDisk: cfg.EnableDisk,
}
- go g.poll(backoff.DefaultInitialInterval, maxPollingTime)
+ g.isReady = true
return g, nil
}
diff --git a/internal/source/gitlab/gitlab_poll.go b/internal/source/gitlab/gitlab_poll.go
deleted file mode 100644
index 110eb829..00000000
--- a/internal/source/gitlab/gitlab_poll.go
+++ /dev/null
@@ -1,45 +0,0 @@
-package gitlab
-
-import (
- "time"
-
- "github.com/cenkalti/backoff/v4"
- "gitlab.com/gitlab-org/labkit/log"
-)
-
-const (
- // maxPollingTime is the maximum duration to try to call the Status API
- maxPollingTime = 60 * time.Minute
-)
-
-// Poll tries to call the /internal/pages/status API endpoint once plus
-// for `maxElapsedTime`
-// TODO: Remove in https://gitlab.com/gitlab-org/gitlab-pages/-/issues/449
-func (g *Gitlab) poll(interval, maxElapsedTime time.Duration) {
- backOff := backoff.NewExponentialBackOff()
- backOff.InitialInterval = interval
- backOff.MaxElapsedTime = maxElapsedTime
-
- operation := func() error {
- log.Info("Checking GitLab internal API availability")
-
- err := g.client.Status()
- if err != nil {
- log.WithError(err).Warn("attempted to connect to the API")
- }
-
- return err
- }
-
- err := backoff.Retry(operation, backOff)
- if err != nil {
- log.WithError(err).Errorf("failed to connect to the internal GitLab API after %.2fs", maxElapsedTime.Seconds())
- return
- }
-
- g.mu.Lock()
- g.isReady = true
- g.mu.Unlock()
-
- log.Info("GitLab internal pages status API connected successfully")
-}
diff --git a/internal/source/gitlab/gitlab_poll_test.go b/internal/source/gitlab/gitlab_poll_test.go
deleted file mode 100644
index 33f9f6a4..00000000
--- a/internal/source/gitlab/gitlab_poll_test.go
+++ /dev/null
@@ -1,79 +0,0 @@
-package gitlab
-
-import (
- "fmt"
- "testing"
- "time"
-
- "github.com/sirupsen/logrus/hooks/test"
- "github.com/stretchr/testify/require"
-
- "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/client"
-)
-
-func TestClient_Poll(t *testing.T) {
- hook := test.NewGlobal()
- tests := []struct {
- name string
- retries int
- maxTime time.Duration
- expectedFail bool
- }{
- {
- name: "success_with_no_retry",
- retries: 0,
- maxTime: 10 * time.Millisecond,
- expectedFail: false,
- },
- {
- name: "success_after_N_retries",
- retries: 3,
- maxTime: 30 * time.Millisecond,
- expectedFail: false,
- },
- {
- name: "fail_with_no_retries",
- retries: 0,
- maxTime: 10 * time.Millisecond,
- expectedFail: true,
- },
- {
- name: "fail_after_N_retries",
- retries: 3,
- maxTime: 30 * time.Millisecond,
- expectedFail: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- defer hook.Reset()
- var counter int
- client := client.StubClient{StatusErr: func() error {
- if tt.expectedFail {
- return fmt.Errorf(client.ConnectionErrorMsg)
- }
-
- if counter < tt.retries {
- counter++
- return fmt.Errorf(client.ConnectionErrorMsg)
- }
-
- return nil
- }}
-
- glClient := Gitlab{client: client}
-
- glClient.poll(3*time.Millisecond, tt.maxTime)
- if tt.expectedFail {
- require.False(t, glClient.isReady)
-
- s := fmt.Sprintf("failed to connect to the internal GitLab API after %.2fs", tt.maxTime.Seconds())
- require.Equal(t, s, hook.LastEntry().Message)
- return
- }
-
- require.True(t, glClient.isReady)
- require.Equal(t, "GitLab internal pages status API connected successfully", hook.LastEntry().Message)
- })
- }
-}
diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go
index c5fd8319..b75add4b 100644
--- a/test/acceptance/helpers_test.go
+++ b/test/acceptance/helpers_test.go
@@ -545,10 +545,8 @@ func waitForRoundtrips(t *testing.T, listeners []ListenSpec, timeout time.Durati
type stubOpts struct {
m sync.RWMutex
apiCalled bool
- statusReadyCount int
authHandler http.HandlerFunc
userHandler http.HandlerFunc
- statusHandler http.HandlerFunc
pagesHandler http.HandlerFunc
pagesStatusResponse int
pagesRoot string
@@ -558,23 +556,7 @@ func NewGitlabDomainsSourceStub(t *testing.T, opts *stubOpts) *httptest.Server {
t.Helper()
require.NotNil(t, opts)
- currentStatusCount := 0
-
router := mux.NewRouter()
- statusHandler := func(w http.ResponseWriter, r *http.Request) {
- if currentStatusCount < opts.statusReadyCount {
- w.WriteHeader(http.StatusBadGateway)
- return
- }
-
- w.WriteHeader(http.StatusNoContent)
- }
-
- if opts.statusHandler != nil {
- statusHandler = opts.statusHandler
- }
-
- router.HandleFunc("/api/v4/internal/pages/status", statusHandler)
pagesHandler := defaultAPIHandler(t, opts)
if opts.pagesHandler != nil {
diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go
index fb73e03e..77f8118e 100644
--- a/test/acceptance/serving_test.go
+++ b/test/acceptance/serving_test.go
@@ -387,7 +387,6 @@ func TestDomainsSource(t *testing.T) {
configSource string
domain string
urlSuffix string
- readyCount int
}
type want struct {
statusCode int
@@ -459,41 +458,12 @@ func TestDomainsSource(t *testing.T) {
apiCalled: false,
},
},
- {
- name: "auto_source_gitlab_is_not_ready",
- args: args{
- configSource: "auto",
- domain: "test.domain.com",
- urlSuffix: "/",
- readyCount: 100, // big number to ensure the API is in bad state for a while
- },
- want: want{
- statusCode: http.StatusOK,
- content: "main-dir\n",
- apiCalled: false,
- },
- },
- {
- name: "auto_source_gitlab_is_ready",
- args: args{
- configSource: "auto",
- domain: "new-source-test.gitlab.io",
- urlSuffix: "/my/pages/project/",
- readyCount: 0,
- },
- want: want{
- statusCode: http.StatusOK,
- content: "New Pages GitLab Source TEST OK\n",
- apiCalled: true,
- },
- },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &stubOpts{
- apiCalled: false,
- statusReadyCount: tt.args.readyCount,
+ apiCalled: false,
}
source := NewGitlabDomainsSourceStub(t, opts)