Skip to content

Commit da2fb45

Browse files
author
Christian Hernandez
committed
first PoC done
Signed-off-by: Christian Hernandez <christian@codefresh.io>
1 parent 6457535 commit da2fb45

File tree

5 files changed

+69
-76
lines changed

5 files changed

+69
-76
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# mta
2-
mta exports and imports
2+
3+
The `mta` cli will export Flux components to Argo CD consumable
4+
CRs. This can be used in order to help migrating from Flux to Argo
5+
CD. This is in the "proof of concept" phase and I make no guarantees
6+
7+
Currently working:
8+
9+
- [*] Migrate Kustomizations
10+
- [ ] Migrate HelmReleases
11+
- [ ] Auto Scan/Migrate
12+
- [ ] Uninstall Flux
13+
14+
# Installation
15+
16+
TBD
17+
18+
# Quickstarts
19+
20+
Quickstarts to test the functionality after downloading the CLI
21+
22+
* [Kustomizations](#)

cmd/kustomization.go

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 Christian Hernandez christian@email.com
2+
Copyright © 2022 Christian Hernandez christian@chernand.io
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"encoding/base64"
2121
"os"
22+
"strings"
2223

2324
"github.com/christianh814/mta/pkg/utils"
2425
"github.com/christianh814/mta/vars/templates"
@@ -34,14 +35,17 @@ import (
3435

3536
// kustomizationCmd represents the kustomization command
3637
var kustomizationCmd = &cobra.Command{
37-
Use: "kustomization",
38-
Short: "A brief description of your command",
39-
Long: `A longer description that spans multiple lines and likely contains examples
40-
and usage of using your command. For example:
41-
42-
Cobra is a CLI library for Go that empowers applications.
43-
This application is a tool to generate the needed files
44-
to quickly create a Cobra application.`,
38+
Use: "kustomization",
39+
Aliases: []string{"k"},
40+
Short: "Exports a Kustomization into an ApplicationSet",
41+
Long: `This is a migration tool that helps you move your Flux Kustomizations
42+
into an Argo CD ApplicationSet. Example:
43+
44+
mta kustomization --name=mykustomization --namespace=flux-system | kubectl apply -n argocd -f -
45+
46+
This utilty exports the named Kustomization and the source Git repo and
47+
creates a manifests to stdout, which you can pipe into an apply command
48+
with kubectl.`,
4549
Run: func(cmd *cobra.Command, args []string) {
4650
// Get the options from the CLI
4751
kubeConfig, err := cmd.Flags().GetString("kubeconfig")
@@ -99,28 +103,39 @@ to quickly create a Cobra application.`,
99103
log.Fatal()
100104
}
101105

102-
//TODO: Figure out how to "sanitize" gitSource.Spec.URL so that it plays nice with Argo CD Applicationsets
103-
/*
104-
https://go.dev/play/p/BKOC8-SJmH3
105-
*/
106+
// Argo CD ApplicationSet is sensitive about how you give it paths in the Git Dir generator. We need to figure some things out
107+
var sourcePath string
108+
var sourcePathExclude string
109+
110+
spl := strings.SplitAfter(kustomization.Spec.Path, "./")
111+
112+
if len(spl[1]) == 0 {
113+
sourcePath = `'*'`
114+
sourcePathExclude = "flux-system"
115+
} else {
116+
sourcePath = spl[1] + "/*"
117+
sourcePathExclude = spl[1] + "/flux-system"
118+
}
106119

107120
// Generate Template YAML based on things we've figured out
108121
argoCDYAMLVars := struct {
109-
SSHPrivateKey string
110-
GitOpsRepoB64 string
111-
SourcePath string
112-
GitOpsRepo string
113-
GitOpsRepoBranch string
114-
RawPathBasename string
115-
RawPath string
122+
SSHPrivateKey string
123+
GitOpsRepoB64 string
124+
SourcePath string
125+
SourcePathExclude string
126+
GitOpsRepo string
127+
GitOpsRepoBranch string
128+
RawPathBasename string
129+
RawPath string
116130
}{
117-
SSHPrivateKey: base64.StdEncoding.EncodeToString(secret.Data["identity"]),
118-
GitOpsRepoB64: base64.StdEncoding.EncodeToString([]byte(gitSource.Spec.URL)),
119-
SourcePath: kustomization.Spec.Path,
120-
GitOpsRepo: gitSource.Spec.URL,
121-
GitOpsRepoBranch: gitSource.Spec.Reference.Branch,
122-
RawPathBasename: `'{{path.basename}}'`,
123-
RawPath: `'{{path}}'`,
131+
SSHPrivateKey: base64.StdEncoding.EncodeToString(secret.Data["identity"]),
132+
GitOpsRepoB64: base64.StdEncoding.EncodeToString([]byte(gitSource.Spec.URL)),
133+
SourcePath: sourcePath,
134+
SourcePathExclude: sourcePathExclude,
135+
GitOpsRepo: gitSource.Spec.URL,
136+
GitOpsRepoBranch: gitSource.Spec.Reference.Branch,
137+
RawPathBasename: `'{{path.basename}}'`,
138+
RawPath: `'{{path}}'`,
124139
}
125140
//Send the YAML to stdout
126141
err = utils.WriteTemplate(templates.ArgoCDMigrationYAML, argoCDYAMLVars)

cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 Christian Hernandez christian@email.com
2+
Copyright © 2022 Christian Hernandez christian@chernand.io
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -32,11 +32,11 @@ var rootCmd = &cobra.Command{
3232
Use: "mta",
3333
Short: "This commands turns Flux Kustomizations and HelmReleases into Argo CD Applications",
3434
Long: `This is a migration tool that helps you move your Flux Kustomizations and HelmReleases
35-
into Argo CD Applications example:
35+
into Argo CD ApplicationSet. Kustomization example:
3636
3737
mta kustomization --name=mykustomization --namespace=flux-system | kubectl apply -n argocd -f -
3838
39-
This utilty exports the named Kustomization and the source Git repo and
39+
This utilty exports the named Kustomization or HelmRelease and the source Git repo and
4040
creates a manifests to stdout, which you can pipe into an apply command
4141
with kubectl.`,
4242
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 Christian Hernandez christian@email.com
2+
Copyright © 2022 Christian Hernandez christian@chernand.io
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

vars/templates/templates.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,5 @@
11
package templates
22

3-
var ArgoCDMigrationYAMLOLD string = `apiVersion: v1
4-
kind: Secret
5-
metadata:
6-
name: mta-migration
7-
namespace: argocd
8-
labels:
9-
argocd.argoproj.io/secret-type: repository
10-
type: Opaque
11-
data:
12-
sshPrivateKey: {{.SSHPrivateKey}}
13-
type: Z2l0
14-
url: {{.GitOpsRepoB64}}
15-
---
16-
apiVersion: argoproj.io/v1alpha1
17-
kind: Application
18-
metadata:
19-
name: mta-migration
20-
namespace: argocd
21-
spec:
22-
destination:
23-
server: https://kubernetes.default.svc
24-
project: default
25-
source:
26-
path: {{.SourcePath}}
27-
repoURL: {{.GitOpsRepo}}
28-
targetRevision: {{.GitOpsRepoBranch}}
29-
directory:
30-
recurse: true
31-
syncPolicy:
32-
syncOptions:
33-
- Validate=false
34-
- CreateNamespace=true
35-
automated:
36-
prune: true
37-
selfHeal: true
38-
retry:
39-
limit: 5
40-
backoff:
41-
duration: 5s
42-
factor: 2
43-
maxDuration: 3m
44-
`
453
var ArgoCDMigrationYAML string = `apiVersion: v1
464
kind: Secret
475
metadata:
@@ -66,8 +24,8 @@ spec:
6624
repoURL: {{.GitOpsRepo}}
6725
revision: {{.GitOpsRepoBranch}}
6826
directories:
69-
- path: {{.SourcePath}}/*
70-
- path: {{.SourcePath}}/flux-system
27+
- path: {{.SourcePath}}
28+
- path: {{.SourcePathExclude}}
7129
exclude: true
7230
template:
7331
metadata:

0 commit comments

Comments
 (0)