-
Notifications
You must be signed in to change notification settings - Fork 9
/
execscoped.go
142 lines (121 loc) · 3.48 KB
/
execscoped.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cluster
import (
"context"
"net"
"os"
"os/exec"
"strings"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/localexec"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/internal/sdk/kubectl"
)
func NewExecScoped() *cobra.Command {
cmd := &cobra.Command{
Use: "exec-scoped --service=docker|kubernetes <clusterid> <cmd...>",
Short: "Runs the specified command (e.g. a script) with the corresponding environment variables set, based on the services selected (e.g. DOCKER_HOST, KUBECONFIG).",
Args: cobra.MinimumNArgs(1),
}
service := cmd.Flags().StringSlice("service", nil, "Which services to inject, any of: docker, kubernetes")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
clusterId := args[0]
command := args[1:]
if len(command) == 0 {
return fnerrors.New("at least one command is required")
}
svcs := unique(*service)
if len(svcs) == 0 {
return fnerrors.New("at least one --service is required")
}
response, err := api.EnsureCluster(ctx, api.Methods, clusterId)
if err != nil {
return err
}
var injected []injected
defer func() {
for _, inj := range injected {
inj.Cleanup()
}
}()
for _, svc := range svcs {
if constructor, ok := constructors[svc]; ok {
inj, err := constructor(ctx, response.Cluster)
if err != nil {
return err
}
injected = append(injected, inj)
} else {
return fnerrors.New("no such service %q", svc)
}
}
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
cmd.Env = os.Environ()
for _, inj := range injected {
cmd.Env = append(cmd.Env, inj.Env...)
}
return localexec.RunInteractive(ctx, cmd)
})
return cmd
}
type injected struct {
Env []string
Cleanup func()
}
var constructors = map[string]func(context.Context, *api.KubernetesCluster) (injected, error){
"docker": func(ctx context.Context, cluster *api.KubernetesCluster) (injected, error) {
p, err := runUnixSocketProxy(ctx, cluster.ClusterId, unixSockProxyOpts{
Kind: "docker",
Connect: func(ctx context.Context) (net.Conn, error) {
token, err := fnapi.FetchToken(ctx)
if err != nil {
return nil, err
}
return connectToDocker(ctx, token, cluster)
},
})
if err != nil {
return injected{}, err
}
return injected{
Env: []string{
"DOCKER_HOST=unix://" + p.SocketAddr,
},
Cleanup: p.Cleanup,
}, nil
},
"kubernetes": func(ctx context.Context, cluster *api.KubernetesCluster) (injected, error) {
response, err := api.GetKubernetesConfig(ctx, api.Methods, cluster.ClusterId)
if err != nil {
return injected{}, err
}
cfg, err := kubectl.WriteKubeconfig(ctx, []byte(response.Kubeconfig), true)
if err != nil {
return injected{}, err
}
return injected{
Env: []string{
"KUBECONFIG=" + cfg.Kubeconfig,
},
Cleanup: func() {
_ = os.Remove(cfg.Kubeconfig)
},
}, nil
},
}
func unique(strs []string) []string {
m := map[string]struct{}{}
for _, str := range strs {
m[strings.ToLower(str)] = struct{}{}
}
keys := maps.Keys(m)
slices.Sort(keys)
return keys
}