-
Notifications
You must be signed in to change notification settings - Fork 30
/
boot_secret.go
68 lines (61 loc) · 2.21 KB
/
boot_secret.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
68
package boot
import (
"context"
corev1 "k8s.io/api/core/v1"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// BootSecret loads the boot secret
type BootSecret struct {
// URL the git URL to poll for git operator
URL string
// GitProviderURL the git provider URL such as: https://github.com
GitProviderURL string
// Username the git user name to clone git
Username string
// Password the git password/token to clone git
Password string
// GitInitCommands the optional commands to configure git before we clone
GitInitCommands string
}
// LoadBootSecret loads the boot secret from the current namespace
func LoadBootSecret(kubeClient kubernetes.Interface, ns, operatorNamespace, secretName, defaultUserName string) (*BootSecret, error) {
secret, err := getBootSecret(kubeClient, ns, operatorNamespace, secretName)
if err != nil {
return nil, errors.Wrapf(err, "failed to find boot secret")
}
answer := &BootSecret{}
data := secret.Data
if data != nil {
if secret.Annotations != nil {
answer.GitProviderURL = secret.Annotations["tekton.dev/git-0"]
}
answer.URL = string(data["url"])
if answer.URL == "" {
log.Logger().Debugf("secret %s in namespace %s does not have a url entry", secretName, ns)
}
answer.Username = string(data["username"])
if answer.Username == "" {
answer.Username = defaultUserName
}
answer.Password = string(data["password"])
answer.GitInitCommands = string(data["gitInitCommands"])
}
return answer, nil
}
func getBootSecret(kubeClient kubernetes.Interface, ns string, operatorNamespace string, secretName string) (*corev1.Secret, error) {
secret, err := kubeClient.CoreV1().Secrets(ns).Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil {
// lets try either the namespace: jx-git-operator or jx whichever is different
if operatorNamespace == ns {
operatorNamespace = "jx"
}
secret, err = kubeClient.CoreV1().Secrets(operatorNamespace).Get(context.TODO(), secretName, metav1.GetOptions{})
}
if err != nil {
return nil, errors.Wrapf(err, "failed to find secret %s in namespace %s or %s", secretName, ns, operatorNamespace)
}
return secret, nil
}