forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
90 lines (77 loc) · 2.55 KB
/
root.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
package commands
import (
"os"
"github.com/argoproj/argo"
"github.com/argoproj/argo/util/cmd"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/executor"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
// CLIName is the name of the CLI
CLIName = "argoexec"
)
var (
// GlobalArgs hold global CLI flags
GlobalArgs globalFlags
)
type globalFlags struct {
podAnnotationsPath string // --pod-annotations
kubeConfig string // --kubeconfig
}
func init() {
RootCmd.PersistentFlags().StringVar(&GlobalArgs.kubeConfig, "kubeconfig", "", "Kubernetes config (used when running outside of cluster)")
RootCmd.PersistentFlags().StringVar(&GlobalArgs.podAnnotationsPath, "pod-annotations", common.PodMetadataAnnotationsPath, "Pod annotations file from k8s downward API")
RootCmd.AddCommand(cmd.NewVersionCmd(CLIName))
}
// RootCmd is the argo root level command
var RootCmd = &cobra.Command{
Use: CLIName,
Short: "argoexec is the executor sidecar to workflow containers",
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
// getClientConfig return rest config, if path not specified, assume in cluster config
func getClientConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func initExecutor() *executor.WorkflowExecutor {
podAnnotationsPath := common.PodMetadataAnnotationsPath
// Use the path specified from the flag
if GlobalArgs.podAnnotationsPath != "" {
podAnnotationsPath = GlobalArgs.podAnnotationsPath
}
config, err := getClientConfig(GlobalArgs.kubeConfig)
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
podName, ok := os.LookupEnv(common.EnvVarPodName)
if !ok {
log.Fatalf("Unable to determine pod name from environment variable %s", common.EnvVarPodName)
}
namespace, ok := os.LookupEnv(common.EnvVarNamespace)
if !ok {
log.Fatalf("Unable to determine pod namespace from environment variable %s", common.EnvVarNamespace)
}
wfExecutor := executor.NewExecutor(clientset, podName, namespace, podAnnotationsPath)
err = wfExecutor.LoadTemplate()
if err != nil {
panic(err.Error())
}
yamlBytes, _ := yaml.Marshal(&wfExecutor.Template)
log.Infof("Executor (version: %s) initialized with template:\n%s", argo.GetVersion(), string(yamlBytes))
return &wfExecutor
}