Skip to content
utils.go 3.04 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 parameters
Timothee Gosselin's avatar
Timothee Gosselin committed

import (
Timothee Gosselin's avatar
Timothee Gosselin committed
	"context"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"reflect"
Timothee Gosselin's avatar
Timothee Gosselin committed

	corev1 "k8s.io/api/core/v1"

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

func Marshal(i interface{}) (Parameters, error) {
	parameters := Parameters{}

	t := reflect.TypeOf(i)

	ifv := reflect.ValueOf(i)

	for ii := 0; ii < t.NumField(); ii++ {
		f := t.Field(ii)

		fv := ifv.Field(ii)

		parameter, ok := fv.Interface().(Parameter)
Timothee Gosselin's avatar
Timothee Gosselin committed
		// TODO Return error
Timothee Gosselin's avatar
Timothee Gosselin committed
		if !ok {
			//	return nil, ok
		}

		env, ok := f.Tag.Lookup("env")
		if !ok {
			//	return nil, ok
		}

		if len(parameter.Key) == 0 {
			parameter.Key = env
		}

		//	if len(parameter.MountType) == 0 {
		//		parameter.MountType = MountEnvFile
		//	}
Timothee Gosselin's avatar
Timothee Gosselin committed

		parameters = append(parameters, &parameter)
	}

	return parameters, nil
}

// Sort parameters by keys
Timothee Gosselin's avatar
Timothee Gosselin committed
func ParametersByKey(p *Parameters) map[string]*Parameter {
	paramsByKey := map[string]*Parameter{}

Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, param := range *p {
		/* 		if len(param.FromKey) > 0 {
			paramsByKey[param.FromKey] = param

		} else { */
		paramsByKey[param.Key] = param
		//	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
	return paramsByKey
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// Transform parameters to key:value
Timothee Gosselin's avatar
Timothee Gosselin committed
func KeyPairValues(p *Parameters) map[string]string {
	data := make(map[string]string)

	for _, param := range *p {
		data[param.Key] = param.Value
	}

	return data
}
Timothee Gosselin's avatar
Timothee Gosselin committed

func InitParametersFromResources() {

}

// TODO SPLIT IN GETPARAMETERSFROMSOURCES AND MERGEPARAMETERSBYKEYS
Timothee Gosselin's avatar
Timothee Gosselin committed
func GetAndMergeParameters(
	params Parameters,
	paramsByKey map[string]*Parameter,
	c client.Client,
	obj interfaces.Object) error {

	var pType ParameterType
	data := map[string]string{}

	objectKey, _ := client.ObjectKeyFromObject(obj)

	err := c.Get(context.Background(), objectKey, obj)

	if err != nil {
		return err
	}

	switch obj.GetObjectKind().GroupVersionKind().Kind {
	case "Secret":
Timothee Gosselin's avatar
Timothee Gosselin committed
		pType = SecretParameter
		for k, v := range obj.(*corev1.Secret).Data {
			data[k] = string(v)
		}
	case "ConfigMap":
Timothee Gosselin's avatar
Timothee Gosselin committed
		pType = ConfigParameter
		for k, v := range obj.(*corev1.ConfigMap).Data {
			data[k] = v
		}
	default:
		return errors.New("object kind should be ConfigMap or Secret")
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	if len(params) > 0 {
		for _, pp := range params {
			paramsByKey[pp.Key].Value = string(data[pp.FromKey])
			paramsByKey[pp.Key].Generate = ""
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
	} else {
		for kk, vv := range data {
			if paramsByKey[kk] == nil {
				paramsByKey[kk] = &Parameter{}

			}
			paramsByKey[kk].Value = vv
			paramsByKey[kk].Key = kk
			//		paramsByKey[kk].MountType = MountEnvFile
			// TODO TO FIX This should be checked
Timothee Gosselin's avatar
Timothee Gosselin committed
			paramsByKey[kk].Type = pType
		}
	}

	return nil
}