forked from istio/istio
/
root.go
156 lines (125 loc) · 6.26 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
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
// Copyright 2017 Istio Authors
//
// 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 (
"flag"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"istio.io/istio/mixer/cmd/shared"
"istio.io/istio/pkg/collateral"
"istio.io/istio/pkg/tracing"
"istio.io/istio/pkg/version"
)
type rootArgs struct {
// attributes is the list of name/value pairs of auto-sensed attributes that will be sent with requests
attributes string
// stringAttributes is the list of name/value pairs of string attributes that will be sent with requests.
stringAttributes string
// int64Attributes is the list of name/value pairs of int64 attributes that will be sent with requests.
int64Attributes string
// float64Attributes is the list of name/value pairs of float64 attributes that will be sent with requests.
doubleAttributes string
// boolAttributes is the list of name/value pairs of bool attributes that will be sent with requests.
boolAttributes string
// timestampAttributes is the list of name/value pairs of timestamp attributes that will be sent with requests.
timestampAttributes string
// durationAttributes is the list of name/value pairs of duration attributes that will be sent with requests.
durationAttributes string
// bytesAttributes is the list of name/value pairs of bytes attributes that will be sent with requests.
bytesAttributes string
// stringMapAttributes is the list of string maps that will be sent with requests
stringMapAttributes string
// mixerAddress is the full address (including port) of a mixer instance to call.
mixerAddress string
// number of times to repeat the operation
repeat int
// number of workers to send requests concurrently.
concurrency int
// rate limit of requests per second for each worker.
rate int
// whether to print mixer's response. It is useful when using mixc to generate heavy load.
printResponse bool
// Maximum number of report instances to include in each report API call.
reportBatchSize int
tracingOptions *tracing.Options
}
func addAttributeFlags(cmd *cobra.Command, rootArgs *rootArgs) {
cmd.PersistentFlags().StringVarP(&rootArgs.mixerAddress, "mixer", "m", "localhost:9091",
"Address and port of a running Mixer instance")
cmd.PersistentFlags().IntVarP(&rootArgs.repeat, "repeat", "r", 1,
"Sends the specified number of requests in quick succession")
cmd.PersistentFlags().IntVarP(&rootArgs.concurrency, "concurrency", "c", 1,
"Controls the number of workers used to send requests to Mixer concurrently.")
cmd.PersistentFlags().IntVarP(&rootArgs.rate, "rate", "", -1,
"Maximum number of requests per second sent by each worker.")
cmd.PersistentFlags().BoolVarP(&rootArgs.printResponse, "print_response", "", true,
"Whether to print mixer's response, useful when generating heavy load with mixc.")
cmd.PersistentFlags().IntVarP(&rootArgs.reportBatchSize, "report_batch_size", "", 1,
"Maximum number of report instances to include in each report API call.")
cmd.PersistentFlags().StringVarP(&rootArgs.attributes, "attributes", "a", "",
"List of name/value auto-sensed attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.stringAttributes, "string_attributes", "s", "",
"List of name/value string attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.int64Attributes, "int64_attributes", "i", "",
"List of name/value int64 attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.doubleAttributes, "double_attributes", "d", "",
"List of name/value float64 attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.boolAttributes, "bool_attributes", "b", "",
"List of name/value bool attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.timestampAttributes, "timestamp_attributes", "t", "",
"List of name/value timestamp attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.durationAttributes, "duration_attributes", "", "",
"List of name/value duration attributes specified as name1=value1,name2=value2,...")
cmd.PersistentFlags().StringVarP(&rootArgs.bytesAttributes, "bytes_attributes", "", "",
"List of name/value bytes attributes specified as name1=b0:b1:b3,name2=b4:b5:b6,...")
cmd.PersistentFlags().StringVarP(&rootArgs.stringMapAttributes, "stringmap_attributes", "", "",
"List of name/value string map attributes specified as name1=k1:v1;k2:v2,name2=k3:v3...")
}
// GetRootCmd returns the root of the cobra command-tree.
func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
rootCmd := &cobra.Command{
Use: "mixc",
Short: "Utility to trigger direct calls to Mixer's API.",
Long: "This command lets you interact with a running instance of\n" +
"Mixer. Note that you need a pretty good understanding of Mixer's\n" +
"API in order to use this command.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("'%s' is an invalid argument", args[0])
}
return nil
},
}
rootCmd.SetArgs(args)
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
rootArgs := &rootArgs{
tracingOptions: tracing.DefaultOptions(),
}
cc := checkCmd(rootArgs, printf, fatalf)
rc := reportCmd(rootArgs, printf, fatalf)
addAttributeFlags(cc, rootArgs)
addAttributeFlags(rc, rootArgs)
rootArgs.tracingOptions.AttachCobraFlags(cc)
rootArgs.tracingOptions.AttachCobraFlags(rc)
rootCmd.AddCommand(cc)
rootCmd.AddCommand(rc)
rootCmd.AddCommand(version.CobraCommand())
rootCmd.AddCommand(collateral.CobraCommand(rootCmd, &doc.GenManHeader{
Title: "Istio Mixer Client",
Section: "mixc CLI",
Manual: "Istio Mixer Client",
}))
return rootCmd
}