forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secretmanager.go
75 lines (60 loc) · 1.77 KB
/
secretmanager.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
package configsyncer
import (
"reflect"
"github.com/pkg/errors"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
k8scorev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// This manager is responsible for init/update config in secret
type SecretManager struct {
secrets v1.SecretInterface
}
func NewSecretManager(cluster *config.UserContext) *SecretManager {
return &SecretManager{
secrets: cluster.Core.Secrets(loggingconfig.LoggingNamespace),
}
}
func (s *SecretManager) newSecret(name, namespace string, data map[string][]byte) (err error) {
secret, err := s.secrets.Controller().Lister().Get(namespace, name)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if secret != nil {
return nil
}
secret = &k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: data,
}
if _, err = s.secrets.Create(secret); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func (s *SecretManager) updateSecret(name, namespace string, data map[string][]byte) error {
existSecret, err := s.secrets.GetNamespaced(namespace, name, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "get secret %s:%s failed", namespace, name)
}
newSecret := existSecret.DeepCopy()
if newSecret.Data == nil {
newSecret.Data = make(map[string][]byte)
}
for k, v := range data {
newSecret.Data[k] = v
}
if reflect.DeepEqual(existSecret.Data, newSecret.Data) {
return nil
}
if _, err = s.secrets.Update(newSecret); err != nil {
return errors.Wrapf(err, "update secret %s:%s failed", namespace, name)
}
return nil
}