@@ -2,13 +2,17 @@ package argocd
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "fmt"
5
7
6
8
"github.com/projectsyn/lieutenant-api/pkg/api"
9
+ "k8s.io/apimachinery/pkg/api/errors"
7
10
k8err "k8s.io/apimachinery/pkg/api/errors"
8
11
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
12
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10
13
"k8s.io/apimachinery/pkg/runtime/schema"
11
14
"k8s.io/client-go/dynamic"
15
+ "k8s.io/client-go/kubernetes"
12
16
"k8s.io/client-go/rest"
13
17
"k8s.io/klog"
14
18
)
@@ -23,20 +27,48 @@ var (
23
27
argoProjectGVR = argoGroupVersion .WithResource ("appprojects" )
24
28
25
29
localKubernetesAPI = "https://kubernetes.default.svc"
30
+
31
+ additionalRootAppsConfigKey = "teams"
26
32
)
27
33
28
- func createArgoProject (ctx context.Context , cluster * api.Cluster , config * rest.Config , namespace string ) error {
34
+ func readAdditionalRootAppsConfigMap (ctx context.Context , clientset * kubernetes.Clientset , namespace , additionalRootAppsConfigMapName string ) ([]string , error ) {
35
+ cm , err := clientset .CoreV1 ().ConfigMaps (namespace ).Get (ctx , additionalRootAppsConfigMapName , v1.GetOptions {})
36
+ if err != nil {
37
+ if errors .IsNotFound (err ) {
38
+ klog .Info ("Additional root apps config map not present" )
39
+ return []string {}, nil
40
+ } else {
41
+ return nil , fmt .Errorf ("unable to fetch the additional root apps config map: %w" , err )
42
+ }
43
+ }
44
+ teamsJson , ok := cm .Data [additionalRootAppsConfigKey ]
45
+ if ! ok {
46
+ return nil , fmt .Errorf ("additional root apps ConfigMap doesn't have key %s" , additionalRootAppsConfigKey )
47
+ }
48
+ var teams []string
49
+ if err := json .Unmarshal ([]byte (teamsJson ), & teams ); err != nil {
50
+ return nil , fmt .Errorf ("unmarshalling additional root apps ConfigMap contents: %v" , err )
51
+ }
52
+ return teams , nil
53
+ }
54
+
55
+ func createArgoProject (ctx context.Context , cluster * api.Cluster , config * rest.Config , namespace , name string ) error {
29
56
dynamicClient , err := dynamic .NewForConfig (config )
30
57
if err != nil {
31
58
return err
32
59
}
33
60
argoProjectClient := dynamicClient .Resource (argoProjectGVR )
61
+
62
+ if _ , err = argoProjectClient .Namespace (namespace ).Get (ctx , name , v1.GetOptions {}); err == nil {
63
+ return nil
64
+ }
65
+
34
66
project := & unstructured.Unstructured {
35
67
Object : map [string ]interface {}{
36
68
"apiVersion" : argoProjectGVR .Group + "/" + argoProjectGVR .Version ,
37
69
"kind" : "AppProject" ,
38
70
"metadata" : map [string ]interface {}{
39
- "name" : argoProjectName ,
71
+ "name" : name ,
40
72
},
41
73
"spec" : map [string ]interface {}{
42
74
"clusterResourceWhitelist" : []map [string ]interface {}{{
@@ -56,39 +88,44 @@ func createArgoProject(ctx context.Context, cluster *api.Cluster, config *rest.C
56
88
57
89
if _ , err = argoProjectClient .Namespace (namespace ).Create (ctx , project , v1.CreateOptions {}); err != nil {
58
90
if k8err .IsAlreadyExists (err ) {
59
- klog .Warning ("Argo Project already exists, skip" )
91
+ klog .Warning ("Argo Project already exists, skipping... app=" , name )
60
92
} else {
61
93
return err
62
94
}
63
95
} else {
64
- klog .Info ("Argo Project created" )
96
+ klog .Info ("Argo Project created: " , name )
65
97
}
66
98
return nil
67
99
}
68
100
69
- func createArgoApp (ctx context.Context , cluster * api.Cluster , config * rest.Config , namespace string ) error {
101
+ func createArgoApp (ctx context.Context , cluster * api.Cluster , config * rest.Config , namespace , projectName , name , appsPath string ) error {
70
102
dynamicClient , err := dynamic .NewForConfig (config )
71
103
if err != nil {
72
104
return err
73
105
}
74
106
argoAppClient := dynamicClient .Resource (argoAppGVR )
107
+
108
+ if _ , err = argoAppClient .Namespace (namespace ).Get (ctx , name , v1.GetOptions {}); err == nil {
109
+ return nil
110
+ }
111
+
75
112
app := & unstructured.Unstructured {
76
113
Object : map [string ]interface {}{
77
114
"apiVersion" : argoAppGVR .Group + "/" + argoAppGVR .Version ,
78
115
"kind" : "Application" ,
79
116
"metadata" : map [string ]interface {}{
80
- "name" : argoRootAppName ,
117
+ "name" : name ,
81
118
},
82
119
"spec" : map [string ]interface {}{
83
- "project" : argoProjectName ,
120
+ "project" : projectName ,
84
121
"source" : map [string ]interface {}{
85
122
"repoURL" : * cluster .GitRepo .Url ,
86
- "path" : argoAppsPath ,
123
+ "path" : appsPath + "/" ,
87
124
"targetRevision" : "HEAD" ,
88
125
},
89
126
"syncPolicy" : map [string ]interface {}{
90
127
"automated" : map [string ]interface {}{
91
- "prune" : true ,
128
+ "prune" : false ,
92
129
"selfHeal" : true ,
93
130
},
94
131
},
@@ -102,12 +139,12 @@ func createArgoApp(ctx context.Context, cluster *api.Cluster, config *rest.Confi
102
139
103
140
if _ , err = argoAppClient .Namespace (namespace ).Create (ctx , app , v1.CreateOptions {}); err != nil {
104
141
if k8err .IsAlreadyExists (err ) {
105
- klog .Warning ("Argo App already exists, skip" )
142
+ klog .Warning ("Argo App already exists, skipping... app=" , name )
106
143
} else {
107
144
return err
108
145
}
109
146
} else {
110
- klog .Info ("Argo App created" )
147
+ klog .Info ("Argo App created: " , name )
111
148
}
112
149
return nil
113
150
}
0 commit comments