Newer
Older
/*
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
// - provided in a resource in the same namespace - only secrets and configmaps are supported
package settings
import (
"sort"
corev1 "k8s.io/api/core/v1"
"k8s.libre.sh/controller-utils/application/settings/parameters"
// +kubebuilder:object:generate=true
type Sources []Source
// +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
// Only secrets and configmaps in the same namespace are supported as references
// +kubebuilder:object:generate=true
// 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"`
// Parameters is a list of parameters
*parameters.Parameters `json:"parameters,omitempty"`
}
// GetParameters return the parameters
func (c *SettingsSpec) GetParameters() *parameters.Parameters {
if c.Parameters == nil {
c.Parameters = ¶meters.Parameters{}
}
return c.Parameters
}
func (c *SettingsSpec) GetSources() *Sources {
if c.Sources == nil {
c.Sources = &Sources{}
}
return c.Sources
}
// GetEnvFrom returns a list of EnvFromSource to populate environment variables in the container.
func (c *SettingsSpec) GetEnvFrom() []corev1.EnvFromSource {
envFromSecret := []corev1.EnvFromSource{}
envFromConfigMap := []corev1.EnvFromSource{}
envFrom := corev1.EnvFromSource{}
if c.Sources != nil {
for _, source := range *c.Sources {
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
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
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *SettingsSpec) DeepCopyInto(out *SettingsSpec) {
*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)
}
}
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 {