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 parameters
import (
"bytes"
"sort"
"text/template"
"github.com/presslabs/controller-util/rand"
corev1 "k8s.io/api/core/v1"
"k8s.libre.sh/interfaces"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// InitRandValues initialies the parameters random values
func (p *Parameters) InitRandValues() {
for _, param := range *p {
if len(param.Key) > 0 && param.IsRand() && len(param.Value) == 0 {
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.
// The values will be replaced from the result of the template processing.
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 returns the data for the secret or configmap as a key:value pairs
// This function only returns data which should be either in a secret or a configmap
func (p *Parameters) GetData() map[string]string {
data := make(map[string]string)
for _, param := range *p {
param.Type != ObjectFieldParameter &&
len(param.Value) > 0 {
data[param.Key] = param.Value
}
}
return data
}
// GetConfigData returns the parameters of type ConfigParameter as key:value pairs
// It implements the configmap interface
func (p *Parameters) GetConfigData() map[string]string {
data := make(map[string]string)
for _, param := range *p {
if param.IsMount() &&
param.Type == ConfigParameter &&
len(param.Value) > 0 {
data[param.Key] = param.Value
}
}
return data
}
// GetSecretData returns the parameters of type ConfigParameter as key:value pairs
// It implements the secret interface
func (p *Parameters) GetSecretData() map[string][]byte {
data := make(map[string][]byte)
for _, param := range *p {
if param.IsMount() &&
// TODO TO FIX ?
(param.Type == SecretParameter || param.Type == "") &&
len(param.Value) > 0 {
data[param.Key] = []byte(param.Value)
}
}
return data
}
// GetEnvVar return a list of EnvVar 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
}
// GetVolumeMounts returns a list of VolumeMount to set in the container.
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
}
// GetPodVolumes returns a list of Volume to set in the pod.
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
}
// MergeData merges the parameters with a set of data provided as key:values pairs.
// Data values takes over parameters values and are appended to the parameters if not set.
func (ps *Parameters) MergeData(data map[string]string) error {
paramsByKey := ByKey(ps)
if len(data) > 0 {
if len(*ps) > 0 {
for _, p := range *ps {
if len(string(data[p.FromKey])) == 0 {
return errors.New("parameter is not in resource data")
}
paramsByKey[p.Key].Value = string(data[p.FromKey])
// TODO to fix, is it needed if data is generated only if value is empty ?
// Reset default Generate
paramsByKey[p.Key].Generate = ""
}
} else {
for k, v := range data {
*ps = append(*ps, &Parameter{
Value: v,
Key: k,
})
}
}
}
return nil
}
// Merge mergers parameters with new parameters.
// The new parameters provided takes over the parameters and are appended to the parameters if not set.
func (ps *Parameters) Merge(in *Parameters) error {
inByKey := ByKey(in)
destByKey := ByKey(ps)
for k, p := range inByKey {
if destByKey[k] != nil {
p.DeepCopyInto(destByKey[k])
} else {
*ps = append(*ps, p)
}
}
return nil
}
// InitValueFrom intialise the parameters value from external resources
// Only secrets and configmaps are supported
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
func (ps *Parameters) InitValueFrom(c client.Client, owner interfaces.Object) error {
var obj interfaces.Object
sorted := OrderByResourceRef(ps)
for ptype, byRef := range sorted {
switch ptype {
case ConfigParameter:
obj = &corev1.ConfigMap{}
case SecretParameter:
obj = &corev1.Secret{}
default:
return nil
}
for ref, params := range byRef {
obj.SetName(ref)
obj.SetNamespace(owner.GetNamespace())
data, err := GetDataFromResource(c, obj, owner)
if err != nil {
return err
}
err = params.MergeData(data)
if err != nil {
return err
}
}
}
return nil
}