Skip to content
settings.go 2.24 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 settings
Timothee Gosselin's avatar
Timothee Gosselin committed

import (
Timothee Gosselin's avatar
Timothee Gosselin committed
	"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() Config
Timothee Gosselin's avatar
Timothee Gosselin committed
	SetDefaults()
Timothee Gosselin's avatar
Timothee Gosselin committed
	Init(c client.Client) error
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func NewSettings(s Settings) *Component {
	return &Component{
Timothee Gosselin's avatar
Timothee Gosselin committed
		CommonMeta: s.GetMeta().(*meta.ObjectMeta),
		SecretMeta: s.GetSecretMeta().(*meta.ObjectMeta),
		ConfigMeta: s.GetConfigMapMeta().(*meta.ObjectMeta),
Timothee Gosselin's avatar
Timothee Gosselin committed
		ConfigSpec: &ConfigSpec{
Timothee Gosselin's avatar
Timothee Gosselin committed
			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)
Timothee Gosselin's avatar
Timothee Gosselin committed
	params := s.GetConfig()

	for _, p := range *params.GetParameters() {
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
}