-
Notifications
You must be signed in to change notification settings - Fork 5
/
applications.go
136 lines (110 loc) · 5.34 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
package argo_client
import (
"context"
"fmt"
"strings"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/applicationset"
"github.com/rs/zerolog/log"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/pkg/errors"
"github.com/zapier/kubechecks/telemetry"
"go.opentelemetry.io/otel"
)
// GetApplicationByName takes a context and a name, then queries the Argo Application client to retrieve the Application with the specified name.
// It returns the found Application and any error encountered during the process.
// If successful, the Application client connection is closed before returning.
func (argo *ArgoClient) GetApplicationByName(ctx context.Context, name string) (*v1alpha1.Application, error) {
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "GetApplicationByName")
defer span.End()
closer, appClient := argo.GetApplicationClient()
defer closer.Close()
resp, err := appClient.Get(ctx, &application.ApplicationQuery{Name: &name})
if err != nil {
telemetry.SetError(span, err, "Argo Get Application error")
return nil, fmt.Errorf("failed to retrieve the application: %v", err)
}
return resp, nil
}
// GetKubernetesVersionByApplicationName is a method on the ArgoClient struct that takes a context and an application name as parameters,
// and returns the Kubernetes version of the destination cluster where the specified application is running.
// It returns an error if the application or cluster information cannot be retrieved.
func (argo *ArgoClient) GetKubernetesVersionByApplicationName(ctx context.Context, appName string) (string, error) {
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "GetKubernetesVersionByApplicationName")
defer span.End()
// Get application
app, err := argo.GetApplicationByName(ctx, appName)
if err != nil {
telemetry.SetError(span, err, "Argo Get Application By Name error")
return "", err
}
// Get destination cluster
// Some app specs have a Name defined, some have a Server defined, some have both, take a valid one and use it
log.Debug().Msgf("for appname %s, server dest says: %s and name dest says: %s", appName, app.Spec.Destination.Server, app.Spec.Destination.Name)
var clusterRequest *cluster.ClusterQuery
if app.Spec.Destination.Server != "" {
clusterRequest = &cluster.ClusterQuery{Server: app.Spec.Destination.Server}
} else {
clusterRequest = &cluster.ClusterQuery{Name: app.Spec.Destination.Name}
}
// Get cluster client
clusterCloser, clusterClient := argo.GetClusterClient()
defer clusterCloser.Close()
// Get cluster
clusterResponse, err := clusterClient.Get(ctx, clusterRequest)
if err != nil {
telemetry.SetError(span, err, "Argo Get Cluster error")
return "", fmt.Errorf("failed to retrieve the destination Kubernetes cluster: %v", err)
}
// Get Kubernetes version
version := clusterResponse.Info.GetKubeVersion()
// cleanup trailing "+"
version = strings.TrimSuffix(version, "+")
return version, nil
}
// GetApplicationsByLabels takes a context and a labelselector, then queries the Argo Application client to retrieve the Applications with the specified label.
// It returns the found ApplicationList and any error encountered during the process.
// If successful, the Application client connection is closed before returning.
func (argo *ArgoClient) GetApplicationsByLabels(ctx context.Context, labels string) (*v1alpha1.ApplicationList, error) {
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "GetApplicationsByLabels")
defer span.End()
closer, appClient := argo.GetApplicationClient()
defer closer.Close()
resp, err := appClient.List(ctx, &application.ApplicationQuery{Selector: &labels})
if err != nil {
telemetry.SetError(span, err, "Argo List Application error")
return nil, fmt.Errorf("failed to retrieve applications from labels: %v", err)
}
return resp, nil
}
// GetApplicationsByAppset takes a context and an appset, then queries the Argo Application client to retrieve the Applications managed by the appset
// It returns the found ApplicationList and any error encountered during the process.
func (argo *ArgoClient) GetApplicationsByAppset(ctx context.Context, name string) (*v1alpha1.ApplicationList, error) {
appsetLabelSelector := "argocd.argoproj.io/application-set-name=" + name
return argo.GetApplicationsByLabels(ctx, appsetLabelSelector)
}
func (argo *ArgoClient) GetApplications(ctx context.Context) (*v1alpha1.ApplicationList, error) {
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "GetApplications")
defer span.End()
closer, appClient := argo.GetApplicationClient()
defer closer.Close()
resp, err := appClient.List(ctx, new(application.ApplicationQuery))
if err != nil {
telemetry.SetError(span, err, "Argo List All Applications error")
return nil, errors.Wrap(err, "failed to applications")
}
return resp, nil
}
func (argo *ArgoClient) GetApplicationSets(ctx context.Context) (*v1alpha1.ApplicationSetList, error) {
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "GetApplications")
defer span.End()
closer, appClient := argo.GetApplicationSetClient()
defer closer.Close()
resp, err := appClient.List(ctx, new(applicationset.ApplicationSetListQuery))
if err != nil {
telemetry.SetError(span, err, "Argo List All Applications error")
return nil, errors.Wrap(err, "failed to applications")
}
return resp, nil
}