Skip to content

Commit

Permalink
Set writer for printer
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <[email protected]>
  • Loading branch information
qiujian16 committed Dec 24, 2024
1 parent 900d29c commit 0b56729
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
6 changes: 5 additions & 1 deletion pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ func (o *Options) run() error {
}

if !o.ClusteradmFlags.DryRun {
if err := helperwait.WaitUntilCRDReady(apiExtensionsClient, "clustermanagers.operator.open-cluster-management.io", o.wait); err != nil {
if err := helperwait.WaitUntilCRDReady(
o.Streams.Out, apiExtensionsClient, "clustermanagers.operator.open-cluster-management.io", o.wait); err != nil {
return err
}
}
if o.wait && !o.ClusteradmFlags.DryRun {
if err := helperwait.WaitUntilRegistrationOperatorReady(
o.Streams.Out,
o.ClusteradmFlags.KubectlFactory,
int64(o.ClusteradmFlags.Timeout)); err != nil {
return err
Expand All @@ -202,6 +204,7 @@ func (o *Options) run() error {

if o.wait && !o.ClusteradmFlags.DryRun {
if err := helperwait.WaitUntilClusterManagerRegistrationReady(
o.Streams.Out,
o.ClusteradmFlags.KubectlFactory,
int64(o.ClusteradmFlags.Timeout)); err != nil {
return err
Expand Down Expand Up @@ -318,6 +321,7 @@ func (o *Options) deploySingletonControlplane(kubeClient kubernetes.Interface) e
// fetch the kubeconfig and get the token
if o.wait && !o.ClusteradmFlags.DryRun {
if err := helperwait.WaitUntilMulticlusterControlplaneReady(
o.Streams.Out,
o.ClusteradmFlags.KubectlFactory,
o.SingletonName,
int64(o.ClusteradmFlags.Timeout)); err != nil {
Expand Down
21 changes: 15 additions & 6 deletions pkg/cmd/join/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/pem"
"fmt"
gherrors "github.com/pkg/errors"
"io"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -395,26 +396,30 @@ func (o *Options) applyKlusterlet(r *reader.ResourceReader, operatorClient opera
}

if !o.ClusteradmFlags.DryRun {
if err := wait.WaitUntilCRDReady(apiExtensionsClient, "klusterlets.operator.open-cluster-management.io", o.wait); err != nil {
if err := wait.WaitUntilCRDReady(
o.Streams.Out, apiExtensionsClient, "klusterlets.operator.open-cluster-management.io", o.wait); err != nil {
return err
}
}

if !available && o.wait && !o.ClusteradmFlags.DryRun {
err = waitUntilRegistrationOperatorConditionIsTrue(o.ClusteradmFlags.KubectlFactory, int64(o.ClusteradmFlags.Timeout))
err = waitUntilRegistrationOperatorConditionIsTrue(
o.Streams.Out, o.ClusteradmFlags.KubectlFactory, int64(o.ClusteradmFlags.Timeout))
if err != nil {
return err
}
}

if o.wait && !o.ClusteradmFlags.DryRun {
if o.mode == string(operatorv1.InstallModeHosted) {
err = waitUntilKlusterletConditionIsTrue(operatorClient, int64(o.ClusteradmFlags.Timeout), o.klusterletChartConfig.Klusterlet.Name)
err = waitUntilKlusterletConditionIsTrue(
o.Streams.Out, operatorClient, int64(o.ClusteradmFlags.Timeout), o.klusterletChartConfig.Klusterlet.Name)
if err != nil {
return err
}
} else {
err = waitUntilKlusterletConditionIsTrue(operatorClient, int64(o.ClusteradmFlags.Timeout), o.klusterletChartConfig.Klusterlet.Name)
err = waitUntilKlusterletConditionIsTrue(
o.Streams.Out, operatorClient, int64(o.ClusteradmFlags.Timeout), o.klusterletChartConfig.Klusterlet.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -475,6 +480,7 @@ func (o *Options) waitUntilManagedClusterIsCreated(timeout int64, clusterName st
phase := &atomic.Value{}
phase.Store("")
operatorSpinner := printer.NewSpinnerWithStatus(
o.Streams.Out,
"Waiting for managed cluster to be created...",
time.Millisecond*500,
"Managed cluster is created.\n",
Expand Down Expand Up @@ -505,7 +511,7 @@ func (o *Options) waitUntilManagedClusterIsCreated(timeout int64, clusterName st
})
}

func waitUntilRegistrationOperatorConditionIsTrue(f util.Factory, timeout int64) error {
func waitUntilRegistrationOperatorConditionIsTrue(w io.Writer, f util.Factory, timeout int64) error {
var restConfig *rest.Config
restConfig, err := f.ToRESTConfig()
if err != nil {
Expand All @@ -519,6 +525,7 @@ func waitUntilRegistrationOperatorConditionIsTrue(f util.Factory, timeout int64)
phase := &atomic.Value{}
phase.Store("")
operatorSpinner := printer.NewSpinnerWithStatus(
w,
"Waiting for registration operator to become ready...",
time.Millisecond*500,
"Registration operator is now available.\n",
Expand Down Expand Up @@ -556,10 +563,12 @@ func waitUntilRegistrationOperatorConditionIsTrue(f util.Factory, timeout int64)
}

// Wait until the klusterlet condition available=true, or timeout in $timeout seconds
func waitUntilKlusterletConditionIsTrue(client operatorclient.Interface, timeout int64, klusterletName string) error {
func waitUntilKlusterletConditionIsTrue(
w io.Writer, client operatorclient.Interface, timeout int64, klusterletName string) error {
phase := &atomic.Value{}
phase.Store("")
klusterletSpinner := printer.NewSpinnerWithStatus(
w,
"Waiting for klusterlet agent to become ready...",
time.Millisecond*500,
"Klusterlet is now available.\n",
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/upgrade/clustermanager/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ func (o *Options) run() error {
}

if !o.ClusteradmFlags.DryRun {
if err := wait.WaitUntilCRDReady(apiExtensionsClient, "clustermanagers.operator.open-cluster-management.io", o.wait); err != nil {
if err := wait.WaitUntilCRDReady(
o.Streams.Out, apiExtensionsClient, "clustermanagers.operator.open-cluster-management.io", o.wait); err != nil {
return err
}
}
if o.wait && !o.ClusteradmFlags.DryRun {
if err := wait.WaitUntilRegistrationOperatorReady(
o.Streams.Out,
o.ClusteradmFlags.KubectlFactory,
int64(o.ClusteradmFlags.Timeout)); err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/upgrade/klusterlet/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ func (o *Options) run() error {
}

if !o.ClusteradmFlags.DryRun {
if err := wait.WaitUntilCRDReady(apiExtensionsClient, "klusterlets.operator.open-cluster-management.io", o.wait); err != nil {
if err := wait.WaitUntilCRDReady(
o.Streams.Out, apiExtensionsClient, "klusterlets.operator.open-cluster-management.io", o.wait); err != nil {
return err
}
}
if o.wait && !o.ClusteradmFlags.DryRun {
if err := wait.WaitUntilRegistrationOperatorReady(
o.Streams.Out,
o.ClusteradmFlags.KubectlFactory,
int64(o.ClusteradmFlags.Timeout)); err != nil {
return err
Expand Down
8 changes: 5 additions & 3 deletions pkg/helpers/printer/prefixwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ type flusher interface {
Flush()
}

func NewSpinner(suffix string, interval time.Duration) *spinner.Spinner {
func NewSpinner(w io.Writer, suffix string, interval time.Duration) *spinner.Spinner {
return spinner.New(
spinner.CharSets[14],
interval,
spinner.WithWriter(w),
spinner.WithColor("green"),
spinner.WithHiddenCursor(true),
spinner.WithSuffix(suffixColor.Sprintf(" %s", suffix)))
}

func NewSpinnerWithStatus(suffix string, interval time.Duration, final string, statusFunc func() string) *spinner.Spinner {
s := NewSpinner(suffix, interval)
func NewSpinnerWithStatus(
w io.Writer, suffix string, interval time.Duration, final string, statusFunc func() string) *spinner.Spinner {
s := NewSpinner(w, suffix, interval)
s.FinalMSG = final
s.PreUpdate = func(s *spinner.Spinner) {
status := statusFunc()
Expand Down
14 changes: 9 additions & 5 deletions pkg/helpers/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package wait
import (
"context"
"fmt"
"io"
"sync/atomic"
"time"

Expand All @@ -21,20 +22,20 @@ import (
"open-cluster-management.io/clusteradm/pkg/helpers/printer"
)

func WaitUntilCRDReady(apiExtensionsClient apiextensionsclient.Interface, crdName string, wait bool) error {
func WaitUntilCRDReady(w io.Writer, apiExtensionsClient apiextensionsclient.Interface, crdName string, wait bool) error {
b := retry.DefaultBackoff
b.Duration = 200 * time.Millisecond

if wait {
crdSpinner := printer.NewSpinner("Waiting for CRD to be ready...", time.Second)
crdSpinner := printer.NewSpinner(w, "Waiting for CRD to be ready...", time.Second)
crdSpinner.FinalMSG = "CRD successfully registered.\n"
crdSpinner.Start()
defer crdSpinner.Stop()
}
return helpers.WaitCRDToBeReady(apiExtensionsClient, crdName, b, wait)
}

func WaitUntilRegistrationOperatorReady(f util.Factory, timeout int64) error {
func WaitUntilRegistrationOperatorReady(w io.Writer, f util.Factory, timeout int64) error {
var restConfig *rest.Config
restConfig, err := f.ToRESTConfig()
if err != nil {
Expand All @@ -49,6 +50,7 @@ func WaitUntilRegistrationOperatorReady(f util.Factory, timeout int64) error {
phase.Store("")
text := "Waiting for registration operator to become ready..."
operatorSpinner := printer.NewSpinnerWithStatus(
w,
text,
time.Second,
"Registration operator is now available.\n",
Expand Down Expand Up @@ -85,7 +87,7 @@ func WaitUntilRegistrationOperatorReady(f util.Factory, timeout int64) error {
})
}

func WaitUntilClusterManagerRegistrationReady(f util.Factory, timeout int64) error {
func WaitUntilClusterManagerRegistrationReady(w io.Writer, f util.Factory, timeout int64) error {
var restConfig *rest.Config
restConfig, err := f.ToRESTConfig()
if err != nil {
Expand All @@ -100,6 +102,7 @@ func WaitUntilClusterManagerRegistrationReady(f util.Factory, timeout int64) err
phase.Store("")
text := "Waiting for cluster manager registration to become ready..."
clusterManagerSpinner := printer.NewSpinnerWithStatus(
w,
text,
time.Second,
"ClusterManager registration is now available.\n",
Expand Down Expand Up @@ -136,7 +139,7 @@ func WaitUntilClusterManagerRegistrationReady(f util.Factory, timeout int64) err
})
}

func WaitUntilMulticlusterControlplaneReady(f util.Factory, ns string, timeout int64) error {
func WaitUntilMulticlusterControlplaneReady(w io.Writer, f util.Factory, ns string, timeout int64) error {
var restConfig *rest.Config
restConfig, err := f.ToRESTConfig()
if err != nil {
Expand All @@ -151,6 +154,7 @@ func WaitUntilMulticlusterControlplaneReady(f util.Factory, ns string, timeout i
phase.Store("")
text := "Waiting for multicluster controlplane to become ready..."
clusterManagerSpinner := printer.NewSpinnerWithStatus(
w,
text,
time.Second,
"Multicluster controlplane is now available.\n",
Expand Down

0 comments on commit 0b56729

Please sign in to comment.