GitLabgitlab-org
cli
main
1package update_kubeconfig
3import (
4 "fmt"
5 "io/fs"
6 "os"
7 "path/filepath"
8 "strconv"
9 "strings"
10 "time"
12 "github.com/MakeNowJust/heredoc/v2"
13 "github.com/spf13/cobra"
14 "k8s.io/client-go/tools/clientcmd"
15 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
19 "gitlab.com/gitlab-org/cli/internal/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/commands/cluster/agent/agentutils"
21 "gitlab.com/gitlab-org/cli/internal/glrepo"
22 "gitlab.com/gitlab-org/cli/internal/iostreams"
23 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
24)
26const (
27 k8sAuthInfoExecApiVersion = "client.authentication.k8s.io/v1"
29 flagAgent = "agent"
30 flagUseContext = "use-context"
31 flagTokenExpiryDuration = "token-expiry-duration"
32 flagCheckRevoked = "check-revoked"
33 tokenExpiryDurationDefault = 24 * time.Hour
34 minTokenExpiryDuration = 24 * time.Hour
36 snapRealHomeEnvVar = "SNAP_REAL_HOME"
37)
39var sanitizeReplacer = strings.NewReplacer("/", "_", ".", "_")
41type options struct {
42 gitlabClient func() (*gitlab.Client, error)
43 baseRepo func() (glrepo.Interface, error)
44 io *iostreams.IOStreams
45 configAccess clientcmd.ConfigAccess
47 useContext bool
48 agentID int64
49 tokenExpiryDuration time.Duration
50 cacheMode agentutils.CacheMode
51 checkRevoked bool
52}
54func NewCmdAgentUpdateKubeconfig(f cmdutils.Factory) *cobra.Command {
55 pathOptions := newPathOptions()
56 if pathOptions.ExplicitFileFlag == "" {
57 pathOptions.ExplicitFileFlag = clientcmd.RecommendedConfigPathFlag
58 }
60 opts := options{
61 io: f.IO(),
62 gitlabClient: f.GitLabClient,
63 baseRepo: f.BaseRepo,
64 configAccess: pathOptions,
65 }
67 agentUpdateKubeconfigCmd := &cobra.Command{
68 Use: "update-kubeconfig [flags]",
69 Short: `Update your kubeconfig for use with a GitLab Agent for Kubernetes.`,
70 Long: heredoc.Docf(`
71 Configures %[1]skubectl%[1]s to authenticate through the GitLab Agent for Kubernetes
72 using an exec credential plugin.
74 - Use %[1]s--agent%[1]s to specify the agent by its numeric ID.
75 - Use %[1]s--use-context%[1]s to set the agent as the default %[1]skubectl%[1]s context.
76 - Use %[1]s--token-expiry-duration%[1]s to control how long the generated token is valid.
77 - Use %[1]s--cache-mode%[1]s to control how generated tokens are cached.
78 `, "`"),
79 Example: heredoc.Doc(`
80 # Update kubeconfig for agent 123
81 glab cluster agent update-kubeconfig --agent 123
83 # Update kubeconfig and set as the default context
84 glab cluster agent update-kubeconfig --agent 123 --use-context`),
85 Args: cobra.NoArgs,
86 ValidArgsFunction: cobra.NoFileCompletions,
87 Annotations: map[string]string{
88 mcpannotations.Destructive: "true",
89 },
90 RunE: func(cmd *cobra.Command, args []string) error {
91 if err := opts.validate(); err != nil {
92 return err
93 }
94 return opts.run()
95 },
96 }
97 fl := agentUpdateKubeconfigCmd.Flags()
98 fl.Int64VarP(&opts.agentID, flagAgent, "a", opts.agentID, "The numeric agent ID to create the kubeconfig entry for.")
99 fl.StringVar(&pathOptions.LoadingRules.ExplicitPath, pathOptions.ExplicitFileFlag, pathOptions.LoadingRules.ExplicitPath, "Use a particular kubeconfig file.")
100 fl.BoolVarP(&opts.useContext, flagUseContext, "u", opts.useContext, "Use as default context.")
101 fl.DurationVar(&opts.tokenExpiryDuration, flagTokenExpiryDuration, tokenExpiryDurationDefault, "Duration for generated token's validity. Minimum is 1 day. Expires at end of day, and ignores time.")
102 fl.BoolVar(&opts.checkRevoked, flagCheckRevoked, false, "Check if a cached token is revoked. Requires an API call to GitLab, which adds latency every time a cached token is accessed.")
103 agentutils.AddTokenCacheModeFlag(fl, &opts.cacheMode)
104 cobra.CheckErr(agentUpdateKubeconfigCmd.MarkFlagRequired(flagAgent))
106 return agentUpdateKubeconfigCmd
107}
109// newPathOptions resolves the default kubeconfig against SNAP_REAL_HOME when
110// running as a strictly confined snap, because snapd rewrites HOME to the
111// per-revision snap data directory, which kubectl never reads.
112func newPathOptions() *clientcmd.PathOptions {
113 pathOptions := clientcmd.NewDefaultPathOptions()
114 if realHome := os.Getenv(snapRealHomeEnvVar); realHome != "" {
115 pathOptions.GlobalFile = filepath.Join(realHome, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName)
116 pathOptions.LoadingRules.MigrationRules = nil
117 }
118 return pathOptions
119}
121// client-go formats kubeconfig read errors with %v, so errors.Is cannot see
122// the underlying permission error and the message has to be matched instead.
123func withSnapPermissionHint(err error, kubeconfigFiles []string) error {
124 realHome := os.Getenv(snapRealHomeEnvVar)
125 if realHome == "" || len(kubeconfigFiles) == 0 || !strings.Contains(err.Error(), fs.ErrPermission.Error()) {
126 return err
127 }
128 // The dot-kube-config plug only grants access to ~/.kube/config and its lock, so connecting it can't fix other paths.
129 defaultKubeconfig := filepath.Join(realHome, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName)
130 for _, file := range kubeconfigFiles {
131 if filepath.Clean(file) != defaultKubeconfig {
132 return err
133 }
134 }
135 return fmt.Errorf("%w\nTo allow the glab snap to access ~/.kube/config, run: sudo snap connect glab:dot-kube-config", err)
136}
138func (o *options) validate() error {
139 if o.tokenExpiryDuration < minTokenExpiryDuration {
140 return fmt.Errorf("token expiry duration must be at least 24 hours")
141 }
142 return nil
143}
145func (o *options) run() error {
146 client, err := o.gitlabClient()
147 if err != nil {
148 return err
149 }
151 repo, err := o.baseRepo()
152 if err != nil {
153 return err
154 }
156 // Retrieve metadata of the instance to determine KAS URL
157 metadata, _, err := client.Metadata.GetMetadata()
158 if err != nil {
159 return err
160 }
161 if !metadata.KAS.Enabled {
162 return fmt.Errorf("the GitLab agent server for Kubernetes is disabled on %s; ask your administrator to enable and configure it", repo.RepoHost())
163 }
164 kasK8SProxyURL, err := agentutils.GetKasK8SProxyURL(metadata)
165 if err != nil {
166 return err
167 }
169 // Retrieve agent information, most importantly its name to use it as context name.
170 repoFullName := repo.FullName()
171 agent, _, err := client.ClusterAgents.GetAgent(repoFullName, o.agentID)
172 if err != nil {
173 return err
174 }
176 // Retrieve user information
177 user, _, err := client.Users.CurrentUser()
178 if err != nil {
179 return err
180 }
182 // Retrieve glab executable path for exec
183 glabExecutable, err := os.Executable()
184 if err != nil {
185 return err
186 }
188 startingConfig, err := o.configAccess.GetStartingConfig()
189 if err != nil {
190 return withSnapPermissionHint(err, o.configAccess.GetLoadingPrecedence())
191 }
193 params := updateKubeconfigParams{
194 startingConfig: *startingConfig,
195 glabExecutable: glabExecutable,
196 glHost: repo.RepoHost(),
197 glRepoFullName: repoFullName,
198 glUser: user.Username,
199 kasK8sProxyURL: kasK8SProxyURL,
200 agent: agent,
201 tokenExpiryDuration: o.tokenExpiryDuration,
202 cacheMode: o.cacheMode,
203 checkRevoked: o.checkRevoked,
204 }
205 config, contextName := updateKubeconfig(params)
207 if o.useContext {
208 config.CurrentContext = contextName
209 }
211 if err := clientcmd.ModifyConfig(o.configAccess, config, true); err != nil {
212 return withSnapPermissionHint(err, o.configAccess.GetLoadingPrecedence())
213 }
215 o.io.LogInfof("Updated context %s.\n", contextName)
217 if o.useContext {
218 o.io.LogInfof("Using context %s.\n", contextName)
219 }
220 return nil
221}
223type updateKubeconfigParams struct {
224 startingConfig clientcmdapi.Config
225 glabExecutable string
226 glHost string
227 glRepoFullName string
228 glUser string
229 kasK8sProxyURL string
230 agent *gitlab.Agent
231 tokenExpiryDuration time.Duration
232 cacheMode agentutils.CacheMode
233 checkRevoked bool
234}
236func updateKubeconfig(params updateKubeconfigParams) (clientcmdapi.Config, string) {
237 config := params.startingConfig
239 // Updating `clusters` entry: `kubectl config set-cluster ...`
240 clusterName := sanitizeForKubeconfig(params.glHost)
241 startingCluster, exists := config.Clusters[clusterName]
242 if !exists {
243 startingCluster = clientcmdapi.NewCluster()
244 }
245 config.Clusters[clusterName] = modifyCluster(*startingCluster, params.kasK8sProxyURL)
247 // Updating `users` entry: `kubectl config set-credentials ...`
248 authInfoName := fmt.Sprintf("%s-%d", clusterName, params.agent.ID)
249 startingAuthInfo, exists := config.AuthInfos[authInfoName]
250 if !exists {
251 startingAuthInfo = clientcmdapi.NewAuthInfo()
252 }
253 config.AuthInfos[authInfoName] = modifyAuthInfo(
254 *startingAuthInfo,
255 params.glabExecutable,
256 params.glHost,
257 params.glRepoFullName,
258 params.agent.ID,
259 params.tokenExpiryDuration,
260 params.cacheMode,
261 params.checkRevoked,
262 )
264 // Updating `contexts` entry: `kubectl config set-context ...`
265 contextName := fmt.Sprintf("%s-%s-%s", clusterName, sanitizeForKubeconfig(params.agent.ConfigProject.PathWithNamespace), params.agent.Name)
266 startingContext, exists := config.Contexts[contextName]
267 if !exists {
268 startingContext = clientcmdapi.NewContext()
269 }
270 config.Contexts[contextName] = modifyContext(*startingContext, clusterName, authInfoName)
272 return config, contextName
273}
275func modifyCluster(cluster clientcmdapi.Cluster, server string) *clientcmdapi.Cluster {
276 cluster.Server = server
277 return &cluster
278}
280func modifyAuthInfo(authInfo clientcmdapi.AuthInfo, glabExecutable string, glabHost string, glRepoFullName string, agentID int64, tokenExpiryDuration time.Duration, cacheMode agentutils.CacheMode, checkRevoked bool) *clientcmdapi.AuthInfo {
281 // Clear existing auth info
282 authInfo.Token = ""
283 authInfo.TokenFile = ""
285 // Two reasons to set --repo and GITLAB_HOST:
286 // - Propagate the host and repo if it's not the default one.
287 // - Isolate from the variable(s) that might be set to a different value when kubectl calls this command to get a token.
288 // This requires ALWAYS setting this variable, even it has the default value as the caller might have
289 // a custom value set, but it must be ignored.
291 args := []string{
292 "cluster", "agent", "get-token",
293 "--agent", strconv.FormatInt(agentID, 10),
294 "--repo", glRepoFullName,
295 "--token-expiry-duration", tokenExpiryDuration.String(),
296 "--cache-mode", cacheMode,
297 }
298 if checkRevoked {
299 args = append(args, "--check-revoked")
300 }
301 authInfo.Exec = &clientcmdapi.ExecConfig{
302 Command: glabExecutable,
303 Args: args,
304 Env: []clientcmdapi.ExecEnvVar{
305 {
306 Name: "GITLAB_HOST",
307 Value: glabHost,
308 },
309 },
310 APIVersion: k8sAuthInfoExecApiVersion,
311 InstallHint: heredoc.Doc(`
312 To authenticate to the current cluster, glab is required.
314 Follow the installation instructions at https://gitlab.com/gitlab-org/cli#installation.
315 `),
316 InteractiveMode: clientcmdapi.NeverExecInteractiveMode,
317 }
319 return &authInfo
320}
322func modifyContext(ctx clientcmdapi.Context, clusterName, authInfoName string) *clientcmdapi.Context {
323 ctx.Cluster = clusterName
324 ctx.AuthInfo = authInfoName
325 return &ctx
326}
328func sanitizeForKubeconfig(name string) string {
329 return sanitizeReplacer.Replace(name)
330}
Diagnostics9 git calls · 208 ms
CallStatusSizeTime0208 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
fetchtree:0 · deepen 1020032 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=32926 git.response.cache=HIT git.response.duration_ms=2 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
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: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 · 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=200 git.response.cache=HIT git.response.duration_ms=2 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 · 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=405 git.response.cache=HIT git.response.duration_ms=3 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:0200155 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=155 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=08c4e6a65fb9c1ba574151c1ff6b1c7552c6f984 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=155 git.pkt.payload.max=128 git.sideband.band1.bytes=128 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=128 git.pack.trailer=c3d76cd4a9cafbfa1321fea3678012ad974c1479Open pack map
fetch2003.7 KiB193 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=193 git.response.bytes=3796 git.response.cache=MISS git.response.duration_ms=193 git.fetch.wants=3d3b8708ef83ea02d9dc0f6169ed1fb6c2d3cd7c 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=3796 git.pkt.payload.max=3769 git.sideband.band1.bytes=3769 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
- a5eb08f24b8921ca
- Request
- 41232716…
- 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