GitLabgitlab-org

cli

main
ply10 git calls611 ms98 KiB8 cachedprotocol v2
307 lines7.8 KiBRaw
1package bootstrap
3import (
4 "encoding/base64"
5 "errors"
6 "fmt"
7 "net/http"
8 "slices"
9 "time"
11 "go.yaml.in/yaml/v3"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 glab_api "gitlab.com/gitlab-org/cli/internal/api"
18const (
19 // agentTokenLimit specifies the maximal amount of agent tokens that can be active per agent at any given time.
20 agentTokenLimit = 2
23var agentNotFoundErr = errors.New("agent not found")
25var _ API = (*apiWrapper)(nil)
27type commitAuthor struct {
28 name, email string
31func NewAPI(client *gitlab.Client, projectID any, commitAuthor *commitAuthor) API {
32 return &apiWrapper{client: client, projectID: projectID, commitAuthor: commitAuthor}
35type apiWrapper struct {
36 client *gitlab.Client
37 projectID any
38 commitAuthor *commitAuthor
41func (a *apiWrapper) GetDefaultBranch() (string, error) {
42 project, err := glab_api.GetProject(a.client, a.projectID)
43 if err != nil {
44 return "", err
45 }
46 return project.DefaultBranch, nil
49func (a *apiWrapper) GetAgentByName(name string) (*gitlab.Agent, error) {
50 opts := &gitlab.ListAgentsOptions{
51 ListOptions: gitlab.ListOptions{
52 Page: 1,
53 PerPage: 100,
54 },
55 }
57 for agent, err := range gitlab.Scan2(func(p gitlab.PaginationOptionFunc) ([]*gitlab.Agent, *gitlab.Response, error) {
58 return a.client.ClusterAgents.ListAgents(a.projectID, opts, p)
59 }) {
60 if err != nil {
61 return nil, err
62 }
64 if agent.Name == name {
65 // found
66 return agent, nil
67 }
68 }
70 return nil, agentNotFoundErr
73func (a *apiWrapper) RegisterAgent(name string) (*gitlab.Agent, error) {
74 agent, _, err := a.client.ClusterAgents.RegisterAgent(a.projectID, &gitlab.RegisterAgentOptions{Name: new(name)})
75 return agent, err
78type agentConfig struct {
79 UserAccess *agentConfigUserAccess `yaml:"user_access"`
82type agentConfigUserAccess struct {
83 AccessAs *agentConfigAccessAs `yaml:"access_as"`
84 Projects []*agentConfigProject `yaml:"projects"`
87type agentConfigAccessAs struct {
88 Agent struct{}
91type agentConfigProject struct {
92 ID string `yaml:"id"`
95func (a *apiWrapper) ConfigureAgent(agent *gitlab.Agent, branch string) error {
96 configPath := fmt.Sprintf(".gitlab/agents/%s/config.yaml", agent.Name)
97 file, _, err := a.client.RepositoryFiles.GetFile(a.projectID, configPath, &gitlab.GetFileOptions{Ref: new(branch)})
98 if err != nil && !glab_api.Is404(err) {
99 return err
100 }
102 cfg := agentConfig{}
103 if glab_api.Is404(err) {
104 cfg.UserAccess = &agentConfigUserAccess{
105 AccessAs: &agentConfigAccessAs{Agent: struct{}{}},
106 Projects: []*agentConfigProject{
107 {
108 ID: agent.ConfigProject.PathWithNamespace,
109 },
110 },
111 }
113 configuredContent, err := yaml.Marshal(cfg)
114 if err != nil {
115 return err
116 }
118 opts := &gitlab.CreateFileOptions{
119 Branch: new(branch),
120 Content: new(string(configuredContent)),
121 CommitMessage: new(fmt.Sprintf("Add %s via glab file sync", configPath)),
122 }
124 if a.commitAuthor != nil {
125 opts.AuthorName = new(a.commitAuthor.name)
126 opts.AuthorEmail = new(a.commitAuthor.email)
127 }
129 _, _, err = a.client.RepositoryFiles.CreateFile(a.projectID, configPath, opts)
130 return err
131 } else {
132 content, err := base64.StdEncoding.DecodeString(file.Content)
133 if err != nil {
134 return err
135 }
137 err = yaml.Unmarshal(content, &cfg)
138 if err != nil {
139 return err
140 }
142 if cfg.UserAccess == nil {
143 cfg.UserAccess = &agentConfigUserAccess{
144 AccessAs: &agentConfigAccessAs{Agent: struct{}{}},
145 }
146 }
148 if !slices.ContainsFunc(cfg.UserAccess.Projects, func(p *agentConfigProject) bool { return p.ID == agent.ConfigProject.PathWithNamespace }) {
149 cfg.UserAccess.Projects = append(cfg.UserAccess.Projects, &agentConfigProject{ID: agent.ConfigProject.PathWithNamespace})
150 }
152 configuredContent, err := yaml.Marshal(cfg)
153 if err != nil {
154 return err
155 }
157 opts := &gitlab.UpdateFileOptions{
158 Branch: new(branch),
159 Content: new(string(configuredContent)),
160 CommitMessage: new(fmt.Sprintf("Update %s via glab file sync", configPath)),
161 }
163 if a.commitAuthor != nil {
164 opts.AuthorName = new(a.commitAuthor.name)
165 opts.AuthorEmail = new(a.commitAuthor.email)
166 }
168 _, _, err = a.client.RepositoryFiles.UpdateFile(a.projectID, configPath, opts)
169 return err
170 }
173func (a *apiWrapper) ConfigureEnvironment(agentID int64, name string, kubernetesNamespace string, fluxResourcePath string) error {
174 env, err := a.getEnvironmentByName(name)
175 if err != nil {
176 return err
177 }
179 if env == nil {
180 _, _, err := a.client.Environments.CreateEnvironment(a.projectID, &gitlab.CreateEnvironmentOptions{
181 Name: new(name),
182 ClusterAgentID: new(agentID),
183 KubernetesNamespace: new(kubernetesNamespace),
184 FluxResourcePath: new(fluxResourcePath),
185 })
186 return err
187 } else {
188 _, _, err := a.client.Environments.EditEnvironment(a.projectID, env.ID, &gitlab.EditEnvironmentOptions{
189 Name: new(name),
190 ClusterAgentID: new(agentID),
191 KubernetesNamespace: new(kubernetesNamespace),
192 FluxResourcePath: new(fluxResourcePath),
193 })
194 return err
195 }
198func (a *apiWrapper) CreateAgentToken(agentID int64) (*gitlab.AgentToken, error) {
199 tokens, _, err := a.client.ClusterAgents.ListAgentTokens(a.projectID, agentID, &gitlab.ListAgentTokensOptions{
200 ListOptions: gitlab.ListOptions{
201 PerPage: agentTokenLimit,
202 },
203 })
204 if err != nil {
205 return nil, err
206 }
207 if len(tokens) == agentTokenLimit {
208 slices.SortFunc(tokens, agentTokenSortFunc)
209 longestUnusedToken := tokens[0]
211 _, err := a.client.ClusterAgents.RevokeAgentToken(a.projectID, agentID, longestUnusedToken.ID)
212 if err != nil {
213 return nil, err
214 }
215 }
217 // create new token
218 token, _, err := a.client.ClusterAgents.CreateAgentToken(a.projectID, agentID, &gitlab.CreateAgentTokenOptions{
219 Name: new(fmt.Sprintf("glab-bootstrap-%d", time.Now().UTC().Unix())),
220 Description: new("Created by the `glab cluster agent bootstrap command"),
221 })
222 return token, err
225func (a *apiWrapper) SyncFile(f file, branch string) error {
226 _, _, err := a.client.RepositoryFiles.GetFileMetaData(a.projectID, f.path, &gitlab.GetFileMetaDataOptions{
227 Ref: new(branch),
228 })
229 if err != nil {
230 if !gitlab.HasStatusCode(err, http.StatusNotFound) {
231 return err
232 }
234 opts := &gitlab.CreateFileOptions{
235 Branch: new(branch),
236 Content: new(string(f.content)),
237 CommitMessage: new(fmt.Sprintf("Add %s via glab file sync", f.path)),
238 }
240 if a.commitAuthor != nil {
241 opts.AuthorName = new(a.commitAuthor.name)
242 opts.AuthorEmail = new(a.commitAuthor.email)
243 }
245 // file does not exist yet, lets create it
246 _, _, err := a.client.RepositoryFiles.CreateFile(a.projectID, f.path, opts)
247 return err
248 }
250 opts := &gitlab.UpdateFileOptions{
251 Branch: new(branch),
252 Content: new(string(f.content)),
253 CommitMessage: new(fmt.Sprintf("Update %s via glab file sync", f.path)),
254 }
256 if a.commitAuthor != nil {
257 opts.AuthorName = new(a.commitAuthor.name)
258 opts.AuthorEmail = new(a.commitAuthor.email)
259 }
261 // file already exists, lets update it
262 _, _, err = a.client.RepositoryFiles.UpdateFile(a.projectID, f.path, opts)
263 return err
266func (a *apiWrapper) GetKASAddress() (string, error) {
267 metadata, _, err := a.client.Metadata.GetMetadata()
268 if err != nil {
269 return "", err
270 }
272 if !metadata.KAS.Enabled {
273 return "", errors.New("KAS is not configured in this GitLab instance; contact your administrator")
274 }
276 return metadata.KAS.ExternalURL, nil
279func (a *apiWrapper) getEnvironmentByName(name string) (*gitlab.Environment, error) {
280 opts := &gitlab.ListEnvironmentsOptions{
281 Name: new(name),
282 }
284 envs, _, err := a.client.Environments.ListEnvironments(a.projectID, opts)
285 if err != nil {
286 if glab_api.Is404(err) {
287 return nil, nil
288 }
289 return nil, err
290 }
292 if len(envs) == 0 {
293 return nil, nil
294 }
296 return envs[0], nil
299func agentTokenSortFunc(a, b *gitlab.AgentToken) int {
300 if a.LastUsedAt == nil {
301 return 1
302 }
303 if b.LastUsedAt == nil {
304 return -1
305 }
306 return a.LastUsedAt.Compare(*b.LastUsedAt)
Diagnostics10 git calls · 611 ms
CallStatusSizeTime0611 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit161 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=141
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=161
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit8 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=8
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.1 KiB · hit7 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=7
git.response.bytes=1112
git.response.cache=HIT
git.response.duration_ms=7
git.fetch.filter=tree:0
git.fetch.wants=fed5065f0120a857fec6fa567dcdacfd34c3187b
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1112
git.pkt.payload.max=1085
git.sideband.band1.bytes=1085
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1085
git.pack.trailer=792a8e6885c3db50fcc6be7a28de09363c6fb5e4
Open pack map
fetchtree:0200890 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=890
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=11daffb8433226c87be08ba8cfc35154e4957d77
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=890
git.pkt.payload.max=863
git.sideband.band1.bytes=863
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=863
git.pack.trailer=b909e7b7573f11b0bb74a32781c8c5e4e19277f4
Open pack map
fetchtree:02001.8 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200200 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=200
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=200
git.pkt.payload.max=173
git.sideband.band1.bytes=173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=173
git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9
Open pack map
fetchtree:0200405 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=405
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=405
git.pkt.payload.max=378
git.sideband.band1.bytes=378
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=378
git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934
Open pack map
fetchtree:0200414 B · hit9 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=7
git.response.bytes=414
git.response.cache=HIT
git.response.duration_ms=9
git.fetch.filter=tree:0
git.fetch.wants=3c487cc16dcfeca6d024165580948fbb3a622e23
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=414
git.pkt.payload.max=387
git.sideband.band1.bytes=387
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=387
git.pack.trailer=a621ec095c3ac7a33563831f77c04827c9c343d3
Open pack map
fetch2002.1 KiB356 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=355
git.response.bytes=2167
git.response.cache=MISS
git.response.duration_ms=356
git.fetch.wants=689d3902e7ce58538d841106f9fab737293a57b3
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2167
git.pkt.payload.max=2140
git.sideband.band1.bytes=2140
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5eb08972edd21ca
Request
0107a3e1…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format