forked from openshift-kni/eco-goinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
231 lines (174 loc) · 5.75 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
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"
v1 "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 *v1.Secret
// Created secret object.
Object *v1.Secret
// Used in functions that define or mutate secret definitions. errorMsg is processed before the secret
// object is created.
errorMsg string
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 v1.SecretType) *Builder {
glog.V(100).Infof(
"Initializing new secret structure with the following params: %s, %s, %s",
name, nsname, string(secretType))
builder := Builder{
apiClient: apiClient,
Definition: &v1.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"
}
if nsname == "" {
glog.V(100).Infof("The namespace of the secret is empty")
builder.errorMsg = "secret 'nsname' cannot be empty"
}
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)
builder := Builder{
apiClient: apiClient,
Definition: &v1.Secret{
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: nsname,
},
},
}
if name == "" {
builder.errorMsg = "secret 'name' cannot be empty"
}
if nsname == "" {
builder.errorMsg = "secret 'namespace' cannot be empty"
}
if !builder.Exists() {
return nil, fmt.Errorf("secret object %s doesn't 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() {
return nil
}
err := builder.apiClient.Secrets(builder.Definition.Namespace).Delete(
context.TODO(), builder.Object.Name, metaV1.DeleteOptions{})
if err != nil {
return err
}
builder.Object = nil
return err
}
// 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.Background(), builder.Definition.Name, metaV1.GetOptions{})
return err == nil || !k8serrors.IsNotFound(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(
"Creating 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
}
// 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)
builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD)
}
if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)
builder.errorMsg = fmt.Sprintf("%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
}