-
Notifications
You must be signed in to change notification settings - Fork 760
/
export.go
105 lines (90 loc) · 3.26 KB
/
export.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
// 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"
"github.com/sethvargo/go-password/password"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
argoprojv1a1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
argoprojv1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
)
// generateBackupKey will generate and return the backup key for the export process.
func generateBackupKey() ([]byte, error) {
pass, err := password.Generate(
common.ArgoCDDefaultBackupKeyLength,
common.ArgoCDDefaultBackupKeyNumDigits,
common.ArgoCDDefaultBackupKeyNumSymbols,
false, false)
return []byte(pass), err
}
// reconcileExport will ensure that the resources for the export process are present for the ArgoCDExport.
func (r *ReconcileArgoCDExport) reconcileExport(cr *argoprojv1a1.ArgoCDExport) error {
log.Info("reconciling export secret")
if err := r.reconcileExportSecret(cr); err != nil {
return err
}
if cr.Spec.Schedule != nil && len(*cr.Spec.Schedule) > 0 {
log.Info("reconciling export cronjob")
if err := r.reconcileCronJob(cr); err != nil {
return err
}
} else {
log.Info("reconciling export job")
if err := r.reconcileJob(cr); err != nil {
return err
}
}
return nil
}
// reconcileExportSecret will ensure that the Secret used for the export process is present.
func (r *ReconcileArgoCDExport) reconcileExportSecret(cr *argoprojv1a1.ArgoCDExport) error {
name := argoutil.FetchStorageSecretName(cr)
// Dummy CR to retrieve secret
a := &argoprojv1a1.ArgoCD{}
a.ObjectMeta = cr.ObjectMeta
secret := argoutil.NewSecretWithName(a, name)
if argoutil.IsObjectFound(r.Client, cr.Namespace, name, secret) {
backupKey := secret.Data[common.ArgoCDKeyBackupKey]
if len(backupKey) <= 0 {
backupKey, err := generateBackupKey()
if err != nil {
return err
}
secret.Data[common.ArgoCDKeyBackupKey] = backupKey
return r.Client.Update(context.TODO(), secret)
}
return nil // TODO: Handle case where backup key changes, should trigger a new export?
}
backupKey, err := generateBackupKey()
if err != nil {
return err
}
secret.Data = map[string][]byte{
common.ArgoCDKeyBackupKey: backupKey,
}
if err := controllerutil.SetControllerReference(cr, secret, r.Scheme); err != nil {
return err
}
return r.Client.Create(context.TODO(), secret)
}
// validateExport will ensure that the given ArgoCDExport is valid.
func (r *ReconcileArgoCDExport) validateExport(cr *argoprojv1alpha1.ArgoCDExport) error {
if len(cr.Status.Phase) <= 0 {
cr.Status.Phase = "Pending"
return r.Client.Status().Update(context.TODO(), cr)
}
return nil
}