forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
379 lines (350 loc) · 13 KB
/
install.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package install
import (
"fmt"
"strings"
"syscall"
"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/errors"
"github.com/argoproj/argo-cd/util/diff"
"github.com/argoproj/argo-cd/util/kube"
"github.com/argoproj/argo-cd/util/password"
"github.com/argoproj/argo-cd/util/session"
"github.com/argoproj/argo-cd/util/settings"
tlsutil "github.com/argoproj/argo-cd/util/tls"
"github.com/ghodss/yaml"
"github.com/gobuffalo/packr"
log "github.com/sirupsen/logrus"
"github.com/yudai/gojsondiff/formatter"
"golang.org/x/crypto/ssh/terminal"
appsv1beta2 "k8s.io/api/apps/v1beta2"
apiv1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
// These values will be overridden by the link flags during build
// (e.g. imageTag will use the official release tag on tagged builds)
imageNamespace = "argoproj"
imageTag = "latest"
// Default namespace and image names which `argocd install` uses during install
DefaultInstallNamespace = "argocd"
DefaultControllerImage = imageNamespace + "/argocd-application-controller:" + imageTag
DefaultUIImage = imageNamespace + "/argocd-ui:" + imageTag
DefaultServerImage = imageNamespace + "/argocd-server:" + imageTag
DefaultRepoServerImage = imageNamespace + "/argocd-repo-server:" + imageTag
)
// InstallOptions stores a collection of installation settings.
type InstallOptions struct {
DryRun bool
Upgrade bool
UpdateSuperuser bool
UpdateSignature bool
SuperuserPassword string
Namespace string
ControllerImage string
UIImage string
ServerImage string
RepoServerImage string
ImagePullPolicy string
}
type Installer struct {
InstallOptions
box packr.Box
config *rest.Config
dynClientPool dynamic.ClientPool
disco discovery.DiscoveryInterface
}
func NewInstaller(config *rest.Config, opts InstallOptions) (*Installer, error) {
shallowCopy := *config
inst := Installer{
InstallOptions: opts,
box: packr.NewBox("./manifests"),
config: &shallowCopy,
}
if opts.Namespace == "" {
inst.Namespace = DefaultInstallNamespace
}
var err error
inst.dynClientPool = dynamic.NewDynamicClientPool(inst.config)
inst.disco, err = discovery.NewDiscoveryClientForConfig(inst.config)
if err != nil {
return nil, err
}
return &inst, nil
}
func (i *Installer) Install() {
i.InstallNamespace()
i.InstallApplicationCRD()
i.InstallSettings()
i.InstallApplicationController()
i.InstallArgoCDServer()
i.InstallArgoCDRepoServer()
}
func (i *Installer) Uninstall(deleteNamespace, deleteCRD bool) {
manifests := i.box.List()
for _, manifestPath := range manifests {
if strings.HasSuffix(manifestPath, ".yaml") || strings.HasSuffix(manifestPath, ".yml") {
var obj unstructured.Unstructured
i.unmarshalManifest(manifestPath, &obj)
switch strings.ToLower(obj.GetKind()) {
case "namespace":
if !deleteNamespace {
log.Infof("Skipped deletion of Namespace: '%s'", obj.GetName())
continue
}
case "customresourcedefinition":
if !deleteCRD {
log.Infof("Skipped deletion of CustomResourceDefinition: '%s'", obj.GetName())
continue
}
}
i.MustUninstallResource(&obj)
}
}
}
func (i *Installer) InstallNamespace() {
if i.Namespace != DefaultInstallNamespace {
// don't create namespace if a different one was supplied
return
}
var namespace apiv1.Namespace
i.unmarshalManifest("00_namespace.yaml", &namespace)
namespace.ObjectMeta.Name = i.Namespace
i.MustInstallResource(kube.MustToUnstructured(&namespace))
}
func (i *Installer) InstallApplicationCRD() {
var applicationCRD apiextensionsv1beta1.CustomResourceDefinition
i.unmarshalManifest("01_application-crd.yaml", &applicationCRD)
i.MustInstallResource(kube.MustToUnstructured(&applicationCRD))
}
func (i *Installer) InstallSettings() {
kubeclientset, err := kubernetes.NewForConfig(i.config)
errors.CheckError(err)
settingsMgr := settings.NewSettingsManager(kubeclientset, i.Namespace)
cdSettings, err := settingsMgr.GetSettings()
if err != nil {
if apierr.IsNotFound(err) {
cdSettings = &settings.ArgoCDSettings{}
} else {
log.Fatal(err)
}
}
if cdSettings.ServerSignature == nil || i.UpdateSignature {
// set JWT signature
signature, err := session.MakeSignature(32)
errors.CheckError(err)
cdSettings.ServerSignature = signature
}
if cdSettings.LocalUsers == nil {
cdSettings.LocalUsers = make(map[string]string)
}
if _, ok := cdSettings.LocalUsers[common.ArgoCDAdminUsername]; !ok || i.UpdateSuperuser {
passwordRaw := i.SuperuserPassword
if passwordRaw == "" {
passwordRaw = readAndConfirmPassword()
}
hashedPassword, err := password.HashPassword(passwordRaw)
errors.CheckError(err)
cdSettings.LocalUsers = map[string]string{
common.ArgoCDAdminUsername: hashedPassword,
}
}
if cdSettings.Certificate == nil {
// generate TLS cert
hosts := []string{
"localhost",
"argocd-server",
fmt.Sprintf("argocd-server.%s", i.Namespace),
fmt.Sprintf("argocd-server.%s.svc", i.Namespace),
fmt.Sprintf("argocd-server.%s.svc.cluster.local", i.Namespace),
}
certOpts := tlsutil.CertOptions{
Hosts: hosts,
Organization: "Argo CD",
IsCA: true,
}
cert, err := tlsutil.GenerateX509KeyPair(certOpts)
errors.CheckError(err)
cdSettings.Certificate = cert
}
err = settingsMgr.SaveSettings(cdSettings)
errors.CheckError(err)
}
func readAndConfirmPassword() string {
for {
fmt.Print("*** Enter an admin password: ")
password, err := terminal.ReadPassword(syscall.Stdin)
errors.CheckError(err)
fmt.Print("\n")
fmt.Print("*** Confirm the admin password: ")
confirmPassword, err := terminal.ReadPassword(syscall.Stdin)
errors.CheckError(err)
fmt.Print("\n")
if string(password) == string(confirmPassword) {
return string(password)
}
log.Error("Passwords do not match")
}
}
func (i *Installer) InstallApplicationController() {
var applicationControllerServiceAccount apiv1.ServiceAccount
var applicationControllerRole rbacv1.Role
var applicationControllerRoleBinding rbacv1.RoleBinding
var applicationControllerDeployment appsv1beta2.Deployment
i.unmarshalManifest("03a_application-controller-sa.yaml", &applicationControllerServiceAccount)
i.unmarshalManifest("03b_application-controller-role.yaml", &applicationControllerRole)
i.unmarshalManifest("03c_application-controller-rolebinding.yaml", &applicationControllerRoleBinding)
i.unmarshalManifest("03d_application-controller-deployment.yaml", &applicationControllerDeployment)
applicationControllerDeployment.Spec.Template.Spec.Containers[0].Image = i.ControllerImage
applicationControllerDeployment.Spec.Template.Spec.Containers[0].ImagePullPolicy = apiv1.PullPolicy(i.ImagePullPolicy)
i.MustInstallResource(kube.MustToUnstructured(&applicationControllerServiceAccount))
i.MustInstallResource(kube.MustToUnstructured(&applicationControllerRole))
i.MustInstallResource(kube.MustToUnstructured(&applicationControllerRoleBinding))
i.MustInstallResource(kube.MustToUnstructured(&applicationControllerDeployment))
}
func (i *Installer) InstallArgoCDServer() {
var argoCDServerServiceAccount apiv1.ServiceAccount
var argoCDServerControllerRole rbacv1.Role
var argoCDServerControllerRoleBinding rbacv1.RoleBinding
var argoCDServerControllerDeployment appsv1beta2.Deployment
var argoCDServerService apiv1.Service
i.unmarshalManifest("04a_argocd-server-sa.yaml", &argoCDServerServiceAccount)
i.unmarshalManifest("04b_argocd-server-role.yaml", &argoCDServerControllerRole)
i.unmarshalManifest("04c_argocd-server-rolebinding.yaml", &argoCDServerControllerRoleBinding)
i.unmarshalManifest("04d_argocd-server-deployment.yaml", &argoCDServerControllerDeployment)
i.unmarshalManifest("04e_argocd-server-service.yaml", &argoCDServerService)
argoCDServerControllerDeployment.Spec.Template.Spec.InitContainers[0].Image = i.ServerImage
argoCDServerControllerDeployment.Spec.Template.Spec.InitContainers[0].ImagePullPolicy = apiv1.PullPolicy(i.ImagePullPolicy)
argoCDServerControllerDeployment.Spec.Template.Spec.InitContainers[1].Image = i.UIImage
argoCDServerControllerDeployment.Spec.Template.Spec.InitContainers[1].ImagePullPolicy = apiv1.PullPolicy(i.ImagePullPolicy)
argoCDServerControllerDeployment.Spec.Template.Spec.Containers[0].Image = i.ServerImage
argoCDServerControllerDeployment.Spec.Template.Spec.Containers[0].ImagePullPolicy = apiv1.PullPolicy(i.ImagePullPolicy)
i.MustInstallResource(kube.MustToUnstructured(&argoCDServerServiceAccount))
i.MustInstallResource(kube.MustToUnstructured(&argoCDServerControllerRole))
i.MustInstallResource(kube.MustToUnstructured(&argoCDServerControllerRoleBinding))
i.MustInstallResource(kube.MustToUnstructured(&argoCDServerControllerDeployment))
i.MustInstallResource(kube.MustToUnstructured(&argoCDServerService))
}
func (i *Installer) InstallArgoCDRepoServer() {
var argoCDRepoServerControllerDeployment appsv1beta2.Deployment
var argoCDRepoServerService apiv1.Service
i.unmarshalManifest("05a_argocd-repo-server-deployment.yaml", &argoCDRepoServerControllerDeployment)
i.unmarshalManifest("05b_argocd-repo-server-service.yaml", &argoCDRepoServerService)
argoCDRepoServerControllerDeployment.Spec.Template.Spec.Containers[0].Image = i.RepoServerImage
argoCDRepoServerControllerDeployment.Spec.Template.Spec.Containers[0].ImagePullPolicy = apiv1.PullPolicy(i.ImagePullPolicy)
i.MustInstallResource(kube.MustToUnstructured(&argoCDRepoServerControllerDeployment))
i.MustInstallResource(kube.MustToUnstructured(&argoCDRepoServerService))
}
func (i *Installer) unmarshalManifest(fileName string, obj interface{}) {
yamlBytes, err := i.box.MustBytes(fileName)
errors.CheckError(err)
err = yaml.Unmarshal(yamlBytes, obj)
errors.CheckError(err)
}
func (i *Installer) MustInstallResource(obj *unstructured.Unstructured) *unstructured.Unstructured {
obj, err := i.InstallResource(obj)
errors.CheckError(err)
return obj
}
func isNamespaced(obj *unstructured.Unstructured) bool {
switch obj.GetKind() {
case "Namespace", "ClusterRole", "ClusterRoleBinding", "CustomResourceDefinition":
return false
}
return true
}
// InstallResource creates or updates a resource. If installed resource is up-to-date, does nothing
func (i *Installer) InstallResource(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
if isNamespaced(obj) {
obj.SetNamespace(i.Namespace)
}
// remove 'creationTimestamp' and 'status' fields from object so that the diff will not be modified
obj.SetCreationTimestamp(metav1.Time{})
delete(obj.Object, "status")
if i.DryRun {
printYAML(obj)
return nil, nil
}
gvk := obj.GroupVersionKind()
dclient, err := i.dynClientPool.ClientForGroupVersionKind(gvk)
if err != nil {
return nil, err
}
apiResource, err := kube.ServerResourceForGroupVersionKind(i.disco, gvk)
if err != nil {
return nil, err
}
reIf := dclient.Resource(apiResource, i.Namespace)
liveObj, err := reIf.Create(obj)
if err == nil {
fmt.Printf("%s '%s' created\n", liveObj.GetKind(), liveObj.GetName())
return liveObj, nil
}
if !apierr.IsAlreadyExists(err) {
return nil, err
}
liveObj, err = reIf.Get(obj.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
diffRes := diff.Diff(obj, liveObj)
if !diffRes.Modified {
fmt.Printf("%s '%s' up-to-date\n", liveObj.GetKind(), liveObj.GetName())
return liveObj, nil
}
if !i.Upgrade {
log.Println(diffRes.ASCIIFormat(obj, formatter.AsciiFormatterConfig{}))
return nil, fmt.Errorf("%s '%s' already exists. Rerun with --upgrade to update", obj.GetKind(), obj.GetName())
}
liveObj, err = reIf.Update(obj)
if err != nil {
return nil, err
}
fmt.Printf("%s '%s' updated\n", liveObj.GetKind(), liveObj.GetName())
return liveObj, nil
}
func (i *Installer) MustUninstallResource(obj *unstructured.Unstructured) {
err := i.UninstallResource(obj)
errors.CheckError(err)
}
// UninstallResource deletes a resource from the cluster
func (i *Installer) UninstallResource(obj *unstructured.Unstructured) error {
if isNamespaced(obj) {
obj.SetNamespace(i.Namespace)
}
gvk := obj.GroupVersionKind()
dclient, err := i.dynClientPool.ClientForGroupVersionKind(gvk)
if err != nil {
return err
}
apiResource, err := kube.ServerResourceForGroupVersionKind(i.disco, gvk)
if err != nil {
return err
}
reIf := dclient.Resource(apiResource, i.Namespace)
deletePolicy := metav1.DeletePropagationForeground
err = reIf.Delete(obj.GetName(), &metav1.DeleteOptions{PropagationPolicy: &deletePolicy})
if err != nil {
if apierr.IsNotFound(err) {
fmt.Printf("%s '%s' not found\n", obj.GetKind(), obj.GetName())
return nil
}
return err
}
fmt.Printf("%s '%s' deleted\n", obj.GetKind(), obj.GetName())
return nil
}
func printYAML(obj interface{}) {
objBytes, err := yaml.Marshal(obj)
if err != nil {
log.Fatalf("Failed to marshal %v", obj)
}
fmt.Printf("---\n%s\n", string(objBytes))
}