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"
"k8s.libre.sh/objects/configmap"
"k8s.libre.sh/objects/secret"
"sigs.k8s.io/controller-runtime/pkg/client"
type Component interface {
GetMeta() meta.Instance
GetConfig() Config
SetDefaults()
Config
GetCreateOptions() *CreateOptions
GetObjects() map[int]objects.Object
}
func TemplateValues(s Component) map[string]interface{} {
keyPairValues := parameters.KeyPairValues(s.GetConfig().GetParameters())
values[s.GetMeta().GetComponent()] = keyPairValues
func GetObjects(s Component) map[int]objects.Object {
cm := &configmap.ConfigMap{
ObjectMeta: s.GetCreateOptions().ConfigMeta,
Data: s.GetParameters().GetConfigData(),
secret := &secret.Secret{
ObjectMeta: s.GetCreateOptions().SecretMeta,
Data: s.GetParameters().GetSecretData(),
genConfigParams := []string{}
genSecretParams := []string{}
for _, p := range *s.GetConfig().GetParameters() {
if p.IsMount() && len(p.Value) > 0 && p.IsRand() {
switch p.Type {
case parameters.SecretParameter, "":
genSecretParams = append(genSecretParams, p.Key)
case parameters.ConfigParameter:
genConfigParams = append(genConfigParams, p.Key)
}
}
}
// 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, 1)
}
secret.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genSecretParams, ",")
cm.ObjectMeta.Annotations = make(map[string]string, 1)
}
cm.ObjectMeta.Annotations["settings.k8s.libre.sh/generate"] = strings.Join(genConfigParams, ",")
if len(secret.Data) > 0 {
objs[0] = secret
}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
func GetGenAnnotionForType(ps *parameters.Parameters, pType parameters.ParameterType) map[string]string {
list := []string{}
annot := make(map[string]string)
for _, p := range *ps {
if p.IsMount() && len(p.Value) > 0 && p.IsRand() && p.Type == pType {
list = append(list, p.Key)
}
}
if len(list) > 0 {
annot["settings.k8s.libre.sh/generate"] = strings.Join(list, ",")
}
return annot
}
func GetConfigGenAnnotion(ps *parameters.Parameters) map[string]string {
list := []string{}
annot := make(map[string]string)
for _, p := range *ps {
if p.IsMount() && len(p.Value) > 0 && p.IsRand() && p.Type == parameters.ConfigParameter {
list = append(list, p.Key)
}
}
if len(list) > 0 {
annot["settings.k8s.libre.sh/generate"] = strings.Join(list, ",")
}
return annot
}
func GetSecretGenAnnotion(ps *parameters.Parameters) map[string]string {
list := []string{}
annot := make(map[string]string)
for _, p := range *ps {
if p.IsMount() && len(p.Value) > 0 && p.IsRand() && (p.Type == parameters.SecretParameter || p.Type == "") {
list = append(list, p.Key)
}
}
if len(list) > 0 {
annot["settings.k8s.libre.sh/generate"] = strings.Join(list, ",")
}
return annot
}
func Init(s Component, c client.Client, owner interfaces.Object) error {
opts := s.GetCreateOptions()
if opts == nil {
opts = &CreateOptions{}
meta.SetObjectMeta(opts.CommonMeta, opts.ConfigMeta)
meta.SetObjectMeta(opts.CommonMeta, opts.SecretMeta)
if err != nil {
return err
}
return nil
}
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// InitParametersValueFrom intialise the parameters with values provided in external resources in the same namespace
// All parameters values are filled from those resources and new parameters are created when needed
// Only Secrets and Configmaps are supported
func InitParametersValueFrom(s Component, c client.Client, owner interfaces.Object) error {
err := s.GetParameters().InitValueFrom(c, owner)
if err != nil {
return err
}
ps, err := ParametersFromSources(s.GetSources(), c, owner)
s.GetParameters().Merge(ps)
return nil
}
func ParametersFromSources(srcs *Sources, c client.Client, owner interfaces.Object) (*parameters.Parameters, error) {
var obj interfaces.Object
ps := ¶meters.Parameters{}
for _, src := range *srcs {
switch src.Type {
case "configmap":
obj = &corev1.ConfigMap{}
case "secret":
obj = &corev1.Secret{}
default:
return ps, nil
}
obj.SetName(src.Ref)
obj.SetNamespace(owner.GetNamespace())
}
data, err := parameters.GetDataFromResource(c, obj, owner)
if err != nil {
return ps, err
}
if len(data) > 0 {
for k, v := range data {
*ps = append(*ps, ¶meters.Parameter{
Value: v,
Key: k,
})
}
}
return ps, nil
}