Skip to content
component.go 4.68 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Timothee Gosselin's avatar
Timothee Gosselin committed
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
Timothee Gosselin's avatar
Timothee Gosselin committed
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Timothee Gosselin's avatar
Timothee Gosselin committed
    https://www.gnu.org/licenses/agpl-3.0.html
Timothee Gosselin's avatar
Timothee Gosselin committed

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.
*/

Timothee Gosselin's avatar
Timothee Gosselin committed
package settings
Timothee Gosselin's avatar
Timothee Gosselin committed

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
Timothee Gosselin's avatar
Timothee Gosselin committed
type Component struct {
Timothee Gosselin's avatar
Timothee Gosselin committed
	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"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	*ConfigSpec `json:",inline"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) GetConfig() Config { return s.ConfigSpec }
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) GetMeta() meta.Instance { return s.CommonMeta }
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) GetSecretMeta() meta.Instance { return s.SecretMeta }
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) GetConfigMapMeta() meta.Instance { return s.ConfigMeta }
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) SetDefaults() {
Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO TO FIX DUPLICATE WITH INIT
Timothee Gosselin's avatar
Timothee Gosselin committed
	meta.SetObjectMeta(s.CommonMeta, s.SecretMeta)
	meta.SetObjectMeta(s.CommonMeta, s.ConfigMeta)
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) GetObjects() map[int]objects.Object {
Timothee Gosselin's avatar
Timothee Gosselin committed

	cm := &configmap.ConfigMap{
		ObjectMeta: s.GetConfigMapMeta().(*meta.ObjectMeta),
	}

	secret := &secret.Secret{
		ObjectMeta: s.GetSecretMeta().(*meta.ObjectMeta),
	}

	objs := make(map[int]objects.Object, 2)

Timothee Gosselin's avatar
Timothee Gosselin committed
	//	meta.SetObjectMeta(s.CommonMeta, cm.ObjectMeta)
	//	meta.SetObjectMeta(s.CommonMeta, secret.ObjectMeta)
Timothee Gosselin's avatar
Timothee Gosselin committed

	// TODO REPLACE BY GET SECRET PARAMETERS & GET CONFIG PARAMETERS
Timothee Gosselin's avatar
Timothee Gosselin committed
	// params := s.GetConfig()
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, p := range *s.ConfigSpec.GetParameters() {
Timothee Gosselin's avatar
Timothee Gosselin committed
		// 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)
		}
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO IMPROVE
Timothee Gosselin's avatar
Timothee Gosselin committed
	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
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Component) Init(c client.Client) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO TO FIX DUPLICATE WITH SETDEFAULTS
	meta.SetObjectMeta(s.CommonMeta, s.ConfigMeta)
	meta.SetObjectMeta(s.CommonMeta, s.SecretMeta)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// 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
Timothee Gosselin's avatar
Timothee Gosselin committed
func InitParametersValueFrom(s *Component, c client.Client) error {
Timothee Gosselin's avatar
Timothee Gosselin committed

	params := parameters.Parameters{}

	paramsByKey := parameters.ParametersByKey(s.Parameters)
Timothee Gosselin's avatar
Timothee Gosselin committed
	paramsBySecretSource, paramsByConfigMapSource := OrderByResourceRef(s.ConfigSpec)
Timothee Gosselin's avatar
Timothee Gosselin committed

	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)
Timothee Gosselin's avatar
Timothee Gosselin committed
		err := parameters.GetAndMergeParameters(v, paramsByKey, c, cm)
Timothee Gosselin's avatar
Timothee Gosselin committed
		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)
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	// if defaultMountType == EnvFile {

	//	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	// 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{}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	s.Parameters = &params

	return nil
}