Skip to content

Commit

Permalink
feat: make phaseCollectPrometheusBundle optional
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <jack.yu@suse.com>
  • Loading branch information
Yu-Jack committed Jul 15, 2024
1 parent b36c74e commit d108fcc
Showing 1 changed file with 60 additions and 19 deletions.
79 changes: 60 additions & 19 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ type SupportBundleManager struct {
expectedNodes map[string]string
}

type RunPhase struct {
Name types.ManagerPhase
Run func() error
}

func (m *SupportBundleManager) check() error {
if len(m.Namespaces) == 0 || len(m.Namespaces[0]) == 0 {
return errors.New("namespace is not specified")
Expand Down Expand Up @@ -108,10 +113,7 @@ func (m *SupportBundleManager) getBundlefilesize() (int64, error) {
}

func (m *SupportBundleManager) Run() error {
phases := []struct {
Name types.ManagerPhase
Run func() error
}{
requiredPhases := []RunPhase{
{
types.ManagerPhaseInit,
m.phaseInit,
Expand All @@ -120,14 +122,23 @@ func (m *SupportBundleManager) Run() error {
types.ManagerPhaseClusterBundle,
m.phaseCollectClusterBundle,
},
{
types.ManagerPhasePrometheusBundle,
m.phaseCollectPrometheusBundle,
},

{
types.ManagerPhaseNodeBundle,
m.phaseCollectNodeBundles,
},
}

// optionalPhases should have independent phases
// if logic is dependent, put it into one function
optionalPhases := []RunPhase{
{
types.ManagerPhasePrometheusBundle,
m.phaseCollectPrometheusBundle,
},
}

postPhases := []RunPhase{
{
types.ManagerPhasePackaging,
m.phasePackaging,
Expand All @@ -138,21 +149,51 @@ func (m *SupportBundleManager) Run() error {
},
}

for i, phase := range phases {
logrus.Infof("Running phase %s", phase.Name)
m.status.SetPhase(phase.Name)
if err := phase.Run(); err != nil {
m.status.SetError(err.Error())
logrus.Errorf("Failed to run phase %s: %s", phase.Name, err.Error())
break
m.runAllPhases(requiredPhases, optionalPhases, postPhases)

<-m.context.Done()
return nil
}

func (m *SupportBundleManager) runAllPhases(requiredPhases []RunPhase, optionalPhases []RunPhase, postPhases []RunPhase) {
progressCount := 0
maxProgressCount := len(requiredPhases) + len(optionalPhases) + len(postPhases)

for _, phase := range requiredPhases {
if err := m.runPhase(phase, &progressCount, maxProgressCount); err != nil {
logrus.Errorf("Failed to run requiredPhases %s: %s", phase.Name, err.Error())
return
}
}

progress := 100 * (i + 1) / len(phases)
m.status.SetProgress(progress)
logrus.Infof("Succeed to run phase %s. Progress (%d).", phase.Name, progress)
for _, phase := range optionalPhases {
if err := m.runPhase(phase, &progressCount, maxProgressCount); err != nil {
logrus.Errorf("Failed to run optionalPhases %s: %s", phase.Name, err.Error())
// Since it's optional, don't return error.
continue
}
}

<-m.context.Done()
for _, phase := range postPhases {
if err := m.runPhase(phase, &progressCount, maxProgressCount); err != nil {
logrus.Errorf("Failed to run postPhases %s: %s", phase.Name, err.Error())
return
}
}
}

func (m *SupportBundleManager) runPhase(phase RunPhase, progressCount *int, maxProgressCount int) error {
logrus.Infof("Running phase %s", phase.Name)
m.status.SetPhase(phase.Name)
if err := phase.Run(); err != nil {
m.status.SetError(err.Error())
logrus.Errorf("Failed to run phase %s: %s", phase.Name, err.Error())
return err
}
*progressCount++
progress := 100 * (*progressCount) / maxProgressCount
m.status.SetProgress(progress)
logrus.Infof("Succeed to run phase %s. Progress (%d).", phase.Name, progress)
return nil
}

Expand Down

0 comments on commit d108fcc

Please sign in to comment.