Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netmaster cli refactor #1088

Merged
merged 5 commits into from
Dec 5, 2017
Merged

netmaster cli refactor #1088

merged 5 commits into from
Dec 5, 2017

Conversation

tiewei
Copy link
Contributor

@tiewei tiewei commented Nov 28, 2017

Refactor netmaster cli to use "github.com/urfave/cli", it brings
benefit like setting forwarding mode, infra type and network mode,
allows setting multi etcd/consul endpoints and using SSL, enabling
syslog,json log and multi log levels.

Signed-off-by: Wei Tie wtie@cisco.com

@tiewei
Copy link
Contributor Author

tiewei commented Nov 29, 2017

TODO: update container entrypoint scripts

@dseevr
Copy link
Contributor

dseevr commented Nov 29, 2017

build pr

@tiewei
Copy link
Contributor Author

tiewei commented Nov 29, 2017

build PR

Copy link
Contributor

@dseevr dseevr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Name: "infra, infra-type",
Value: "default",
EnvVar: "CONTIV_NETMASTER_PLUGIN_HOST",
Usage: "set netmaster infra tyoe, options [aci, default]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: tyoe -> type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if err := parseOpts(&opts); err != nil {
log.Fatalf("Failed to parse cli options. Error: %s", err)
internalAddress := ctx.String("interal-address")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: interal -> internal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

log.Fatalf("Failed to parse cli options. Error: %s", err)
internalAddress := ctx.String("interal-address")
if internalAddress == "" {
return nil, errors.New("netmaster interal-address is not set")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: interal -> internal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

// execute options
execOpts(&opts)
logrus.Infof("Using netmaster interal-address: %s", internalAddress)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: interal -> internal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

func main() {
localIP, _ := netutils.GetDefaultAddr()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check error here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if networkMode == "vxlan" && vtepIP == "" {
return plugin.Config{}, fmt.Errorf("vtep-ip should be set when using VXLAN mode")
if netConfigs.NetworkMode == "vxlan" && vtepIP == "" {
return nil, fmt.Errorf("vtep-ip should be set when using VXLAN mode")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should -> must

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if networkMode == "vlan" && len(vlanUpLinks) == 0 {
return plugin.Config{}, fmt.Errorf("vlan-uplinks should be set when using VLAN mode")
if netConfigs.NetworkMode == "vlan" && len(vlanUpLinks) == 0 {
return nil, fmt.Errorf("vlan-uplinks should be set when using VLAN mode")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should -> must

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -53,7 +54,8 @@ func TestMain(m *M) {
flag.StringVar(&integ.arpMode, "arp-mode", "proxy", "ARP mode [ proxy | flood ]")
flag.StringVar(&integ.encap, "encap", "vlan", "Encap [ vlan | vxlan ]")
flag.StringVar(&integ.fabricMode, "fabric-mode", "default", "fabric-mode [ aci | default ]")
flag.StringVar(&integ.clusterStore, "cluster-store", "etcd://127.0.0.1:2379", "Cluster store URL")
flag.StringVar(&integ.clusterStoreDriver, "cluster-store-driver", "etcd", "Cluster store dirver")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: dirver -> driver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

utils/configs.go Outdated

hook, err = logrus_syslog.NewSyslogHook(syslogURL.Scheme, syslogURL.Host, priority, binary)
if err != nil {
return fmt.Errorf("Failed connectting to syslog %q: %v", syslogRawURL, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: connectting -> connecting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

port, err := strconv.Atoi(addr[1])
if err != nil {
return fmt.Errorf("bind port is a integer, got %v", port)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe validate that the port is >= 1 and <= 65535 here?

(I don't think we need to support listening on port 0 lol)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@dseevr
Copy link
Contributor

dseevr commented Nov 29, 2017

@tiewei How do you feel about raising an error if etcd and consul endpoints are set? Right now it says "consul endpoints are ignored if etcd endpoints are set"... if both of them are set, we can't really determine what the user intent was IMO. I think we should just explode in that case.

@tiewei
Copy link
Contributor Author

tiewei commented Nov 30, 2017

@chrisplo mentioned that as well in last commit ... i'll error it out then

@@ -161,7 +160,9 @@ func main() {
if err != nil {
errmsg := err.Error()
logrus.Error(errmsg)
return cli.NewExitError(errmsg, (len(errmsg)%254 + 1))
// use 22 Invalid argument as error return code
// http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

tiewei and others added 4 commits November 30, 2017 21:58
Refactor netmaster cli to use "github.com/urfave/cli", it brings
benefit like setting forarding mode, infra type and network mode,
allows setting multi etcd/consul endpoints and using SSL, enabling
syslog,json log and multi log levels.

Signed-off-by: Wei Tie <wtie@cisco.com>
@tiewei
Copy link
Contributor Author

tiewei commented Dec 2, 2017

build PR

@tiewei tiewei changed the title WIP: netmaster cli refactor netmaster cli refactor Dec 2, 2017
@unclejack
Copy link
Contributor

What release is this meant to be included in? It'd be nice to deprecate the old flags instead of removing them. This should make it easier if we make a 1.2 release which is still backwards compatible and some users upgrade an existing setup. What do you think?

@tiewei
Copy link
Contributor Author

tiewei commented Dec 4, 2017

Most old flag was kept except --cluster-store due to the schema of url change, yes 1.2 would be ideal for this type of changes

cli.StringFlag{
Name: "infra, infra-type",
Value: "default",
EnvVar: "CONTIV_NETMASTER_PLUGIN_HOST",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be CONTIV_NETMASTER_INFRA

cli.StringFlag{
Name: "name, plugin-name",
Value: "netplugin",
EnvVar: "CONTIV_NETMASTER_PLUGIN_HOST",
Copy link
Contributor Author

@tiewei tiewei Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be CONTIV_NETMASTER_PLUGIN_NAME

Copy link
Contributor

@chrisplo chrisplo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3/4 done, some of the below I'd strongly suggest but they probably all slide under non-blocking

apiConfig := &objApi.APIControllerConfig{
NetForwardMode: d.NetForwardMode,
NetInfraType: d.NetInfraType,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFACT this isn't supposed to be modifiable, why not have NewAPIController accept APIControllerConfig by value?

Copy link
Contributor Author

@tiewei tiewei Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this object only got used here (and integ testings), I used object was because of intending to add more parameters from configs .

have NewAPIController accept APIControllerConfig by value

this is troublesome in golang, especially when having the parameters in different type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the struct is fine, just dunno why you need a pointer to it

if err != nil {
log.Fatalf("Error connecting to state store: %v. Err: %v", d.ClusterStore, err)
log.Fatalf("Error connecting to state store: driver %q, URLs %q. Err: %v", d.ClusterStoreDriver, d.ClusterStoreURL, err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should check for ':' in ControlURL and ListenURL since those are parsed later in startListeners

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it actually got checked in the CLI part

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see it now

internalAddr := strings.Split(d.ControlURL, ":")
if externalAddr[0] == "0.0.0.0" && externalAddr[1] == internalAddr[1] {
// ignore internal bind if external and internal are on the same port and external bind on 0.0.0.0
log.Infof("Ignore creating API listener on %q because %q covers it", d.ControlURL, d.ListenURL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems we could have external and internal on same IP as well?
externalAddr[1] == internalAddr[1] && (externalAddr[0] == "0.0.0.0" || externalAddr[0] == internalAddr[0])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if they are on the same ip but different port will go to else where create a listener on different port with same ip. Only got ignored when they have overlapping

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nvm, I wasn't looking at the outer if restriction


// FlattenFlags concatenate slices of flags into one slice
func FlattenFlags(flagSlices ...[]cli.Flag) []cli.Flag {
flags := flagSlices[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start with flags := [] then range starting at 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, I think you can just do instead of this function:
append(flags, flagsA..., flagsB...) where the ... is a literal
https://stackoverflow.com/a/16248257/1322463
https://golang.org/ref/spec#Passing_arguments_to_..._parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think golang allows more than one slice parameter ( it only works for the last parameter)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here, i thought i was on to something . . ok fine, though now I wonder if this could be ...[]interface{} arg and be a general purpose

Anyway . . . I'm back to first comment, start range at 0 so that you can handle 0 length input?

utils/configs.go Outdated
cli.BoolFlag{
Name: "use-syslog, syslog",
EnvVar: fmt.Sprintf("CONTIV_%s_USE_SYSLOG", binUpper),
Usage: fmt.Sprintf("set %s send log to syslog or not", binLower),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe say what true value does to be more explicit?
true value sends logs to syslog

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the parameter is pretty self-explained ... --use-syslog is actually not used as --use-syslog=True , it is if flag --use-syslog is provided, this var is treated as True.
I'll update the Usage to make it more clear

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k

command = "sudo " + self.binpath + "/netplugin --vlan-if eth2 --vlan-if eth3 " + fwd_arg + fwd_arg + store_args + args + "> /tmp/netplugin.log 2>&1"
# NOTE: this testing only used in mesos-docker
mode_args = " --fwdmode bridge --netmode vlan --mode docker "
command = "sudo " + self.binpath + "/netplugin --vlan-if eth2 --vlan-if eth3 " + mode_args + gen_cluster_store_args() + args + "> /tmp/netplugin.log 2>&1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd make it in different PR, codes under systest are pretty sick

} else {
storeArgs = " --consul-endpoints " + strings.Replace(cStore, "consul", "http", 1) + " "
storeArgs = " --consul-endpoints " + d.node.suite.basicInfo.ClusterStoreURLs + " "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d.node.suite.basicInfo.ClusterStoreURLs + " " could be added to storeArgs outside the if block, not asking for change, just sayin

storeArgs = " --consul-endpoints " + k.node.suite.basicInfo.ClusterStoreURLs + " "
}
return " --netmode " + netMode + " --fwdmode " + fwdMode + " --mode " + mode + storeArgs
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the only diff with docker setup tset is the mode? reuse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's due to the way how our sys testing was done, k8setpup_test.go and docker_test.go are separated scheduler and codes between them are not able to share, to fix this we need to introduce a new util and generates shared codes ... i'd leave it in different PR

}
return " --netmode " + netMode + " --fwdmode " + fwdMode + " --mode " + mode + storeArgs
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate?

var podName string
for retry := 60; retry > 0; retry-- {
// only get running pods name
podNameCmd := `kubectl -n kube-system get pods -o wide | grep Running | grep ` + podRegex + ` | grep ` + nodeName + ` | cut -d " " -f 1`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe there is a better kubectl query for this? dunno if this helps, there is also all that json path stuff

kubectl get pods --field-selector=status.phase=Running

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is better, what i did this this line is just added grep Running piece ... I'd add it into the systest improvement PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not supported until 1.9
kubernetes/kubernetes#50140

@chrisplo chrisplo dismissed their stale review December 5, 2017 03:30

reviewing own comments, none are truly blockers

Signed-off-by: Wei Tie <nuaafe@gmail.com>
@tiewei
Copy link
Contributor Author

tiewei commented Dec 5, 2017

Build PR

logrus.Infof("Starting netplugin on %s", w.node.Name())

cmd := "sudo " + w.node.suite.basicInfo.BinPath + "/netplugin --netmode " + netMode + " --fwdmode " + fwdMode + " --plugin-mode docker --vlan-if " + w.node.suite.hostInfo.HostDataInterfaces + storeArgs + args + "&> /tmp/netplugin.log"
cmd := "sudo " + w.node.suite.basicInfo.BinPath + "/netplugin --vlan-if " + w.node.suite.hostInfo.HostDataInterfaces + w.commonArgs() + args + "&> /tmp/netplugin.log"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap?

if w.node.suite.basicInfo.AciMode == "on" {
infraType = " --infra aci "
}
return w.node.tbnode.RunCommandBackground("sudo " + w.node.suite.basicInfo.BinPath + "/netmaster" + infraType + w.commonArgs() + " &> /tmp/netmaster.log")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap?


// FlattenFlags concatenate slices of flags into one slice
func FlattenFlags(flagSlices ...[]cli.Flag) []cli.Flag {
flags := flagSlices[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here, i thought i was on to something . . ok fine, though now I wonder if this could be ...[]interface{} arg and be a general purpose

Anyway . . . I'm back to first comment, start range at 0 so that you can handle 0 length input?

if [[ $# -gt 9 ]] && [[ $10 != "" ]]; then
shift; shift; shift; shift; shift; shift; shift; shift; shift
if [[ $# -gt 10 ]] && [[ ${11} != "" ]]; then
shift; shift; shift; shift; shift; shift; shift; shift; shift; shift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use shift 10 instead

@@ -166,7 +167,9 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
end
node.vm.provision "shell" do |s|
s.inline = provision_common_once
s.args = [node_name, node_addr, cluster_ip_nodes, ENV["http_proxy"] || "", ENV["https_proxy"] || "", ENV["NIGHTLY_RELEASE"] || "", ENV["CONTIV_CLUSTER_STORE"] || "etcd://localhost:2379", ENV["CONTIV_DOCKER_VERSION"] || "", ENV['CONTIV_NODE_OS'] || "", *ENV['CONTIV_ENV']]
s.args = [node_name, node_addr, cluster_ip_nodes, ENV["http_proxy"] || "", ENV["https_proxy"] || "", ENV["NIGHTLY_RELEASE"] || "", \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more than 80 here?

if [[ $# -gt 9 ]] && [[ $10 != "" ]]; then
shift; shift; shift; shift; shift; shift; shift; shift; shift
if [[ $# -gt 10 ]] && [[ ${11} != "" ]]; then
shift; shift; shift; shift; shift; shift; shift; shift; shift; shift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shift 10

internalAddr := strings.Split(d.ControlURL, ":")
if externalAddr[0] == "0.0.0.0" && externalAddr[1] == internalAddr[1] {
// ignore internal bind if external and internal are on the same port and external bind on 0.0.0.0
log.Infof("Ignore creating API listener on %q because %q covers it", d.ControlURL, d.ListenURL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nvm, I wasn't looking at the outer if restriction

cli.BoolFlag{
Name: "use-json-log, json-log",
EnvVar: fmt.Sprintf("CONTIV_%s_USE_JSON_LOG", binUpper),
Usage: fmt.Sprintf("set %s log format to json if this flag is provided", binLower),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is OK, but might be easier to read "format logs with json"

cli.BoolFlag{
Name: "use-syslog, syslog",
EnvVar: fmt.Sprintf("CONTIV_%s_USE_SYSLOG", binUpper),
Usage: fmt.Sprintf("set %s send log to syslog if this flag is provided", binLower),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is OK, but might be easier to read "send logs to syslog"

utils/configs.go Outdated
cli.BoolFlag{
Name: "use-syslog, syslog",
EnvVar: fmt.Sprintf("CONTIV_%s_USE_SYSLOG", binUpper),
Usage: fmt.Sprintf("set %s send log to syslog or not", binLower),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k

@chrisplo
Copy link
Contributor

chrisplo commented Dec 5, 2017

overall substance looks good, just some style nits

@tiewei tiewei merged commit 43d7b3f into contiv:master Dec 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants