-
Notifications
You must be signed in to change notification settings - Fork 612
/
job.go
300 lines (257 loc) · 9.04 KB
/
job.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
// Copyright 2019 ArgoCD Operator Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package argocdexport
import (
"context"
"fmt"
"strings"
"sigs.k8s.io/controller-runtime/pkg/client"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
argoprojv1a1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argocd"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
)
// getArgoExportCommand will return the command for the ArgoCD export process.
func getArgoExportCommand(cr *argoprojv1a1.ArgoCDExport) []string {
cmd := make([]string, 0)
cmd = append(cmd, "uid_entrypoint.sh")
cmd = append(cmd, "argocd-operator-util")
cmd = append(cmd, "export")
cmd = append(cmd, cr.Spec.Storage.Backend)
return cmd
}
func getArgoExportContainerEnv(cr *argoprojv1a1.ArgoCDExport) []corev1.EnvVar {
env := make([]corev1.EnvVar, 0)
switch cr.Spec.Storage.Backend {
case common.ArgoCDExportStorageBackendAWS:
env = append(env, corev1.EnvVar{
Name: "AWS_ACCESS_KEY_ID",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: argoutil.FetchStorageSecretName(cr),
},
Key: "aws.access.key.id",
},
},
})
env = append(env, corev1.EnvVar{
Name: "AWS_SECRET_ACCESS_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: argoutil.FetchStorageSecretName(cr),
},
Key: "aws.secret.access.key",
},
},
})
}
return env
}
// getArgoExportContainerImage will return the container image for ArgoCD.
func getArgoExportContainerImage(cr *argoprojv1a1.ArgoCDExport) string {
img := cr.Spec.Image
if len(img) <= 0 {
img = common.ArgoCDDefaultExportJobImage
}
tag := cr.Spec.Version
if len(tag) <= 0 {
tag = common.ArgoCDDefaultExportJobVersion
}
return argoutil.CombineImageTag(img, tag)
}
// getArgoExportVolumeMounts will return the VolumneMounts for the given ArgoCDExport.
func getArgoExportVolumeMounts() []corev1.VolumeMount {
mounts := make([]corev1.VolumeMount, 0)
mounts = append(mounts, corev1.VolumeMount{
Name: "backup-storage",
MountPath: "/backups",
})
mounts = append(mounts, corev1.VolumeMount{
Name: "secret-storage",
MountPath: "/secrets",
})
return mounts
}
// getArgoSecretVolume will return the Secret Volume for the export process.
func getArgoSecretVolume(name string, cr *argoprojv1a1.ArgoCDExport) corev1.Volume {
volume := corev1.Volume{
Name: name,
}
volume.VolumeSource = corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: argoutil.FetchStorageSecretName(cr),
},
}
return volume
}
// getArgoStorageVolume will return the storage Volume for the export process.
func getArgoStorageVolume(name string, cr *argoprojv1a1.ArgoCDExport) corev1.Volume {
volume := corev1.Volume{
Name: name,
}
if cr.Spec.Storage == nil || strings.ToLower(cr.Spec.Storage.Backend) == common.ArgoCDExportStorageBackendLocal {
volume.VolumeSource = corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: cr.Name,
},
}
} else {
volume.VolumeSource = corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
}
}
return volume
}
// newJob returns a new Job instance for the given ArgoCDExport.
func newJob(cr *argoprojv1a1.ArgoCDExport) *batchv1.Job {
return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: common.DefaultLabels(cr.Name),
},
}
}
// newCronJob returns a new CronJob instance for the given ArgoCDExport.
func newCronJob(cr *argoprojv1a1.ArgoCDExport) *batchv1.CronJob {
return &batchv1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: common.DefaultLabels(cr.Name),
},
}
}
func newExportPodSpec(cr *argoprojv1a1.ArgoCDExport, argocdName string, client client.Client) corev1.PodSpec {
pod := corev1.PodSpec{}
boolPtr := func(value bool) *bool {
return &value
}
pod.Containers = []corev1.Container{{
Command: getArgoExportCommand(cr),
Env: getArgoExportContainerEnv(cr),
Image: getArgoExportContainerImage(cr),
ImagePullPolicy: corev1.PullAlways,
Name: "argocd-export",
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: boolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsNonRoot: boolPtr(true),
},
VolumeMounts: getArgoExportVolumeMounts(),
}}
pod.RestartPolicy = corev1.RestartPolicyOnFailure
pod.ServiceAccountName = fmt.Sprintf("%s-%s", argocdName, "argocd-application-controller")
pod.Volumes = []corev1.Volume{
getArgoStorageVolume("backup-storage", cr),
getArgoSecretVolume("secret-storage", cr),
}
// Configure runAsUser, runAsGroup and fsGroup so that the job can write to the PV
// 999 is the uid/gid of the argocd user that the container runs as
id := int64(999)
pod.SecurityContext = &corev1.PodSecurityContext{
RunAsUser: &id,
RunAsGroup: &id,
FSGroup: &id,
}
argocd.AddSeccompProfileForOpenShift(client, &pod)
return pod
}
func newPodTemplateSpec(cr *argoprojv1a1.ArgoCDExport, argocdName string, client client.Client) corev1.PodTemplateSpec {
return corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: common.DefaultLabels(cr.Name),
},
Spec: newExportPodSpec(cr, argocdName, client),
}
}
// reconcileCronJob will ensure that the CronJob for the ArgoCDExport is present.
func (r *ReconcileArgoCDExport) reconcileCronJob(cr *argoprojv1a1.ArgoCDExport) error {
if cr.Spec.Storage == nil {
return nil // Do nothing if storage options not set
}
cj := newCronJob(cr)
if argoutil.IsObjectFound(r.Client, cr.Namespace, cj.Name, cj) {
if *cr.Spec.Schedule != cj.Spec.Schedule {
cj.Spec.Schedule = *cr.Spec.Schedule
return r.Client.Update(context.TODO(), cj)
}
return nil
}
cj.Spec.Schedule = *cr.Spec.Schedule
// To create the job, we need the name of the argocd instance. Although the argocd export cr contains a field with
// the argocd instance name, it's never used anywhere, and so there may be existing argocd export resources with the
// wrong name. To avoid these breaking, we look up the name of the argocd instance in the namespace of the export cr.
argocdName, err := r.argocdName(cr.Namespace)
if err != nil {
return err
}
job := newJob(cr)
job.Spec.Template = newPodTemplateSpec(cr, argocdName, r.Client)
cj.Spec.JobTemplate.Spec = job.Spec
if err := controllerutil.SetControllerReference(cr, cj, r.Scheme); err != nil {
return err
}
return r.Client.Create(context.TODO(), cj)
}
// reconcileJob will ensure that the Job for the ArgoCDExport is present.
func (r *ReconcileArgoCDExport) reconcileJob(cr *argoprojv1a1.ArgoCDExport) error {
if cr.Spec.Storage == nil {
return nil // Do nothing if storage options not set
}
job := newJob(cr)
if argoutil.IsObjectFound(r.Client, cr.Namespace, job.Name, job) {
if job.Status.Succeeded > 0 && cr.Status.Phase != common.ArgoCDStatusCompleted {
// Mark status Phase as Complete
cr.Status.Phase = common.ArgoCDStatusCompleted
return r.Client.Status().Update(context.TODO(), cr)
}
return nil // Job not complete, move along...
}
// To create the job, we need the name of the argocd instance. Although the argocd export cr contains a field with
// the argocd instance name, it's never used anywhere, and so there may be existing argocd export resources with the
// wrong name. To avoid these breaking, we look up the name of the argocd instance in the namespace of the export cr.
argocdName, err := r.argocdName(cr.Namespace)
if err != nil {
return err
}
job.Spec.Template = newPodTemplateSpec(cr, argocdName, r.Client)
if err := controllerutil.SetControllerReference(cr, job, r.Scheme); err != nil {
return err
}
return r.Client.Create(context.TODO(), job)
}
func (r *ReconcileArgoCDExport) argocdName(namespace string) (string, error) {
argocds := &argoprojv1a1.ArgoCDList{}
if err := r.Client.List(context.TODO(), argocds, &client.ListOptions{Namespace: namespace}); err != nil {
return "", err
}
if len(argocds.Items) != 1 {
return "", fmt.Errorf("No Argo CD instance found in namespace %s", namespace)
}
argocd := argocds.Items[0]
return argocd.Name, nil
}