Skip to content
settings.go 2.44 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.
*/

Timothee Gosselin's avatar
Timothee Gosselin committed
package application
Timothee Gosselin's avatar
Timothee Gosselin committed

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

	"sigs.k8s.io/controller-runtime/pkg/client"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

type Settings interface {
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetMeta() meta.Instance
	GetSecretMeta() meta.Instance
	GetConfigMapMeta() meta.Instance
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetConfig() settings.Config
Timothee Gosselin's avatar
Timothee Gosselin committed
	SetDefaults()
Timothee Gosselin's avatar
Timothee Gosselin committed
	Init(c client.Client) error
}

func NewSettings(s Settings) *components.Settings {
	return &components.Settings{
		CommonMeta: s.GetMeta().(*meta.ObjectMeta),
		SecretMeta: s.GetSecretMeta().(*meta.ObjectMeta),
		ConfigMeta: s.GetConfigMapMeta().(*meta.ObjectMeta),
		ConfigSpec: &settings.ConfigSpec{
			ConfigRefs: s.GetConfig().GetConfigRefs(),
			SecretRefs: s.GetConfig().GetSecretRefs(),
			Parameters: s.GetConfig().GetParameters(),
		},
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
func GetObjects(s Settings) map[int]objects.Object {
Timothee Gosselin's avatar
Timothee Gosselin committed

	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

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

Timothee Gosselin's avatar
Timothee Gosselin committed
	meta.SetObjectMeta(s.GetConfigMapMeta(), cm.ObjectMeta)
	meta.SetObjectMeta(s.GetSecretMeta(), secret.ObjectMeta)

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

	for _, p := range *params.GetParameters() {
		// TODO check not mountLiteral ??
Timothee Gosselin's avatar
Timothee Gosselin committed
		if p.MountType == parameters.MountEnvFile &&
			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.MountType == parameters.MountEnvFile &&
			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
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO IMPROVE
	if len(cm.Parameters) > 0 {
Timothee Gosselin's avatar
Timothee Gosselin committed
		objs[0] = cm
	}

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

		} else {
			objs[1] = secret
		}
	}

	return objs
}