Skip to content
parameter_helper.go 3.55 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
	corev1 "k8s.io/api/core/v1"
)

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetEnvVar return the EnvVar to set in the container.
Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameter) GetEnvVar() (envVar corev1.EnvVar, err error) {
	switch p.MountType {
	case MountLiteral:
		if len(p.Value) > 0 && len(p.Key) > 0 {
			envVar := corev1.EnvVar{
				Name:  p.Key,
				Value: p.Value,
			}
			return envVar, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
		return envVar, errors.New("missing key and/or value argument")
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
		if len(p.FromKey) > 0 && len(p.Ref) > 0 && len(p.Key) > 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
			switch p.Type {
			case SecretParameter:
				envVar = corev1.EnvVar{
					Name: p.Key,
					ValueFrom: &corev1.EnvVarSource{
						SecretKeyRef: &corev1.SecretKeySelector{
							LocalObjectReference: corev1.LocalObjectReference{
Timothee Gosselin's avatar
Timothee Gosselin committed
								Name: p.Ref,
							},
							Key: p.FromKey,
						},
					},
				}
				return envVar, nil
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
			case ConfigParameter:
				envVar = corev1.EnvVar{
					Name: p.Key,
					ValueFrom: &corev1.EnvVarSource{
						ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
							LocalObjectReference: corev1.LocalObjectReference{
Timothee Gosselin's avatar
Timothee Gosselin committed
								Name: p.Ref,
Timothee Gosselin's avatar
Timothee Gosselin committed
							},
							Key: p.FromKey,
Timothee Gosselin's avatar
Timothee Gosselin committed
						},
					},
Timothee Gosselin's avatar
Timothee Gosselin committed
				}
				return envVar, nil
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
			case ObjectFieldParameter:
				envVar = corev1.EnvVar{
					Name: p.Key,
					ValueFrom: &corev1.EnvVarSource{
						FieldRef: &corev1.ObjectFieldSelector{
Timothee Gosselin's avatar
Timothee Gosselin committed
							APIVersion: p.Ref,
Timothee Gosselin's avatar
Timothee Gosselin committed
							FieldPath:  p.FromKey,
						},
					},
				}
				return envVar, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
			}
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
		return envVar, errors.New("missing arguments")
Timothee Gosselin's avatar
Timothee Gosselin committed

	default:
		return envVar, errors.New("wrong MountType")
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
}
Timothee Gosselin's avatar
Timothee Gosselin committed
// GetPodVolume return the Volume to set in the pod.
func (p *Parameter) GetPodVolume() corev1.Volume {
Timothee Gosselin's avatar
Timothee Gosselin committed

	volumeSource := corev1.VolumeSource{}

	if p.Type == ConfigParameter {
		volumeSource = corev1.VolumeSource{
			ConfigMap: &corev1.ConfigMapVolumeSource{
				LocalObjectReference: corev1.LocalObjectReference{
					Name: p.ValueFrom.Ref,
				},
				Items: []corev1.KeyToPath{
					{
						Key:  p.Key,
						Path: p.MountPath.SubPath,
					},
				},
			},
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
	} else {
		volumeSource = corev1.VolumeSource{
			Secret: &corev1.SecretVolumeSource{
				SecretName: p.ValueFrom.Ref,
				Items: []corev1.KeyToPath{
					{
						Key:  p.Key,
						Path: p.MountPath.SubPath,
					},
				},
			},
		}
Timothee Gosselin's avatar
Timothee Gosselin committed

	volume := corev1.Volume{
		Name:         p.Key,
		VolumeSource: volumeSource,
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetVolumeMount returns the VolumeMount to set in the container.
func (p *Parameter) GetVolumeMount() corev1.VolumeMount {
	return corev1.VolumeMount{
		Name:      p.Key,
		ReadOnly:  true,
		MountPath: p.MountPath.Path,
		SubPath:   p.MountPath.SubPath,
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
// IsRand if the the parameter is randomly generated.
Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameter) IsRand() bool {
Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO if should generate random, is we add other types it will fail
Timothee Gosselin's avatar
Timothee Gosselin committed
	if p.Generate != GenerateTemplate && p.Generate != "" {
		return true
	}
	return false
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// IsMount returns  if the parameter is mounted in a secret or configmap
Timothee Gosselin's avatar
Timothee Gosselin committed
func (p *Parameter) IsMount() bool {
	if p.MountType == MountEnvFile || p.MountType == MountFile {
		return true
	}
	return false
}