-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
secrets.go
161 lines (138 loc) · 4.7 KB
/
secrets.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package db
import (
"fmt"
"hash/fnv"
"net/url"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
informerv1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/tools/cache"
"github.com/argoproj/argo-cd/v2/common"
)
func (db *db) listSecretsByType(types ...string) ([]*apiv1.Secret, error) {
labelSelector := labels.NewSelector()
req, err := labels.NewRequirement(common.LabelKeySecretType, selection.Equals, types)
if err != nil {
return nil, err
}
labelSelector = labelSelector.Add(*req)
secretsLister, err := db.settingsMgr.GetSecretsLister()
if err != nil {
return nil, err
}
secrets, err := secretsLister.Secrets(db.ns).List(labelSelector)
if err != nil {
return nil, err
}
return secrets, nil
}
func boolOrFalse(secret *apiv1.Secret, key string) (bool, error) {
val, present := secret.Data[key]
if !present {
return false, nil
}
return strconv.ParseBool(string(val))
}
func intOrZero(secret *apiv1.Secret, key string) (int64, error) {
val, present := secret.Data[key]
if !present {
return 0, nil
}
return strconv.ParseInt(string(val), 10, 64)
}
func updateSecretBool(secret *apiv1.Secret, key string, value bool) {
if _, present := secret.Data[key]; present || value {
secret.Data[key] = []byte(strconv.FormatBool(value))
}
}
func updateSecretInt(secret *apiv1.Secret, key string, value int64) {
if _, present := secret.Data[key]; present || value != 0 {
secret.Data[key] = []byte(strconv.FormatInt(value, 10))
}
}
func updateSecretString(secret *apiv1.Secret, key, value string) {
if _, present := secret.Data[key]; present || len(value) > 0 {
secret.Data[key] = []byte(value)
}
}
func (db *db) createSecret(ctx context.Context, secretType string, secret *apiv1.Secret) (*apiv1.Secret, error) {
if secret.Annotations == nil {
secret.Annotations = map[string]string{}
}
secret.Annotations[common.AnnotationKeyManagedBy] = common.AnnotationValueManagedByArgoCD
if secret.Labels == nil {
secret.Labels = map[string]string{}
}
secret.Labels[common.LabelKeySecretType] = secretType
secret, err := db.kubeclientset.CoreV1().Secrets(db.ns).Create(ctx, secret, metav1.CreateOptions{})
return secret, err
}
func (db *db) deleteSecret(ctx context.Context, secret *apiv1.Secret) error {
var err error
canDelete := secret.Annotations != nil && secret.Annotations[common.AnnotationKeyManagedBy] == common.AnnotationValueManagedByArgoCD
if canDelete {
err = db.kubeclientset.CoreV1().Secrets(db.ns).Delete(ctx, secret.Name, metav1.DeleteOptions{})
} else {
delete(secret.Labels, common.LabelKeySecretType)
_, err = db.kubeclientset.CoreV1().Secrets(db.ns).Update(ctx, secret, metav1.UpdateOptions{})
}
return err
}
func (db *db) watchSecrets(ctx context.Context,
secretType string,
handleAddEvent func(secret *apiv1.Secret),
handleModEvent func(oldSecret *apiv1.Secret, newSecret *apiv1.Secret),
handleDeleteEvent func(secret *apiv1.Secret)) {
secretListOptions := func(options *metav1.ListOptions) {
labelSelector := fields.ParseSelectorOrDie(common.LabelKeySecretType + "=" + secretType)
options.LabelSelector = labelSelector.String()
}
secretEventHandler := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if secretObj, ok := obj.(*apiv1.Secret); ok {
handleAddEvent(secretObj)
}
},
DeleteFunc: func(obj interface{}) {
if secretObj, ok := obj.(*apiv1.Secret); ok {
handleDeleteEvent(secretObj)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if oldSecretObj, ok := oldObj.(*apiv1.Secret); ok {
if newSecretObj, ok := newObj.(*apiv1.Secret); ok {
handleModEvent(oldSecretObj, newSecretObj)
}
}
},
}
indexers := cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}
clusterSecretInformer := informerv1.NewFilteredSecretInformer(db.kubeclientset, db.ns, 3*time.Minute, indexers, secretListOptions)
clusterSecretInformer.AddEventHandler(secretEventHandler)
log.Info("Starting secretInformer for", secretType)
go func() {
clusterSecretInformer.Run(ctx.Done())
log.Info("secretInformer for", secretType, "cancelled")
}()
<-ctx.Done()
}
// URIToSecretName hashes an uri address to the secret name using a formula.
// Part of the uri address is incorporated for debugging purposes
func URIToSecretName(uriType, uri string) (string, error) {
parsedURI, err := url.ParseRequestURI(uri)
if err != nil {
return "", err
}
h := fnv.New32a()
_, _ = h.Write([]byte(uri))
host := strings.ToLower(strings.Split(parsedURI.Host, ":")[0])
return fmt.Sprintf("%s-%s-%v", uriType, host, h.Sum32()), nil
}