Newer
Older
/*
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 (
Timothee Gosselin
committed
"k8s.libre.sh/interfaces"
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 {
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) }
// SetDefaults sets the defaults from the create options
secretSrc := Source{
Ref: s.GetCreateOptions().SecretMeta.GetName(),
Type: "secret",
}
configSrc := Source{
Ref: s.GetCreateOptions().ConfigMeta.GetName(),
Type: "configmap",
}
// 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
} else if p.Type == parameters.SecretParameter || p.Type == "" {
srcs = AppendSourceIfUnique(srcs, secretSrc)
}
// Reset valueFrom, we do not want to mount orginal resources
}
if s.GetCreateOptions().SettingsType == "configmap" {
} else if s.GetCreateOptions().SettingsType == "secret" {
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())
}
// Init initialise the settings.
// ObjectMeta are set from commons meta.
// Parameters are initialised from external resources set in ValueFrom and random values are generated.
func (s *Settings) Init(c client.Client, owner interfaces.Object) error {
if s.CreateOptions == nil {
s.CreateOptions = &CreateOptions{}
meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.ConfigMeta)
meta.SetObjectMeta(s.CreateOptions.CommonMeta, s.CreateOptions.SecretMeta)