-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
agent.go
128 lines (110 loc) · 3.73 KB
/
agent.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
package commands
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"github.com/argoproj/argo-workflows/v3"
executorplugins "github.com/argoproj/argo-workflows/v3/pkg/plugins/executor"
"github.com/argoproj/argo-workflows/v3/util/logs"
"github.com/argoproj/argo-workflows/v3/workflow/common"
"github.com/argoproj/argo-workflows/v3/workflow/executor"
"github.com/argoproj/argo-workflows/v3/workflow/executor/plugins/rpc"
)
func NewAgentCommand() *cobra.Command {
cmd := cobra.Command{
Use: "agent",
SilenceUsage: true, // this prevents confusing usage message being printed on error
}
cmd.AddCommand(NewAgentInitCommand())
cmd.AddCommand(NewAgentMainCommand())
return &cmd
}
func NewAgentInitCommand() *cobra.Command {
return &cobra.Command{
Use: "init",
Run: func(cmd *cobra.Command, args []string) {
for _, name := range getPluginNames() {
filename := tokenFilename(name)
log.WithField("plugin", name).
WithField("filename", filename).
Info("creating token file for plugin")
if err := os.Mkdir(filepath.Dir(filename), 0o770); err != nil {
log.Fatal(err)
}
token := rand.String(32) // this could have 26^32 ~= 2 x 10^45 possible values, not guessable in reasonable time
if err := os.WriteFile(filename, []byte(token), 0o440); err != nil {
log.Fatal(err)
}
}
},
}
}
func tokenFilename(name string) string {
return filepath.Join(common.VarRunArgoPath, name, "token")
}
func getPluginNames() []string {
var names []string
if err := json.Unmarshal([]byte(os.Getenv(common.EnvVarPluginNames)), &names); err != nil {
log.Fatal(err)
}
return names
}
func getPluginAddresses() []string {
var addresses []string
if err := json.Unmarshal([]byte(os.Getenv(common.EnvVarPluginAddresses)), &addresses); err != nil {
log.Fatal(err)
}
return addresses
}
func NewAgentMainCommand() *cobra.Command {
return &cobra.Command{
Use: "main",
RunE: func(cmd *cobra.Command, args []string) error {
return initAgentExecutor().Agent(context.Background())
},
}
}
func initAgentExecutor() *executor.AgentExecutor {
version := argo.GetVersion()
log.WithFields(log.Fields{"version": version.Version}).Info("Starting Workflow Executor")
config, err := clientConfig.ClientConfig()
checkErr(err)
config = restclient.AddUserAgent(config, fmt.Sprintf("argo-workflows/%s argo-executor/%s", version.Version, "agent Executor"))
logs.AddK8SLogTransportWrapper(config) // lets log all request as we should typically do < 5 per pod, so this is will show up problems
namespace, _, err := clientConfig.Namespace()
checkErr(err)
clientSet, err := kubernetes.NewForConfig(config)
checkErr(err)
restClient := clientSet.RESTClient()
workflowName, ok := os.LookupEnv(common.EnvVarWorkflowName)
if !ok {
log.Fatalf("Unable to determine workflow name from environment variable %s", common.EnvVarWorkflowName)
}
workflowUID, ok := os.LookupEnv(common.EnvVarWorkflowUID)
if !ok {
log.Fatalf("Unable to determine workflow Uid from environment variable %s", common.EnvVarWorkflowUID)
}
addresses := getPluginAddresses()
names := getPluginNames()
var plugins []executorplugins.TemplateExecutor
for i, address := range addresses {
name := names[i]
filename := tokenFilename(name)
log.WithField("plugin", name).
WithField("filename", filename).
Info("loading token file for plugin")
data, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
plugins = append(plugins, rpc.New(address, string(data)))
}
return executor.NewAgentExecutor(clientSet, restClient, config, namespace, workflowName, workflowUID, plugins)
}