-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.go
332 lines (308 loc) · 10.9 KB
/
Client.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
package api
import (
"context"
"errors"
"fmt"
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/io"
"github.com/argoproj/gitops-engine/pkg/health"
argocdtypes "github.com/argoproj/gitops-engine/pkg/sync/common"
"github.com/entigolabs/entigo-k8s-gitops/internal/app/gitops/common"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/watch"
"strconv"
"strings"
"time"
)
const (
initialRetryDelay int = 1
maxRetryDelay int = 30
)
type Client interface {
DeleteRequest(applicationName string, cascade bool, timeout int)
GetRequest(applicationName string, timeout int, refresh bool) *v1alpha1.Application
SyncRequest(applicationName string, timeout int) *v1alpha1.Application
WaitApplicationSync(applicationName string, timeout int, resourceVersion string, waitFailure bool)
}
type client struct {
ArgoCDClient apiclient.Client
}
func NewClientOrDie(flags *common.Flags) Client {
common.Logger.Println(fmt.Sprintf("ArgoCD server: %s", flags.ArgoCD.ServerAddr))
options := apiclient.ClientOptions{
ServerAddr: flags.ArgoCD.ServerAddr,
Insecure: flags.ArgoCD.Insecure,
AuthToken: flags.ArgoCD.AuthToken,
GRPCWeb: flags.ArgoCD.GRPCWeb,
}
var (
client client
err error
)
client.ArgoCDClient, err = apiclient.NewClient(&options)
if err != nil {
common.Logger.Fatal(&common.PrefixedError{Reason: err})
}
return &client
}
func (c *client) GetRequest(applicationName string, timeout int, refresh bool) *v1alpha1.Application {
common.Logger.Println(fmt.Sprintf("Getting ArgoCD app: %s, timeout: %d seconds", applicationName, timeout))
ctx, cancel := context.WithCancel(context.Background())
if timer := addFatalTimeout(timeout, cancel, "Getting application timed out"); timer != nil {
defer timer.Stop()
}
defer cancel()
retryDelay := initialRetryDelay
for ctx.Err() == nil {
connection, applicationServiceClient := c.newApplicationClient()
if applicationServiceClient != nil {
refreshString := strconv.FormatBool(refresh)
appQuery := &applicationpkg.ApplicationQuery{
Name: &applicationName,
Refresh: &refreshString,
}
app, err := applicationServiceClient.Get(ctx, appQuery)
closeConnection(connection)
if err == nil {
return app
} else {
status, _ := grpcstatus.FromError(err)
switch status.Code() {
case codes.Unavailable:
common.Logger.Println("Connection failed")
case codes.NotFound:
return nil
default:
common.Logger.Fatal(&common.PrefixedError{Reason: err})
}
}
}
sleep(&retryDelay)
}
common.Logger.Fatal(&common.PrefixedError{Reason: ctx.Err()})
return nil
}
func (c *client) SyncRequest(applicationName string, timeout int) *v1alpha1.Application {
common.Logger.Println(fmt.Sprintf("Syncing ArgoCD app: %s, timeout: %d seconds", applicationName, timeout))
ctx, cancel := context.WithCancel(context.Background())
if timer := addFatalTimeout(timeout, cancel, "Syncing application timed out"); timer != nil {
defer timer.Stop()
}
defer cancel()
syncReq := applicationpkg.ApplicationSyncRequest{
Name: &applicationName,
Prune: true,
}
syncReq.Strategy = &argoappv1.SyncStrategy{Hook: &argoappv1.SyncStrategyHook{}}
syncReq.Strategy.Hook.Force = true
return c.sendSyncReq(ctx, syncReq)
}
func (c *client) sendSyncReq(ctx context.Context, syncReq applicationpkg.ApplicationSyncRequest) *v1alpha1.Application {
retryDelay := initialRetryDelay
for ctx.Err() == nil {
connection, applicationServiceClient := c.newApplicationClient()
if applicationServiceClient != nil {
application, err := applicationServiceClient.Sync(ctx, &syncReq)
closeConnection(connection)
if err == nil {
return application
} else {
status, _ := grpcstatus.FromError(err)
switch status.Code() {
case codes.FailedPrecondition:
common.Logger.Println("Another operation in progress")
case codes.Unavailable:
common.Logger.Println("Connection failed")
default:
common.Logger.Fatal(&common.PrefixedError{Reason: err})
}
}
}
sleep(&retryDelay)
}
common.Logger.Fatal(&common.PrefixedError{Reason: ctx.Err()})
return nil
}
func (c *client) DeleteRequest(applicationName string, cascade bool, timeout int) {
common.Logger.Println(fmt.Sprintf("Deleting ArgoCD app: %s, timeout: %d seconds", applicationName, timeout))
ctx, cancel := context.WithCancel(context.Background())
if timer := addFatalTimeout(timeout, cancel, "Deleting application timed out"); timer != nil {
defer timer.Stop()
}
defer cancel()
deleteReq := applicationpkg.ApplicationDeleteRequest{
Name: &applicationName,
Cascade: &cascade,
}
retryDelay := initialRetryDelay
for ctx.Err() == nil {
connection, applicationServiceClient := c.newApplicationClient()
if applicationServiceClient != nil {
_, err := applicationServiceClient.Delete(ctx, &deleteReq)
closeConnection(connection)
if err == nil {
return
} else {
status, _ := grpcstatus.FromError(err)
switch status.Code() {
case codes.Unavailable:
common.Logger.Println("Connection failed")
default:
common.Logger.Fatal(&common.PrefixedError{Reason: err})
}
}
}
sleep(&retryDelay)
}
common.Logger.Fatal(&common.PrefixedError{Reason: ctx.Err()})
}
func (c *client) WaitApplicationSync(applicationName string, timeout int, resourceVersion string, waitFailure bool) {
ctx, cancel := context.WithCancel(context.Background())
timer := addTimeout(timeout, cancel, "Waiting for application to sync timed out", waitFailure)
if timer != nil {
defer timer.Stop()
}
defer cancel()
appEventCh := c.ArgoCDClient.WatchApplicationWithRetry(ctx, applicationName, resourceVersion)
var statusMessage string // Holds last statusMessage to avoid logging duplicates
for appEvent := range appEventCh {
if isApplicationReady(appEvent, &statusMessage) {
common.Logger.Println("Application is synced and healthy")
return
}
}
if waitFailure && ctx.Err() != context.Canceled {
common.Logger.Fatal(&common.PrefixedError{Reason: errors.New("waiting was interrupted before sync completed")})
}
}
func addFatalTimeout(timeout int, cancel context.CancelFunc, message string) *time.Timer {
return addTimeout(timeout, cancel, message, true)
}
func addTimeout(timeout int, cancel context.CancelFunc, message string, fatal bool) *time.Timer {
if timeout > 0 {
return time.AfterFunc(time.Duration(timeout)*time.Second, func() {
if fatal {
common.Logger.Fatal(&common.PrefixedError{Reason: errors.New(message)})
} else {
cancel()
common.Logger.Println(&common.Warning{Reason: errors.New(message)})
}
})
} else {
return nil
}
}
func (c *client) newApplicationClient() (io.Closer, applicationpkg.ApplicationServiceClient) {
connection, applicationServiceClient, err := c.ArgoCDClient.NewApplicationClient()
if err == nil {
return connection, applicationServiceClient
} else {
if err.Error() == "EOF" {
common.Logger.Println(&common.Warning{Reason: errors.New("failed to connect to the server")})
} else {
common.Logger.Fatal(&common.PrefixedError{Reason: err})
}
}
return nil, nil
}
func closeConnection(conn io.Closer) {
if conn != nil {
if err := conn.Close(); err != nil {
common.Logger.Println(&common.Warning{Reason: err})
}
}
}
func sleep(retryDelay *int) {
common.Logger.Println(fmt.Sprintf("Retrying in %d seconds", *retryDelay))
time.Sleep(time.Duration(*retryDelay) * time.Second)
if *retryDelay < maxRetryDelay {
*retryDelay = common.MinInt(maxRetryDelay, *retryDelay*2)
}
}
func isApplicationReady(applicationEvent *v1alpha1.ApplicationWatchEvent, lastStatusMessage *string) bool {
checkEventType(applicationEvent)
application := applicationEvent.Application
if application.Operation != nil {
return false
} else if application.Status.OperationState != nil {
status := application.Status
operationState := status.OperationState
if operationState.Phase == argocdtypes.OperationFailed {
syncFailure(operationState)
} else if operationState.FinishedAt == nil || (status.ReconciledAt == nil ||
status.ReconciledAt.Before(operationState.FinishedAt)) {
logStatus(getStatus(application, true), lastStatusMessage)
return false
}
}
healthStatus := string(application.Status.Health.Status)
syncStatus := string(application.Status.Sync.Status)
logStatus(getStatus(application, false), lastStatusMessage)
return healthStatus == string(health.HealthStatusHealthy) && syncStatus == string(argoappv1.SyncStatusCodeSynced)
}
func checkEventType(applicationEvent *v1alpha1.ApplicationWatchEvent) {
switch applicationEvent.Type {
case watch.Error:
common.Logger.Fatal(applicationEvent)
case watch.Deleted:
common.Logger.Fatal(&common.PrefixedError{Reason: errors.New("application was deleted while waiting")})
}
}
func syncFailure(operationState *argoappv1.OperationState) {
common.Logger.Println(&common.PrefixedError{Reason: errors.New("argoCD operation failed with message: " + operationState.Message)})
result := operationState.SyncResult
if result != nil && result.Resources != nil {
for i := range result.Resources {
resource := result.Resources[i]
if resource.Status == argocdtypes.ResultCodeSyncFailed {
common.Logger.Println(fmt.Sprintf("%s (%s) - %s, %s", resource.Name,
resource.Kind, resource.Status, resource.Message))
}
}
}
common.Logger.Fatal(&common.PrefixedError{Reason: errors.New("application failed to reach synced and ready status")})
}
func logStatus(message string, lastStatusMessage *string) {
if message != *lastStatusMessage {
common.Logger.Println(message)
*lastStatusMessage = message
}
}
func getStatus(application argoappv1.Application, operationInProgress bool) string {
var sb strings.Builder
if operationInProgress {
sb.WriteString("Operation in progress")
} else {
sb.WriteString("Operation finished")
}
sb.WriteString(", resource statuses: ")
sb.WriteString(getResourceStatuses(application.Status.Resources))
return sb.String()
}
func getResourceStatuses(resources []v1alpha1.ResourceStatus) string {
if resources == nil || len(resources) == 0 {
return "no resources found"
} else {
var statuses []string
for i := range resources {
resource := resources[i]
if string(resource.Status) != string(argoappv1.SyncStatusCodeSynced) {
statuses = append(statuses, getResourceStatus(resource, string(resource.Status)))
} else if resource.Health != nil && string(resource.Health.Status) != string(health.HealthStatusHealthy) {
statuses = append(statuses, getResourceStatus(resource, string(resource.Health.Status)))
}
}
if len(statuses) == 0 {
return "all ready"
} else {
return strings.Join(statuses, "; ")
}
}
}
func getResourceStatus(resource v1alpha1.ResourceStatus, status string) string {
return fmt.Sprintf("%s (%s) - %s", resource.Name, resource.Kind, status)
}