Skip to content

Commit

Permalink
Add a simple progress report
Browse files Browse the repository at this point in the history
  • Loading branch information
bk201 authored and Vicente-Cheng committed Mar 20, 2024
1 parent abc028b commit 901398c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
27 changes: 20 additions & 7 deletions pkg/simulator/objects/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
supportbundlekit "github.com/rancher/support-bundle-kit/pkg/simulator/apis/supportbundlekit.io/v1"
)

type ProgressHandler func(int, int)

type ObjectManager struct {
ctx context.Context
path string
Expand Down Expand Up @@ -84,13 +86,16 @@ func (o *ObjectManager) CreateUnstructuredClusterObjects() error {
return err
}

progressMgr := NewProgressManager("Step 1/4: Cluster CRDs")

// apply CRDs first
err = o.ApplyObjects(crds, false, nil)
err = o.ApplyObjects(crds, false, nil, progressMgr.progress)
if err != nil {
return err
}

err = o.ApplyObjects(clusterObjs, true, nil)
progressMgr = NewProgressManager("Step 1/4: Cluster objects")
err = o.ApplyObjects(clusterObjs, true, nil, progressMgr.progress)

if err != nil {
return err
Expand All @@ -106,24 +111,31 @@ func (o *ObjectManager) CreateUnstructuredObjects() error {
return err
}

progressMgr := NewProgressManager("Step 2/4: Namespaced non-pods")
// apply non pods first
err = o.ApplyObjects(nonpods, true, nil)
err = o.ApplyObjects(nonpods, true, nil, progressMgr.progress)
if err != nil {
return err
}

err = o.ApplyObjects(pods, true, nil)
progressMgr = NewProgressManager("Step 2/4: Namespaced pods and events")
err = o.ApplyObjects(pods, true, nil, progressMgr.progress)
if err != nil {
return err
}
return nil
}

// ApplyObjects is a wrapper to convert runtime.Objects to unstructured.Unstructured, perform some housekeeping before submitting the same to apiserver
func (o *ObjectManager) ApplyObjects(objs []runtime.Object, patchStatus bool, skipGVR *schema.GroupVersionResource) error {
func (o *ObjectManager) ApplyObjects(objs []runtime.Object, patchStatus bool, skipGVR *schema.GroupVersionResource, progressHandler ProgressHandler) error {
var dr dynamic.ResourceInterface
var resp *unstructured.Unstructured
for _, v := range objs {
total := len(objs)
for i, v := range objs {
if progressHandler != nil {
progressHandler(i+1, total)
}

unstructuredObj, err := wranglerunstructured.ToUnstructured(v)
if err != nil {
return fmt.Errorf("error converting obj to unstructured %v", err)
Expand Down Expand Up @@ -387,8 +399,9 @@ func (o *ObjectManager) CreatedFailedObjectsList() error {
APIVersion: "supportbundlekit.io/v1",
},
}
progressMgr := NewProgressManager("Step 4/4: CreatedFailedObjectsList")
failedObject.Spec = o.failedObjs
return o.ApplyObjects([]runtime.Object{&failedObject}, false, nil)
return o.ApplyObjects([]runtime.Object{&failedObject}, false, nil, progressMgr.progress)
}

func (o *ObjectManager) addToFailedObjects(obj *unstructured.Unstructured, err error) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/simulator/objects/process_node_zips.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/sirupsen/logrus"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -72,12 +71,14 @@ func (o *ObjectManager) CreateNodeZipObjects() error {
return err
}

err = o.ApplyObjects(noStatusObjs, false, nil)
progressMgr := NewProgressManager("Step 3/4: NodeZipObjects (no status)")
err = o.ApplyObjects(noStatusObjs, false, nil, progressMgr.progress)
if err != nil {
return err
}

return o.ApplyObjects(withStatusObjs, true, nil)
progressMgr = NewProgressManager("Step 3/4: NodeZipObjects (status)")
return o.ApplyObjects(withStatusObjs, true, nil, progressMgr.progress)
}

// ProcessNodeZipObjects will read the contents of the zip file and generate associated runtime.Objects
Expand Down
30 changes: 30 additions & 0 deletions pkg/simulator/objects/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package objects

import (
"fmt"
"time"
)

type ProgressManager struct {
name string
start time.Time
}

func NewProgressManager(name string) *ProgressManager {
return &ProgressManager{
name: name,
}
}

func (p *ProgressManager) progress(current, total int) {
fmt.Printf("[%s] %d/%d\r", p.name, current, total)

if current == 1 {
p.start = time.Now()
}

if current == total {
fmt.Printf("\n")
fmt.Printf("Time to load all objects: %s seconds\n\n", time.Since(p.start))
}
}

0 comments on commit 901398c

Please sign in to comment.