Skip to content
setttingsspec.go 4.88 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
Timothee Gosselin's avatar
Timothee Gosselin committed
// - provided in a resource in the same namespace - only secrets and configmaps are supported
Timothee Gosselin's avatar
Timothee Gosselin committed
package settings

import (
	"sort"

	corev1 "k8s.io/api/core/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/controller-utils/application/settings/parameters"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

// +kubebuilder:object:generate=true
type Sources []Source

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

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

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

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetSources return the sources
func (c *SettingsSpec) GetSources() *Sources {
	if c.Sources == nil {
		c.Sources = &Sources{}
	}
	return c.Sources
}
Timothee Gosselin's avatar
Timothee Gosselin committed

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

	envFrom := corev1.EnvFromSource{}

	if c.Sources != nil {
		for _, source := range *c.Sources {
Timothee Gosselin's avatar
Timothee Gosselin committed
			if source.Type == "configmap" {
				envFrom = corev1.EnvFromSource{
					ConfigMapRef: &corev1.ConfigMapEnvSource{
						LocalObjectReference: corev1.LocalObjectReference{
							Name: source.Ref,
						},
					},
				}
				envFromConfigMap = append(envFromConfigMap, envFrom)
			} else if source.Type == "secret" {
				envFrom = corev1.EnvFromSource{
					SecretRef: &corev1.SecretEnvSource{
						LocalObjectReference: corev1.LocalObjectReference{
							Name: source.Ref,
						},
					},
				}
				envFromSecret = append(envFromSecret, envFrom)
			}
		}
	}

	// We need to sort so the order is always the same and container is not updated
Timothee Gosselin's avatar
Timothee Gosselin committed
	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
	}

	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.
func (in *SettingsSpec) DeepCopyInto(out *SettingsSpec) {
Timothee Gosselin's avatar
Timothee Gosselin committed
	*out = *in
	if in.Sources != nil {
		in, out := &in.Sources, &out.Sources
		*out = new(Sources)
		if **in != nil {
			in, out := *in, *out
			*out = make([]Source, len(*in))
			copy(*out, *in)
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
	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 SettingsSpec.
func (in *SettingsSpec) DeepCopy() *SettingsSpec {
Timothee Gosselin's avatar
Timothee Gosselin committed
	if in == nil {
		return nil
	}
	out := new(SettingsSpec)
Timothee Gosselin's avatar
Timothee Gosselin committed
	in.DeepCopyInto(out)
	return out
}