diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-07-28 02:44:37 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-07-28 02:44:37 +0300 |
commit | 98b61cf4663926bec3ae822f60cc34cce6e11b75 (patch) | |
tree | 0c3441a103255da9ba240e606f41929b210c1ffc | |
parent | faa6a4f1ed63dfd3d9f6c193275b1186e66978ec (diff) |
Replace checker in gitlab package
Add `Status` back to the Gitlab struct and the interfaces it needs.
Remove checker interface.
-rw-r--r-- | internal/source/gitlab/api/client.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/api/resolver.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/cache/cache.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 4 | ||||
-rw-r--r-- | internal/source/gitlab/client/client_stub.go | 7 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab.go | 13 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab_poll.go | 14 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab_poll_test.go | 16 |
8 files changed, 38 insertions, 31 deletions
diff --git a/internal/source/gitlab/api/client.go b/internal/source/gitlab/api/client.go index 7206e25a..181c580b 100644 --- a/internal/source/gitlab/api/client.go +++ b/internal/source/gitlab/api/client.go @@ -6,6 +6,9 @@ import ( // Client represents an interface we use to retrieve information from GitLab type Client interface { - // GetLookup retrives an VirtualDomain from GitLab API and wraps it into Lookup + // 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 061a1ddd..738278e2 100644 --- a/internal/source/gitlab/api/resolver.go +++ b/internal/source/gitlab/api/resolver.go @@ -7,6 +7,9 @@ import ( // Resolver represents an interface we use to retrieve information from GitLab // in a more generic way. It can be a concrete API client or cached client. type Resolver interface { - // Resolve retrives an VirtualDomain from GitLab API and wraps it into Lookup + // 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 c8d166b5..37cef111 100644 --- a/internal/source/gitlab/cache/cache.go +++ b/internal/source/gitlab/cache/cache.go @@ -109,3 +109,8 @@ func (c *Cache) Resolve(ctx context.Context, domain string) *api.Lookup { metrics.DomainsSourceCacheMiss.Inc() return entry.Retrieve(ctx, c.client) } + +// 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/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index 7a12cd3b..52a3a489 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -69,6 +69,10 @@ func (c *client) GetLookup(ctx context.Context, _ string) api.Lookup { return lookup } +func (c *client) Status() error { + return nil +} + func withTestCache(config resolverConfig, cacheConfig *cacheConfig, block func(*Cache, *client)) { var chanSize int diff --git a/internal/source/gitlab/client/client_stub.go b/internal/source/gitlab/client/client_stub.go index 604f127b..de6161e6 100644 --- a/internal/source/gitlab/client/client_stub.go +++ b/internal/source/gitlab/client/client_stub.go @@ -10,7 +10,8 @@ import ( // StubClient is a stubbed client used for testing type StubClient struct { - File string + File string + StatusErr func() error } // Resolve implements api.Resolver @@ -35,3 +36,7 @@ func (c StubClient) GetLookup(ctx context.Context, host string) api.Lookup { return lookup } + +func (c StubClient) Status() error { + return c.StatusErr() +} diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 77192478..5bacb603 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -20,28 +20,23 @@ import ( // information about domains from GitLab instance. type Gitlab struct { client api.Resolver - checker checker mu *sync.RWMutex isReady bool } -type checker interface { - Status() error -} - // New returns a new instance of gitlab domain source. func New(config client.Config) (*Gitlab, error) { client, err := client.NewFromConfig(config) if err != nil { return nil, err } + g := &Gitlab{ - client: cache.NewCache(client, nil), - mu: &sync.RWMutex{}, - checker: client, + client: cache.NewCache(client, nil), + mu: &sync.RWMutex{}, } - go g.Poll(defaultPollingMaxRetries, defaultPollingInterval) + go g.poll(defaultPollingMaxRetries, defaultPollingInterval) // using nil for cache config will use the default values specified in internal/source/gitlab/cache/cache.go#12 return g, nil diff --git a/internal/source/gitlab/gitlab_poll.go b/internal/source/gitlab/gitlab_poll.go index 07c59725..70644822 100644 --- a/internal/source/gitlab/gitlab_poll.go +++ b/internal/source/gitlab/gitlab_poll.go @@ -7,20 +7,20 @@ import ( ) const ( - // defaultPollingMaxRetries to be used by Poll + // defaultPollingMaxRetries to be used by poll defaultPollingMaxRetries = 30 - // defaultPollingInterval to be used by Poll + // defaultPollingInterval to be used by poll defaultPollingInterval = time.Minute ) -// Poll tries to call the /internal/pages/status API endpoint once plus +// poll tries to call the /internal/pages/status API endpoint once plus // `retries` every `interval`. // TODO: Remove in https://gitlab.com/gitlab-org/gitlab/-/issues/218357 -func (g *Gitlab) Poll(retries int, interval time.Duration) { +func (g *Gitlab) poll(retries int, interval time.Duration) { var err error for i := 0; i <= retries; i++ { - log.Info("polling GitLab internal pages status API") - err = g.checker.Status() + log.Info("Checking GitLab internal API availability") + err = g.client.Status() if err == nil { log.Info("GitLab internal pages status API connected successfully") g.mu.Lock() @@ -34,5 +34,5 @@ func (g *Gitlab) Poll(retries int, interval time.Duration) { time.Sleep(interval) } - log.WithError(err).Errorf("polling failed after %d tries every %.2fs", retries+1, interval.Seconds()) + log.WithError(err).Errorf("Failed to connect to the internal GitLab API after %d tries every %.2fs", retries+1, interval.Seconds()) } diff --git a/internal/source/gitlab/gitlab_poll_test.go b/internal/source/gitlab/gitlab_poll_test.go index 4d650d52..01f13846 100644 --- a/internal/source/gitlab/gitlab_poll_test.go +++ b/internal/source/gitlab/gitlab_poll_test.go @@ -49,7 +49,7 @@ func TestClient_Poll(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer hook.Reset() var counter int - checkerMock := checkerMock{StatusErr: func() error { + client := client.StubClient{StatusErr: func() error { if tt.expectedFail { return fmt.Errorf(client.ConnectionErrorMsg) } @@ -62,12 +62,12 @@ func TestClient_Poll(t *testing.T) { return nil }} - glClient := Gitlab{checker: checkerMock, mu: &sync.RWMutex{}} + glClient := Gitlab{client: client, mu: &sync.RWMutex{}} - glClient.Poll(tt.retries, tt.interval) + glClient.poll(tt.retries, tt.interval) if tt.expectedFail { require.False(t, glClient.isReady) - s := fmt.Sprintf("polling failed after %d tries every %.2fs", tt.retries+1, tt.interval.Seconds()) + s := fmt.Sprintf("Failed to connect to the internal GitLab API after %d tries every %.2fs", tt.retries+1, tt.interval.Seconds()) require.Equal(t, s, hook.LastEntry().Message) return } @@ -77,11 +77,3 @@ func TestClient_Poll(t *testing.T) { }) } } - -type checkerMock struct { - StatusErr func() error -} - -func (c checkerMock) Status() error { - return c.StatusErr() -} |