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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
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 parameters
import (
"bytes"
"sort"
"text/template"
"github.com/presslabs/controller-util/rand"
corev1 "k8s.io/api/core/v1"
)
func (p *Parameters) InitValues() {
p.InitRandValues()
p.InitTemplateValues(KeyPairValues(p))
}
// InitRandValues initialies the parameters random values
func (p *Parameters) InitRandValues() {
for _, param := range *p {
if len(param.Key) > 0 && len(param.Value) == 0 && param.Generate != GenerateTemplate && param.Type != ObjectFieldParameter {
var size int
switch param.Generate {
case GenerateRand24:
size = 24
random, err := rand.AlphaNumericString(size)
if err != nil {
}
param.Value = random
case GenerateRand12:
size = 12
random, err := rand.AlphaNumericString(size)
if err != nil {
}
param.Value = random
}
}
}
}
// InitTemplateValues initialies the parameters values from a template and a key pair value set
// The template shoud be in the parameter value when GenerateTemplate is specified and the values
// will be replaced from the result of the template processing.
func (p *Parameters) InitTemplateValues(values map[string]string) error {
for _, param := range *p {
if param.Generate == GenerateTemplate {
tmpl, err := template.New(param.Key).Parse(param.Value)
if err != nil {
return err
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, values)
if err != nil {
return err
}
param.Value = tpl.String()
}
}
return nil
}
// GetData get the data for the secret or configmap as a key:values
// This function only returns data which should be either in a secret or a configmap
// It implements the configmap and secret interfaces
func (p *Parameters) GetData() map[string]string {
data := make(map[string]string)
for _, param := range *p {
// TODO TO FIX
if (param.MountType == MountEnvFile || param.MountType == "") &&
param.Type != ObjectFieldParameter &&
len(param.Value) > 0 {
data[param.Key] = param.Value
}
}
return data
}
// GetEnvVar gets a list of environment variables to set in the container.
func (p *Parameters) GetEnvVar() []corev1.EnvVar {
envVars := []corev1.EnvVar{}
envVar := corev1.EnvVar{}
var err error
if p != nil {
for _, param := range *p {
envVar, err = param.GetEnvVar()
// TODO TOFIX
if err != nil {
}
if len(envVar.Name) > 0 {
envVars = append(envVars, envVar)
}
}
}
if len(envVars) > 0 {
// Sort var to avoid update of the object if var are not in the same order?
sort.SliceStable(envVars, func(i, j int) bool {
return envVars[i].Name < envVars[j].Name
})
return envVars
}
return nil
}
131
132
133
134
135
136
137
138
139
140
141
142
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
func (p *Parameters) GetVolumeMounts() []corev1.VolumeMount {
volumeMounts := []corev1.VolumeMount{}
if p != nil {
for _, param := range *p {
if param.MountType == MountFile {
volumeMount := param.GetVolumeMount()
// TODO TOFIX
/* if err != nil {
} */
if len(volumeMount.Name) > 0 {
volumeMounts = append(volumeMounts, volumeMount)
}
}
}
}
return volumeMounts
}
func (p *Parameters) GetPodVolumes() []corev1.Volume {
volumes := []corev1.Volume{}
if p != nil {
for _, param := range *p {
if param.MountType == MountFile {
volume := param.GetPodVolume()
// TODO TOFIX
/* if err != nil {
} */
if len(volume.Name) > 0 {
volumes = append(volumes, volume)
}
}
}
}
return volumes
}