Skip to content
config.go 5.44 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 provides some api types and interfaces to manage settings in kubernetes
//
// Settings can be provided in:
// - The parent CRD with the parameter type
// - ConfigMaps and Secrets
//
// Settings can be passed to a container:
// - As EnvVar in the container spec (name & value)
// - As EnvVarFrom in the container spec with a fromKey and secret/configMap source
// - As EnvVarFrom object
// - As EnvFrom mounting all the data from a secret/configmap as env var
// - As a file configmap/secret
//
// Settings value:
// - can be randomly generate
// - provided in the crd
// - generate by template
package settings

import (
	"sort"

	corev1 "k8s.io/api/core/v1"
	"k8s.libre.sh/application/settings/parameters"
)

// +kubebuilder:object:generate=true
type Source struct {
	// Ref is the name of a resource in the same namespace
	Ref string `json:"ref,omitempty"`
	// Type is the type of the resource
	// Only Secrets and ConfigMaps are supported
	Type string `json:"type,omitempty"`
}

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

// SetParameters sets the parameters
func (c *ConfigSpec) SetParameters(parameters *parameters.Parameters) { c.Parameters = parameters }

// GetParameters return the parameters
func (c *ConfigSpec) GetParameters() *parameters.Parameters {
	if c.Parameters == nil {
		c.Parameters = &parameters.Parameters{}
	}
	return c.Parameters
}

func (c *ConfigSpec) GetSources() []Source { return c.Sources }

func (c *ConfigSpec) SetSources(sources []Source) { c.Sources = sources }

// GetEnvFrom returns a list of EnvFromSource to populate environment variables in the container.
func (c *ConfigSpec) GetEnvFrom() []corev1.EnvFromSource {
	//	envFroms := []corev1.EnvFromSource{}
	envFromSecret := []corev1.EnvFromSource{}
	envFromConfigMap := []corev1.EnvFromSource{}

	// envFromMap := map[int]corev1.EnvFromSource{}
	envFrom := corev1.EnvFromSource{}
	src := Source{}

	for _, p := range *c.Parameters {
		if p.MountType == parameters.MountEnvFile && len(p.Ref) > 0 {
			if p.Type == parameters.ConfigParameter {
				src = Source{
					Ref:  p.Ref,
					Type: string(p.Type),
				}
				c.Sources = append(c.Sources, src)
			} else if p.Type == parameters.SecretParameter {
				src = Source{
					Ref:  p.Ref,
					Type: string(p.Type),
				}
				c.Sources = append(c.Sources, src)
			}
		}
	}

	if len(c.Sources) > 0 {
		for _, source := range c.Sources {
			if source.Type == "configmap" {
				envFrom = corev1.EnvFromSource{
					ConfigMapRef: &corev1.ConfigMapEnvSource{
						LocalObjectReference: corev1.LocalObjectReference{
							Name: source.Ref,
						},
					},
				}
				//	envFromMap[k] = envFrom
				envFromConfigMap = append(envFromConfigMap, envFrom)
			} else if source.Type == "secret" {
				envFrom = corev1.EnvFromSource{
					SecretRef: &corev1.SecretEnvSource{
						LocalObjectReference: corev1.LocalObjectReference{
							Name: source.Ref,
						},
					},
				}
				//	envFromMap[k] = envFrom
				envFromSecret = append(envFromSecret, envFrom)
			}
		}
	}

	// We need to sort so the order is always the same and container is not update
	//	if len(envFromSecret) > 0 {
	sort.SliceStable(envFromSecret, func(i, j int) bool {
		return envFromSecret[i].SecretRef.Name < envFromSecret[j].SecretRef.Name
	})
	sort.SliceStable(envFromConfigMap, func(i, j int) bool {
		return envFromConfigMap[i].ConfigMapRef.Name < envFromConfigMap[j].ConfigMapRef.Name
	})
	//	}

	envFroms := append(envFromSecret, envFromConfigMap...)

	/* 	if len(envFroms) > 0 {
	   		return envFroms
	   	}
	*/
	/* 	for _, v := range envFromMap {
	   		envFroms = append(envFroms, v)
	   	}
	*/
	return envFroms

	//	return nil
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
	*out = *in
	if in.Sources != nil {
		in, out := &in.Sources, &out.Sources
		*out = make([]Source, len(*in))
		copy(*out, *in)
	}
	if in.Parameters != nil {
		in, out := &in.Parameters, &out.Parameters
		*out = new(parameters.Parameters)
		if **in != nil {
			in, out := *in, *out
			*out = make([]*parameters.Parameter, len(*in))
			for i := range *in {
				if (*in)[i] != nil {
					in, out := &(*in)[i], &(*out)[i]
					*out = new(parameters.Parameter)
					**out = **in
				}
			}
		}
	}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ConfigSpec.
func (in *ConfigSpec) DeepCopy() *ConfigSpec {
	if in == nil {
		return nil
	}
	out := new(ConfigSpec)
	in.DeepCopyInto(out)
	return out
}