From fa0179292a9a6f4121c69be36d12f6305c55443b Mon Sep 17 00:00:00 2001 From: "Renan I. Del Valle" Date: Wed, 29 Jan 2020 16:12:53 -0800 Subject: [PATCH] Refactoring start sla-drain (#7) Changing sla-drain command to be less cumbersome. sla-drain command now takes --count or --percentage and they are mutually exclusive. --- CHANGELOG | 3 ++ cmd/root.go | 4 +- cmd/start.go | 102 +++++++++++++++++++---------------------------- debian/changelog | 7 +++- go.mod | 2 + 5 files changed, 55 insertions(+), 63 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 638b9a6..4e3328c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ 0.0.9 (unreleased) * Added ability to create jobs which contain an executorless docker container. +* Sla-aware draining sub-command has been simplified. Instead of having a count/percentage + subcommand, it now has a flag for each of these options. The count and percentage flag are + mutually exclusive, and one of them has to be set. 0.0.8 diff --git a/cmd/root.go b/cmd/root.go index 756567e..916da5d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -80,7 +80,9 @@ var rootCmd = &cobra.Command{ } func Execute() { - rootCmd.Execute() + if err := rootCmd.Execute(); err != nil { + log.Fatal(err) + } } // TODO(rdelvalle): Move more from connect into this function diff --git a/cmd/start.go b/cmd/start.go index bad3814..f32ddb7 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -21,6 +21,9 @@ import ( "github.com/spf13/cobra" ) +const countFlag = "count" +const percentageFlag = "percentage" + func init() { rootCmd.AddCommand(startCmd) @@ -32,28 +35,17 @@ func init() { startDrainCmd.cmd.Flags().DurationVar(&startDrainCmd.monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.") startDrainCmd.cmd.Flags().DurationVar(&startDrainCmd.monitorTimeout, "timeout", time.Minute*10, "Time after which the monitor will stop polling and throw an error.") - startCmd.AddCommand(startSLADrainCmd) - /* SLA Aware commands */ - startSLADrainCmd.AddCommand(startSLACountDrainCmd.cmd) - startSLACountDrainCmd.cmd.Run = SLACountDrain + startCmd.AddCommand(startSLADrainCmd.cmd) + startSLADrainCmd.cmd.Run = slaDrain // SLA Maintenance specific flags - startSLACountDrainCmd.cmd.Flags().DurationVar(&startSLACountDrainCmd.monitorInterval, "interval", time.Second*10, "Interval at which to poll scheduler.") - startSLACountDrainCmd.cmd.Flags().DurationVar(&startSLACountDrainCmd.monitorTimeout, "timeout", time.Minute*20, "Time after which the monitor will stop polling and throw an error.") - startSLACountDrainCmd.cmd.Flags().Int64Var(&count, "count", 5, "Instances count that should be running to meet SLA.") - startSLACountDrainCmd.cmd.Flags().DurationVar(&duration, "duration", time.Minute*1, "Minimum time duration a task needs to be `RUNNING` to be treated as active.") - startSLACountDrainCmd.cmd.Flags().DurationVar(&forceDrainTimeout, "sla-limit", time.Minute*60, "Time limit after which SLA-Aware drain sheds SLA Awareness.") - - startSLADrainCmd.AddCommand(startSLAPercentageDrainCmd.cmd) - startSLAPercentageDrainCmd.cmd.Run = SLAPercentageDrain - - // SLA Maintenance specific flags - startSLAPercentageDrainCmd.cmd.Flags().DurationVar(&startSLAPercentageDrainCmd.monitorInterval, "interval", time.Second*10, "Interval at which to poll scheduler.") - startSLAPercentageDrainCmd.cmd.Flags().DurationVar(&startSLAPercentageDrainCmd.monitorTimeout, "timeout", time.Minute*20, "Time after which the monitor will stop polling and throw an error.") - startSLAPercentageDrainCmd.cmd.Flags().Float64Var(&percent, "percent", 80.0, "Percentage of instances that should be running to meet SLA.") - startSLAPercentageDrainCmd.cmd.Flags().DurationVar(&duration, "duration", time.Minute*1, "Minimum time duration a task needs to be `RUNNING` to be treated as active.") - startSLAPercentageDrainCmd.cmd.Flags().DurationVar(&forceDrainTimeout, "sla-limit", time.Minute*60, "Time limit after which SLA-Aware drain sheds SLA Awareness.") + startSLADrainCmd.cmd.Flags().Int64Var(&count, countFlag, 5, "Instances count that should be running to meet SLA.") + startSLADrainCmd.cmd.Flags().Float64Var(&percent, percentageFlag, 80.0, "Percentage of instances that should be running to meet SLA.") + startSLADrainCmd.cmd.Flags().DurationVar(&duration, "duration", time.Minute*1, "Minimum time duration a task needs to be `RUNNING` to be treated as active.") + startSLADrainCmd.cmd.Flags().DurationVar(&forceDrainTimeout, "sla-limit", time.Minute*60, "Time limit after which SLA-Aware drain sheds SLA Awareness.") + startSLADrainCmd.cmd.Flags().DurationVar(&startSLADrainCmd.monitorInterval, "interval", time.Second*10, "Interval at which to poll scheduler.") + startSLADrainCmd.cmd.Flags().DurationVar(&startSLADrainCmd.monitorTimeout, "timeout", time.Minute*20, "Time after which the monitor will stop polling and throw an error.") startCmd.AddCommand(startMaintenanceCmd.cmd) startMaintenanceCmd.cmd.Run = maintenance @@ -80,30 +72,17 @@ expects a space separated list of hosts to place into maintenance mode.`, }, } -var startSLADrainCmd = &cobra.Command{ - Use: "sla-drain", - Short: "Place a list of space separated Mesos Agents into maintenance mode using SLA aware strategies.", - Long: `Adds a Mesos Agent to Aurora's Drain list. Agents in this list +var startSLADrainCmd = monitorCmdConfig{ + cmd: &cobra.Command{ + Use: "sla-drain [space separated host list]", + Short: "Place a list of space separated Mesos Agents into maintenance mode using SLA aware strategies.", + Long: `Adds a Mesos Agent to Aurora's Drain list. Agents in this list are not allowed to schedule new tasks and any tasks already running on this Agent are killed and rescheduled in an Agent that is not in maintenance mode. Command -expects a space separated list of hosts to place into maintenance mode.`, -} - -var startSLACountDrainCmd = monitorCmdConfig{ - cmd: &cobra.Command{ - Use: "count [space separated host list]", - Short: "Place a list of space separated Mesos Agents into maintenance mode using the count SLA aware policy as a fallback.", - Long: `Adds a Mesos Agent to Aurora's Drain list. Tasks will be drained using the count SLA policy as a fallback -when a Job does not have a defined SLA policy.`, - Args: cobra.MinimumNArgs(1), - }, -} - -var startSLAPercentageDrainCmd = monitorCmdConfig{ - cmd: &cobra.Command{ - Use: "percentage [space separated host list]", - Short: "Place a list of space separated Mesos Agents into maintenance mode using the percentage SLA aware policy as a fallback.", - Long: `Adds a Mesos Agent to Aurora's Drain list. Tasks will be drained using the percentage SLA policy as a fallback +expects a space separated list of hosts to place into maintenance mode. +If the --count argument is passed, tasks will be drained using the count SLA policy as a fallback +when a Job does not have a defined SLA policy. +If the --percentage argument is passed, tasks will be drained using the percentage SLA policy as a fallback when a Job does not have a defined SLA policy.`, Args: cobra.MinimumNArgs(1), }, @@ -125,7 +104,7 @@ func drain(cmd *cobra.Command, args []string) { log.Infoln(args) result, err := client.DrainHosts(args...) if err != nil { - log.Fatalf("error: %+v\n", err) + log.Fatalf("error: %+v", err) } log.Debugln(result) @@ -145,7 +124,7 @@ func drain(cmd *cobra.Command, args []string) { } } -func slaDrain(policy *aurora.SlaPolicy, interval, timeout time.Duration, hosts ...string) { +func slaDrainHosts(policy *aurora.SlaPolicy, interval, timeout time.Duration, hosts ...string) { result, err := client.SLADrainHosts(policy, int64(forceDrainTimeout.Seconds()), hosts...) if err != nil { @@ -168,27 +147,30 @@ func slaDrain(policy *aurora.SlaPolicy, interval, timeout time.Duration, hosts . log.Fatalf("error: %+v", err) } } +func slaDrain(cmd *cobra.Command, args []string) { + // This check makes sure only a single flag is set. + // If they're both set or both not set, the statement will evaluate to true. + if cmd.Flags().Changed(percentageFlag) == cmd.Flags().Changed(countFlag) { + log.Fatal("Either percentage or count must be set exclusively.") + } -func SLACountDrain(cmd *cobra.Command, args []string) { - log.Infoln("Setting hosts to DRAINING with the Count SLA policy.") - log.Infoln(args) + policy := &aurora.SlaPolicy{} - slaDrain(&aurora.SlaPolicy{ - CountSlaPolicy: &aurora.CountSlaPolicy{Count: count, DurationSecs: int64(duration.Seconds())}}, - startSLACountDrainCmd.monitorInterval, - startSLACountDrainCmd.monitorTimeout, - args...) -} + if cmd.Flags().Changed(percentageFlag) { + log.Infoln("Setting hosts to DRAINING with the Percentage SLA policy.") + policy.PercentageSlaPolicy = &aurora.PercentageSlaPolicy{ + Percentage: percent, + DurationSecs: int64(duration.Seconds()), + } + } -func SLAPercentageDrain(cmd *cobra.Command, args []string) { - log.Infoln("Setting hosts to DRAINING with the Percentage SLA policy.") - log.Infoln(args) + if cmd.Flags().Changed(countFlag) { + log.Infoln("Setting hosts to DRAINING with the Count SLA policy.") + policy.CountSlaPolicy = &aurora.CountSlaPolicy{Count: count, DurationSecs: int64(duration.Seconds())} + } - slaDrain(&aurora.SlaPolicy{ - PercentageSlaPolicy: &aurora.PercentageSlaPolicy{Percentage: percent, DurationSecs: int64(duration.Seconds())}}, - startSLAPercentageDrainCmd.monitorInterval, - startSLAPercentageDrainCmd.monitorTimeout, - args...) + log.Infoln("Hosts affected: ", args) + slaDrainHosts(policy, startDrainCmd.monitorInterval, startDrainCmd.monitorTimeout, args...) } func maintenance(cmd *cobra.Command, args []string) { diff --git a/debian/changelog b/debian/changelog index 06f361c..b289234 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,11 @@ australis (0.0.9) unstable; urgency=medium - * Unreleased + * added ability to create jobs which contain an executorless docker container. + * sla-aware draining sub-command has been simplified. instead of having a count/percentage + subcommand, it now has a flag for each of these options. the count and percentage flag are + mutually exclusive, and one of them has to be set. - -- Renan DelValle Fri, 25 Mar 2019 15:10:00 -0700 + -- Renan DelValle Wed, 29 Jan 2020 15:10:00 -0700 australis (0.0.8) unstable; urgency=medium diff --git a/go.mod b/go.mod index 606dea2..bcc57ae 100644 --- a/go.mod +++ b/go.mod @@ -12,3 +12,5 @@ require ( github.com/spf13/viper v1.3.1 gopkg.in/yaml.v2 v2.2.2 ) + +go 1.13