/
dag.go
149 lines (131 loc) · 3.49 KB
/
dag.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
143
144
145
146
147
148
149
package cmd
import (
"fmt"
nineinfrav1alpha1 "github.com/nineinfra/nineinfra/api/v1alpha1"
"github.com/spf13/cobra"
"io"
"path/filepath"
"strings"
)
const (
dagDesc = `'etl' command help you to complete the DAG-related operations with the NineCluster.`
dagExample = `1. Upload dags to the NineCluster
$ kubectl nine dag --command=upload --namespace=ns
2. Clear dags from the NineCluster from a namespace
$ kubectl nine dag --command=clear --namespace=ns`
)
var (
dagSubCommandList = "upload,clear,list"
)
type dagCmd struct {
out io.Writer
errOut io.Writer
subCommand string
ns string
nineName string
dagsPath string
}
func newDagCmd(out io.Writer, errOut io.Writer) *cobra.Command {
c := &dagCmd{out: out, errOut: errOut}
cmd := &cobra.Command{
Use: "dag",
Short: "Helps you to complete the DAG-related operations with the NineCluster.",
Long: dagDesc,
Example: dagExample,
Args: func(cmd *cobra.Command, args []string) error {
return c.validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
err := c.run()
if err != nil {
return err
}
return nil
},
}
cmd = DisableHelp(cmd)
f := cmd.Flags()
f.StringVarP(&c.subCommand, "command", "c", "", fmt.Sprintf("command for tools,%s are supported now", dagSubCommandList))
f.StringVar(&c.dagsPath, "dags-path", "", "local path of the airflow dags")
f.StringVarP(&c.ns, "namespace", "n", "", "k8s namespace for the NineCluster")
f.BoolVar(&DEBUG, "debug", false, "print debug information")
return cmd
}
func (o *dagCmd) validate(args []string) error {
if !strings.Contains(dagSubCommandList, o.subCommand) {
return fmt.Errorf("unsupported subcommand %s, only %s supported", o.subCommand, dagSubCommandList)
}
return nil
}
func (o *dagCmd) uploadFileToAirflow(filename string, cluster *nineinfrav1alpha1.NineCluster) error {
podNames, err := GetAirflowPodNames(cluster.Name, "scheduler", cluster.Namespace)
if err != nil {
return err
}
_, _, err = runCommand("kubectl", "cp", filename, fmt.Sprintf("%s:%s/%s", podNames[0], DefaultAirflowDagsPath, filepath.Base(filename)), "-n", cluster.Namespace)
if err != nil {
return err
}
return nil
}
func (o *dagCmd) list(parameters []string) error {
listClusters, err := GetNineCLusters(o.ns)
if err != nil {
return err
}
o.nineName = listClusters.Items[0].Name
podNames, err := GetAirflowPodNames(o.nineName, "scheduler", o.ns)
if err != nil {
return err
}
err = runCommandWithOSIO("kubectl", "exec", "-it", podNames[0], "-n", o.ns, "-c", "scheduler", "--", "/bin/ls", "-l", "/opt/airflow/dags")
if err != nil {
return err
}
return nil
}
func (o *dagCmd) clear(parameters []string) error {
return nil
}
func (o *dagCmd) upload(parameters []string) error {
listClusters, err := GetNineCLusters(o.ns)
if err != nil {
return err
}
o.nineName = listClusters.Items[0].Name
if err != nil {
return err
}
if o.dagsPath != "" {
err = o.uploadFileToAirflow(o.dagsPath, &listClusters.Items[0])
if err != nil {
return err
}
}
return err
}
func (o *dagCmd) run() error {
path, _ := rootCmd.Flags().GetString(kubeconfig)
var parameters []string
if path != "" {
parameters = append(parameters, []string{"--kubeconfig", path}...)
}
switch o.subCommand {
case "upload":
err := o.upload(parameters)
if err != nil {
return err
}
case "clear":
err := o.clear(parameters)
if err != nil {
return err
}
case "list":
err := o.list(parameters)
if err != nil {
return err
}
}
return nil
}