-
Notifications
You must be signed in to change notification settings - Fork 40
/
run.go
184 lines (157 loc) · 4.93 KB
/
run.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 Mirantis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"github.com/Mirantis/k8s-AppController/pkg/client"
"github.com/Mirantis/k8s-AppController/pkg/interfaces"
"github.com/Mirantis/k8s-AppController/pkg/scheduler"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/labels"
)
var argRe = regexp.MustCompile(`\s*(\w+)\W+(.*)`)
func run(cmd *cobra.Command, args []string) {
concurrency, err := cmd.Flags().GetInt("concurrency")
if err != nil {
log.Fatal(err)
}
anyArgs, err := cmd.Flags().GetBool("undeclared-args")
if err != nil {
log.Fatal(err)
}
deleteExternal, err := cmd.Flags().GetBool("delete-external")
if err != nil {
log.Fatal(err)
}
inplace, err := cmd.Flags().GetBool("deploy")
if err != nil {
log.Fatal(err)
}
url, err := cmd.Flags().GetString("url")
if err != nil {
log.Fatal(err)
}
labelSelector, err := getLabelSelector(cmd)
if err != nil {
log.Fatal(err)
}
flowArgs, err := cmd.Flags().GetStringArray("arg")
if err != nil {
log.Fatal(err)
}
flowArgMap := map[string]string{}
for _, val := range flowArgs {
key, value, err := parseArg(val)
if err != nil {
log.Fatal(err)
}
flowArgMap[key] = value
}
flowName := interfaces.DefaultFlowName
if len(args) > 1 {
log.Fatal("Too many command line arguments")
}
if len(args) == 1 {
flowName = args[0]
}
c, err := client.New(url)
if err != nil {
log.Fatal(err)
}
sel, err := labels.Parse(labelSelector)
if err != nil {
log.Fatal(err)
}
replicasString, err := cmd.Flags().GetString("replicas")
if err != nil {
log.Fatal(err)
}
replicasString = strings.TrimSpace(replicasString)
replicas := 0
minReplicaCount := 1
if replicasString != "" {
replicas, err = strconv.Atoi(replicasString)
if err != nil {
log.Fatal("Invalid replica count format")
}
minReplicaCount = 0
}
log.Println("Using concurrency:", concurrency)
log.Println("Using label selector:", labelSelector)
sched := scheduler.New(c, sel, concurrency)
options := interfaces.DependencyGraphOptions{
FlowName: flowName,
ExportedOnly: true,
Args: flowArgMap,
AllowUndeclaredArgs: anyArgs,
ReplicaCount: replicas,
FixedNumberOfReplicas: replicasString != "" && !strings.ContainsAny(replicasString, "+-"),
MinReplicaCount: minReplicaCount,
AllowDeleteExternalResources: deleteExternal,
}
_, err = scheduler.Deploy(sched, options, inplace, nil)
if err != nil {
log.Fatal(err)
}
}
func parseArg(arg string) (string, string, error) {
res := argRe.FindStringSubmatch(arg)
if len(res) != 3 {
return "", "", fmt.Errorf("invalid argument %s", arg)
}
return res[1], strings.TrimSpace(res[2]), nil
}
func getLabelSelector(cmd *cobra.Command) (string, error) {
labelSelector, err := cmd.Flags().GetString("label")
if labelSelector == "" {
labelSelector = os.Getenv("KUBERNETES_AC_LABEL_SELECTOR")
}
return labelSelector, err
}
// InitRunCommand returns cobra command for creating AppController graph deployment
func InitRunCommand() (*cobra.Command, error) {
run := &cobra.Command{
Use: "run [flow-name]",
Short: "Create deployment of AppController graph flow",
Long: "Create deployment of AppController graph flow",
Run: run,
}
run.Flags().StringP("label", "l", "",
"Label selector. Overrides KUBERNETES_AC_LABEL_SELECTOR env variable in AppController pod.")
run.Flags().Bool("undeclared-args", false, "Allow undeclared arguments")
run.Flags().StringArray("arg", []string{}, "Flow arguments (key=value)")
concurrencyString := os.Getenv("KUBERNETES_AC_CONCURRENCY")
var err error
var concurrencyDefault int
if len(concurrencyString) > 0 {
concurrencyDefault, err = strconv.Atoi(concurrencyString)
if err != nil {
log.Printf("KUBERNETES_AC_CONCURRENCY is set to '%s' but it does not look like an integer: %v",
concurrencyString, err)
concurrencyDefault = 0
}
}
run.Flags().IntP("concurrency", "c", concurrencyDefault, "concurrency")
run.Flags().String("url", os.Getenv("KUBERNETES_CLUSTER_URL"),
"URL of the Kubernetes cluster. Overrides KUBERNETES_CLUSTER_URL env variable in AppController pod.")
run.Flags().StringP("replicas", "n", "", "number of flow replicas: [+|-]number")
run.Flags().Bool("delete-external", false, "permit delete of external resources")
run.Flags().Bool("deploy", false, "deploy in-place (for debugging)")
return run, err
}