/
install.go
296 lines (251 loc) · 7.29 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"text/template"
"time"
"github.com/flux-subsystem-argo/cli/pkg/utils"
"github.com/fluxcd/flux2/v2/pkg/status"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/krusty"
)
var installCmd = &cobra.Command{
Use: "install",
Short: "Install the Flux Subsystem for Argo",
Long: fmt.Sprintf(`
# Install the Flux Subsystem for Argo
flamingo install --version=%s
# Install the Flux Subsystem for Argo with the anonymous UI enabled
flamingo install --version=%s --anonymous
`, installFlags.version, installFlags.version),
RunE: installCmdRun,
}
var installFlags struct {
version string
dev bool
anonymous bool
export bool
mode string
}
const CRDsOnlyMode = "crds-only"
const AllMode = "all"
const TenantMode = "tenant"
var validModes = map[string]bool{
CRDsOnlyMode: true,
AllMode: true,
TenantMode: true,
}
func init() {
installCmd.Flags().StringVarP(&installFlags.version, "version", "v", ServerVersion, "version of Flamingo to install")
installCmd.Flags().BoolVar(&installFlags.anonymous, "anonymous", false, "enable anonymous UI")
installCmd.Flags().StringVar(&installFlags.mode, "mode", AllMode, "installation mode [crds-only, all, tenant]")
installCmd.Flags().BoolVar(&installFlags.export, "export", false, "export manifests instead of installing")
rootCmd.AddCommand(installCmd)
}
func installCmdRun(cmd *cobra.Command, args []string) error {
if _, valid := validModes[installFlags.mode]; !valid {
return fmt.Errorf("invalid mode: %s", installFlags.mode)
}
if installFlags.export {
logger.stderr = io.Discard
}
if installFlags.version == "" {
return cmd.Help()
}
if strings.HasSuffix(installFlags.version, "-dev") {
installFlags.dev = true
}
logger.Actionf("obtaining version info")
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
// Set headers to prevent caching
req.Header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
req.Header.Set("Pragma", "no-cache")
// Execute the request
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Read the entire response body into a byte buffer
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var candidates CandidateList
if err := json.Unmarshal(buf, &candidates); err != nil {
return err
}
// filter out .Flamingo that ends with -dev if --dev flag is not set
if !installFlags.dev {
var filteredCandidates []Candidate
for _, candidate := range candidates.Candidates {
if !isDev(candidate) {
filteredCandidates = append(filteredCandidates, candidate)
}
}
candidates.Candidates = filteredCandidates
}
var candidate *Candidate
for i, c := range candidates.Candidates {
if c.Flamingo == installFlags.version {
candidate = &candidates.Candidates[i]
break
}
}
if candidate == nil {
return fmt.Errorf("version %s not found", installFlags.version)
}
if err := installFluxSubsystemForArgo(*candidate, installFlags.mode, installFlags.anonymous, installFlags.export); err != nil {
return err
}
if installFlags.export || installFlags.mode == CRDsOnlyMode {
// do not verify the installation if we are exporting the manifests or installing CRDs only
} else {
if err := verifyTheInstallation(); err != nil {
return err
}
}
return nil
}
func buildComponentObjectRefs(namespace string, components ...string) ([]object.ObjMetadata, error) {
var objRefs []object.ObjMetadata
for _, deployment := range components {
objRefs = append(objRefs, object.ObjMetadata{
Namespace: namespace,
Name: deployment,
GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"},
})
}
return objRefs, nil
}
func verifyTheInstallation() error {
logger.Waitingf("verifying installation")
kubeConfig, err := utils.KubeConfig(kubeconfigArgs, kubeclientOptions)
if err != nil {
return fmt.Errorf("install failed: %w", err)
}
statusChecker, err := status.NewStatusChecker(kubeConfig, 5*time.Second, rootArgs.timeout, logger)
if err != nil {
return fmt.Errorf("install failed: %w", err)
}
// we install Argo CD components in the application namespace (argocd), not the flux-system namespace
objectRefs, err := buildComponentObjectRefs(
rootArgs.applicationNamespace,
"argocd-redis",
"argocd-dex-server",
"argocd-repo-server",
"argocd-server",
"argocd-notifications-controller",
"argocd-applicationset-controller",
)
if err != nil {
return fmt.Errorf("install failed: %w", err)
}
if err := statusChecker.Assess(objectRefs...); err != nil {
return fmt.Errorf("install failed: %w", err)
}
logger.Successf("install finished")
return nil
}
func installFluxSubsystemForArgo(candidate Candidate, installMode string, anonymous bool, export bool) error {
logger.Generatef("generating manifests")
var tmpl string
switch installMode {
case AllMode:
tmpl = allInstallTemplate
case CRDsOnlyMode:
tmpl = crdsOnlyInstallTemplate
case TenantMode:
tmpl = namespaceInstallTemplate
}
patches := ""
if anonymous {
patches = anonymousPatches
}
var tpl bytes.Buffer
t, err := template.New("template").Parse(tmpl)
if err != nil {
return err
}
if err := t.Execute(&tpl, struct {
Flamingo string
ArgoCD string
Image string
Namespace string
AnonymousPatches string
}{
Flamingo: candidate.Flamingo,
ArgoCD: candidate.ArgoCD,
Image: candidate.Image,
Namespace: rootArgs.applicationNamespace,
AnonymousPatches: patches,
}); err != nil {
return err
}
// Use Kustomize (krusty) to build the kustomization
fSys := filesys.MakeFsInMemory()
kustomizationPath := "/app/kustomization.yaml"
fSys.WriteFile(kustomizationPath, tpl.Bytes())
namespacePath := "/app/namespace.yaml"
if installMode == CRDsOnlyMode {
fSys.WriteFile(namespacePath, []byte("# empty"))
} else {
fSys.WriteFile(namespacePath, []byte(fmt.Sprintf(namespaceTemplate, rootArgs.applicationNamespace)))
}
clusterPath := "/app/cluster.yaml"
if installMode == TenantMode {
fSys.WriteFile(clusterPath, []byte(
fmt.Sprintf(defaultClusterSecretTemplate,
rootArgs.applicationNamespace,
rootArgs.applicationNamespace,
rootArgs.applicationNamespace,
rootArgs.applicationNamespace,
rootArgs.applicationNamespace,
)))
} else {
fSys.WriteFile(clusterPath, []byte("# empty"))
}
opts := krusty.MakeDefaultOptions()
opts.Reorder = krusty.ReorderOptionLegacy
k := krusty.MakeKustomizer(opts)
m, err := k.Run(fSys, "/app")
if err != nil {
return err
}
yamlOutput, err := m.AsYaml()
if err != nil {
return err
}
logger.Successf("manifests build completed")
if export {
fmt.Println(string(yamlOutput))
return nil
}
if installMode == CRDsOnlyMode {
// install CRDs only
logger.Actionf("installing CRDs only")
} else {
// install everything
logger.Actionf("installing components in %s namespace", rootArgs.applicationNamespace)
}
applyOutput, err := utils.Apply(context.Background(), kubeconfigArgs, kubeclientOptions, yamlOutput)
if err != nil {
return fmt.Errorf("install failed: %w", err)
}
fmt.Fprintln(os.Stderr, applyOutput)
return nil
}