/* 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/application/settings/parameters" "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(co *CreateOptions, srcs *Sources, ps *parameters.Parameters) *Settings { return &Settings{ CreateOptions: co, SettingsSpec: &SettingsSpec{ Sources: srcs, Parameters: ps, }, } } 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 { return GetObjects(s) } func (s *Settings) SetDefaults() { secretSrc := Source{ Ref: s.GetCreateOptions().SecretMeta.GetName(), Type: "secret", } configSrc := Source{ Ref: s.GetCreateOptions().ConfigMeta.GetName(), Type: "configmap", } srcs := &Sources{} // Reset sources, we do not want to mount orginal resources if s.GetCreateOptions().Generate == GenEnvFile { srcs = &Sources{} } for _, p := range *s.GetParameters() { if p.MountType == parameters.MountEnvFile || s.GetCreateOptions().Generate == GenEnvFile { p.MountType = parameters.MountEnvFile // add newly created resource ref to sources if p.Type == parameters.ConfigParameter { srcs = AppendSourceIfUnique(srcs, configSrc) } else if p.Type == parameters.SecretParameter || p.Type == "" { srcs = AppendSourceIfUnique(srcs, secretSrc) } // Reset valueFrom, we do not want to mount orginal resources p.ValueFrom = parameters.ValueFrom{} } if s.GetCreateOptions().SettingsType == "configmap" { p.Type = parameters.ConfigParameter } else if s.GetCreateOptions().SettingsType == "secret" { p.Type = parameters.SecretParameter } if p.MountType == parameters.MountFile && len(p.Ref) == 0 { if p.Type == parameters.ConfigParameter { p.Ref = s.GetCreateOptions().ConfigMeta.GetName() } if p.Type == parameters.SecretParameter { p.Ref = s.GetCreateOptions().SecretMeta.GetName() } } } srcs.DeepCopyInto(s.GetSources()) } 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 }