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

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

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 (
Timothee Gosselin's avatar
Timothee Gosselin committed
	corev1 "k8s.io/api/core/v1"
)

func (p *Parameter) GetConfigData() map[string]string {
	if len(p.Value) > 0 && len(p.Key) > 0 {

		data := make(map[string]string)
		data[p.Key] = p.Value
		return data
	}
	return nil
}

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

	case MountFrom:
		if len(p.FromKey) > 0 && len(p.Source) > 0 && len(p.Key) > 0 {
			if p.Type == "Secret" {
				envVar = corev1.EnvVar{
					Name: p.Key,
					ValueFrom: &corev1.EnvVarSource{
						SecretKeyRef: &corev1.SecretKeySelector{
							LocalObjectReference: corev1.LocalObjectReference{
								Name: p.Source,
							},
							Key: p.FromKey,
						},
					},
				}
				return envVar, nil
			}
Timothee Gosselin's avatar
Timothee Gosselin committed
			envVar = corev1.EnvVar{
				Name: p.Key,
				ValueFrom: &corev1.EnvVarSource{
					ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
Timothee Gosselin's avatar
Timothee Gosselin committed
						LocalObjectReference: corev1.LocalObjectReference{
							Name: p.Source,
						},
						Key: p.FromKey,
					},
				},
			}
			return envVar, nil
		}
	default:
		return envVar, errors.New("wrong MountType")
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	return envVar, errors.New("missing arguments")
Timothee Gosselin's avatar
Timothee Gosselin committed
}

func (p *Parameter) GetEnvFrom() (envFrom corev1.EnvFromSource, err error) {
	if len(p.Source) > 0 && p.MountType == MountFrom {
		if p.Type == ConfigParameter {
Timothee Gosselin's avatar
Timothee Gosselin committed
			envFrom = corev1.EnvFromSource{
				ConfigMapRef: &corev1.ConfigMapEnvSource{
					LocalObjectReference: corev1.LocalObjectReference{
						Name: p.Source,
					},
				},
			}
			return envFrom, nil
		}
		envFrom = corev1.EnvFromSource{
			SecretRef: &corev1.SecretEnvSource{
				LocalObjectReference: corev1.LocalObjectReference{
					Name: p.Source,
				},
			},
		}
		return envFrom, nil
	}
	return envFrom, errors.New("missing source argument")
Timothee Gosselin's avatar
Timothee Gosselin committed
}