/* 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 ( "sigs.k8s.io/controller-runtime/pkg/client" "k8s.libre.sh/interfaces" "k8s.libre.sh/meta" "k8s.libre.sh/objects" ) const ( // Generate settings as env variables in a configMap or Secret GenEnvFile SettingsGenerate = "envFile" ) const ( // Generate settings as env variables in a configMap or Secret SecretSettings SettingsType = "secret" ) type SettingsGenerate string type SettingsType string // +kubebuilder:object:generate=true type Settings struct { CreateOptions *CreateOptions `json:"createOptions,omitempty"` *SettingsSpec `json:",inline"` } // +kubebuilder:object:generate=true type CreateOptions struct { CommonMeta *meta.ObjectMeta `json:"commonMeta,omitempty"` SecretMeta *meta.ObjectMeta `json:"secretMeta,omitempty"` ConfigMeta *meta.ObjectMeta `json:"configMeta,omitempty"` Generate SettingsGenerate `json:"generate,omitempty"` SettingsType SettingsType `json:"type,omitempty"` } func NewSettings(s Component) *Settings { return &Settings{ CreateOptions: s.GetCreateOptions(), SettingsSpec: &SettingsSpec{ Sources: s.GetConfig().GetSources(), Parameters: s.GetConfig().GetParameters(), }, } } func (opts *CreateOptions) Init() { if opts.CommonMeta == nil { opts.CommonMeta = &meta.ObjectMeta{} } if opts.CommonMeta.Labels == nil { opts.CommonMeta.Labels = make(map[string]string) } if opts.SecretMeta == nil { opts.SecretMeta = &meta.ObjectMeta{} } if opts.SecretMeta.Labels == nil { opts.SecretMeta.Labels = make(map[string]string) } if opts.ConfigMeta == nil { opts.ConfigMeta = &meta.ObjectMeta{} } if opts.ConfigMeta.Labels == nil { opts.SecretMeta.Labels = make(map[string]string) } } func (s *Settings) GetCreateOptions() *CreateOptions { return s.CreateOptions } func (s *Settings) GetConfig() Config { return s.SettingsSpec } func (s *Settings) GetMeta() meta.Instance { return s.CreateOptions.CommonMeta } func (s *Settings) GetObjects() map[int]objects.Object { /* cm := &configmap.ConfigMap{ ObjectMeta: s.CreateOptions.ConfigMeta, } secret := &secret.Secret{ ObjectMeta: s.CreateOptions.SecretMeta, } objs := make(map[int]objects.Object, 2) genConfigParams := []string{} genSecretParams := []string{} for _, p := range *s.SettingsSpec.Parameters { // TODO TO FIX if (p.MountType == parameters.MountEnvFile || p.MountType == parameters.MountFile) && // TODO TO FIX (p.Type == parameters.SecretParameter || p.Type == "") && len(p.Value) > 0 { if p.Generate != parameters.GenerateTemplate && p.Generate != "" { genSecretParams = append(genSecretParams, p.Key) } secret.Parameters = append(secret.Parameters, p) } if (p.MountType == parameters.MountEnvFile || p.MountType == parameters.MountFile) && p.Type == parameters.ConfigParameter && len(p.Value) > 0 { if p.Generate != parameters.GenerateTemplate && p.Generate != "" { genConfigParams = append(genConfigParams, p.Key) } cm.Parameters = append(cm.Parameters, p) } } // Sort annotations as it is sorted in kubernetes and will have a diff otherwise sort.Strings(genConfigParams) sort.Strings(genSecretParams) // TODO TO FIX remove annotation if secret is not generated anymore if len(genSecretParams) > 0 { if len(secret.ObjectMeta.Annotations) == 0 { secret.ObjectMeta.Annotations = make(map[string]string, len(secret.ObjectMeta.Annotations)) } secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genSecretParams, ",") } if len(genConfigParams) > 0 { if len(secret.ObjectMeta.Annotations) == 0 { cm.ObjectMeta.Annotations = make(map[string]string, len(genConfigParams)) } secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",") } if len(secret.Parameters) > 0 { objs[0] = secret } if len(cm.Parameters) > 0 { objs[1] = cm } */ return GetObjects(s) // return objs } func (s *Settings) SetDefaults() { } // func (s *Settings) Init(r interfaces.Reconcile, owner interfaces.Object) error { func (s *Settings) Init(c client.Client, owner interfaces.Object) error { if s.CreateOptions == nil { s.CreateOptions = &CreateOptions{} } s.CreateOptions.Init() meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.ConfigMeta) meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.SecretMeta) err := InitParametersValueFrom(s, c, owner) if err != nil { return err } s.InitRandValues() if err != nil { return err } return nil }