Skip to content

Commit 4a6b702

Browse files
committed
net(virt): Remove virtctl if not correctly retrieved
If the virtctl download/unpack fails in the middle the downloadFile can leave a corrupted file behind, this change ensure file is removed if function do not correctly finish. Signed-off-by: Enrique Llorente <ellorent@redhat.com>
1 parent 5c72de0 commit 4a6b702

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

test/extended/networking/kubevirt/client.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"sigs.k8s.io/yaml"
2020

2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/apimachinery/pkg/util/wait"
23+
"k8s.io/client-go/util/retry"
2224
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
2325

2426
consolev1client "github.com/openshift/client-go/console/clientset/versioned"
@@ -175,13 +177,31 @@ func ensureVirtctl(oc *exutil.CLI, dir string) (string, error) {
175177
_, err := os.Stat(filepath)
176178
if err != nil {
177179
if errors.Is(err, os.ErrNotExist) {
178-
url, err := discoverVirtctlURL(oc)
179-
if err != nil {
180-
return "", err
180+
backoff := wait.Backoff{
181+
Steps: 5,
182+
Duration: 2 * time.Second,
183+
Factor: 2.0,
184+
Jitter: 0.1,
181185
}
182-
if err := downloadFile(url, filepath); err != nil {
183-
return "", err
186+
var url string
187+
allErrors := func(_ error) bool { return true }
188+
err := retry.OnError(backoff, allErrors, func() error {
189+
var err error
190+
url, err = discoverVirtctlURL(oc)
191+
if err != nil {
192+
return err
193+
}
194+
195+
if err := downloadFile(url, filepath); err != nil {
196+
return err
197+
}
198+
199+
return nil
200+
})
201+
if err != nil {
202+
return "", fmt.Errorf("failed to setup virtctl after retries: %w", err)
184203
}
204+
185205
if err := os.Chmod(filepath, 0755); err != nil {
186206
log.Fatal(err)
187207
}
@@ -210,6 +230,14 @@ func discoverVirtctlURL(oc *exutil.CLI) (string, error) {
210230
}
211231

212232
func downloadFile(url string, filepath string) error {
233+
success := false
234+
// Ensure cleanup on error - remove the file if we don't complete successfully
235+
defer func() {
236+
if !success {
237+
os.Remove(filepath)
238+
}
239+
}()
240+
213241
transport := http.DefaultTransport.(*http.Transport).Clone()
214242
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
215243
client := &http.Client{Transport: transport}
@@ -224,7 +252,7 @@ func downloadFile(url string, filepath string) error {
224252
return err
225253
}
226254
tarReader := tar.NewReader(gzipReader)
227-
for true {
255+
for {
228256
header, err := tarReader.Next()
229257
if err == io.EOF {
230258
break
@@ -243,5 +271,6 @@ func downloadFile(url string, filepath string) error {
243271
}
244272
}
245273
}
274+
success = true
246275
return nil
247276
}

0 commit comments

Comments
 (0)