kraken

Форк
0
81 строка · 2.2 Кб
1
// Copyright (c) 2016-2019 Uber Technologies, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
package agentclient
15

16
import (
17
	"errors"
18
	"fmt"
19
	"io"
20
	"io/ioutil"
21
	"net/url"
22

23
	"github.com/uber/kraken/core"
24
	"github.com/uber/kraken/utils/httputil"
25
)
26

27
// Client errors.
28
var (
29
	ErrTagNotFound = errors.New("tag not found")
30
)
31

32
// Client defines a client for accessing the agent server.
33
type Client interface {
34
	GetTag(tag string) (core.Digest, error)
35
	Download(namespace string, d core.Digest) (io.ReadCloser, error)
36
}
37

38
// HTTPClient provides a wrapper for HTTP operations on an agent.
39
type HTTPClient struct {
40
	addr string
41
}
42

43
// New creates a new client for an agent at addr.
44
func New(addr string) *HTTPClient {
45
	return &HTTPClient{addr}
46
}
47

48
// GetTag resolves tag into a digest. Returns ErrTagNotFound if the tag does
49
// not exist.
50
func (c *HTTPClient) GetTag(tag string) (core.Digest, error) {
51
	resp, err := httputil.Get(fmt.Sprintf("http://%s/tags/%s", c.addr, url.PathEscape(tag)))
52
	if err != nil {
53
		if httputil.IsNotFound(err) {
54
			return core.Digest{}, ErrTagNotFound
55
		}
56
		return core.Digest{}, err
57
	}
58
	defer resp.Body.Close()
59
	b, err := ioutil.ReadAll(resp.Body)
60
	if err != nil {
61
		return core.Digest{}, fmt.Errorf("read body: %s", err)
62
	}
63
	d, err := core.ParseSHA256Digest(string(b))
64
	if err != nil {
65
		return core.Digest{}, fmt.Errorf("parse digest: %s", err)
66
	}
67
	return d, nil
68
}
69

70
// Download returns the blob of d. Callers should close the returned ReadCloser
71
// when done reading the blob.
72
func (c *HTTPClient) Download(namespace string, d core.Digest) (io.ReadCloser, error) {
73
	resp, err := httputil.Get(
74
		fmt.Sprintf(
75
			"http://%s/namespace/%s/blobs/%s",
76
			c.addr, url.PathEscape(namespace), d))
77
	if err != nil {
78
		return nil, err
79
	}
80
	return resp.Body, nil
81
}
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.