-
Notifications
You must be signed in to change notification settings - Fork 119
/
common.go
267 lines (239 loc) · 7.24 KB
/
common.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
package commands
import (
"context"
_ "embed"
"fmt"
"os"
"time"
"github.com/argoproj-labs/argocd-autopilot/pkg/argocd"
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
"github.com/argoproj-labs/argocd-autopilot/pkg/log"
"github.com/argoproj-labs/argocd-autopilot/pkg/store"
"github.com/argoproj-labs/argocd-autopilot/pkg/util"
appset "github.com/argoproj/applicationset/api/v1alpha1"
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/ghodss/yaml"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// used for mocking
var (
die = util.Die
exit = os.Exit
DefaultApplicationSetGeneratorInterval int64 = 20
//go:embed assets/cluster_res_readme.md
clusterResReadmeTpl []byte
//go:embed assets/projects_readme.md
projectReadme []byte
//go:embed assets/apps_readme.md
appsReadme []byte
getRepo = func(ctx context.Context, cloneOpts *git.CloneOptions) (git.Repository, fs.FS, error) {
return cloneOpts.GetRepo(ctx)
}
prepareRepo = func(ctx context.Context, cloneOpts *git.CloneOptions, projectName string) (git.Repository, fs.FS, error) {
log.G(ctx).WithFields(log.Fields{
"repoURL": cloneOpts.URL(),
"revision": cloneOpts.Revision(),
"forWrite": cloneOpts.CloneForWrite,
}).Debug("starting with options: ")
// clone repo
log.G(ctx).Infof("cloning git repository: %s", cloneOpts.URL())
r, repofs, err := getRepo(ctx, cloneOpts)
if err != nil {
return nil, nil, fmt.Errorf("failed cloning the repository: %w", err)
}
root := repofs.Root()
log.G(ctx).Infof("using revision: \"%s\", installation path: \"%s\"", cloneOpts.Revision(), root)
if !repofs.ExistsOrDie(store.Default.BootsrtrapDir) {
return nil, nil, fmt.Errorf("bootstrap directory not found, please execute `repo bootstrap` command")
}
if projectName != "" {
projExists := repofs.ExistsOrDie(repofs.Join(store.Default.ProjectsDir, projectName+".yaml"))
if !projExists {
return nil, nil, fmt.Errorf(util.Doc(fmt.Sprintf("project '%[1]s' not found, please execute `<BIN> project create %[1]s`", projectName)))
}
}
log.G(ctx).Debug("repository is ok")
return r, repofs, nil
}
)
type createAppOptions struct {
name string
namespace string
repoURL string
revision string
srcPath string
destServer string
noFinalizer bool
labels map[string]string
}
func createApp(opts *createAppOptions) ([]byte, error) {
if opts.destServer == "" {
opts.destServer = store.Default.DestServer
}
app := &argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
Kind: argocdv1alpha1.ApplicationSchemaGroupVersionKind.Kind,
APIVersion: argocdv1alpha1.ApplicationSchemaGroupVersionKind.GroupVersion().String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: opts.namespace,
Name: opts.name,
Labels: map[string]string{
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
"app.kubernetes.io/name": opts.name,
},
Finalizers: []string{
"resources-finalizer.argocd.argoproj.io",
},
},
Spec: argocdv1alpha1.ApplicationSpec{
Project: "default",
Source: argocdv1alpha1.ApplicationSource{
RepoURL: opts.repoURL,
Path: opts.srcPath,
TargetRevision: opts.revision,
},
Destination: argocdv1alpha1.ApplicationDestination{
Server: opts.destServer,
Namespace: opts.namespace,
},
SyncPolicy: &argocdv1alpha1.SyncPolicy{
Automated: &argocdv1alpha1.SyncPolicyAutomated{
SelfHeal: true,
Prune: true,
AllowEmpty: true,
},
SyncOptions: []string{
"allowEmpty=true",
},
},
IgnoreDifferences: []argocdv1alpha1.ResourceIgnoreDifferences{
{
Group: "argoproj.io",
Kind: "Application",
JSONPointers: []string{
"/status",
},
},
},
},
}
if opts.noFinalizer {
app.ObjectMeta.Finalizers = []string{}
}
if len(opts.labels) > 0 {
for k, v := range opts.labels {
app.ObjectMeta.Labels[k] = v
}
}
return yaml.Marshal(app)
}
func waitAppSynced(ctx context.Context, f kube.Factory, timeout time.Duration, appName, namespace, revision string, waitForCreation bool) error {
return f.Wait(ctx, &kube.WaitOptions{
Interval: store.Default.WaitInterval,
Timeout: timeout,
Resources: []kube.Resource{
{
Name: appName,
Namespace: namespace,
WaitFunc: argocd.GetAppSyncWaitFunc(revision, waitForCreation),
},
},
})
}
type createAppSetOptions struct {
name string
namespace string
appName string
appNamespace string
appProject string
repoURL string
revision string
srcPath string
destServer string
destNamespace string
prune bool
preserveResourcesOnDeletion bool
appLabels map[string]string
generators []appset.ApplicationSetGenerator
}
func createAppSet(o *createAppSetOptions) ([]byte, error) {
if o.destServer == "" {
o.destServer = store.Default.DestServer
}
if o.appProject == "" {
o.appProject = "default"
}
if o.appLabels == nil {
// default labels
o.appLabels = map[string]string{
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
"app.kubernetes.io/name": o.appName,
}
}
appSet := &appset.ApplicationSet{
TypeMeta: metav1.TypeMeta{
Kind: "ApplicationSet",
APIVersion: appset.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: o.name,
Namespace: o.namespace,
Annotations: map[string]string{
"argocd.argoproj.io/sync-wave": "0",
},
},
Spec: appset.ApplicationSetSpec{
Generators: o.generators,
Template: appset.ApplicationSetTemplate{
ApplicationSetTemplateMeta: appset.ApplicationSetTemplateMeta{
Namespace: o.appNamespace,
Name: o.appName,
Labels: o.appLabels,
},
Spec: argocdv1alpha1.ApplicationSpec{
Project: o.appProject,
Source: argocdv1alpha1.ApplicationSource{
RepoURL: o.repoURL,
Path: o.srcPath,
TargetRevision: o.revision,
},
Destination: argocdv1alpha1.ApplicationDestination{
Server: o.destServer,
Namespace: o.destNamespace,
},
SyncPolicy: &argocdv1alpha1.SyncPolicy{
Automated: &argocdv1alpha1.SyncPolicyAutomated{
SelfHeal: true,
Prune: o.prune,
AllowEmpty: true,
},
},
IgnoreDifferences: []argocdv1alpha1.ResourceIgnoreDifferences{
{
Group: "argoproj.io",
Kind: "Application",
JSONPointers: []string{
"/status",
},
},
},
},
},
SyncPolicy: &appset.ApplicationSetSyncPolicy{
PreserveResourcesOnDeletion: o.preserveResourcesOnDeletion,
},
},
}
return yaml.Marshal(appSet)
}
var getInstallationNamespace = func(repofs fs.FS) (string, error) {
path := repofs.Join(store.Default.BootsrtrapDir, store.Default.ArgoCDName+".yaml")
a := &argocdv1alpha1.Application{}
if err := repofs.ReadYamls(path, a); err != nil {
return "", fmt.Errorf("failed to unmarshal namespace: %w", err)
}
return a.Spec.Destination.Namespace, nil
}