Skip to content
settings.go 3 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 instance

import (
	meta "k8s.libre.sh/meta"
	"k8s.libre.sh/objects/configmap"
Timothee Gosselin's avatar
Timothee Gosselin committed
	secret "k8s.libre.sh/objects/secret"
	parameters "k8s.libre.sh/parameters"
)

type Dependency interface {
	GetParameters() parameters.Interface
	//	GetSecretParameters() *parameters.Settings
	//	GetConfigParameters() *parameters.Settings
	SetDefaults(i Instance)
}

Timothee Gosselin's avatar
Timothee Gosselin committed
type Settings interface {
	GetMeta() Meta
	GetSecretMeta() Meta
	GetConfigMapMeta() Meta
	GetParameters() parameters.Interface
	//	GetSecretParameters() *parameters.Settings
	//	GetConfigParameters() *parameters.Settings
	//	GetSecret() secret.Mutate
	//	GetConfigMap() configmap.Mutate
Timothee Gosselin's avatar
Timothee Gosselin committed
	SetDefaults(i Instance)
}

func InitParametersFromSettings(s Settings, p parameters.Interface) {

	secretRefs := p.GetSecretRefs()
	configRefs := p.GetConfigRefs()

	for _, param := range *p.GetParameters() {
		if len(param.MountType) == 0 {
			param.MountType = parameters.MountFrom

			if len(param.ValueFrom.Ref) == 0 {
				if param.Type == parameters.SecretParameter {
					secretRefs = append(secretRefs, s.GetMeta().GetName())
				}
				configRefs = append(configRefs, s.GetMeta().GetName())
			}
		}

		if len(param.Type) == 0 {
			param.Type = parameters.ConfigParameter
		}
	}

	p.SetConfigRefs(Unique(configRefs))
	p.SetSecretRefs(Unique(secretRefs))
}

func Unique(stringSlice []string) []string {
	keys := make(map[string]bool)
	list := []string{}
	for _, entry := range stringSlice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func GetObjects(s Settings) map[int]Object {

	cm := &configmap.ConfigMap{
		ObjectMeta: &meta.ObjectMeta{},
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	secret := &secret.Secret{
		ObjectMeta: &meta.ObjectMeta{},
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	objs := make(map[int]Object, 2)
Timothee Gosselin's avatar
Timothee Gosselin committed

	SetObjectMeta(s.GetMeta(), cm.ObjectMeta)
	SetObjectMeta(s.GetMeta(), secret.ObjectMeta)

	// TODO REPLACE BY GET SECRET PARAMETERS & GET CONFIG PARAMETERS
Timothee Gosselin's avatar
Timothee Gosselin committed
	params := s.GetParameters()

	for _, p := range *params.GetParameters() {
		// TODO check not mountLiteral ??
Timothee Gosselin's avatar
Timothee Gosselin committed
		if p.Type == parameters.SecretParameter && len(p.Value) > 0 {
			secret.Parameters = append(secret.Parameters, p)
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
		if p.Type == parameters.ConfigParameter && len(p.Value) > 0 {
			cm.Parameters = append(cm.Parameters, p)
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO IMPROVE
	if len(cm.Parameters) > 0 {
		//	cm.Parameters = cmParameters
Timothee Gosselin's avatar
Timothee Gosselin committed
		objs[0] = cm
	}

	if len(secret.Parameters) > 0 {
		//	secret.Parameters = secretParameters
Timothee Gosselin's avatar
Timothee Gosselin committed
		if objs[0] == nil {
			objs[0] = secret

		} else {
			objs[1] = secret
		}
	}

	return objs
}