-
Notifications
You must be signed in to change notification settings - Fork 34
/
secret.go
338 lines (247 loc) · 8.34 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package secret
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Builder provides struct for secret object containing connection to the cluster and the secret definitions.
type Builder struct {
// Secret definition. Used to store the secret object.
Definition *corev1.Secret
// Created secret object.
Object *corev1.Secret
// Used in functions that define or mutate secret definitions. errorMsg is processed before the secret
// object is created.
errorMsg string
// api client to interact with the cluster.
apiClient *clients.Settings
}
// AdditionalOptions additional options for Secret object.
type AdditionalOptions func(builder *Builder) (*Builder, error)
// NewBuilder creates a new instance of Builder.
func NewBuilder(apiClient *clients.Settings, name, nsname string, secretType corev1.SecretType) *Builder {
glog.V(100).Infof(
"Initializing new secret structure with the following params: %s, %s, %s",
name, nsname, string(secretType))
if apiClient == nil {
glog.V(100).Infof("secret 'apiClient' cannot be empty")
return nil
}
builder := &Builder{
apiClient: apiClient,
Definition: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
Type: secretType,
},
}
if name == "" {
glog.V(100).Infof("The name of the secret is empty")
builder.errorMsg = "secret 'name' cannot be empty"
return builder
}
if nsname == "" {
glog.V(100).Infof("The namespace of the secret is empty")
builder.errorMsg = "secret 'nsname' cannot be empty"
return builder
}
if secretType == "" {
glog.V(100).Infof("The secretType of the secret is empty")
builder.errorMsg = "secret 'secretType' cannot be empty"
return builder
}
return builder
}
// Pull loads an existing secret into Builder struct.
func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
glog.V(100).Infof("Pulling existing secret name: %s under namespace: %s", name, nsname)
if apiClient == nil {
glog.V(100).Infof("The apiClient is empty")
return nil, fmt.Errorf("secret 'apiClient' cannot be empty")
}
builder := Builder{
apiClient: apiClient,
Definition: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
},
}
if name == "" {
glog.V(100).Infof("secret name is empty")
return nil, fmt.Errorf("secret 'name' cannot be empty")
}
if nsname == "" {
glog.V(100).Infof("The namespace of the secret is empty")
return nil, fmt.Errorf("secret 'nsname' cannot be empty")
}
if !builder.Exists() {
return nil, fmt.Errorf("secret object %s does not exist in namespace %s", name, nsname)
}
builder.Definition = builder.Object
return &builder, nil
}
// Create makes a secret in the cluster and stores the created object in struct.
func (builder *Builder) Create() (*Builder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Creating the secret %s in namespace %s", builder.Definition.Name, builder.Definition.Namespace)
var err error
if !builder.Exists() {
builder.Object, err = builder.apiClient.Secrets(builder.Definition.Namespace).Create(
context.TODO(), builder.Definition, metav1.CreateOptions{})
}
return builder, err
}
// Delete removes a secret from the cluster.
func (builder *Builder) Delete() error {
if valid, err := builder.validate(); !valid {
return err
}
glog.V(100).Infof("Deleting the secret %s from namespace %s", builder.Definition.Name, builder.Definition.Namespace)
if !builder.Exists() {
glog.V(100).Infof("Secret %s does not exist in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
builder.Object = nil
return nil
}
err := builder.apiClient.Secrets(builder.Definition.Namespace).Delete(
context.TODO(), builder.Definition.Name, metav1.DeleteOptions{})
if err != nil {
return err
}
builder.Object = nil
return nil
}
// Exists checks whether the given secret exists.
func (builder *Builder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}
glog.V(100).Infof("Checking if secret %s exists in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
var err error
builder.Object, err = builder.apiClient.Secrets(builder.Definition.Namespace).Get(
context.TODO(), builder.Definition.Name, metav1.GetOptions{})
return err == nil || !k8serrors.IsNotFound(err)
}
// Update modifies the existing secret in the cluster.
func (builder *Builder) Update() (*Builder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Updating secret %s in namespace %s",
builder.Definition.Name,
builder.Definition.Namespace)
var err error
builder.Object, err = builder.apiClient.Secrets(builder.Definition.Namespace).Update(
context.TODO(), builder.Definition, metav1.UpdateOptions{})
return builder, err
}
// WithData defines the data placed in the secret.
func (builder *Builder) WithData(data map[string][]byte) *Builder {
if valid, _ := builder.validate(); !valid {
return builder
}
glog.V(100).Infof(
"Defining secret %s in namespace %s with this data: %s",
builder.Definition.Name, builder.Definition.Namespace, data)
if len(data) == 0 {
glog.V(100).Infof("The data of the secret is empty")
builder.errorMsg = "'data' cannot be empty"
}
if builder.errorMsg != "" {
return builder
}
builder.Definition.Data = data
return builder
}
// WithStringData defines the stringData placed in the secret.
func (builder *Builder) WithStringData(data map[string]string) *Builder {
if valid, _ := builder.validate(); !valid {
return builder
}
glog.V(100).Infof(
"Defining secret %s in namespace %s with this stringData: %s",
builder.Definition.Name, builder.Definition.Namespace, data)
if len(data) == 0 {
glog.V(100).Infof("The stringData of the secret is empty")
builder.errorMsg = "'stringData' cannot be empty"
}
if builder.errorMsg != "" {
return builder
}
builder.Definition.StringData = data
return builder
}
// WithAnnotations defines the annotations in the secret.
func (builder *Builder) WithAnnotations(annotations map[string]string) *Builder {
glog.V(100).Infof("Adding annotations %v to the secret %s in namespace %s",
annotations, builder.Definition.Name, builder.Definition.Namespace)
if valid, _ := builder.validate(); !valid {
return builder
}
if len(annotations) == 0 {
glog.V(100).Infof("'annotations' argument cannot be empty")
builder.errorMsg = "'annotations' argument cannot be empty"
return builder
}
for key := range annotations {
if key == "" {
glog.V(100).Infof("The 'annotations' key cannot be empty")
builder.errorMsg = "can not apply an annotations with an empty key"
return builder
}
}
builder.Definition.Annotations = annotations
return builder
}
// WithOptions creates secret with generic mutation options.
func (builder *Builder) WithOptions(options ...AdditionalOptions) *Builder {
if valid, _ := builder.validate(); !valid {
return builder
}
glog.V(100).Infof("Setting secret additional options")
for _, option := range options {
if option != nil {
builder, err := option(builder)
if err != nil {
glog.V(100).Infof("Error occurred in mutation function")
builder.errorMsg = err.Error()
return builder
}
}
}
return builder
}
// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *Builder) validate() (bool, error) {
resourceCRD := "Secret"
if builder == nil {
glog.V(100).Infof("The %s builder is uninitialized", resourceCRD)
return false, fmt.Errorf("error: received nil %s builder", resourceCRD)
}
if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)
return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}
if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}
if builder.errorMsg != "" {
glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg)
return false, fmt.Errorf(builder.errorMsg)
}
return true, nil
}