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

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 (
	"sort"
	"strings"

	corev1 "k8s.io/api/core/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/application/settings/parameters"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/meta"
	"k8s.libre.sh/objects"
	"k8s.libre.sh/objects/configmap"
	"k8s.libre.sh/objects/secret"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"sigs.k8s.io/controller-runtime/pkg/client"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

Timothee Gosselin's avatar
Timothee Gosselin committed
type Component interface {
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetMeta() meta.Instance
	GetConfig() Config
	SetDefaults()
	GetParameters() *parameters.Parameters
	GetSources() *Sources
Timothee Gosselin's avatar
Timothee Gosselin committed
	//	Init(c client.Client, owner interfaces.Object) error
	GetCreateOptions() *CreateOptions
	GetObjects() map[int]objects.Object
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func TemplateValues(s Component) map[string]interface{} {
Timothee Gosselin's avatar
Timothee Gosselin committed

	values := make(map[string]interface{})

	keyPairValues := parameters.KeyPairValues(s.GetConfig().GetParameters())
	values[s.GetMeta().GetComponent()] = keyPairValues

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

	cm := &configmap.ConfigMap{
		ObjectMeta: s.GetCreateOptions().ConfigMeta,
	}

	secret := &secret.Secret{
		ObjectMeta: s.GetCreateOptions().SecretMeta,
	}

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

	genConfigParams := []string{}
	genSecretParams := []string{}

	for _, p := range *s.GetConfig().GetParameters() {
Timothee Gosselin's avatar
Timothee Gosselin committed

		if p.IsMount() && len(p.Value) > 0 {
			if p.Type == parameters.SecretParameter || p.Type == "" {
				if p.IsRand() {
					genSecretParams = append(genSecretParams, p.Key)
				}
				secret.Parameters = append(secret.Parameters, p)
Timothee Gosselin's avatar
Timothee Gosselin committed
			if p.Type == parameters.ConfigParameter {
				if p.IsRand() {
					genConfigParams = append(genConfigParams, p.Key)
				}
				cm.Parameters = append(cm.Parameters, p)
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
	// Sort annotations as it is sorted in kubernetes and will have a diff otherwise
	sort.Strings(genConfigParams)
	sort.Strings(genSecretParams)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// secretAnnot := Annotations(genSecretParams)
	// configAnnot := Annotations(genConfigParams)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// 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, 1)
Timothee Gosselin's avatar
Timothee Gosselin committed
		secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genSecretParams, ",")
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(genConfigParams) > 0 {
		if len(secret.ObjectMeta.Annotations) == 0 {
			cm.ObjectMeta.Annotations = make(map[string]string, 1)
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
		secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",")
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(secret.Parameters) > 0 {
		objs[0] = secret
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(cm.Parameters) > 0 {
		objs[1] = cm
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	return objs

Timothee Gosselin's avatar
Timothee Gosselin committed
/* func GetAnnotations(ps *parameters.Parameters) map[string]string {
	//	genParamsByType := map[string]string
Timothee Gosselin's avatar
Timothee Gosselin committed

	for _, p := range *ps {
		if IsMount(p) && len(p.Value) > 0 {
			if p.Type == parameters.SecretParameter || p.Type == "" {
				if IsRand(p) {
					genSecretParams = append(genSecretParams, p.Key)
				}
				secret.Parameters = append(secret.Parameters, p)
			}
			if p.Type == parameters.ConfigParameter {
				if IsRand(p) {
					genConfigParams = append(genConfigParams, p.Key)
				}
				cm.Parameters = append(cm.Parameters, p)
			}

		}
	}

	if len(paramsList) > 0 {
		return map[string]string{
			"settings.k8s.libre.sh/generate": strings.Join(genConfigParams, ","),
		}
	}

	return nil
Timothee Gosselin's avatar
Timothee Gosselin committed
} */
// func (s *Component) Init(r interfaces.Reconcile, owner interfaces.Object) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
func Init(s Component, c client.Client, owner interfaces.Object) error {

	opts := s.GetCreateOptions()
	if opts == nil {
		opts = &CreateOptions{}
	}
	opts.Init()

	meta.SetObjectMeta(opts.CommonMeta, opts.ConfigMeta)
	meta.SetObjectMeta(opts.CommonMeta, opts.SecretMeta)

Timothee Gosselin's avatar
Timothee Gosselin committed
	err := InitParametersValueFrom(s, c, owner)

	if err != nil {
		return err
	}

	s.GetParameters().InitRandValues()

	if err != nil {
		return err
	}

	return nil
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func OrderByResourceRef(s Component) (secrets, configMaps map[string]parameters.Parameters) {
	secrets = make(map[string]parameters.Parameters)
	configMaps = make(map[string]parameters.Parameters)

	for _, source := range *s.GetSources() {
		if source.Type == "secret" {
			secrets[source.Ref] = parameters.Parameters{}
		} else if source.Type == "configmap" {
			configMaps[source.Ref] = parameters.Parameters{}
		}
	}

	// Then we get the sources that are provided for each parameters and will need a transformation
	for _, p := range *s.GetParameters() {
		if p != nil {
			// TOFIX only refType ? by default secret ?
			if len(p.ValueFrom.Ref) > 0 && (p.Type == parameters.SecretParameter || p.RefType == "secret") {
				// TODO add a warning, if secret given a source and in valuefrom only keys in valuefrom will be fetched
				secrets[p.ValueFrom.Ref] = append(secrets[p.ValueFrom.Ref], p)
			}
			if len(p.ValueFrom.Ref) > 0 && (p.Type == parameters.ConfigParameter || p.RefType == "configmap") {
				configMaps[p.ValueFrom.Ref] = append(configMaps[p.ValueFrom.Ref], p)
			}
		}

	}

	return secrets, configMaps
}

// InitParametersValueFrom intialise the parameters with values provided in external resources in the same namespace
Timothee Gosselin's avatar
Timothee Gosselin committed
// All parameters values are filled from those resources and new parameters are created when needed
// Only Secrets and Configmaps are supported
Timothee Gosselin's avatar
Timothee Gosselin committed
func InitParametersValueFrom(s Component, c client.Client, owner interfaces.Object) error {

	params := parameters.Parameters{}
	cm := &corev1.ConfigMap{}
	sec := &corev1.Secret{}

Timothee Gosselin's avatar
Timothee Gosselin committed
	paramsBySecretSource, paramsByConfigMapSource := OrderByResourceRef(s)
Timothee Gosselin's avatar
Timothee Gosselin committed
	for resName, params := range paramsBySecretSource {
		sec.SetNamespace(s.GetCreateOptions().CommonMeta.GetNamespace())
Timothee Gosselin's avatar
Timothee Gosselin committed
		sec.SetName(resName)
		/* 		for _, p := range params {
			fmt.Println(p)
		} */
Timothee Gosselin's avatar
Timothee Gosselin committed
		ps, err := parameters.MergeParametersFromResource(c, sec, owner, params)

		if err != nil {
			return err
		}

		params = append(params, ps...)

	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	for resName, params := range paramsByConfigMapSource {
		cm.SetName(resName)
		cm.SetNamespace(s.GetCreateOptions().CommonMeta.GetNamespace())

Timothee Gosselin's avatar
Timothee Gosselin committed
		ps, err := parameters.MergeParametersFromResource(c, cm, owner, params)

		if err != nil {
			return err
		}

		params = append(params, ps...)

	}

	secretSrc := Source{
		Ref:  s.GetCreateOptions().SecretMeta.GetName(),
		Type: "secret",
	}
	configSrc := Source{
		Ref:  s.GetCreateOptions().ConfigMeta.GetName(),
		Type: "configmap",
	}

	srcs := &Sources{}
	// Reset sources, we do not want to mount orginal resources
	if s.GetCreateOptions().Generate == GenEnvFile {
		srcs = &Sources{}
	}

	bar := parameters.MergeParameters(s.GetParameters(), &params)

	bar.DeepCopyInto(s.GetParameters())

Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, p := range *s.GetParameters() {
		if p.MountType == parameters.MountEnvFile || s.GetCreateOptions().Generate == GenEnvFile {
			p.MountType = parameters.MountEnvFile
			// add newly created resource ref to sources
Timothee Gosselin's avatar
Timothee Gosselin committed
			if p.Type == parameters.ConfigParameter {
				srcs = AppendSourceIfUnique(srcs, configSrc)
Timothee Gosselin's avatar
Timothee Gosselin committed
			} else if p.Type == parameters.SecretParameter || p.Type == "" {
				srcs = AppendSourceIfUnique(srcs, secretSrc)
			}
			// Reset valueFrom, we do not want to mount orginal resources
Timothee Gosselin's avatar
Timothee Gosselin committed
			p.ValueFrom = parameters.ValueFrom{}
		}

		if s.GetCreateOptions().SettingsType == "configmap" {
Timothee Gosselin's avatar
Timothee Gosselin committed
			p.Type = parameters.ConfigParameter
		} else if s.GetCreateOptions().SettingsType == "secret" {
Timothee Gosselin's avatar
Timothee Gosselin committed
			p.Type = parameters.SecretParameter
Timothee Gosselin's avatar
Timothee Gosselin committed
		if p.MountType == parameters.MountFile && len(p.Ref) == 0 {
			if p.Type == parameters.ConfigParameter {
				p.Ref = s.GetCreateOptions().ConfigMeta.GetName()
Timothee Gosselin's avatar
Timothee Gosselin committed
			if p.Type == parameters.SecretParameter {
				p.Ref = s.GetCreateOptions().SecretMeta.GetName()

			}
		}
	}

	srcs.DeepCopyInto(s.GetSources())

	return nil
}