Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
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 = ¶meters.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 {
envFromSecret := []corev1.EnvFromSource{}
envFromConfigMap := []corev1.EnvFromSource{}
envFrom := corev1.EnvFromSource{}
if c.Parameters != nil {
for _, p := range *c.Parameters {
if p.MountType == parameters.MountEnvFile && len(p.Ref) > 0 {
if p.Type == parameters.ConfigParameter {
Ref: p.Ref,
Type: string(p.Type),
}
} else if p.Type == parameters.SecretParameter {
Ref: p.Ref,
Type: string(p.Type),
}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
}
}
}
}
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,
},
},
}
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
}
// 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
}