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
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.
*/
import (
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
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"`
*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"`
}
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) GetConfig() Config { return s.ConfigSpec }
func (s *Component) GetMeta() meta.Instance { return s.CommonMeta }
func (s *Component) GetSecretMeta() meta.Instance { return s.SecretMeta }
func (s *Component) GetConfigMapMeta() meta.Instance { return s.ConfigMeta }
meta.SetObjectMeta(s.CommonMeta, s.SecretMeta)
meta.SetObjectMeta(s.CommonMeta, s.ConfigMeta)
}
func (s *Component) GetObjects() map[int]objects.Object {
if p.MountType == parameters.MountEnvFile &&
p.Type == parameters.SecretParameter &&
len(p.Value) > 0 {
secret.Parameters = append(secret.Parameters, p)
}
if p.MountType == parameters.MountEnvFile &&
p.Type == parameters.ConfigParameter &&
len(p.Value) > 0 {
cm.Parameters = append(cm.Parameters, p)
}
}
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) Init(c client.Client) error {
// TODO TO FIX DUPLICATE WITH SETDEFAULTS
meta.SetObjectMeta(s.CommonMeta, s.ConfigMeta)
meta.SetObjectMeta(s.CommonMeta, s.SecretMeta)
err := InitParametersValueFrom(s, c)
if err != nil {
return err
}
s.InitRandValues()
err = s.InitTemplateValues(parameters.KeyPairValues(s.Parameters))
if err != nil {
return err
}
// InitParametersValueFrom intialise the parameters with values provided in external resources in the same namespace
// 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.CommonMeta.GetNamespace())
sec.SetNamespace(s.CommonMeta.GetNamespace())
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)
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
secretSrc := Source{
Ref: s.GetSecretMeta().GetName(),
Type: "secret",
}
configSrc := Source{
Ref: s.GetSecretMeta().GetName(),
Type: "configmap",
}
if s.Generate == GenEnvFile {
// s.SecretRefs = []string{s.GetSecretMeta().GetName()}
// s.ConfigRefs = []string{s.GetConfigMapMeta().GetName()}
s.Sources = []Source{
secretSrc,
configSrc,
}
for _, v := range paramsByKey {
v.MountType = parameters.MountEnvFile
v.Ref = ""
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
}
}
params = append(params, v)
}
s.Parameters = ¶ms
return nil
}
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
// 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
}