GitLabgitlab-org
cli
main
1package check_manifest_usage
3import (
4 "errors"
5 "fmt"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
9 "go.yaml.in/yaml/v3"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 "gitlab.com/gitlab-org/cli/internal/api"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/text"
18)
20type options struct {
21 group string
22 recursive bool
23 projectPerPage int
24 projectPage int
25 agentPerPage int
26 agentPage int
28 apiClient func(repoHost string) (*api.Client, error)
29 io *iostreams.IOStreams
30}
32type AgentConfig struct {
33 GitOps struct {
34 ManifestProjects []struct{} `yaml:"manifest_projects,omitempty"`
35 } `yaml:"gitops,omitempty"`
36}
38func NewCmdCheckManifestUsage(f cmdutils.Factory) *cobra.Command {
39 opts := &options{
40 io: f.IO(),
41 apiClient: f.ApiClient,
42 }
43 checkManifestUsageCmd := &cobra.Command{
44 Use: "check-manifest-usage [flags]",
45 // Retain the original snake_case name so existing invocations keep working.
46 // TODO: remove this alias in the next major version.
47 Aliases: []string{"check_manifest_usage"},
48 Short: `Find agents using deprecated GitOps manifest settings. (EXPERIMENTAL)`,
49 Long: heredoc.Docf(`
50 Searches a group and its descendant projects for registered agents whose
51 configuration files include the deprecated %[1]sgitops.manifest_projects%[1]s setting.
53 The output is tab-separated and can be redirected to a TSV file.
54 `, "`") + text.ExperimentalString,
55 Example: heredoc.Doc(`
56 # Check a group for agents using deprecated GitOps manifest settings
57 glab cluster agent check-manifest-usage --group my-group
59 # Recursively check a group and all its subgroups
60 glab cluster agent check-manifest-usage --group my-group --recursive`),
61 Args: cobra.NoArgs,
62 ValidArgsFunction: cobra.NoFileCompletions,
63 Annotations: map[string]string{
64 mcpannotations.Safe: "true",
65 },
66 RunE: func(cmd *cobra.Command, args []string) error {
67 return opts.run()
68 },
69 }
70 // Boolean to authorize experimental features
71 checkManifestUsageCmd.Flags().StringVarP(&opts.group, "group", "g", "", "Group ID to check.")
72 cobra.CheckErr(checkManifestUsageCmd.MarkFlagRequired("group"))
73 checkManifestUsageCmd.Flags().IntVarP(&opts.projectPage, "page", "p", 1, "Page number for projects.")
74 checkManifestUsageCmd.Flags().IntVarP(&opts.projectPerPage, "per-page", "P", 30, "Number of projects to list per page.")
75 checkManifestUsageCmd.Flags().IntVarP(&opts.agentPage, "agent-page", "a", 1, "Page number for agents.")
76 checkManifestUsageCmd.Flags().IntVarP(&opts.agentPerPage, "agent-per-page", "A", 30, "Number of agents to list per page.")
77 checkManifestUsageCmd.Flags().BoolVarP(&opts.recursive, "recursive", "r", false, "Recursively check subgroups.")
79 return checkManifestUsageCmd
80}
82func (o *options) run() error {
83 c, err := o.apiClient("")
84 if err != nil {
85 return err
86 }
87 client := c.Lab()
89 // new line
90 err = checkGroup(client, o.group, o)
91 if err != nil {
92 return err
93 }
95 if o.recursive {
96 var groups []*gitlab.Group
97 groups, _, err = listAllGroupsForGroup(client, o.group)
98 if err != nil {
99 return err
100 }
102 for _, group := range groups {
103 err = checkGroup(client, group.FullPath, o)
104 if err != nil {
105 return err
106 }
107 }
108 }
110 return nil
111}
113func checkGroup(apiClient *gitlab.Client, group string, opts *options) error {
114 var projects []*gitlab.Project
115 var resp *gitlab.Response
116 projects, resp, err := listAllProjectsForGroup(apiClient, group, *opts)
117 if err != nil {
118 return err
119 }
121 color := opts.io.Color()
122 opts.io.LogInfo(color.ProgressIcon(), fmt.Sprintf("Checking %d of %d projects (Page %d of %d)\n", len(projects), resp.TotalItems, resp.CurrentPage, resp.TotalPages))
123 for _, prj := range projects {
124 err = checkManifestUsageInProject(apiClient, opts, prj)
125 if err != nil {
126 return err
127 }
128 }
130 return nil
131}
133func listAllGroupsForGroup(apiClient *gitlab.Client, group string) ([]*gitlab.Group, *gitlab.Response, error) {
134 l := &gitlab.ListDescendantGroupsOptions{}
135 return apiClient.Groups.ListDescendantGroups(group, l)
136}
138func listAllProjectsForGroup(apiClient *gitlab.Client, group string, opts options) ([]*gitlab.Project, *gitlab.Response, error) {
139 l := &gitlab.ListGroupProjectsOptions{
140 ListOptions: gitlab.ListOptions{
141 PerPage: int64(opts.projectPerPage),
142 Page: int64(opts.projectPage),
143 },
144 }
146 return apiClient.Groups.ListGroupProjects(group, l)
147}
149func checkManifestUsageInProject(apiClient *gitlab.Client, opts *options, project *gitlab.Project) error {
150 color := opts.io.Color()
151 opts.io.StartSpinner(fmt.Sprintf("Checking project %s for agents.\n", project.PathWithNamespace))
152 defer opts.io.StopSpinner("")
154 agents, _, err := apiClient.ClusterAgents.ListAgents(project.ID, &gitlab.ListAgentsOptions{
155 ListOptions: gitlab.ListOptions{
156 Page: int64(opts.agentPage),
157 PerPage: int64(opts.agentPerPage),
158 },
159 })
160 if err != nil {
161 return err
162 }
164 opts.io.LogInfo(color.ProgressIcon(), fmt.Sprintf("Found %d agents.\n", len(agents)))
165 for _, agent := range agents {
166 found, err := agentUsesManifestProjects(apiClient, opts, agent)
167 if err != nil {
168 opts.io.LogError(color.RedCheck(), "An error happened.", err)
169 continue
170 }
171 if found {
172 opts.io.LogInfo(fmt.Sprintf("%s\t%s\t%d", agent.ConfigProject.PathWithNamespace, agent.Name, 1))
173 } else {
174 opts.io.LogInfo(fmt.Sprintf("%s\t%s\t%d", agent.ConfigProject.PathWithNamespace, agent.Name, 0))
175 }
176 }
178 return nil
179}
181func agentUsesManifestProjects(apiClient *gitlab.Client, opts *options, agent *gitlab.Agent) (bool, error) {
182 color := opts.io.Color()
183 opts.io.StartSpinner(fmt.Sprintf("Checking manifests of agent %s.\n", agent.Name))
184 defer opts.io.StopSpinner("")
186 file, _, err := apiClient.RepositoryFiles.GetRawFile(agent.ConfigProject.ID, ".gitlab/agents/"+agent.Name+"/config.yaml", &gitlab.GetRawFileOptions{})
187 if err != nil {
188 if errors.Is(err, gitlab.ErrNotFound) {
189 opts.io.LogInfo(color.WarnIcon(), fmt.Sprintf("Agent %s uses the default configuration.", agent.Name))
190 return false, nil
191 }
192 return false, fmt.Errorf("failed to read configuration for agent %q: %w", agent.Name, err)
193 }
195 // Check that gitops.manifest_projects json path does not exist in configFile
196 var configData AgentConfig
197 err = yaml.Unmarshal(file, &configData)
198 if err != nil {
199 opts.io.LogError("Unmarshal error", fmt.Sprintf("%s\n", string(file)))
200 return false, err
201 }
203 if len(configData.GitOps.ManifestProjects) == 0 {
204 opts.io.LogInfo(color.GreenCheck(), fmt.Sprintf("Agent %s does not have manifest projects configured.", agent.Name))
205 return false, nil
206 } else {
207 opts.io.LogInfo(color.FailedIcon(), fmt.Sprintf("Agent %s has %d manifest projects configured.", agent.Name, len(configData.GitOps.ManifestProjects)))
208 return true, nil
209 }
210}
Diagnostics9 git calls · 255 ms
CallStatusSizeTime0255 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 · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · 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=32926 git.response.cache=HIT git.response.duration_ms=3 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=44Open pack map
fetchtree:02001.8 KiB · hit2 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=1857 git.response.cache=HIT git.response.duration_ms=2 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200200 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=200 git.response.cache=HIT git.response.duration_ms=3 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=948ee807a1ee817af67f2df94421a202e82325f9Open pack map
fetchtree:0200405 B · hit2 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=405 git.response.cache=HIT git.response.duration_ms=2 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=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934Open pack map
fetchtree:0200151 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=151 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0e431975358b47ec839a4b67c86b4bdcb2cff77f 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=151 git.pkt.payload.max=124 git.sideband.band1.bytes=124 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=124 git.pack.trailer=a0f8554c6d9d5ad21596a1e7af91a803eea68025Open pack map
fetch2002.3 KiB240 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=240 git.response.bytes=2345 git.response.cache=MISS git.response.duration_ms=240 git.fetch.wants=95501eceee1ff44d2d5506c02a44b62f19fafa10 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=2345 git.pkt.payload.max=2318 git.sideband.band1.bytes=2318 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
- a5eb08e6eff321ca
- Request
- 5efd6eab…
- 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