diff --git a/cmd/config.go b/cmd/config.go index 9c28c093..c0e5c76c 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -15,6 +15,7 @@ import ( "github.com/hetznercloud/hcloud-go/hcloud" "github.com/thcyron/uiprogress" "github.com/xetys/hetzner-kube/pkg/clustermanager" + "github.com/xetys/hetzner-kube/pkg/hetzner" ) // DefaultConfigPath is the path where the default config is located @@ -182,7 +183,7 @@ func (app *AppConfig) DeleteContextByName(name string) error { // ActionProgress (deprecated) func (app *AppConfig) ActionProgress(ctx context.Context, action *hcloud.Action) error { - errCh, progressCh := waitAction(ctx, app.Client, action) + errCh, progressCh := hetzner.WaitAction(ctx, app.Client, action) if term.IsTerminal(os.Stdout) { progress := uiprogress.New() diff --git a/cmd/util.go b/cmd/util.go index 4b10e4ad..51df07a5 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -1,66 +1,12 @@ package cmd import ( - "context" "fmt" "log" - "time" "github.com/Pallinder/go-randomdata" - "github.com/hetznercloud/hcloud-go/hcloud" ) -func waitAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) (<-chan error, <-chan int) { - errCh := make(chan error, 1) - progressCh := make(chan int) - - go func() { - defer close(errCh) - defer close(progressCh) - - ticker := time.NewTicker(100 * time.Millisecond) - - sendProgress := func(p int) { - select { - case progressCh <- p: - break - default: - break - } - } - - for { - select { - case <-ctx.Done(): - errCh <- ctx.Err() - return - case <-ticker.C: - } - - action, _, err := client.Action.GetByID(ctx, action.ID) - if err != nil { - errCh <- ctx.Err() - return - } - - switch action.Status { - case hcloud.ActionStatusRunning: - sendProgress(action.Progress) - - case hcloud.ActionStatusSuccess: - sendProgress(100) - errCh <- nil - return - case hcloud.ActionStatusError: - errCh <- action.Error() - return - } - } - }() - - return errCh, progressCh -} - func randomName() string { return fmt.Sprintf("%s-%s%s", randomdata.Adjective(), randomdata.Noun(), randomdata.Adjective()) } diff --git a/pkg/hetzner/hetzner_provider.go b/pkg/hetzner/hetzner_provider.go index 11403271..649b320b 100644 --- a/pkg/hetzner/hetzner_provider.go +++ b/pkg/hetzner/hetzner_provider.go @@ -263,7 +263,7 @@ func (provider *Provider) runCreateServer(opts *hcloud.ServerCreateOpts) (*hclou } func (provider *Provider) actionProgress(action *hcloud.Action) error { - errCh, progressCh := waitAction(provider.context, provider.client, action) + errCh, progressCh := WaitAction(provider.context, provider.client, action) if term.IsTerminal(os.Stdout) { progress := uiprogress.New() diff --git a/pkg/hetzner/util.go b/pkg/hetzner/util.go index 5296a125..cf6850ac 100644 --- a/pkg/hetzner/util.go +++ b/pkg/hetzner/util.go @@ -18,7 +18,8 @@ func ProviderAndManager(context context.Context, cluster clustermanager.Cluster, return provider, manager } -func waitAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) (<-chan error, <-chan int) { +// WaitAction is an helper function used to wait for an action +func WaitAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) (<-chan error, <-chan int) { errCh := make(chan error, 1) progressCh := make(chan int)