Skip to content
component.go 6.37 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 (
Timothee Gosselin's avatar
Timothee Gosselin committed
	"sort"
	"strings"
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/controller-utils/application/settings/parameters"
	"k8s.libre.sh/controller-utils/interfaces"
	"k8s.libre.sh/controller-utils/meta"
	"k8s.libre.sh/controller-utils/objects"
	"k8s.libre.sh/controller-utils/objects/configmap"
	"k8s.libre.sh/controller-utils/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
	corev1 "k8s.io/api/core/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.io/apimachinery/pkg/runtime"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

Timothee Gosselin's avatar
Timothee Gosselin committed
type Component interface {
	GetMeta() meta.Instance
	GetConfig() Config
	SetDefaults()
	Config
	GetCreateOptions() *CreateOptions
	GetObjects() map[int]objects.Object
}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
// TemplateValues returns the component as values for the template function
Timothee Gosselin's avatar
Timothee Gosselin committed
func TemplateValues(s Component) map[string]interface{} {
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	values := make(map[string]interface{})
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	keyPairValues := parameters.KeyPairValues(s.GetConfig().GetParameters())
	values[s.GetMeta().GetComponent()] = keyPairValues
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return values
Timothee Gosselin's avatar
Timothee Gosselin committed
// GetObjects returns an ordered list of objects.Object interfaces.
// Annotations are added for the generated parameters.
// Only secrets and configmaps are supported.
Timothee Gosselin's avatar
Timothee Gosselin committed
func GetObjects(s Component) map[int]objects.Object {
Timothee Gosselin's avatar
Timothee Gosselin committed
	cm := &configmap.ConfigMap{
		ObjectMeta: s.GetCreateOptions().ConfigMeta,
		Data:       s.GetParameters().GetConfigData(),
Timothee Gosselin's avatar
Timothee Gosselin committed
	secret := &secret.Secret{
		ObjectMeta: s.GetCreateOptions().SecretMeta,
		Data:       s.GetParameters().GetSecretData(),
Timothee Gosselin's avatar
Timothee Gosselin committed
	objs := make(map[int]objects.Object, 2)
Timothee Gosselin's avatar
Timothee Gosselin committed
	genConfigParams := []string{}
	genSecretParams := []string{}

	for _, p := range *s.GetConfig().GetParameters() {
		if p.IsMount() && len(p.Value) > 0 && p.IsRand() {
			switch p.Type {
			case parameters.SecretParameter, "":
				genSecretParams = append(genSecretParams, p.Key)
			case parameters.ConfigParameter:
				genConfigParams = append(genConfigParams, p.Key)
			}
		}
	}
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)

	// 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)
		}
		secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genSecretParams, ",")
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(genConfigParams) > 0 {
		if len(cm.ObjectMeta.Annotations) == 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
			cm.ObjectMeta.Annotations = make(map[string]string, 1)
		}
		cm.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",")
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(secret.Data) > 0 {
		objs[0] = secret
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(cm.Data) > 0 {
		objs[1] = cm
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return objs
func GetGenAnnotionForType(ps *parameters.Parameters, pType parameters.ParameterType) map[string]string {
	list := []string{}
	annot := make(map[string]string)

	for _, p := range *ps {
		if p.IsMount() && len(p.Value) > 0 && p.IsRand() && p.Type == pType {
			list = append(list, p.Key)
		}
	}

	if len(list) > 0 {
		annot["settings.k8s.libre.sh/generate"] = strings.Join(list, ",")
	}

	return annot

}

func GetConfigGenAnnotion(ps *parameters.Parameters) map[string]string {
	list := []string{}
	annot := make(map[string]string)

	for _, p := range *ps {
		if p.IsMount() && len(p.Value) > 0 && p.IsRand() && p.Type == parameters.ConfigParameter {
			list = append(list, p.Key)
		}
	}

	if len(list) > 0 {
		annot["settings.k8s.libre.sh/generate"] = strings.Join(list, ",")
	}

	return annot
}

func GetSecretGenAnnotion(ps *parameters.Parameters) map[string]string {
	list := []string{}
	annot := make(map[string]string)

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

	if len(list) > 0 {
		annot["settings.k8s.libre.sh/generate"] = strings.Join(list, ",")
	}

	return annot
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func Init(s Component, c client.Client, owner interfaces.Object, scheme *runtime.Scheme) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
	opts := s.GetCreateOptions()
	if opts == nil {
		opts = &CreateOptions{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	opts.Init()
Timothee Gosselin's avatar
Timothee Gosselin committed
	meta.SetObjectMeta(opts.CommonMeta, opts.ConfigMeta)
	meta.SetObjectMeta(opts.CommonMeta, opts.SecretMeta)
Timothee Gosselin's avatar
Timothee Gosselin committed

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

	if err != nil {
		return err
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	s.GetParameters().InitRandValues()
Timothee Gosselin's avatar
Timothee Gosselin committed

	return nil
}
Timothee Gosselin's avatar
Timothee Gosselin committed
// InitParametersValueFrom intialise the parameters with values provided in external resources in the same namespace.
// All parameters values are filled from those resources and new parameters are appended.
// Only Secrets and Configmaps are supported.
Timothee Gosselin's avatar
Timothee Gosselin committed
func InitParametersValueFrom(s Component, c client.Client, owner interfaces.Object, scheme *runtime.Scheme) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
	// TO DO TO FIX, InitValueFrom and ParametersFromSources do the same thing ?
	err := s.GetParameters().InitValueFrom(c, owner, scheme)
Timothee Gosselin's avatar
Timothee Gosselin committed
	if err != nil {
		return err
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	ps, err := ParametersFromSources(s.GetSources(), c, owner, scheme)

	if err != nil {
		return err
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	s.GetParameters().Merge(ps)

	return nil
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// ParametersFromSources returns the parameters from external resources that are provided in the sources.
// Owner is provided to check if resources is owned in that case, only generated data defined in the annotations are fetched.
// Only Secrets and Configmaps are supported.
Timothee Gosselin's avatar
Timothee Gosselin committed
func ParametersFromSources(srcs *Sources, c client.Client, owner interfaces.Object, scheme *runtime.Scheme) (*parameters.Parameters, error) {
Timothee Gosselin's avatar
Timothee Gosselin committed

	var obj interfaces.Object
	ps := &parameters.Parameters{}

	for _, src := range *srcs {
		switch src.Type {
		case "configmap":
			obj = &corev1.ConfigMap{}
		case "secret":
			obj = &corev1.Secret{}
		default:
			return ps, nil
		}
		obj.SetName(src.Ref)
		obj.SetNamespace(owner.GetNamespace())

	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	data, err := parameters.GetDataFromResource(c, obj, owner, scheme)
Timothee Gosselin's avatar
Timothee Gosselin committed
	if err != nil {
		return ps, err
	}

	if len(data) > 0 {
		for k, v := range data {
			*ps = append(*ps, &parameters.Parameter{
				Value: v,
				Key:   k,
			})
		}
	}

	return ps, nil

}