Newer
Older
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
/*
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 (
"strings"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"k8s.libre.sh/application/settings/parameters"
"k8s.libre.sh/meta"
"k8s.libre.sh/objects"
"k8s.libre.sh/objects/configmap"
"k8s.libre.sh/objects/secret"
)
const (
// Generate settings as env variables in a configMap or Secret
GenEnvFile SettingsGenerate = "envFile"
)
const (
// Generate settings as env variables in a configMap or Secret
SecretSettings SettingsType = "secret"
)
type SettingsGenerate string
type SettingsType string
// +kubebuilder:object:generate=true
type Component struct {
CreateOptions *CreateOptions `json:"createOptions,omitempty"`
*ConfigSpec `json:",inline"`
}
// ConfigSpec defines a list of parameters and references to resources from which those parameters can be fetched
// Only secrets and configmaps in the same namespace are supported as references
// +kubebuilder:object:generate=true
type SettingsSpec struct {
Sources []Source `json:"sources,omitempty"`
// Parameters is a list of parameters
*parameters.Parameters `json:"parameters,omitempty"`
}
// +kubebuilder:object:generate=true
type CreateOptions struct {
CommonMeta *meta.ObjectMeta `json:"commonMeta,omitempty"`
SecretMeta *meta.ObjectMeta `json:"secretMeta,omitempty"`
ConfigMeta *meta.ObjectMeta `json:"configMeta,omitempty"`
// DefaultType parameters.ParameterType `json:"defaultType,omitempty"`
// DefaultMountType parameters.MountType `json:"defaultMountType,omitempty"`
// Template string json:"template,omitempty"`
Generate SettingsGenerate `json:"generate,omitempty"`
SettingsType SettingsType `json:"type,omitempty"`
}
func (s *Component) GetCreateOptions() *CreateOptions { return s.CreateOptions }
func (s *Component) GetConfig() Config { return s.ConfigSpec }
func (s *Component) GetMeta() meta.Instance { return s.CreateOptions.CommonMeta }
func (s *Component) GetSecretMeta() meta.Instance { return s.CreateOptions.SecretMeta }
func (s *Component) GetConfigMapMeta() meta.Instance { return s.CreateOptions.ConfigMeta }
func (s *Component) GetObjects() map[int]objects.Object {
cm := &configmap.ConfigMap{
}
objs := make(map[int]objects.Object, 2)
genConfigParams := []string{}
genSecretParams := []string{}
for _, p := range *s.ConfigSpec.Parameters {
// TODO TO FIX
if p.MountType == parameters.MountEnvFile &&
// 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.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)
}
}
// TODO TO FIX remove annotation if secret is not generated anymore
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
if len(genSecretParams) > 0 {
if len(secret.ObjectMeta.Annotations) == 0 {
secret.ObjectMeta.Annotations = make(map[string]string)
}
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)
}
secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",")
}
// TODO IMPROVE
if len(cm.Parameters) > 0 {
objs[0] = cm
}
if len(secret.Parameters) > 0 {
if objs[0] == nil {
objs[0] = secret
} else {
objs[1] = secret
}
}
return objs
}
func (s *Component) SetDefaults() {
// TODO TO FIX
}
if s.CreateOptions.CommonMeta == nil {
s.CreateOptions.CommonMeta = &meta.ObjectMeta{}
}
if len(s.CreateOptions.CommonMeta.Labels) == 0 {
s.CreateOptions.CommonMeta.Labels = make(map[string]string)
}
if s.CreateOptions.SecretMeta == nil {
s.CreateOptions.SecretMeta = &meta.ObjectMeta{}
}
if len(s.CreateOptions.SecretMeta.Labels) == 0 {
s.CreateOptions.SecretMeta.Labels = make(map[string]string)
}
if s.CreateOptions.ConfigMeta == nil {
s.CreateOptions.ConfigMeta = &meta.ObjectMeta{}
}
if len(s.CreateOptions.ConfigMeta.Labels) == 0 {
s.CreateOptions.ConfigMeta.Labels = make(map[string]string)
}
meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.ConfigMeta)
meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.SecretMeta)
// fmt.Println(s.CreateOptions.CommonMeta)
err := InitParametersValueFrom(s, c)
if err != nil {
return err
}
s.InitRandValues()
err = s.InitTemplateValues(parameters.KeyPairValues(s.Parameters))
if err != nil {
return err
}
return nil
}
// 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, c client.Client) error {
params := parameters.Parameters{}
paramsByKey := parameters.ParametersByKey(s.Parameters)
paramsBySecretSource, paramsByConfigMapSource := OrderByResourceRef(s.ConfigSpec)
cm := &corev1.ConfigMap{}
sec := &corev1.Secret{}
cm.SetNamespace(s.CreateOptions.CommonMeta.GetNamespace())
sec.SetNamespace(s.CreateOptions.CommonMeta.GetNamespace())
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
for k, v := range paramsBySecretSource {
sec.SetName(k)
err := parameters.GetAndMergeParameters(v, paramsByKey, c, sec)
if err != nil {
return err
}
}
for k, v := range paramsByConfigMapSource {
cm.SetName(k)
err := parameters.GetAndMergeParameters(v, paramsByKey, c, cm)
if err != nil {
return err
}
}
secretSrc := Source{
Ref: s.GetSecretMeta().GetName(),
Type: "secret",
}
configSrc := Source{
Ref: s.GetSecretMeta().GetName(),
Type: "configmap",
}
if s.CreateOptions.Generate == GenEnvFile {
s.Sources = []Source{}
if len(paramsByConfigMapSource) > 0 {
s.Sources = append(s.Sources, configSrc)
}
if len(paramsBySecretSource) > 0 {
s.Sources = append(s.Sources, secretSrc)
}
for _, v := range paramsByKey {
v.MountType = parameters.MountEnvFile
if len(v.Ref) == 0 {
if v.Type == parameters.ConfigParameter {
s.Sources = AppendSourceIfUnique(s.Sources, configSrc)
} else {
s.Sources = AppendSourceIfUnique(s.Sources, secretSrc)
}
} else {
v.Ref = ""
if v.Type == parameters.ConfigParameter {
s.Sources = AppendSourceIfUnique(s.Sources, configSrc)
} else {
s.Sources = AppendSourceIfUnique(s.Sources, secretSrc)
}
}
params = append(params, v)
}
s.Parameters = ¶ms
return nil
}
addSecretSrc := false
addConfigSrc := false
for _, v := range paramsByKey {
if v.MountType == parameters.MountEnvFile {
v.Ref = ""
if !addConfigSrc && v.Type == parameters.ConfigParameter {
s.Sources = append(s.Sources, configSrc)
addConfigSrc = true
} else if !addSecretSrc && v.Type == parameters.SecretParameter {
s.Sources = append(s.Sources, secretSrc)
addSecretSrc = true
}
}
// TODO TOFIX should not happen here
if v.MountType == parameters.MountFile && len(v.Ref) == 0 {
if v.Type == parameters.ConfigParameter {
v.Ref = s.GetConfigMapMeta().GetName()
}
if v.Type == parameters.SecretParameter {
v.Ref = s.GetSecretMeta().GetName()
}
}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
params = append(params, v)
}
s.Parameters = ¶ms
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *SettingsSpec) DeepCopyInto(out *SettingsSpec) {
*out = *in
if in.Sources != nil {
in, out := &in.Sources, &out.Sources
*out = make([]Source, len(*in))
copy(*out, *in)
}
if in.Parameters != nil {
in, out := &in.Parameters, &out.Parameters
*out = new(parameters.Parameters)
if **in != nil {
in, out := *in, *out
*out = make([]*parameters.Parameter, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(parameters.Parameter)
**out = **in
}
}
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SettingsSpec.
func (in *SettingsSpec) DeepCopy() *SettingsSpec {
if in == nil {
return nil
}
out := new(SettingsSpec)
in.DeepCopyInto(out)
return out
}