Skip to content

Commit f16821a

Browse files
author
Power Cloud Robot
authored
Merge pull request #617 from kishen-v/remove-k8s-dep
Remove k8s.io/apimachinery dependency for wait mechanism
2 parents 4ea4862 + 8021ace commit f16821a

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

cmd/image/import/import.go

+22-31
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/IBM-Cloud/bluemix-go/models"
2727
pmodels "github.com/IBM-Cloud/power-go-client/power/models"
2828
"github.com/spf13/cobra"
29-
"k8s.io/apimachinery/pkg/util/wait"
3029
"k8s.io/klog/v2"
3130

3231
"github.com/ppc64le-cloud/pvsadm/pkg"
@@ -36,6 +35,9 @@ import (
3635

3736
const (
3837
serviceCredPrefix = "pvsadm-service-cred"
38+
imageStateActive = "active"
39+
jobStateCompleted = "completed"
40+
jobStateFailed = "failed"
3941
)
4042

4143
// Find COSINSTANCE details of the Provided bucket
@@ -182,71 +184,60 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object rhel-83-100
182184
if opt.Public {
183185
bucketAccess = "public"
184186
}
187+
klog.Infof("Importing image %s. Please wait...", opt.ImageName)
185188
jobRef, err := pvmclient.ImgClient.ImportImage(opt.ImageName, opt.ImageFilename, opt.Region,
186189
opt.AccessKey, opt.SecretKey, opt.BucketName, strings.ToLower(opt.StorageType), bucketAccess)
187190
if err != nil {
188191
return err
189192
}
190-
191193
start := time.Now()
192-
pollErr := wait.PollImmediate(2*time.Minute, opt.WatchTimeout, func() (bool, error) {
194+
err = utils.PollUntil(time.Tick(2*time.Minute), time.After(opt.WatchTimeout), func() (bool, error) {
193195
job, err := pvmclient.JobClient.Get(*jobRef.ID)
194196
if err != nil {
195-
return false, err
197+
return false, fmt.Errorf("image import job failed to complete, err: %v", err)
196198
}
197-
if *job.Status.State == "completed" {
199+
if *job.Status.State == jobStateCompleted {
200+
klog.Infof("Image imported successfully, took %s", time.Since(start))
198201
return true, nil
199202
}
200-
if *job.Status.State == "failed" {
203+
if *job.Status.State == jobStateFailed {
201204
return false, fmt.Errorf("image import job failed to complete, err: %v", job.Status.Message)
202205
}
203-
klog.Infof("Image Import Job in-progress, current state: %s", *job.Status.State)
206+
klog.Infof("Image import is in-progress, current state: %s", *job.Status.State)
204207
return false, nil
205208
})
206-
if pollErr == wait.ErrWaitTimeout {
207-
pollErr = fmt.Errorf("timed out while waiting for image import job to complete")
208-
}
209-
210-
if pollErr != nil {
211-
return fmt.Errorf("image import job failed to complete, err: %v", pollErr)
209+
if err != nil {
210+
return err
212211
}
213212

214213
var image *pmodels.ImageReference = &pmodels.ImageReference{}
215-
for image.ImageID == nil {
214+
klog.V(1).Info("Retrieving image details")
215+
216+
if image.ImageID == nil {
216217
image, err = pvmclient.ImgClient.GetImageByName(opt.ImageName)
217218
if err != nil {
218219
return err
219220
}
220-
klog.V(1).Info("Retrieving image details")
221221
}
222222

223223
if !opt.Watch {
224224
klog.Infof("Image import for %s is currently in %s state, Please check the progress in the IBM cloud UI", *image.Name, *image.State)
225225
return nil
226226
}
227-
228-
pollErr = wait.PollImmediate(2*time.Minute, opt.WatchTimeout, func() (bool, error) {
227+
klog.Infof("Waiting for image %s to be active. Please wait...", opt.ImageName)
228+
start = time.Now()
229+
return utils.PollUntil(time.Tick(10*time.Second), time.After(opt.WatchTimeout), func() (bool, error) {
229230
img, err := pvmclient.ImgClient.Get(*image.ImageID)
230231
if err != nil {
231-
return false, err
232+
return false, fmt.Errorf("failed to import the image, err: %v\n\nRun the command \"pvsadm get events -i %s\" to get more information about the failure", err, pvmclient.InstanceID)
232233
}
233-
if img.State == "active" {
234+
if img.State == imageStateActive {
235+
klog.Infof("Successfully imported the image: %s with ID: %s in %s", *image.Name, *image.ImageID, time.Since(start))
234236
return true, nil
235237
}
236-
klog.Infof("Import in-progress, current state: %s", img.State)
238+
klog.Infof("Waiting for image to be active. Current state: %s", img.State)
237239
return false, nil
238240
})
239-
if pollErr == wait.ErrWaitTimeout {
240-
pollErr = fmt.Errorf("timed out while waiting for image to become ready state")
241-
}
242-
243-
if pollErr != nil {
244-
return fmt.Errorf("failed to import the image, err: %v\n\nRun this command to get more information for the failure: pvsadm get events -i %s", pollErr, pvmclient.InstanceID)
245-
}
246-
247-
klog.Infof("Successfully imported the image: %s with ID: %s within %s", *image.Name, *image.ImageID, time.Since(start))
248-
249-
return nil
250241
},
251242
}
252243

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ require (
2525
github.com/stretchr/testify v1.9.0
2626
go.uber.org/mock v0.4.0
2727
gopkg.in/yaml.v2 v2.4.0
28-
k8s.io/apimachinery v0.29.3
2928
k8s.io/klog/v2 v2.120.1
3029
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
3130
)

go.sum

+1-3
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn
114114
github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
115115
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
116116
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
117+
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
117118
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
118-
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
119119
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
120120
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
121121
github.com/go-toolsmith/astcast v1.0.0/go.mod h1:mt2OdQTeAQcY4DQgPSArJjHCcOwlX+Wl/kwN+LbLGQ4=
@@ -572,8 +572,6 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
572572
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
573573
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
574574
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
575-
k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU=
576-
k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU=
577575
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
578576
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
579577
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=

pkg/utils/utils.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package utils
1616

17-
import "strconv"
17+
import (
18+
"fmt"
19+
"strconv"
20+
"time"
21+
)
1822

1923
func FormatProcessor(proc *float64) string {
2024
return strconv.FormatFloat(*proc, 'f', -1, 64)
@@ -32,3 +36,21 @@ func Contains(s []string, e string) bool {
3236
}
3337
return false
3438
}
39+
40+
// PollUntil validates if a certain condition is met at defined poll intervals.
41+
// If a timeout is reached, an associated error is returned to the caller.
42+
// condition contains the use-case specific code that returns true when a certain condition is achieved.
43+
func PollUntil(pollInterval, timeOut <-chan time.Time, condition func() (bool, error)) error {
44+
for {
45+
select {
46+
case <-timeOut:
47+
return fmt.Errorf("timed out while waiting for job to complete")
48+
case <-pollInterval:
49+
if done, err := condition(); err != nil {
50+
return err
51+
} else if done {
52+
return nil
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)