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

Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.gnu.org/licenses/agpl-3.0.html

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 application

import (
Timothee Gosselin's avatar
Timothee Gosselin committed
	settings "k8s.libre.sh/application/settings"
	"k8s.libre.sh/application/settings/parameters"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/interfaces"
	"k8s.libre.sh/meta"
	"k8s.libre.sh/status"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

type Instance interface {
	meta.Instance
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetComponents() map[string]Component
	GetJobs() map[string]Component
	GetOwner() status.ObjectWithStatus
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetSettings() map[string]settings.Component
	GetApplicationStatus() *status.ApplicationStatus
Timothee Gosselin's avatar
Timothee Gosselin committed
	SetDefaults()
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func GetTemplateValues(i Instance, setts map[string]settings.Component) map[string]interface{} {
Timothee Gosselin's avatar
Timothee Gosselin committed
	values := make(map[string]interface{})
	cptsVals := make(map[string]interface{})
	settsVals := make(map[string]interface{})
Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, cpt := range i.GetComponents() {
Timothee Gosselin's avatar
Timothee Gosselin committed
		// TODO create templateValues for Component interface
		cptsVals[cpt.GetMeta().GetComponent()] = cpt.(interface{})
Timothee Gosselin's avatar
Timothee Gosselin committed
		// TODO create templateValues for Settings interface
		settsVals[sett.GetMeta().GetComponent()] = parameters.KeyPairValues(sett.GetConfig().GetParameters())
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	values["components"] = cptsVals
	values["settings"] = settsVals
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return values
Timothee Gosselin's avatar
Timothee Gosselin committed
}

func InitSettings(i Instance, r interfaces.Reconcile) error {

	appStatus := i.GetApplicationStatus()
	setts := i.GetSettings()

	// First we init external values and random values
	for name, sett := range setts {
		if len(appStatus.Settings[name].Resources.Objects) > 0 {
			for _, o := range appStatus.Settings[name].Resources.Objects {
				src := settings.Source{
					Ref: o.Name,
Timothee Gosselin's avatar
Timothee Gosselin committed
					// & TO FIX
					Type: strings.ToLower(o.Kind),
				}
				*sett.GetConfig().GetSources() = append(*sett.GetConfig().GetSources(), src)
			}
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		err := settings.Init(sett, r.GetClient(), i.GetOwner())

		if err != nil {
			return err
		}
	}

	// Transform instance to template values used to init settings
	values := GetTemplateValues(i, i.GetSettings())

Timothee Gosselin's avatar
Timothee Gosselin committed
	// Init the values from templates & set defaults
	for _, sett := range setts {
		err := sett.GetConfig().GetParameters().InitTemplateValues(values)
		if err != nil {
			return err
		}

		sett.SetDefaults()
// Initiatilize & Defaults
func Init(i Instance, r interfaces.Reconcile) error {
	i.SetDefaults()
	err := InitSettings(i, r)
	if err != nil {
		return err
Timothee Gosselin's avatar
Timothee Gosselin committed

	// Init Components
	cpts := i.GetComponents()
	InitComponentsFromSettings(cpts, i.GetSettings())
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return nil
}