-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
repository_gitlab.go
175 lines (144 loc) · 5.21 KB
/
repository_gitlab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package repository
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
)
const (
gitlabHostPrefix = "gitlab."
gitlabPackagesAPIPrefix = "/api/v4/projects/"
gitlabPackagesAPIPackages = "packages"
gitlabPackagesAPIGeneric = "generic"
)
// gitLabRepository provides support for providers hosted on GitLab.
//
// We support GitLab repositories that use the generic packages feature to publish artifacts and versions.
// Repositories must use versioned releases.
type gitLabRepository struct {
providerConfig config.Provider
configVariablesClient config.VariablesClient
httpClient *http.Client
host string
projectSlug string
packageName string
defaultVersion string
rootPath string
componentsPath string
}
var _ Repository = &gitLabRepository{}
// NewGitLabRepository returns a gitLabRepository implementation.
func NewGitLabRepository(providerConfig config.Provider, configVariablesClient config.VariablesClient) (Repository, error) {
if configVariablesClient == nil {
return nil, errors.New("invalid arguments: configVariablesClient can't be nil")
}
rURL, err := url.Parse(providerConfig.URL())
if err != nil {
return nil, errors.Wrap(err, "invalid url")
}
urlSplit := strings.Split(strings.TrimPrefix(rURL.RawPath, "/"), "/")
// Check if the url is a Gitlab repository
if rURL.Scheme != httpsScheme ||
len(urlSplit) != 9 ||
!strings.HasPrefix(rURL.RawPath, gitlabPackagesAPIPrefix) ||
urlSplit[4] != gitlabPackagesAPIPackages ||
urlSplit[5] != gitlabPackagesAPIGeneric {
return nil, errors.New("invalid url: a GitLab repository url should be in the form https://{host}/api/v4/projects/{projectSlug}/packages/generic/{packageName}/{defaultVersion}/{componentsPath}")
}
httpClient := http.DefaultClient
// Extract all the info from url split.
host := rURL.Host
projectSlug := urlSplit[3]
packageName := urlSplit[6]
defaultVersion := urlSplit[7]
rootPath := "."
componentsPath := urlSplit[8]
repo := &gitLabRepository{
providerConfig: providerConfig,
configVariablesClient: configVariablesClient,
httpClient: httpClient,
host: host,
projectSlug: projectSlug,
packageName: packageName,
defaultVersion: defaultVersion,
rootPath: rootPath,
componentsPath: componentsPath,
}
return repo, nil
}
// Host returns host field of gitLabRepository struct.
func (g *gitLabRepository) Host() string {
return g.host
}
// ProjectSlug returns projectSlug field of gitLabRepository struct.
func (g *gitLabRepository) ProjectSlug() string {
return g.projectSlug
}
// DefaultVersion returns defaultVersion field of gitLabRepository struct.
func (g *gitLabRepository) DefaultVersion() string {
return g.defaultVersion
}
// GetVersions returns the list of versions that are available in a provider repository.
func (g *gitLabRepository) GetVersions(_ context.Context) ([]string, error) {
// FIXME Get versions from GitLab API
return []string{g.defaultVersion}, nil
}
// RootPath returns rootPath field of gitLabRepository struct.
func (g *gitLabRepository) RootPath() string {
return g.rootPath
}
// ComponentsPath returns componentsPath field of gitLabRepository struct.
func (g *gitLabRepository) ComponentsPath() string {
return g.componentsPath
}
// GetFile returns a file for a given provider version.
func (g *gitLabRepository) GetFile(ctx context.Context, version, path string) ([]byte, error) {
url := fmt.Sprintf(
"https://%s/api/v4/projects/%s/packages/generic/%s/%s/%s",
g.host,
g.projectSlug,
g.packageName,
version,
path,
)
if content, ok := cacheFiles[url]; ok {
return content, nil
}
timeoutctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
request, err := http.NewRequestWithContext(timeoutctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, errors.Wrapf(err, "failed to get file %q with version %q from %q: failed to create request", path, version, url)
}
response, err := g.httpClient.Do(request)
if err != nil {
return nil, errors.Wrapf(err, "failed to get file %q with version %q from %q", path, version, url)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, errors.Errorf("failed to get file %q with version %q from %q, got %d", path, version, url, response.StatusCode)
}
content, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.Wrapf(err, "failed to get file %q with version %q from %q", path, version, url)
}
cacheFiles[url] = content
return content, nil
}