Skip to content

Commit

Permalink
OPCT-226: cmd/status: Adding flag to set the watch interval (#87)
Browse files Browse the repository at this point in the history
The interval watching, and printing in the stdout is not customized
leading to a high amount of messages in the stdout - it is good for
troubleshooting purpose, but a downside is it increases the amount of
logs stored in CI with actionable information.

This PR introduces a possibility to set the flag `--watch-interval=int`
to the `status` command, also called by the `run` command.

This change is isolated from the `report` enhancement covered in #76
  • Loading branch information
mtulio committed Oct 17, 2023
1 parent 33892e9 commit fb7164a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/retrieve/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewCmdRetrieve() *cobra.Command {
return
}

s := status.NewStatusOptions(false)
s := status.NewStatusOptions(&status.StatusInput{Watch: false})
err = s.PreRunCheck(kclient)
if err != nil {
log.Error(err)
Expand Down
18 changes: 10 additions & 8 deletions pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ type RunOptions struct {
// PluginsImage
// defines the image containing plugins associated with the provider-certification-tool.
// this variable is referenced by plugin manifest templates to dynamically reference the plugins image.
PluginsImage string
timeout int
watch bool
devCount string
mode string
upgradeImage string
PluginsImage string
timeout int
watch bool
watchInterval int
devCount string
mode string
upgradeImage string
}

const (
Expand Down Expand Up @@ -110,9 +111,9 @@ func NewCmdRun() *cobra.Command {
}

// Sleep to give status time to appear
time.Sleep(status.StatusInterval)
s := status.NewStatusOptions(&status.StatusInput{Watch: o.watch, IntervalSeconds: o.watchInterval})
time.Sleep(s.GetIntervalSeconds())

s := status.NewStatusOptions(o.watch)
err = s.WaitForStatusReport(cmd.Context(), sclient)
if err != nil {
log.WithError(err).Fatal("error retrieving aggregator status")
Expand Down Expand Up @@ -144,6 +145,7 @@ func NewCmdRun() *cobra.Command {
cmd.Flags().StringVar(&o.imageRepository, "image-repository", "", "Image repository containing required images test environment. Example: openshift-provider-cert-tool --mirror-repository mirror.repository.net/ocp-cert")
cmd.Flags().IntVar(&o.timeout, "timeout", defaultRunTimeoutSeconds, "Execution timeout in seconds")
cmd.Flags().BoolVarP(&o.watch, "watch", "w", defaultRunWatchFlag, "Keep watch status after running")
cmd.Flags().IntVarP(&o.watchInterval, "watch-interval", "", status.DefaultStatusIntervalSeconds, "Interval to watch the status and print in the stdout")

// Hide optional flags
hideOptionalFlags(cmd, "dedicated")
Expand Down
42 changes: 33 additions & 9 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,44 @@ import (
)

const (
StatusInterval = time.Second * 10
StatusRetryLimit = 10
DefaultStatusIntervalSeconds = 10
StatusRetryLimit = 10
)

// StatusOptions is the interface to store input options to
// interface with Status command.
type StatusOptions struct {
Latest *aggregation.Status
watch bool
shownPostProcessMsg bool
watchInterval int
waitInterval time.Duration
}

func NewStatusOptions(watch bool) *StatusOptions {
return &StatusOptions{
watch: watch,
// StatusInput is the interface to input options when
// creating status object.
type StatusInput struct {
Watch bool
IntervalSeconds int
}

func NewStatusOptions(in *StatusInput) *StatusOptions {
s := &StatusOptions{
watch: in.Watch,
waitInterval: time.Second * DefaultStatusIntervalSeconds,
}
if in.IntervalSeconds != 0 {
s.waitInterval = time.Duration(in.IntervalSeconds) * time.Second
}
return s
}

func (s *StatusOptions) GetIntervalSeconds() time.Duration {
return s.waitInterval
}

func NewCmdStatus() *cobra.Command {
o := NewStatusOptions(false)
o := NewStatusOptions(&StatusInput{Watch: false})

cmd := &cobra.Command{
Use: "status",
Expand Down Expand Up @@ -81,6 +101,10 @@ func NewCmdStatus() *cobra.Command {
}

cmd.PersistentFlags().BoolVarP(&o.watch, "watch", "w", false, "Keep watch status after running")
cmd.Flags().IntVarP(&o.watchInterval, "watch-interval", "", DefaultStatusIntervalSeconds, "Interval to watch the status and print in the stdout")
if o.watchInterval != DefaultStatusIntervalSeconds {
o.waitInterval = time.Duration(o.watchInterval) * time.Second
}

return cmd
}
Expand Down Expand Up @@ -139,7 +163,7 @@ func (s *StatusOptions) GetStatus() string {
// An error will not result in immediate failure and will be retried.
func (s *StatusOptions) WaitForStatusReport(ctx context.Context, sclient sonobuoyclient.Interface) error {
tries := 1
err := wait2.PollImmediateUntilWithContext(ctx, StatusInterval, func(ctx context.Context) (done bool, err error) {
err := wait2.PollImmediateUntilWithContext(ctx, s.waitInterval, func(ctx context.Context) (done bool, err error) {
if tries == StatusRetryLimit {
return false, errors.New("retry limit reached checking for aggregator status")
}
Expand All @@ -152,7 +176,7 @@ func (s *StatusOptions) WaitForStatusReport(ctx context.Context, sclient sonobuo
}

tries++
log.Warnf("waiting %ds to retry", int(StatusInterval.Seconds()))
log.Warnf("waiting %ds to retry", int(s.waitInterval.Seconds()))
return false, nil
})
return err
Expand All @@ -165,7 +189,7 @@ func (s *StatusOptions) Print(cmd *cobra.Command, sclient sonobuoyclient.Interfa
}

tries := 1
return wait2.PollImmediateInfiniteWithContext(cmd.Context(), StatusInterval, func(ctx context.Context) (done bool, err error) {
return wait2.PollImmediateInfiniteWithContext(cmd.Context(), s.waitInterval, func(ctx context.Context) (done bool, err error) {
if tries == StatusRetryLimit {
// we hit back-to-back errors too many times.
return true, errors.New("retry limit reached checking status")
Expand Down

0 comments on commit fb7164a

Please sign in to comment.