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
/*
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"
)
// 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
}
}
Timothee Gosselin
committed
}
}
// 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 {
func (p *Parameters) InitTemplateValues(values map[string]interface{}) 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 == MountFile || 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
}
func (p *Parameters) GetVolumeMounts() []corev1.VolumeMount {
volumeMounts := []corev1.VolumeMount{}
if p != nil {
for _, param := range *p {
if param.MountType == MountFile {
volumeMount := param.GetVolumeMount()
if len(volumeMount.Name) > 0 {
volumeMounts = append(volumeMounts, volumeMount)
}
}
}
}
if len(volumeMounts) == 0 {
return nil
}
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()
if len(volume.Name) > 0 {
volumes = append(volumes, volume)
}
}
}
}
if len(volumes) == 0 {
return nil
}