Skip to content
utils.go 2.21 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 settings

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

func OrderByResourceRef(s *ConfigSpec) (secrets, configMaps map[string]parameters.Parameters) {
	secrets = make(map[string]parameters.Parameters)
	configMaps = make(map[string]parameters.Parameters)

	for _, source := range *s.Sources {
Timothee Gosselin's avatar
Timothee Gosselin committed
		if source.Type == "secret" {
			secrets[source.Ref] = parameters.Parameters{}
		} else if source.Type == "configmap" {
			configMaps[source.Ref] = parameters.Parameters{}
		}
	}

	// Then we get the sources that are provided for each parameters and will need a transformation
	for _, p := range *s.Parameters {
		// TOFIX only refType ? by default secret ?
		if len(p.ValueFrom.Ref) > 0 && (p.Type == parameters.SecretParameter || p.RefType == "secret") {
			// TODO add a warning, if secret given a source and in valuefrom only keys in valuefrom will be fetched
Timothee Gosselin's avatar
Timothee Gosselin committed
			secrets[p.ValueFrom.Ref] = append(secrets[p.ValueFrom.Ref], p)
		}
		if len(p.ValueFrom.Ref) > 0 && (p.Type == parameters.ConfigParameter || p.RefType == "configmap") {
Timothee Gosselin's avatar
Timothee Gosselin committed
			configMaps[p.ValueFrom.Ref] = append(configMaps[p.ValueFrom.Ref], p)
		}
	}

	return secrets, configMaps
}

/* func AppendSourceIfUnique(sources []Source, source Source) []Source {
	if len(sources) == 0 {
		return append(sources, source)
	}
	for _, ele := range sources {
		if ele == source {
			return sources
		}
	}
	return append(sources, source)
} */

func AppendSourceIfUnique(sources *Sources, source Source) *Sources {
	srcs := Sources{}

	if sources != nil {
		/* 		srcs = append(srcs, source)
		   		return &srcs */

		for _, ele := range *sources {
			if ele == source {
				return sources
			}
		}
	}

	srcs = append(srcs, source)

	return &srcs