Add possibility to specify 0 timeout via CLI options
Currently zero timeout won't be applied if specified via CLI options of commands plan run and plan run. This patch fixes it. Change-Id: Icad06ae8c71d78dc81821e2c5b0adb486d547fda Signed-off-by: Ruslan Aliev <raliev@mirantis.com> Relates-To: #566 Closes: #566
This commit is contained in:
parent
d844fdceca
commit
583fda5dfe
@ -16,6 +16,7 @@ package phase
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
"opendev.org/airship/airshipctl/pkg/config"
|
"opendev.org/airship/airshipctl/pkg/config"
|
||||||
"opendev.org/airship/airshipctl/pkg/phase"
|
"opendev.org/airship/airshipctl/pkg/phase"
|
||||||
@ -36,10 +37,8 @@ Run initinfra phase
|
|||||||
|
|
||||||
// NewRunCommand creates a command to run specific phase
|
// NewRunCommand creates a command to run specific phase
|
||||||
func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
|
func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
|
||||||
p := &phase.RunCommand{
|
p := &phase.RunCommand{Factory: cfgFactory}
|
||||||
Factory: cfgFactory,
|
f := &phase.RunFlags{}
|
||||||
Options: phase.RunFlags{},
|
|
||||||
}
|
|
||||||
|
|
||||||
runCmd := &cobra.Command{
|
runCmd := &cobra.Command{
|
||||||
Use: "run PHASE_NAME",
|
Use: "run PHASE_NAME",
|
||||||
@ -48,12 +47,22 @@ func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Example: runExample,
|
Example: runExample,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
p.Options.PhaseID.Name = args[0]
|
p.PhaseID.Name = args[0]
|
||||||
|
// Go through all the flags that have been set
|
||||||
|
fn := func(flag *pflag.Flag) {
|
||||||
|
switch flag.Name {
|
||||||
|
case "dry-run":
|
||||||
|
p.Options.DryRun = f.DryRun
|
||||||
|
case "wait-timeout":
|
||||||
|
p.Options.Timeout = &f.Timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd.Flags().Visit(fn)
|
||||||
return p.RunE()
|
return p.RunE()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
flags := runCmd.Flags()
|
flags := runCmd.Flags()
|
||||||
flags.BoolVar(&p.Options.DryRun, "dry-run", false, "simulate phase execution")
|
flags.BoolVar(&f.DryRun, "dry-run", false, "simulate phase execution")
|
||||||
flags.DurationVar(&p.Options.Timeout, "wait-timeout", 0, "wait timeout")
|
flags.DurationVar(&f.Timeout, "wait-timeout", 0, "wait timeout")
|
||||||
return runCmd
|
return runCmd
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ package plan
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
"opendev.org/airship/airshipctl/pkg/config"
|
"opendev.org/airship/airshipctl/pkg/config"
|
||||||
"opendev.org/airship/airshipctl/pkg/phase"
|
"opendev.org/airship/airshipctl/pkg/phase"
|
||||||
@ -37,10 +38,9 @@ Perform a dry run of a plan
|
|||||||
|
|
||||||
// NewRunCommand creates a command which execute a particular phase plan
|
// NewRunCommand creates a command which execute a particular phase plan
|
||||||
func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
|
func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
|
||||||
r := &phase.PlanRunCommand{
|
r := &phase.PlanRunCommand{Factory: cfgFactory}
|
||||||
Factory: cfgFactory,
|
f := &phase.RunFlags{}
|
||||||
Options: phase.PlanRunFlags{},
|
|
||||||
}
|
|
||||||
runCmd := &cobra.Command{
|
runCmd := &cobra.Command{
|
||||||
Use: "run PLAN_NAME",
|
Use: "run PLAN_NAME",
|
||||||
Short: "Airshipctl command to run plan",
|
Short: "Airshipctl command to run plan",
|
||||||
@ -48,13 +48,22 @@ func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
|
|||||||
Example: runExample,
|
Example: runExample,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
r.Options.PlanID.Name = args[0]
|
r.PlanID.Name = args[0]
|
||||||
|
fn := func(flag *pflag.Flag) {
|
||||||
|
switch flag.Name {
|
||||||
|
case "dry-run":
|
||||||
|
r.Options.DryRun = f.DryRun
|
||||||
|
case "wait-timeout":
|
||||||
|
r.Options.Timeout = &f.Timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd.Flags().Visit(fn)
|
||||||
return r.RunE()
|
return r.RunE()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := runCmd.Flags()
|
flags := runCmd.Flags()
|
||||||
flags.BoolVar(&r.Options.DryRun, "dry-run", false, "simulate phase execution")
|
flags.BoolVar(&f.DryRun, "dry-run", false, "simulate phase execution")
|
||||||
flags.DurationVar(&r.Options.Timeout, "wait-timeout", 0, "wait timeout")
|
flags.DurationVar(&f.Timeout, "wait-timeout", 0, "wait timeout")
|
||||||
return runCmd
|
return runCmd
|
||||||
}
|
}
|
||||||
|
@ -45,12 +45,12 @@ type GenericRunFlags struct {
|
|||||||
// RunFlags options for phase run command
|
// RunFlags options for phase run command
|
||||||
type RunFlags struct {
|
type RunFlags struct {
|
||||||
GenericRunFlags
|
GenericRunFlags
|
||||||
PhaseID ifc.ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunCommand phase run command
|
// RunCommand phase run command
|
||||||
type RunCommand struct {
|
type RunCommand struct {
|
||||||
Options RunFlags
|
PhaseID ifc.ID
|
||||||
|
Options ifc.RunOptions
|
||||||
Factory config.Factory
|
Factory config.Factory
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,11 +68,11 @@ func (c *RunCommand) RunE() error {
|
|||||||
|
|
||||||
client := NewClient(helper)
|
client := NewClient(helper)
|
||||||
|
|
||||||
phase, err := client.PhaseByID(c.Options.PhaseID)
|
phase, err := client.PhaseByID(c.PhaseID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return phase.Run(ifc.RunOptions{DryRun: c.Options.DryRun, Timeout: c.Options.Timeout})
|
return phase.Run(c.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListCommand phase list command
|
// ListCommand phase list command
|
||||||
@ -205,12 +205,12 @@ func (c *PlanListCommand) RunE() error {
|
|||||||
// PlanRunFlags options for phase run command
|
// PlanRunFlags options for phase run command
|
||||||
type PlanRunFlags struct {
|
type PlanRunFlags struct {
|
||||||
GenericRunFlags
|
GenericRunFlags
|
||||||
PlanID ifc.ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlanRunCommand phase run command
|
// PlanRunCommand phase run command
|
||||||
type PlanRunCommand struct {
|
type PlanRunCommand struct {
|
||||||
Options PlanRunFlags
|
PlanID ifc.ID
|
||||||
|
Options ifc.RunOptions
|
||||||
Factory config.Factory
|
Factory config.Factory
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,11 +228,11 @@ func (c *PlanRunCommand) RunE() error {
|
|||||||
|
|
||||||
client := NewClient(helper)
|
client := NewClient(helper)
|
||||||
|
|
||||||
plan, err := client.PlanByID(c.Options.PlanID)
|
plan, err := client.PlanByID(c.PlanID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return plan.Run(ifc.RunOptions{DryRun: c.Options.DryRun, Timeout: c.Options.Timeout})
|
return plan.Run(c.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClusterListCommand options for cluster list command
|
// ClusterListCommand options for cluster list command
|
||||||
|
@ -43,7 +43,7 @@ func TestRunCommand(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
errContains string
|
errContains string
|
||||||
runFlags phase.RunFlags
|
runFlags ifc.RunOptions
|
||||||
factory config.Factory
|
factory config.Factory
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -492,13 +492,11 @@ func TestPlanRunCommand(t *testing.T) {
|
|||||||
tt := tc
|
tt := tc
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := phase.PlanRunCommand{
|
cmd := phase.PlanRunCommand{
|
||||||
Options: phase.PlanRunFlags{
|
Options: ifc.RunOptions{
|
||||||
GenericRunFlags: phase.GenericRunFlags{
|
DryRun: true,
|
||||||
DryRun: true,
|
|
||||||
},
|
|
||||||
PlanID: tt.planID,
|
|
||||||
},
|
},
|
||||||
Factory: tt.factory,
|
Factory: tt.factory,
|
||||||
|
PlanID: tt.planID,
|
||||||
}
|
}
|
||||||
err := cmd.RunE()
|
err := cmd.RunE()
|
||||||
if tt.expectedErr != "" {
|
if tt.expectedErr != "" {
|
||||||
|
@ -125,8 +125,8 @@ func toCommandOptions(i inventoryifc.Inventory,
|
|||||||
spec v1alpha1.BaremetalManagerSpec,
|
spec v1alpha1.BaremetalManagerSpec,
|
||||||
opts ifc.RunOptions) *inventory.CommandOptions {
|
opts ifc.RunOptions) *inventory.CommandOptions {
|
||||||
timeout := time.Duration(spec.Timeout) * time.Second
|
timeout := time.Duration(spec.Timeout) * time.Second
|
||||||
if opts.Timeout != 0 {
|
if opts.Timeout != nil {
|
||||||
timeout = opts.Timeout
|
timeout = *opts.Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
return &inventory.CommandOptions{
|
return &inventory.CommandOptions{
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
@ -89,6 +90,7 @@ func TestNewBMHExecutor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBMHExecutorRun(t *testing.T) {
|
func TestBMHExecutorRun(t *testing.T) {
|
||||||
|
timeout := time.Duration(40)
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
expectedErr string
|
expectedErr string
|
||||||
@ -102,7 +104,7 @@ func TestBMHExecutorRun(t *testing.T) {
|
|||||||
runOptions: ifc.RunOptions{
|
runOptions: ifc.RunOptions{
|
||||||
DryRun: true,
|
DryRun: true,
|
||||||
// any value but zero
|
// any value but zero
|
||||||
Timeout: 40,
|
Timeout: &timeout,
|
||||||
},
|
},
|
||||||
execDoc: executorDoc(t, fmt.Sprintf(bmhExecutorTemplate, "unknown", "")),
|
execDoc: executorDoc(t, fmt.Sprintf(bmhExecutorTemplate, "unknown", "")),
|
||||||
inventory: testBaremetalInventory(),
|
inventory: testBaremetalInventory(),
|
||||||
|
@ -92,8 +92,8 @@ func (e *KubeApplierExecutor) Run(ch chan events.Event, runOpts ifc.RunOptions)
|
|||||||
dryRunStrategy = common.DryRunClient
|
dryRunStrategy = common.DryRunClient
|
||||||
}
|
}
|
||||||
timeout := time.Second * time.Duration(e.apiObject.Config.WaitOptions.Timeout)
|
timeout := time.Second * time.Duration(e.apiObject.Config.WaitOptions.Timeout)
|
||||||
if int64(runOpts.Timeout/time.Second) != 0 {
|
if runOpts.Timeout != nil {
|
||||||
timeout = runOpts.Timeout
|
timeout = *runOpts.Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("WaitTimeout: %v", timeout)
|
log.Debugf("WaitTimeout: %v", timeout)
|
||||||
|
@ -38,10 +38,8 @@ type ExecutorStatus struct{}
|
|||||||
|
|
||||||
// RunOptions holds options for run method
|
// RunOptions holds options for run method
|
||||||
type RunOptions struct {
|
type RunOptions struct {
|
||||||
DryRun bool
|
DryRun bool
|
||||||
Progress bool
|
Timeout *time.Duration
|
||||||
|
|
||||||
Timeout time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderOptions holds options for render method
|
// RenderOptions holds options for render method
|
||||||
|
Loading…
x
Reference in New Issue
Block a user