@@ -73,7 +73,10 @@ type SecretConfig struct {
73
73
var clientCache * ttlcache.Cache
74
74
var cacheMutex sync.Mutex
75
75
76
- const cacheExpiration = time .Duration (1 * time .Hour )
76
+ const ClientConfigMapName = "capc-client-config"
77
+ const ClientConfigMapNamespace = "capc-system"
78
+ const ClientCacheTTLKey = "client-cache-ttl"
79
+ const DefaultClientCacheTTL = time .Duration (1 * time .Hour )
77
80
78
81
// UnmarshalAllSecretConfigs parses a yaml document for each secret.
79
82
func UnmarshalAllSecretConfigs (in []byte , out * []SecretConfig ) error {
@@ -94,7 +97,7 @@ func UnmarshalAllSecretConfigs(in []byte, out *[]SecretConfig) error {
94
97
}
95
98
96
99
// NewClientFromK8sSecret returns a client from a k8s secret
97
- func NewClientFromK8sSecret (endpointSecret * corev1.Secret ) (Client , error ) {
100
+ func NewClientFromK8sSecret (endpointSecret * corev1.Secret , clientConfig * corev1. ConfigMap ) (Client , error ) {
98
101
endpointSecretStrings := map [string ]string {}
99
102
for k , v := range endpointSecret .Data {
100
103
endpointSecretStrings [k ] = string (v )
@@ -103,19 +106,19 @@ func NewClientFromK8sSecret(endpointSecret *corev1.Secret) (Client, error) {
103
106
if err != nil {
104
107
return nil , err
105
108
}
106
- return NewClientFromBytesConfig (bytes )
109
+ return NewClientFromBytesConfig (bytes , clientConfig )
107
110
}
108
111
109
112
// NewClientFromBytesConfig returns a client from a bytes array that unmarshals to a yaml config.
110
- func NewClientFromBytesConfig (conf []byte ) (Client , error ) {
113
+ func NewClientFromBytesConfig (conf []byte , clientConfig * corev1. ConfigMap ) (Client , error ) {
111
114
r := bytes .NewReader (conf )
112
115
dec := yaml .NewDecoder (r )
113
116
var config Config
114
117
if err := dec .Decode (& config ); err != nil {
115
118
return nil , err
116
119
}
117
120
118
- return NewClientFromConf (config )
121
+ return NewClientFromConf (config , clientConfig )
119
122
}
120
123
121
124
// NewClientFromYamlPath returns a client from a yaml config at path.
@@ -139,18 +142,16 @@ func NewClientFromYamlPath(confPath string, secretName string) (Client, error) {
139
142
return nil , errors .Errorf ("config with secret name %s not found" , secretName )
140
143
}
141
144
142
- return NewClientFromConf (conf )
145
+ return NewClientFromConf (conf , nil )
143
146
}
144
147
145
148
// NewClientFromConf creates a new Cloud Client form a map of strings to strings.
146
- func NewClientFromConf (conf Config ) (Client , error ) {
149
+ func NewClientFromConf (conf Config , clientConfig * corev1. ConfigMap ) (Client , error ) {
147
150
cacheMutex .Lock ()
148
151
defer cacheMutex .Unlock ()
149
152
150
153
if clientCache == nil {
151
- clientCache = ttlcache .NewCache ()
152
- clientCache .SetTTL (cacheExpiration )
153
- clientCache .SkipTtlExtensionOnHit (false )
154
+ clientCache = newClientCache (clientConfig )
154
155
}
155
156
156
157
clientCacheKey := generateClientCacheKey (conf )
@@ -189,7 +190,7 @@ func (c *client) NewClientInDomainAndAccount(domain string, account string) (Cli
189
190
c .config .APIKey = user .APIKey
190
191
c .config .SecretKey = user .SecretKey
191
192
192
- return NewClientFromConf (c .config )
193
+ return NewClientFromConf (c .config , nil )
193
194
}
194
195
195
196
// NewClientFromCSAPIClient creates a client from a CloudStack-Go API client. Mostly used for testing.
@@ -201,3 +202,19 @@ func NewClientFromCSAPIClient(cs *cloudstack.CloudStackClient) Client {
201
202
func generateClientCacheKey (conf Config ) string {
202
203
return fmt .Sprintf ("%+v" , conf )
203
204
}
205
+
206
+ func newClientCache (clientConfig * corev1.ConfigMap ) * ttlcache.Cache {
207
+ clientCache := ttlcache .NewCache ()
208
+ var cacheTTL time.Duration
209
+ if clientConfig != nil {
210
+ if ttl , exists := clientConfig .Data [ClientCacheTTLKey ]; exists {
211
+ cacheTTL , _ = time .ParseDuration (ttl )
212
+ }
213
+ }
214
+ if cacheTTL == 0 {
215
+ cacheTTL = DefaultClientCacheTTL
216
+ }
217
+ clientCache .SetTTL (cacheTTL )
218
+ clientCache .SkipTtlExtensionOnHit (false )
219
+ return clientCache
220
+ }
0 commit comments