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"
)
// +kubebuilder:object:generate=true
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"`
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 {
cm := &configmap.ConfigMap{
ObjectMeta: s.GetConfigMapMeta().(*meta.ObjectMeta),
}
secret := &secret.Secret{
ObjectMeta: s.GetSecretMeta().(*meta.ObjectMeta),
}
objs := make(map[int]objects.Object, 2)
// meta.SetObjectMeta(s.CommonMeta, cm.ObjectMeta)
// meta.SetObjectMeta(s.CommonMeta, secret.ObjectMeta)
// TODO REPLACE BY GET SECRET PARAMETERS & GET CONFIG PARAMETERS
// TODO check not mountLiteral ??
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 {
// secret.Parameters = secretParameters
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)
// TODO ADD SECRET REFS HERE ??
err := InitParametersValueFrom(s, c)
if err != nil {
return err
}
s.InitRandValues()
err = s.InitTemplateValues(parameters.KeyPairValues(s.Parameters))
return nil
}
// InitParametersValueFrom intialise the paremeters with values provided in external resources (secrets and configmaps) in the same namespace
// All parameters values are filled from those resources
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)
if err != nil {
return err
}
}
for _, v := range paramsByKey {
if v.MountType == parameters.MountEnvFile {
if v.Type == parameters.ConfigParameter {
v.Ref = s.GetConfigMapMeta().GetName()
}
if v.Type == parameters.SecretParameter {
v.Ref = s.GetSecretMeta().GetName()
}
}
params = append(params, v)
}
// TODO FIX THIS we need to check if we want to mount as EnvFile and reset old resources references
s.SecretRefs = []string{}
s.ConfigRefs = []string{}