forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
59 lines (51 loc) · 1.48 KB
/
util.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
package util
import (
"io/ioutil"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"github.com/argoproj/argo/errors"
"github.com/argoproj/argo/util/retry"
)
type Closer interface {
Close() error
}
// Close is a convenience function to close a object that has a Close() method, ignoring any errors
// Used to satisfy errcheck lint
func Close(c Closer) {
_ = c.Close()
}
// GetSecrets retrieves a secret value and memoizes the result
func GetSecrets(clientSet kubernetes.Interface, namespace, name, key string) ([]byte, error) {
secretsIf := clientSet.CoreV1().Secrets(namespace)
var secret *apiv1.Secret
var err error
_ = wait.ExponentialBackoff(retry.DefaultRetry, func() (bool, error) {
secret, err = secretsIf.Get(name, metav1.GetOptions{})
if err != nil {
log.Warnf("Failed to get secret '%s': %v", name, err)
if !retry.IsRetryableKubeAPIError(err) {
return false, err
}
return false, nil
}
return true, nil
})
if err != nil {
return []byte{}, errors.InternalWrapError(err)
}
val, ok := secret.Data[key]
if !ok {
return []byte{}, errors.Errorf(errors.CodeBadRequest, "secret '%s' does not have the key '%s'", name, key)
}
return val, nil
}
// Write the Terminate message in pod spec
func WriteTeriminateMessage(message string) {
err := ioutil.WriteFile("/dev/termination-log", []byte(message), 0644)
if err != nil {
panic(err)
}
}