Skip to content
parameters_helpers.go 3.09 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Timothee Gosselin's avatar
Timothee Gosselin committed
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
Timothee Gosselin's avatar
Timothee Gosselin committed
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Timothee Gosselin's avatar
Timothee Gosselin committed
    https://www.gnu.org/licenses/agpl-3.0.html
Timothee Gosselin's avatar
Timothee Gosselin committed

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.
*/

Timothee Gosselin's avatar
Timothee Gosselin committed
package parameters
Timothee Gosselin's avatar
Timothee Gosselin committed

import (
Timothee Gosselin's avatar
Timothee Gosselin committed
	"bytes"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"text/template"
Timothee Gosselin's avatar
Timothee Gosselin committed

	"github.com/presslabs/controller-util/rand"
	corev1 "k8s.io/api/core/v1"
)

Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameters) InitValues() {
	p.InitRandValues()
Timothee Gosselin's avatar
Timothee Gosselin committed
	p.InitTemplateValues(KeyPairValues(p))
Timothee Gosselin's avatar
Timothee Gosselin committed
// InitRandValues initialies the parameters random values
Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameters) InitRandValues() {
Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, param := range *p {

Timothee Gosselin's avatar
Timothee Gosselin committed
		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
Timothee Gosselin's avatar
Timothee Gosselin committed
				random, err := rand.AlphaNumericString(size)
				if err != nil {
				}
Timothee Gosselin's avatar
Timothee Gosselin committed
				param.Value = random

			case GenerateRand12:
				size = 12
				random, err := rand.AlphaNumericString(size)
				if err != nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
				}
Timothee Gosselin's avatar
Timothee Gosselin committed
				param.Value = random
Timothee Gosselin's avatar
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.
Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameters) InitTemplateValues(values map[string]string) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, param := range *p {
		if param.Generate == GenerateTemplate {
			tmpl, err := template.New(param.Key).Parse(param.Value)
Timothee Gosselin's avatar
Timothee Gosselin committed

			if err != nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
				return err
Timothee Gosselin's avatar
Timothee Gosselin committed
			}

			var tpl bytes.Buffer
			err = tmpl.Execute(&tpl, values)

			if err != nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
				return err
Timothee Gosselin's avatar
Timothee Gosselin committed
			param.Value = tpl.String()
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	return nil
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// 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
Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameters) GetData() map[string]string {
	data := make(map[string]string)
Timothee Gosselin's avatar
Timothee Gosselin committed

	for _, param := range *p {
		// TODO TO FIX
		if (param.MountType == MountEnvFile || param.MountType == "") &&
Timothee Gosselin's avatar
Timothee Gosselin committed
			param.Type != ObjectFieldParameter &&
			len(param.Value) > 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
			data[param.Key] = param.Value
		}
	}

	return data
Timothee Gosselin's avatar
Timothee Gosselin committed
// GetEnvVar gets a list of environment variables to set in the container.
func (p *Parameters) GetEnvVar() []corev1.EnvVar {
Timothee Gosselin's avatar
Timothee Gosselin committed
	envVars := []corev1.EnvVar{}
	envVar := corev1.EnvVar{}

	var err error

	for _, param := range *p {
		envVar, err = param.GetEnvVar()
Timothee Gosselin's avatar
Timothee Gosselin committed
		if err != nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed

		if len(envVar.Name) > 0 {
			envVars = append(envVars, envVar)
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	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
		})

Timothee Gosselin's avatar
Timothee Gosselin committed
		return envVars
	}
	return nil
Timothee Gosselin's avatar
Timothee Gosselin committed
}