forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
67 lines (57 loc) · 2.23 KB
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package installerpod
import (
"golang.org/x/net/context"
"k8s.io/klog"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/library-go/pkg/operator/resource/retry"
)
// getSecretWithRetry will attempt to get the secret from the API server and retry on any connection errors until
// the context is not done or secret is returned or a HTTP client error is returned.
// In case the optional flag is set, the 404 error is not reported and a nil object is returned instead.
func (o *InstallOptions) getSecretWithRetry(ctx context.Context, name string, isOptional bool) (*v1.Secret, error) {
var secret *v1.Secret
err := retry.RetryOnConnectionErrors(ctx, func(ctx context.Context) (bool, error) {
var clientErr error
secret, clientErr = o.KubeClient.CoreV1().Secrets(o.Namespace).Get(name, metav1.GetOptions{})
if clientErr != nil {
klog.Infof("Failed to get secret %s/%s: %v", o.Namespace, name, clientErr)
return false, clientErr
}
return true, nil
})
switch {
case err == nil:
klog.Infof("Got secret %s/%s", o.Namespace, name)
return secret, nil
case errors.IsNotFound(err) && isOptional:
return nil, nil
default:
return nil, err
}
}
// getConfigMapWithRetry will attempt to get the configMap from the API server and retry on any connection errors until
// the context is not done or configMap is returned or a HTTP client error is returned.
// In case the optional flag is set, the 404 error is not reported and a nil object is returned instead.
func (o *InstallOptions) getConfigMapWithRetry(ctx context.Context, name string, isOptional bool) (*v1.ConfigMap, error) {
var config *v1.ConfigMap
err := retry.RetryOnConnectionErrors(ctx, func(ctx context.Context) (bool, error) {
var clientErr error
config, clientErr = o.KubeClient.CoreV1().ConfigMaps(o.Namespace).Get(name, metav1.GetOptions{})
if clientErr != nil {
klog.Infof("Failed to get config map %s/%s: %v", o.Namespace, name, clientErr)
return false, clientErr
}
return true, nil
})
switch {
case err == nil:
klog.Infof("Got configMap %s/%s", o.Namespace, name)
return config, nil
case errors.IsNotFound(err) && isOptional:
return nil, nil
default:
return nil, err
}
}