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

Timothee Gosselin's avatar
Timothee Gosselin committed
	"sigs.k8s.io/controller-runtime/pkg/client"

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"
)

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

// +kubebuilder:object:generate=true
Timothee Gosselin's avatar
Timothee Gosselin committed
type Settings struct {
	CreateOptions *CreateOptions `json:"createOptions,omitempty"`
	*SettingsSpec `json:",inline"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

// SettingsSpec defines a list of parameters and references to resources from which those parameters can be fetched
Timothee Gosselin's avatar
Timothee Gosselin committed
// Only secrets and configmaps in the same namespace are supported as references
// +kubebuilder:object:generate=true
/* type SettingsSpec struct {
Timothee Gosselin's avatar
Timothee Gosselin committed
	Sources []Source `json:"sources,omitempty"`
	// Parameters is a list of parameters
	*parameters.Parameters `json:"parameters,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed

// +kubebuilder:object:generate=true
Timothee Gosselin's avatar
Timothee Gosselin committed
type CreateOptions 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"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	Generate     SettingsGenerate `json:"generate,omitempty"`
	SettingsType SettingsType     `json:"type,omitempty"`
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func NewSettings(s Component) *Settings {
	return &Settings{
		CreateOptions: s.GetCreateOptions(),
		SettingsSpec: &SettingsSpec{
			Sources:    s.GetConfig().GetSources(),
			Parameters: s.GetConfig().GetParameters(),
		},
	}
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (opts *CreateOptions) Init() {
	if opts.CommonMeta == nil {
		opts.CommonMeta = &meta.ObjectMeta{}
	}

	if opts.CommonMeta.Labels == nil {
		opts.CommonMeta.Labels = make(map[string]string)
	}

	if opts.SecretMeta == nil {
		opts.SecretMeta = &meta.ObjectMeta{}
	}

	if opts.SecretMeta.Labels == nil {
		opts.SecretMeta.Labels = make(map[string]string)
	}

	if opts.ConfigMeta == nil {
		opts.ConfigMeta = &meta.ObjectMeta{}
	}

	if opts.ConfigMeta.Labels == nil {
		opts.SecretMeta.Labels = make(map[string]string)
	}

}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Settings) GetCreateOptions() *CreateOptions { return s.CreateOptions }
Timothee Gosselin's avatar
Timothee Gosselin committed

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

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

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

	cm := &configmap.ConfigMap{
		ObjectMeta: s.CreateOptions.ConfigMeta,
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	secret := &secret.Secret{
		ObjectMeta: s.CreateOptions.SecretMeta,
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

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

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

	for _, p := range *s.SettingsSpec.Parameters {
Timothee Gosselin's avatar
Timothee Gosselin committed
		// TODO TO FIX
Timothee Gosselin's avatar
Timothee Gosselin committed
		if (p.MountType == parameters.MountEnvFile || p.MountType == parameters.MountFile) &&
Timothee Gosselin's avatar
Timothee Gosselin committed
			// TODO TO FIX
			(p.Type == parameters.SecretParameter || p.Type == "") &&
			len(p.Value) > 0 {
			if p.Generate != parameters.GenerateTemplate && p.Generate != "" {
				genSecretParams = append(genSecretParams, p.Key)
Timothee Gosselin's avatar
Timothee Gosselin committed
			}
Timothee Gosselin's avatar
Timothee Gosselin committed
			secret.Parameters = append(secret.Parameters, p)
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
		if (p.MountType == parameters.MountEnvFile || p.MountType == parameters.MountFile) &&
Timothee Gosselin's avatar
Timothee Gosselin committed
			p.Type == parameters.ConfigParameter &&
			len(p.Value) > 0 {
			if p.Generate != parameters.GenerateTemplate && p.Generate != "" {
				genConfigParams = append(genConfigParams, p.Key)
Timothee Gosselin's avatar
Timothee Gosselin committed
			}
Timothee Gosselin's avatar
Timothee Gosselin committed
			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)

	// TODO TO FIX remove annotation if secret is not generated anymore
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(genSecretParams) > 0 {
		if len(secret.ObjectMeta.Annotations) == 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
			secret.ObjectMeta.Annotations = make(map[string]string, len(secret.ObjectMeta.Annotations))
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
		secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genSecretParams, ",")
	}

	if len(genConfigParams) > 0 {
		if len(secret.ObjectMeta.Annotations) == 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
			cm.ObjectMeta.Annotations = make(map[string]string, len(genConfigParams))
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
		secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",")
	}

	if len(secret.Parameters) > 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
		objs[0] = secret
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(cm.Parameters) > 0 {
		objs[1] = cm
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	return objs
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Settings) SetDefaults() {
Timothee Gosselin's avatar
Timothee Gosselin committed
// func (s *Settings) Init(r interfaces.Reconcile, owner interfaces.Object) error {
func (s *Settings) Init(c client.Client, owner interfaces.Object) error {
	if s.CreateOptions == nil {
		s.CreateOptions = &CreateOptions{}
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	s.CreateOptions.Init()

	meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.ConfigMeta)
	meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.SecretMeta)
Timothee Gosselin's avatar
Timothee Gosselin committed

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

	if err != nil {
		return err
	}

	s.InitRandValues()

	if err != nil {
		return err
	}

	return nil
}

// InitParametersValueFrom intialise the parameters with values provided in external resources in the same namespace
// All parameters values are filled from those resources
// Only Secrets and Configmaps are supported
Timothee Gosselin's avatar
Timothee Gosselin committed
//func InitParametersValueFrom(s *Settings, r interfaces.Reconcile, owner interfaces.Object) error {
// func InitParametersValueFrom(s *Settings, c client.Client, owner interfaces.Object) error {
/* 	err := InitParamsValueFrom(s, c, owner)
   	if err != nil {
   		return err
   	}
   	return nil */
/*
Timothee Gosselin's avatar
Timothee Gosselin committed
	params := parameters.Parameters{}
	cm := &corev1.ConfigMap{}
	sec := &corev1.Secret{}

	paramsBySecretSource, paramsByConfigMapSource := OrderByResourceRef(s.SettingsSpec)
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	for k, v := range paramsBySecretSource {
Timothee Gosselin's avatar
Timothee Gosselin committed
		sec.SetNamespace(s.CreateOptions.CommonMeta.GetNamespace())
Timothee Gosselin's avatar
Timothee Gosselin committed
		sec.SetName(k)

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

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

		params = append(params, ps...)

Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	for k, v := range paramsByConfigMapSource {
		cm.SetName(k)
Timothee Gosselin's avatar
Timothee Gosselin committed
		cm.SetNamespace(s.CreateOptions.CommonMeta.GetNamespace())
Timothee Gosselin's avatar
Timothee Gosselin committed

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

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

		params = append(params, ps...)

Timothee Gosselin's avatar
Timothee Gosselin committed
	}

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

Timothee Gosselin's avatar
Timothee Gosselin committed
	// Reset sources, we do not want to mount orginal resources
	if s.CreateOptions.Generate == GenEnvFile {
		s.Sources = &Sources{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	s.Parameters = parameters.MergeParameters(s.Parameters, &params)
Timothee Gosselin's avatar
Timothee Gosselin committed

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

		if s.CreateOptions.SettingsType == "configmap" {
			v.Type = parameters.ConfigParameter
		} else if s.CreateOptions.SettingsType == "secret" {
			v.Type = parameters.SecretParameter
		}

		if v.MountType == parameters.MountFile && len(v.Ref) == 0 {
			if v.Type == parameters.ConfigParameter {
Timothee Gosselin's avatar
Timothee Gosselin committed
				v.Ref = s.GetCreateOptions().ConfigMeta.GetName()

			}
			if v.Type == parameters.SecretParameter {
Timothee Gosselin's avatar
Timothee Gosselin committed
				v.Ref = s.GetCreateOptions().SecretMeta.GetName()
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	return nil
Timothee Gosselin's avatar
Timothee Gosselin committed
*/
// }