This repository has been archived by the owner on Aug 9, 2023. It is now read-only.
/
destcluster.go
338 lines (290 loc) · 8.4 KB
/
destcluster.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2023 Nautes Authors
//
// 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 argocd
import (
"context"
"fmt"
"reflect"
argocrd "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
nautescrd "github.com/nautes-labs/pkg/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
var (
scheme = runtime.NewScheme()
baseAppProject = argocrd.AppProject{
Spec: argocrd.AppProjectSpec{
SourceRepos: []string{},
Destinations: []argocrd.ApplicationDestination{},
ClusterResourceWhitelist: []metav1.GroupKind{
{
Group: "*",
Kind: "*",
},
},
},
}
baseApp = argocrd.Application{
Spec: argocrd.ApplicationSpec{
Destination: argocrd.ApplicationDestination{
Server: kubernetesDefaultService,
},
SyncPolicy: &argocrd.SyncPolicy{
Automated: &argocrd.SyncPolicyAutomated{
Prune: true,
SelfHeal: true,
},
},
},
}
)
func init() {
utilruntime.Must(argocrd.AddToScheme(scheme))
utilruntime.Must(nautescrd.AddToScheme(scheme))
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
}
type destCluster struct {
name string
productName string
client.Client
}
func (d *destCluster) GetClient() client.Client {
return d.Client
}
func findCodeRepoInProject(project argocrd.AppProject, repoName string) (int, bool) {
repos := project.Spec.SourceRepos
isExist := false
index := -1
for i, src := range repos {
if src == repoName {
isExist = true
index = i
break
}
}
return index, isExist
}
func findDestinationInProject(project argocrd.AppProject, namespaceName string) (int, bool) {
dests := project.Spec.Destinations
isExist := false
index := -1
for i, dest := range dests {
if dest.Namespace == namespaceName && dest.Server == "*" {
isExist = true
index = i
break
}
}
return index, isExist
}
type appProject struct {
resource *argocrd.AppProject
productName string
client.Client
}
func NewAppProject(name, namespace, productName string, k8sClient client.Client) (*appProject, error) {
project := &argocrd.AppProject{}
err := k8sClient.Get(context.Background(), types.NamespacedName{
Namespace: namespace,
Name: name,
}, project)
if err != nil {
if client.IgnoreNotFound(err) != nil {
return nil, err
}
project = &argocrd.AppProject{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{nautescrd.LABEL_BELONG_TO_PRODUCT: productName},
},
Spec: argocrd.AppProjectSpec{},
}
}
if reason, ok := isNotTerminatingAndBelongsToProduct(project, productName); !ok {
return nil, fmt.Errorf(reason)
}
return &appProject{
resource: project,
productName: productName,
Client: k8sClient,
}, nil
}
func (p *appProject) GetName() string {
return p.resource.Name
}
func (p *appProject) GetNamespace() string {
return p.resource.Namespace
}
func (p *appProject) SyncAppPermission(ctx context.Context, newURL, oldURL, newNamespace, oldNamespace string) error {
logger := log.FromContext(ctx)
p.addSource(newURL)
p.addNamespace(newNamespace)
if newURL != oldURL {
logger.V(1).Info("coderepo url is change", "NewURL", newURL, "OldURL", oldURL)
}
deletable, err := p.urlIsDeletable(ctx, oldURL)
if err != nil {
return fmt.Errorf("check url is deletable failed: %w", err)
}
if deletable {
logger.V(1).Info("old url with remove from app project", "URL", oldURL)
p.deleteSource(oldURL)
}
p.deleteNamespace(oldNamespace)
return p.syncToKubernetes(ctx)
}
func (p *appProject) SyncAppProject(ctx context.Context, spec argocrd.AppProjectSpec) error {
p.resource.Spec = spec
return p.syncToKubernetes(ctx)
}
func (p *appProject) syncToKubernetes(ctx context.Context) error {
if p.resource.CreationTimestamp.IsZero() {
return p.Create(ctx, p.resource)
}
return p.Update(ctx, p.resource)
}
func (p *appProject) destroy(ctx context.Context) error {
if p.resource.CreationTimestamp.IsZero() {
return nil
}
return p.Delete(ctx, p.resource)
}
func (p *appProject) addSource(url string) {
_, codeRepoIsExist := findCodeRepoInProject(*p.resource, url)
if !codeRepoIsExist && url != "" {
p.resource.Spec.SourceRepos = append(p.resource.Spec.SourceRepos, url)
}
}
func (p *appProject) deleteSource(url string) {
i, isExist := findCodeRepoInProject(*p.resource, url)
if isExist {
p.resource.Spec.SourceRepos = append(p.resource.Spec.SourceRepos[:i], p.resource.Spec.SourceRepos[i+1:]...)
}
}
func (p *appProject) addNamespace(namespace string) {
_, destinationIsExist := findDestinationInProject(*p.resource, namespace)
if !destinationIsExist && namespace != "" {
dest := argocrd.ApplicationDestination{
Server: "*",
Namespace: namespace,
}
p.resource.Spec.Destinations = append(p.resource.Spec.Destinations, dest)
}
}
func (p *appProject) deleteNamespace(namespace string) {
i, isExist := findDestinationInProject(*p.resource, namespace)
if isExist {
p.resource.Spec.Destinations = append(p.resource.Spec.Destinations[:i], p.resource.Spec.Destinations[i+1:]...)
}
}
func (p *appProject) isDeletable() bool {
return len(p.resource.Spec.SourceRepos) == 0
}
func (p *appProject) urlIsDeletable(ctx context.Context, repoURL string) (bool, error) {
apps := &argocrd.ApplicationList{}
listOpts := []client.ListOption{
client.MatchingLabels(map[string]string{nautescrd.LABEL_BELONG_TO_PRODUCT: p.productName}),
client.InNamespace(p.resource.Namespace),
}
err := p.List(ctx, apps, listOpts...)
if err != nil {
return false, err
}
isDeleteable := true
for _, app := range apps.Items {
if app.Spec.Source.RepoURL == repoURL && app.DeletionTimestamp.IsZero() {
isDeleteable = false
break
}
}
return isDeleteable, nil
}
type app struct {
resource *argocrd.Application
productName string
k8sClient client.Client
}
func newApp(name, namespace, productName string, k8sClient client.Client) (*app, error) {
appInK8s := &argocrd.Application{}
err := k8sClient.Get(context.Background(), types.NamespacedName{
Namespace: namespace,
Name: name,
}, appInK8s)
if err != nil {
if client.IgnoreNotFound(err) != nil {
return nil, err
}
appInK8s = &argocrd.Application{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{nautescrd.LABEL_BELONG_TO_PRODUCT: productName}},
Spec: argocrd.ApplicationSpec{},
}
}
if reason, ok := isNotTerminatingAndBelongsToProduct(appInK8s, productName); !ok {
return nil, fmt.Errorf(reason)
}
return &app{
resource: appInK8s,
productName: productName,
k8sClient: k8sClient,
}, nil
}
type updateAppOption func(*argocrd.ApplicationSpec)
func (a *app) SyncApp(ctx context.Context, opts ...updateAppOption) error {
if err := a.UpdateApp(ctx, opts...); err != nil {
return fmt.Errorf("update app failed: %w", err)
}
return a.syncToKubernetes(ctx)
}
func (a *app) syncToKubernetes(ctx context.Context) error {
if a.resource.CreationTimestamp.IsZero() {
return a.k8sClient.Create(ctx, a.resource)
}
return a.k8sClient.Update(ctx, a.resource)
}
func (a *app) UpdateApp(ctx context.Context, opts ...updateAppOption) error {
spec := baseApp.Spec.DeepCopy()
for _, fn := range opts {
fn(spec)
}
if !a.isSame(*spec) {
a.resource.Spec = *spec
}
return nil
}
func (a *app) GetName() string {
return a.resource.Name
}
func (a *app) Destroy(ctx context.Context) error {
if a.resource.CreationTimestamp.IsZero() {
return nil
}
return a.k8sClient.Delete(ctx, a.resource)
}
func (a *app) isSame(spec argocrd.ApplicationSpec) bool {
if reflect.DeepEqual(a.resource.Spec.Source, spec.Source) &&
reflect.DeepEqual(a.resource.Spec.Destination, spec.Destination) &&
a.resource.Spec.Project == spec.Project {
return true
}
return false
}