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 (
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.libre.sh/application/settings/parameters"
Timothee Gosselin
committed
"k8s.libre.sh/interfaces"
"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 (opts *CreateOptions) Init() {
if opts.CommonMeta == nil {
opts.CommonMeta = &meta.ObjectMeta{}
}
if opts.CommonMeta.Labels == nil {
opts.CommonMeta.Labels = make(map[string]string)
}
if opts.SecretMeta == nil {
opts.SecretMeta = &meta.ObjectMeta{}
}
if opts.SecretMeta.Labels == nil {
opts.SecretMeta.Labels = make(map[string]string)
}
if opts.ConfigMeta == nil {
opts.ConfigMeta = &meta.ObjectMeta{}
}
if opts.ConfigMeta.Labels == nil {
opts.SecretMeta.Labels = make(map[string]string)
}
}
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) 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 || 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)
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)
// 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)
}
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, ",")
}
if len(secret.Parameters) > 0 {
func (s *Component) SetDefaults() {
}
Timothee Gosselin
committed
func (s *Component) Init(r interfaces.Reconcile, owner interfaces.Object) error {
meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.ConfigMeta)
meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.SecretMeta)
Timothee Gosselin
committed
err := InitParametersValueFrom(s, r, owner)
if err != nil {
return err
}
s.InitRandValues()
if err != nil {
return err
}
return nil
}
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
func MergeParametersWithData(params parameters.Parameters, data map[string]string) parameters.Parameters {
paramsByKey := parameters.ParametersByKey(¶ms)
if len(data) > 0 {
if len(params) > 0 {
for _, pp := range params {
paramsByKey[pp.Key].Value = string(data[pp.FromKey])
// Reset default Generate
paramsByKey[pp.Key].Generate = ""
}
} else {
for kk, vv := range data {
if paramsByKey[kk] == nil {
paramsByKey[kk] = ¶meters.Parameter{}
}
paramsByKey[kk].Value = vv
paramsByKey[kk].Key = kk
// TODO TO FIX This should be checked
// paramsByKey[kk].Type = pType
// Reset default Generate
paramsByKey[kk].Generate = ""
}
}
}
// TODO TO FIX
ps := parameters.Parameters{}
for _, v := range paramsByKey {
ps = append(ps, v)
}
return ps
}
func MergeParameters(src, dest *parameters.Parameters) *parameters.Parameters {
params := parameters.Parameters{}
srcByKey := parameters.ParametersByKey(src)
destByKey := parameters.ParametersByKey(dest)
for k, param := range destByKey {
// fmt.Println(srcByKey[k])
if srcByKey[k] != nil {
param = srcByKey[k]
}
params = append(params, param)
// fmt.Println(param)
}
return ¶ms
}
// 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
Timothee Gosselin
committed
func InitParametersValueFrom(s *Component, r interfaces.Reconcile, owner interfaces.Object) error {
params := parameters.Parameters{}
cm := &corev1.ConfigMap{}
sec := &corev1.Secret{}
paramsBySecretSource, paramsByConfigMapSource := OrderByResourceRef(s.ConfigSpec)
sec.SetNamespace(s.CreateOptions.CommonMeta.GetNamespace())
data, err := parameters.GetDataValueFrom(r, sec, owner)
if err != nil {
return err
}
ps := MergeParametersWithData(v, data)
params = append(params, ps...)
if err != nil {
return err
}
}
for k, v := range paramsByConfigMapSource {
cm.SetName(k)
cm.SetNamespace(s.CreateOptions.CommonMeta.GetNamespace())
data, err := parameters.GetDataValueFrom(r, sec, owner)
if err != nil {
return err
}
ps := MergeParametersWithData(v, data)
params = append(params, ps...)
if err != nil {
return err
}
}
secretSrc := Source{
if s.CreateOptions.Generate == GenEnvFile {
for _, p := range params {
fmt.Println(p)
}
fmt.Println("------------------------------------------------------------------------")
/* for _, p := range *s.Parameters {
fmt.Println(p)
} */
s.Parameters = MergeParameters(¶ms, s.Parameters)
for _, v := range *s.Parameters {
if v.MountType == parameters.MountEnvFile || s.CreateOptions.Generate == GenEnvFile {
v.MountType = parameters.MountEnvFile
if v.Type == parameters.ConfigParameter {
s.Sources = AppendSourceIfUnique(s.Sources, configSrc)
} else if v.Type == parameters.SecretParameter || v.Type == "" {
s.Sources = AppendSourceIfUnique(s.Sources, secretSrc)
if s.CreateOptions.SettingsType == "configmap" {
v.Type = parameters.ConfigParameter
} else if s.CreateOptions.SettingsType == "secret" {
v.Type = parameters.SecretParameter
}
if v.MountType == parameters.MountFile && len(v.Ref) == 0 {
if v.Type == parameters.ConfigParameter {
}
if v.Type == parameters.SecretParameter {
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
}
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
}