Newer
Older
/*
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package settings
import (
"sort"
"strings"
corev1 "k8s.io/api/core/v1"
Timothee Gosselin
committed
"k8s.libre.sh/interfaces"
"k8s.libre.sh/objects/configmap"
"k8s.libre.sh/objects/secret"
)
type Settings interface {
GetMeta() meta.Instance
GetConfig() Config
SetDefaults()
GetParameters() *parameters.Parameters
GetSources() *Sources
// Init(r interfaces.Reconcile, owner interfaces.Object) error
Init(c client.Client, owner interfaces.Object) error
GetCreateOptions() *CreateOptions
GetObjects() map[int]objects.Object
}
func NewSettings(s Settings) *Component {
return &Component{
CreateOptions: s.GetCreateOptions(),
ConfigSpec: &ConfigSpec{
Sources: s.GetConfig().GetSources(),
Parameters: s.GetConfig().GetParameters(),
},
}
}
func TemplateValues(s Settings) map[string]interface{} {
values := make(map[string]interface{})
keyPairValues := parameters.KeyPairValues(s.GetConfig().GetParameters())
values[s.GetMeta().GetComponent()] = keyPairValues
return values
}
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
func GetObjects(s Settings) map[int]objects.Object {
cm := &configmap.ConfigMap{
ObjectMeta: s.GetCreateOptions().ConfigMeta,
}
secret := &secret.Secret{
ObjectMeta: s.GetCreateOptions().SecretMeta,
}
objs := make(map[int]objects.Object, 2)
genConfigParams := []string{}
genSecretParams := []string{}
for _, p := range *s.GetConfig().GetParameters() {
// TODO TO FIX
if (p.MountType == parameters.MountEnvFile || p.MountType == parameters.MountFile) &&
// TODO TO FIX
(p.Type == parameters.SecretParameter || p.Type == "") &&
len(p.Value) > 0 {
if p.Generate != parameters.GenerateTemplate && p.Generate != "" {
genSecretParams = append(genSecretParams, p.Key)
}
secret.Parameters = append(secret.Parameters, p)
}
if (p.MountType == parameters.MountEnvFile || p.MountType == parameters.MountFile) &&
p.Type == parameters.ConfigParameter &&
len(p.Value) > 0 {
if p.Generate != parameters.GenerateTemplate && p.Generate != "" {
genConfigParams = append(genConfigParams, p.Key)
}
cm.Parameters = append(cm.Parameters, p)
}
}
// Sort annotations as it is sorted in kubernetes and will have a diff otherwise
sort.Strings(genConfigParams)
sort.Strings(genSecretParams)
// TODO TO FIX remove annotation if secret is not generated anymore
if len(genSecretParams) > 0 {
if len(secret.ObjectMeta.Annotations) == 0 {
secret.ObjectMeta.Annotations = make(map[string]string, len(secret.ObjectMeta.Annotations))
}
secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genSecretParams, ",")
}
if len(genConfigParams) > 0 {
if len(secret.ObjectMeta.Annotations) == 0 {
cm.ObjectMeta.Annotations = make(map[string]string, len(genConfigParams))
}
secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",")
}
if len(secret.Parameters) > 0 {
objs[0] = secret
}
if len(cm.Parameters) > 0 {
objs[1] = cm
}
return objs
}
// func (s *Component) Init(r interfaces.Reconcile, owner interfaces.Object) error {
func Init(s Settings, c client.Client, owner interfaces.Object) error {
opts := s.GetCreateOptions()
if opts == nil {
opts = &CreateOptions{}
}
opts.Init()
meta.SetObjectMeta(opts.CommonMeta, opts.ConfigMeta)
meta.SetObjectMeta(opts.CommonMeta, opts.SecretMeta)
err := InitParamsValueFrom(s, c, owner)
if err != nil {
return err
}
s.GetParameters().InitRandValues()
if err != nil {
return err
}
return nil
}
func OOrderByResourceRef(s Settings) (secrets, configMaps map[string]parameters.Parameters) {
secrets = make(map[string]parameters.Parameters)
configMaps = make(map[string]parameters.Parameters)
for _, source := range *s.GetSources() {
if source.Type == "secret" {
secrets[source.Ref] = parameters.Parameters{}
} else if source.Type == "configmap" {
configMaps[source.Ref] = parameters.Parameters{}
}
}
// Then we get the sources that are provided for each parameters and will need a transformation
for _, p := range *s.GetParameters() {
if p != nil {
// TOFIX only refType ? by default secret ?
if len(p.ValueFrom.Ref) > 0 && (p.Type == parameters.SecretParameter || p.RefType == "secret") {
// TODO add a warning, if secret given a source and in valuefrom only keys in valuefrom will be fetched
secrets[p.ValueFrom.Ref] = append(secrets[p.ValueFrom.Ref], p)
}
if len(p.ValueFrom.Ref) > 0 && (p.Type == parameters.ConfigParameter || p.RefType == "configmap") {
configMaps[p.ValueFrom.Ref] = append(configMaps[p.ValueFrom.Ref], p)
}
}
}
return secrets, configMaps
}
// InitParametersValueFrom intialise the parameters with values provided in external resources in the same namespace
// All parameters values are filled from those resources
// Only Secrets and Configmaps are supported
//func InitParametersValueFrom(s *Component, r interfaces.Reconcile, owner interfaces.Object) error {
func InitParamsValueFrom(s Settings, c client.Client, owner interfaces.Object) error {
params := parameters.Parameters{}
cm := &corev1.ConfigMap{}
sec := &corev1.Secret{}
paramsBySecretSource, paramsByConfigMapSource := OOrderByResourceRef(s)
for k, v := range paramsBySecretSource {
sec.SetNamespace(s.GetCreateOptions().CommonMeta.GetNamespace())
sec.SetName(k)
ps, err := parameters.MergeParametersFromResource(c, sec, owner, v)
if err != nil {
return err
}
params = append(params, ps...)
}
for k, v := range paramsByConfigMapSource {
cm.SetName(k)
cm.SetNamespace(s.GetCreateOptions().CommonMeta.GetNamespace())
ps, err := parameters.MergeParametersFromResource(c, cm, owner, v)
if err != nil {
return err
}
params = append(params, ps...)
}
secretSrc := Source{
Ref: s.GetCreateOptions().SecretMeta.GetName(),
Type: "secret",
}
configSrc := Source{
Ref: s.GetCreateOptions().ConfigMeta.GetName(),
Type: "configmap",
}
// sources := s.GetSources()
srcs := &Sources{}
// Reset sources, we do not want to mount orginal resources
if s.GetCreateOptions().Generate == GenEnvFile {
srcs = &Sources{}
}
/// foo := s.GetParameters()
bar := parameters.MergeParameters(s.GetParameters(), ¶ms)
/* if err != nil {
return err
} */
bar.DeepCopyInto(s.GetParameters())
for _, v := range *s.GetParameters() {
if v.MountType == parameters.MountEnvFile || s.GetCreateOptions().Generate == GenEnvFile {
v.MountType = parameters.MountEnvFile
// add newly created resource ref to sources
if v.Type == parameters.ConfigParameter {
srcs = AppendSourceIfUnique(srcs, configSrc)
} else if v.Type == parameters.SecretParameter || v.Type == "" {
srcs = AppendSourceIfUnique(srcs, secretSrc)
}
// Reset valueFrom, we do not want to mount orginal resources
v.ValueFrom = parameters.ValueFrom{}
}
if s.GetCreateOptions().SettingsType == "configmap" {
v.Type = parameters.ConfigParameter
} else if s.GetCreateOptions().SettingsType == "secret" {
v.Type = parameters.SecretParameter
}
if v.MountType == parameters.MountFile && len(v.Ref) == 0 {
if v.Type == parameters.ConfigParameter {
v.Ref = s.GetCreateOptions().ConfigMeta.GetName()
}
if v.Type == parameters.SecretParameter {
v.Ref = s.GetCreateOptions().SecretMeta.GetName()
}
}
}
srcs.DeepCopyInto(s.GetSources())
return nil
}