From f32670058a542fb341d64989f91fea777916df50 Mon Sep 17 00:00:00 2001 From: mmpei Date: Wed, 10 Jul 2019 17:31:59 +0800 Subject: [PATCH] Fix by comments Signed-off-by: mmpei --- src/replication/adapter/helmhub/adapter.go | 6 +++++- .../adapter/helmhub/adapter_test.go | 2 +- .../adapter/helmhub/chart_registry.go | 4 ++-- src/replication/adapter/helmhub/client.go | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/replication/adapter/helmhub/adapter.go b/src/replication/adapter/helmhub/adapter.go index 28c855dc8..45fb7a0a3 100644 --- a/src/replication/adapter/helmhub/adapter.go +++ b/src/replication/adapter/helmhub/adapter.go @@ -72,5 +72,9 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error { // HealthCheck checks health status of a registry func (a *adapter) HealthCheck() (model.HealthStatus, error) { - return model.Healthy, nil + err := a.client.checkHealthy() + if err == nil { + return model.Healthy, nil + } + return model.Unhealthy, err } diff --git a/src/replication/adapter/helmhub/adapter_test.go b/src/replication/adapter/helmhub/adapter_test.go index 0b759038d..ee22fc6dd 100644 --- a/src/replication/adapter/helmhub/adapter_test.go +++ b/src/replication/adapter/helmhub/adapter_test.go @@ -37,7 +37,7 @@ func TestPrepareForPush(t *testing.T) { } func TestHealthCheck(t *testing.T) { - adapter := &adapter{} + adapter, _ := newAdapter(nil) status, err := adapter.HealthCheck() require.Equal(t, model.Healthy, string(status)) require.Nil(t, err) diff --git a/src/replication/adapter/helmhub/chart_registry.go b/src/replication/adapter/helmhub/chart_registry.go index 76f25bae4..db4b79b74 100644 --- a/src/replication/adapter/helmhub/chart_registry.go +++ b/src/replication/adapter/helmhub/chart_registry.go @@ -52,7 +52,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error versionList, err := a.client.fetchChartDetail(repository.Name) if err != nil { log.Errorf("fetch chart detail: %v", err) - continue + return nil, err } vTags := []*adp.VTag{} @@ -112,7 +112,7 @@ func (a *adapter) DownloadChart(name, version string) (io.ReadCloser, error) { return a.download(v) } } - return nil, nil + return nil, errors.New("chart not found") } func (a *adapter) download(version *chartVersion) (io.ReadCloser, error) { diff --git a/src/replication/adapter/helmhub/client.go b/src/replication/adapter/helmhub/client.go index 055e5c08b..a07ebb246 100644 --- a/src/replication/adapter/helmhub/client.go +++ b/src/replication/adapter/helmhub/client.go @@ -91,6 +91,25 @@ func (c *Client) fetchChartDetail(chartName string) (*chartVersionList, error) { return list, nil } +func (c *Client) checkHealthy() error { + request, err := http.NewRequest(http.MethodGet, baseURL, nil) + if err != nil { + return err + } + + resp, err := c.client.Do(request) + if err != nil { + return err + } + defer resp.Body.Close() + + ioutil.ReadAll(resp.Body) + if resp.StatusCode >= 200 && resp.StatusCode < 300 { + return nil + } + return errors.New("helm hub is unhealthy") +} + // do work as a proxy of Do function from net.http func (c *Client) do(req *http.Request) (*http.Response, error) { return c.client.Do(req)