-
Notifications
You must be signed in to change notification settings - Fork 1
/
remote.go
110 lines (94 loc) · 2.5 KB
/
remote.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
package kubernetes
import (
"bytes"
"io"
"log"
"github.com/spf13/viper"
)
type Response struct {
Value []byte
Error error
}
// A ConfigManager retrieves and decrypts configuration from a key/value store.
type ConfigManager interface {
Get(key string) ([]byte, error)
Watch(key string, stop chan bool) <-chan *Response
}
type remoteConfigProvider struct {
}
func (kcp remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
cm, err := getConfigManager(rp)
if err != nil {
log.Printf("Failed to create Config Manager: %v", err)
return nil, err
}
b, err := cm.Get(rp.Path())
if err != nil {
log.Printf("Failed to get config: %v", err)
return nil, err
}
return bytes.NewReader(b), nil
}
func (kcp remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
cm, err := getConfigManager(rp)
if err != nil {
log.Printf("Failed to create Config Manager: %v", err)
return nil, err
}
resp, err := cm.Get(rp.Path())
if err != nil {
log.Printf("Failed to get config: %v", err)
return nil, err
}
return bytes.NewReader(resp), nil
}
func (kcp remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
cm, err := getConfigManager(rp)
if err != nil {
log.Printf("Failed to create Config Manager: %v", err)
return nil, nil
}
quit := make(chan bool)
quitwc := make(chan bool)
viperResponsCh := make(chan *viper.RemoteResponse)
cryptoResponseCh := cm.Watch(rp.Path(), quit)
// need this function to convert the Channel response form crypt.Response to viper.Response
go func(cr <-chan *Response, vr chan<- *viper.RemoteResponse, quitwc chan bool, quit chan<- bool) {
defer close(quit)
defer close(quitwc)
for {
select {
case <-quitwc:
quit <- true
return
case resp := <-cr:
if resp.Error != nil {
log.Printf("Received error in watch event: %v", resp.Error)
}
vr <- &viper.RemoteResponse{
Error: resp.Error,
Value: resp.Value,
}
}
}
}(cryptoResponseCh, viperResponsCh, quitwc, quit)
return viperResponsCh, quitwc
}
func getConfigManager(rp viper.RemoteProvider) (ConfigManager, error) {
var cm ConfigManager
var err error
switch rp.Provider() {
case "configmap":
cm, err = NewConfigMapConfigManager(rp.SecretKeyring())
default:
cm, err = NewSecretConfigManager(rp.SecretKeyring())
}
if err != nil {
return nil, err
}
return cm, nil
}
func init() {
viper.RemoteConfig = &remoteConfigProvider{}
viper.SupportedRemoteProviders = []string{"configmap", "secret"}
}