/* 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. */ package instance import ( meta "k8s.libre.sh/meta" "k8s.libre.sh/objects/configmap" secret "k8s.libre.sh/objects/secret" parameters "k8s.libre.sh/settings" ) type Dependency interface { GetParameters() parameters.Interface // GetSecretParameters() *parameters.Settings // GetConfigParameters() *parameters.Settings SetDefaults(i Instance) } type Settings interface { GetMeta() Meta GetSecretMeta() Meta GetConfigMapMeta() Meta GetParameters() parameters.Interface // GetSecretParameters() *parameters.Settings // GetConfigParameters() *parameters.Settings // GetSecret() secret.Mutate // GetConfigMap() configmap.Mutate SetDefaults(i Instance) } func InitParametersFromSettings(s Settings, p parameters.Interface) { secretRefs := p.GetSecretRefs() configRefs := p.GetConfigRefs() for _, param := range *p.GetParameters() { if len(param.MountType) == 0 { param.MountType = parameters.MountFrom if len(param.ValueFrom.Ref) == 0 { if param.Type == parameters.SecretParameter { secretRefs = append(secretRefs, s.GetMeta().GetName()) } configRefs = append(configRefs, s.GetMeta().GetName()) } } if len(param.Type) == 0 { param.Type = parameters.ConfigParameter } } p.SetConfigRefs(Unique(configRefs)) p.SetSecretRefs(Unique(secretRefs)) } func Unique(stringSlice []string) []string { keys := make(map[string]bool) list := []string{} for _, entry := range stringSlice { if _, value := keys[entry]; !value { keys[entry] = true list = append(list, entry) } } return list } func GetObjects(s Settings) map[int]Object { cm := &configmap.ConfigMap{ ObjectMeta: &meta.ObjectMeta{}, } secret := &secret.Secret{ ObjectMeta: &meta.ObjectMeta{}, } objs := make(map[int]Object, 2) SetObjectMeta(s.GetMeta(), cm.ObjectMeta) SetObjectMeta(s.GetMeta(), secret.ObjectMeta) // TODO REPLACE BY GET SECRET PARAMETERS & GET CONFIG PARAMETERS params := s.GetParameters() for _, p := range *params.GetParameters() { // TODO check not mountLiteral ?? if p.Type == parameters.SecretParameter && len(p.Value) > 0 { secret.Parameters = append(secret.Parameters, p) } if p.Type == parameters.ConfigParameter && len(p.Value) > 0 { cm.Parameters = append(cm.Parameters, p) } } // TODO IMPROVE if len(cm.Parameters) > 0 { // cm.Parameters = cmParameters objs[0] = cm } if len(secret.Parameters) > 0 { // secret.Parameters = secretParameters if objs[0] == nil { objs[0] = secret } else { objs[1] = secret } } return objs }