-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargocd.go
191 lines (159 loc) · 5.93 KB
/
argocd.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
package argocd
import (
"context"
"fmt"
"time"
"github.com/projectsyn/lieutenant-api/pkg/api"
"go.uber.org/multierr"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
var (
argoLabels = map[string]string{
"app.kubernetes.io/part-of": "argocd",
"argocd.argoproj.io/instance": "argocd",
"steward.syn.tools/bootstrap": "true",
}
argoAnnotations = map[string]string{
"argocd.argoproj.io/sync-options": "Prune=false",
}
argoSSHSecretName = "argo-ssh-key"
argoSSHPublicKey = "sshPublicKey"
argoSSHPrivateKey = "sshPrivateKey"
argoSSHConfigMapName = "argocd-ssh-known-hosts-cm"
argoTLSConfigMapName = "argocd-tls-certs-cm"
argoRbacConfigMapName = "argocd-rbac-cm"
argoConfigMapName = "argocd-cm"
argoSecretName = "argocd-secret"
argoClusterSecretName = "syn-argocd-cluster"
argoRbacName = "argocd-application-controller"
argoRootAppName = "root"
argoProjectName = "syn"
argoAppsPath = "manifests/apps/"
)
// Apply reconciles the Argo CD deployments
func Apply(ctx context.Context, config *rest.Config, namespace, operatorNamespace, argoImage, redisArgoImage string, apiClient *api.Client, cluster *api.Cluster) error {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return err
}
gvr := schema.GroupVersionResource{
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "argocds",
}
argos, err := dynamicClient.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}
if err == nil && len(argos.Items) > 0 {
// An ArgoCD custom resource exists in our namespace
err = fixArgoOperatorDeadlock(ctx, clientset, config, namespace, operatorNamespace)
if err != nil {
return fmt.Errorf("could not fix argocd operator deadlock: %w", err)
}
return nil
}
deployments, err := clientset.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/part-of=argocd",
})
if err != nil {
return fmt.Errorf("Could not list ArgoCD deployments: %w", err)
}
expectedDeploymentCount := 3
foundDeploymentCount := len(deployments.Items)
statefulsets, err := clientset.AppsV1().StatefulSets(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/part-of=argocd",
})
if err != nil {
return fmt.Errorf("Could not list ArgoCD statefulsets: %w", err)
}
expectedStatefulSetCount := 1
foundStatefulSetCount := len(statefulsets.Items)
if foundDeploymentCount == expectedDeploymentCount && foundStatefulSetCount == expectedStatefulSetCount {
// Found expected deployments, found expected statefulsets, skip
return nil
}
klog.Infof("Found %d of expected %d deployments, found %d of expected %d statefulsets, bootstrapping now", foundDeploymentCount, expectedDeploymentCount, foundStatefulSetCount, expectedStatefulSetCount)
return bootstrapArgo(ctx, clientset, config, namespace, argoImage, redisArgoImage, apiClient, cluster)
}
func bootstrapArgo(ctx context.Context, clientset *kubernetes.Clientset, config *rest.Config, namespace, argoImage, redisArgoImage string, apiClient *api.Client, cluster *api.Cluster) error {
if err := createArgoCDConfigMaps(ctx, cluster, clientset, namespace); err != nil {
return err
}
if err := createArgoCRDs(ctx, config); err != nil {
return err
}
if err := createRedisDeployment(ctx, clientset, namespace, argoImage, redisArgoImage); err != nil {
return err
}
if err := createRepoServerDeployment(ctx, clientset, namespace, argoImage); err != nil {
return err
}
if err := createServerDeployment(ctx, clientset, namespace, argoImage); err != nil {
return err
}
if err := createArgoProject(ctx, cluster, config, namespace); err != nil {
return err
}
if err := createArgoApp(ctx, cluster, config, namespace); err != nil {
return err
}
if err := createApplicationControllerStatefulSet(ctx, clientset, namespace, argoImage); err != nil {
return err
}
return nil
}
func fixArgoOperatorDeadlock(ctx context.Context, clientset *kubernetes.Clientset, config *rest.Config, namespace, operatorNamespace string) error {
configmaps, err := clientset.CoreV1().ConfigMaps(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/part-of=argocd",
})
if err != nil {
return fmt.Errorf("Could not list ArgoCD config maps: %w", err)
}
if len(configmaps.Items) > 2 {
// no restart required
return nil
}
pods, err := clientset.CoreV1().Pods(operatorNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("Could not list ArgoCD operator pods: %w", err)
}
for _, pod := range pods.Items {
if pod.CreationTimestamp.Time.After(time.Now().Add(-10 * time.Minute)) {
klog.Info("ArgoCD Operator pod was recently created, waiting to reboot...")
return nil
}
}
// if there still exists an argocd-secret not managed by the operator, clean it up:
secret, err := clientset.CoreV1().Secrets(namespace).Get(ctx, argoSecretName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("Could not get ArgoCD secret: %w", err)
}
if err == nil {
if len(secret.ObjectMeta.OwnerReferences) == 0 {
klog.Info("Deleting steward-managed ArgoCD secret")
err := clientset.CoreV1().Secrets(namespace).Delete(ctx, argoSecretName, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("Could not delete steward-managed ArgoCD secret: %w", err)
}
}
}
klog.Info("Rebooting ArgoCD operator to resolve deadlock...")
errors := []error{}
for _, pod := range pods.Items {
klog.Infof("Removing pod %s", pod.Name)
err := clientset.CoreV1().Pods(operatorNamespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
errors = append(errors, err)
}
return multierr.Combine(errors...)
}