Skip to content
config.go 5.44 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 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
Timothee Gosselin's avatar
Timothee Gosselin committed
package settings

import (
Timothee Gosselin's avatar
Timothee Gosselin committed
	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
)

// +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"`
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// 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
Timothee Gosselin's avatar
Timothee Gosselin committed
// +kubebuilder:object:generate=true
Timothee Gosselin's avatar
Timothee Gosselin committed
type ConfigSpec struct {
	// Sources is a list of sources for the parameters from kubernetes resources in the same namespace
	Sources []Source `json:"sources,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	// Parameters is a list of parameters
Timothee Gosselin's avatar
Timothee Gosselin committed
	*parameters.Parameters `json:"parameters,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetParameters sets the parameters
Timothee Gosselin's avatar
Timothee Gosselin committed
func (c *ConfigSpec) SetParameters(parameters *parameters.Parameters) { c.Parameters = parameters }
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetParameters return the parameters
Timothee Gosselin's avatar
Timothee Gosselin committed
func (c *ConfigSpec) GetParameters() *parameters.Parameters {
	if c.Parameters == nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		c.Parameters = &parameters.Parameters{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	return c.Parameters
Timothee Gosselin's avatar
Timothee Gosselin committed
}
Timothee Gosselin's avatar
Timothee Gosselin committed

func (c *ConfigSpec) GetSources() []Source { return c.Sources }
Timothee Gosselin's avatar
Timothee Gosselin committed

func (c *ConfigSpec) SetSources(sources []Source) { c.Sources = sources }
Timothee Gosselin's avatar
Timothee Gosselin committed

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

	// envFromMap := map[int]corev1.EnvFromSource{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	envFrom := corev1.EnvFromSource{}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	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),
Timothee Gosselin's avatar
Timothee Gosselin committed
				}
				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)
Timothee Gosselin's avatar
Timothee Gosselin committed

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

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
Timothee Gosselin's avatar
Timothee Gosselin committed
func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
Timothee Gosselin's avatar
Timothee Gosselin committed
	*out = *in
	if in.Sources != nil {
		in, out := &in.Sources, &out.Sources
		*out = make([]Source, len(*in))
Timothee Gosselin's avatar
Timothee Gosselin committed
		copy(*out, *in)
	}
	if in.Parameters != nil {
		in, out := &in.Parameters, &out.Parameters
Timothee Gosselin's avatar
Timothee Gosselin committed
		*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.
Timothee Gosselin's avatar
Timothee Gosselin committed
func (in *ConfigSpec) DeepCopy() *ConfigSpec {
Timothee Gosselin's avatar
Timothee Gosselin committed
	if in == nil {
		return nil
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	out := new(ConfigSpec)
Timothee Gosselin's avatar
Timothee Gosselin committed
	in.DeepCopyInto(out)
	return out
}