/
applications.go
324 lines (240 loc) · 9.25 KB
/
applications.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
package argocd
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/argocd/argocdtypes"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const (
// APIGroup const definition.
APIGroup = "argoproj.io"
// APIVersion const definition.
APIVersion = "v1alpha1"
)
// ApplicationBuilder provides a struct for an application object from the cluster and a definition.
type ApplicationBuilder struct {
// application Definition, used to create the application object.
Definition *argocdtypes.Application
// created application object.
Object *argocdtypes.Application
// api client to interact with the cluster.
apiClient *clients.Settings
// used to store latest error message upon defining or mutating application definition.
errorMsg string
}
// PullApplication pulls existing application into ApplicationBuilder struct.
func PullApplication(apiClient *clients.Settings, name, nsname string) (*ApplicationBuilder, error) {
glog.V(100).Infof("Pulling existing Application name %s under namespace %s from cluster", name, nsname)
if apiClient == nil {
glog.V(100).Infof("The apiClient is empty")
return nil, fmt.Errorf("application 'apiClient' cannot be empty")
}
builder := ApplicationBuilder{
apiClient: apiClient,
Definition: &argocdtypes.Application{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
},
}
if name == "" {
glog.V(100).Infof("The name of the Application is empty")
return nil, fmt.Errorf("application 'name' cannot be empty")
}
if nsname == "" {
glog.V(100).Infof("The namespace of the Application is empty")
return nil, fmt.Errorf("application 'namespace' cannot be empty")
}
if !builder.Exists() {
return nil, fmt.Errorf("application object %s doesn't exist in namespace %s", name, nsname)
}
builder.Definition = builder.Object
return &builder, nil
}
// Exists checks whether the given argocd application exists.
func (builder *ApplicationBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}
glog.V(100).Infof("Checking if argocd app %s exists in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
var err error
builder.Object, err = builder.Get()
return err == nil || !k8serrors.IsNotFound(err)
}
// Get returns argocd application object if found.
func (builder *ApplicationBuilder) Get() (*argocdtypes.Application, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}
glog.V(100).Infof("Getting argocd app %s in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
unsObject, err := builder.apiClient.Resource(
GetApplicationsGVR()).Namespace(builder.Definition.Namespace).Get(
context.TODO(), builder.Definition.Name, metav1.GetOptions{})
if err != nil {
glog.V(100).Infof(
"Failed to Get Application object in namespace %s", builder.Definition.Name, builder.Definition.Namespace)
return nil, err
}
return builder.convertToStructured(unsObject)
}
// Update renovates the existing argocd application object with the argocd application definition in builder.
func (builder *ApplicationBuilder) Update(force bool) (*ApplicationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Updating the argocd application object %s in namespace %s", builder.Definition.Name,
builder.Definition.Namespace)
unstructuredApplication, err := runtime.DefaultUnstructuredConverter.ToUnstructured(builder.Definition)
if err != nil {
glog.V(100).Infof("Failed to convert structured Application to unstructured object")
return nil, err
}
_, err = builder.apiClient.Resource(
GetApplicationsGVR()).Namespace(builder.Definition.Namespace).Update(
context.TODO(), &unstructured.Unstructured{Object: unstructuredApplication}, metav1.UpdateOptions{})
if err != nil {
if force {
glog.V(100).Infof(
msg.FailToUpdateNotification("Application", builder.Definition.Name, builder.Definition.Namespace))
builder, err := builder.Delete()
if err != nil {
glog.V(100).Infof(
msg.FailToUpdateError("Application", builder.Definition.Name, builder.Definition.Namespace))
return nil, err
}
return builder.Create()
}
}
builder.Object = builder.Definition
return builder, err
}
// Delete removes the argocd application object from a cluster.
func (builder *ApplicationBuilder) Delete() (*ApplicationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Deleting the argocd application object %s from namespace: %s", builder.Definition.Name,
builder.Definition.Namespace)
if !builder.Exists() {
glog.V(100).Infof("application %s in namespace %s cannot be deleted because it does not exist",
builder.Definition.Name, builder.Definition.Namespace)
builder.Object = nil
return builder, nil
}
err := builder.apiClient.Resource(
GetApplicationsGVR()).Namespace(builder.Definition.Namespace).Delete(
context.TODO(), builder.Definition.Name, metav1.DeleteOptions{})
if err != nil {
return builder, fmt.Errorf("can not delete argocd application: %w", err)
}
builder.Object = nil
return builder, nil
}
// Create makes an argocd application in the cluster and stores the created object in a struct.
func (builder *ApplicationBuilder) Create() (*ApplicationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Creating argocd application %s in namespace: %s", builder.Definition.Name,
builder.Definition.Namespace)
var err error
if !builder.Exists() {
unstructuredApplication, err := runtime.DefaultUnstructuredConverter.ToUnstructured(builder.Definition)
if err != nil {
glog.V(100).Infof("Failed to convert structured Application to unstructured object")
return nil, err
}
unsObject, err := builder.apiClient.Resource(
GetApplicationsGVR()).Namespace(builder.Definition.Namespace).Create(
context.TODO(), &unstructured.Unstructured{Object: unstructuredApplication}, metav1.CreateOptions{})
if err != nil {
glog.V(100).Infof("Failed to create Application")
return nil, err
}
builder.Object, err = builder.convertToStructured(unsObject)
if err != nil {
return nil, err
}
}
return builder, err
}
// WithGitDetails applies git details to application definition.
func (builder *ApplicationBuilder) WithGitDetails(gitRepo, gitBranch, gitPath string) *ApplicationBuilder {
if valid, _ := builder.validate(); !valid {
return builder
}
if gitRepo == "" {
glog.V(100).Infof("The 'gitRepo' of the argocd application is empty")
builder.errorMsg = "'gitRepo' parameter is empty"
}
if gitBranch == "" {
glog.V(100).Infof("The 'gitBranch' of the argocd application is empty")
builder.errorMsg = "'gitBranch' parameter is empty"
}
if gitPath == "" {
glog.V(100).Infof("The 'gitPath' of the argocd application is empty")
builder.errorMsg = "'gitPath' parameter is empty"
}
glog.V(100).Infof(
"Adding the following git details to the argocd application: %s in namespace: %s "+
"RepoURL: %s,TargetRevision: %s, Path: %s", builder.Definition.Name, builder.Definition.Namespace,
gitRepo, gitBranch, gitPath,
)
if builder.errorMsg != "" {
return builder
}
builder.Definition.Spec.Source.RepoURL = gitRepo
builder.Definition.Spec.Source.TargetRevision = gitBranch
builder.Definition.Spec.Source.Path = gitPath
return builder
}
// GetApplicationsGVR returns applications GroupVersionResource which could be used for Clean function.
func GetApplicationsGVR() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: APIGroup, Version: APIVersion, Resource: "applications",
}
}
// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *ApplicationBuilder) validate() (bool, error) {
resourceCRD := "Application"
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
}
func (builder *ApplicationBuilder) convertToStructured(
unsObject *unstructured.Unstructured) (*argocdtypes.Application, error) {
application := &argocdtypes.Application{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unsObject.Object, application)
if err != nil {
glog.V(100).Infof(
"Failed to convert from unstructured to Application object in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
return nil, err
}
return application, err
}