Skip to content

Commit b6b209c

Browse files
committed
updated to not use a stringed YAML
Signed-off-by: Christian Hernandez <[email protected]>
1 parent 83b3d37 commit b6b209c

File tree

8 files changed

+1271
-147
lines changed

8 files changed

+1271
-147
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ Install the `mta` binary from the releases page
1919
__Linux/Mac OS X86_64__
2020

2121
```shell
22-
sudo wget -O /usr/local/bin/mta https://github.com/christianh814/mta/releases/download/v0.0.3/mta-amd64-$(uname -s | tr [:upper:] [:lower:])
22+
sudo wget -O /usr/local/bin/mta https://github.com/christianh814/mta/releases/download/v0.0.4/mta-amd64-$(uname -s | tr [:upper:] [:lower:])
2323
```
2424

2525
__Mac OS Apple Silicon__
2626

2727
```shell
28-
sudo wget -O /usr/local/bin/mta https://github.com/christianh814/mta/releases/download/v0.0.3/mta-arm64-darwin
28+
sudo wget -O /usr/local/bin/mta https://github.com/christianh814/mta/releases/download/v0.0.4/mta-arm64-darwin
2929
```
3030

3131
Make sure it's executable

cmd/helmrelease.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ package cmd
1818
import (
1919
"context"
2020
"encoding/json"
21+
"os"
2122

2223
yaml "sigs.k8s.io/yaml"
2324

24-
"github.com/christianh814/mta/pkg/utils"
25-
"github.com/christianh814/mta/vars/templates"
25+
"github.com/christianh814/mta/pkg/argo"
2626
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
2727
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
2828
log "github.com/sirupsen/logrus"
2929
"github.com/spf13/cobra"
3030
"k8s.io/apimachinery/pkg/runtime"
31+
"k8s.io/cli-runtime/pkg/printers"
3132
"k8s.io/client-go/tools/clientcmd"
3233
client "sigs.k8s.io/controller-runtime/pkg/client"
3334
)
@@ -108,13 +109,6 @@ with kubectl.`,
108109
log.Fatal(err)
109110
}
110111

111-
// Variables based on what we got from the cluster
112-
helmAppName := helmRelease.Spec.Chart.Spec.Chart + "-" + helmRelease.Name
113-
helmAppNamespace := helmRelease.Spec.TargetNamespace
114-
helmChart := helmRelease.Spec.Chart.Spec.Chart
115-
helmRepoUrl := helmRepo.Spec.URL
116-
helmTargetRevision := helmRelease.Spec.Chart.Spec.Version
117-
helmValues := string(yaml)
118112
// Createnamespace comes out as a Bool, need to convert into a string
119113
var helmCreateNamespace string
120114
if helmRelease.Spec.Install.CreateNamespace {
@@ -123,33 +117,33 @@ with kubectl.`,
123117
helmCreateNamespace = "false"
124118
}
125119

126-
// Generate Template YAML based on things we've figured out
127-
argoCDHelmYAMLVars := struct {
128-
HelmAppName string
129-
HelmAppNamespace string
130-
HelmChart string
131-
HelmRepoUrl string
132-
HelmTargetRevision string
133-
HelmValues string
134-
HelmCreateNamespace string
135-
ArgoCDNamespace string
136-
}{
137-
HelmAppName: helmAppName,
138-
HelmAppNamespace: helmAppNamespace,
139-
HelmChart: helmChart,
140-
HelmRepoUrl: helmRepoUrl,
141-
HelmTargetRevision: helmTargetRevision,
142-
HelmValues: helmValues,
143-
HelmCreateNamespace: helmCreateNamespace,
144-
ArgoCDNamespace: argoCDNamespace,
120+
// Generate the Argo CD Helm Application
121+
helmApp := argo.ArgoCdHelmApplication{
122+
Name: helmRelease.Spec.Chart.Spec.Chart + "-" + helmRelease.Name,
123+
Namespace: argoCDNamespace,
124+
DestinationNamespace: helmRelease.Spec.TargetNamespace,
125+
DestinationServer: "https://kubernetes.default.svc",
126+
Project: "default",
127+
HelmChart: helmRelease.Spec.Chart.Spec.Chart,
128+
HelmRepo: helmRepo.Spec.URL,
129+
HelmTargetRevision: helmRelease.Spec.Chart.Spec.Version,
130+
HelmValues: string(yaml),
131+
HelmCreateNamespace: helmCreateNamespace,
145132
}
146133

147-
//Send the YAML to stdout
148-
err = utils.WriteTemplate(templates.ArgoCDHelmMigrationYAML, argoCDHelmYAMLVars)
134+
helmArgoCdApp, err := argo.GenArgoCdHelmApplication(helmApp)
149135
if err != nil {
150136
log.Fatal(err)
151137
}
152138

139+
// Set the printer type to YAML
140+
printr := printers.NewTypeSetter(k.Scheme()).ToPrinter(&printers.YAMLPrinter{})
141+
142+
// print the AppSet YAML to Strdout
143+
if err := printr.PrintObj(helmArgoCdApp, os.Stdout); err != nil {
144+
log.Fatal(err)
145+
}
146+
153147
},
154148
}
155149

cmd/kustomization.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ package cmd
1717

1818
import (
1919
"context"
20-
"encoding/base64"
20+
"os"
2121
"strings"
2222

23+
"github.com/christianh814/mta/pkg/argo"
2324
"github.com/christianh814/mta/pkg/utils"
24-
"github.com/christianh814/mta/vars/templates"
2525
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
2626
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
2727
log "github.com/sirupsen/logrus"
2828
"github.com/spf13/cobra"
2929
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/runtime"
31+
"k8s.io/apimachinery/pkg/runtime/schema"
32+
"k8s.io/cli-runtime/pkg/printers"
3133
"k8s.io/client-go/tools/clientcmd"
3234
client "sigs.k8s.io/controller-runtime/pkg/client"
3335
)
@@ -115,41 +117,53 @@ with kubectl.`,
115117
spl := strings.SplitAfter(kustomization.Spec.Path, "./")
116118

117119
if len(spl[1]) == 0 {
118-
sourcePath = `'*'`
120+
sourcePath = `*`
119121
sourcePathExclude = "flux-system"
120122
} else {
121123
sourcePath = spl[1] + "/*"
122124
sourcePathExclude = spl[1] + "/flux-system"
123125
}
124126

125-
// Generate Template YAML based on things we've figured out
126-
argoCDYAMLVars := struct {
127-
SSHPrivateKey string
128-
GitOpsRepoB64 string
129-
SourcePath string
130-
SourcePathExclude string
131-
GitOpsRepo string
132-
GitOpsRepoBranch string
133-
RawPathBasename string
134-
RawPath string
135-
ArgoCDNamespace string
136-
}{
137-
SSHPrivateKey: base64.StdEncoding.EncodeToString(secret.Data["identity"]),
138-
GitOpsRepoB64: base64.StdEncoding.EncodeToString([]byte(gitSource.Spec.URL)),
139-
SourcePath: sourcePath,
140-
SourcePathExclude: sourcePathExclude,
141-
GitOpsRepo: gitSource.Spec.URL,
142-
GitOpsRepoBranch: gitSource.Spec.Reference.Branch,
143-
RawPathBasename: `'{{path.basename}}'`,
144-
RawPath: `'{{path}}'`,
145-
ArgoCDNamespace: argoCDNamespace,
127+
// Generate the ApplicationSet manifest based on the struct
128+
applicationSet := argo.GitDirApplicationSet{
129+
Namespace: argoCDNamespace,
130+
GitRepoURL: gitSource.Spec.URL,
131+
GitRepoRevision: gitSource.Spec.Reference.Branch,
132+
GitIncludeDir: sourcePath,
133+
GitExcludeDir: sourcePathExclude,
134+
AppName: "{{path.basename}}",
135+
AppProject: "default",
136+
AppRepoURL: gitSource.Spec.URL,
137+
AppTargetRevision: gitSource.Spec.Reference.Branch,
138+
AppPath: "{{path}}",
139+
AppDestinationServer: "https://kubernetes.default.svc",
140+
AppDestinationNamespace: kustomization.Spec.TargetNamespace,
141+
SSHPrivateKey: string(secret.Data["identity"]),
142+
GitOpsRepo: gitSource.Spec.URL,
146143
}
147-
//Send the YAML to stdout
148-
err = utils.WriteTemplate(templates.ArgoCDAppSetMigrationYAML, argoCDYAMLVars)
144+
145+
appset, err := argo.GenGitDirAppSet(applicationSet)
149146
if err != nil {
150147
log.Fatal(err)
151148
}
152149

150+
// Generate the ApplicationSet Secret and set the GVK
151+
appsetSecret := utils.GenK8SSecret(applicationSet)
152+
appsetSecret.SetGroupVersionKind(schema.GroupVersionKind{}.GroupKind().WithVersion("v1").GroupVersion().WithKind("Secret"))
153+
154+
// Set the printer type to YAML
155+
printr := printers.NewTypeSetter(k.Scheme()).ToPrinter(&printers.YAMLPrinter{})
156+
157+
// Print the AppSet secret to Stdout
158+
if err := printr.PrintObj(appsetSecret, os.Stdout); err != nil {
159+
log.Fatal(err)
160+
}
161+
162+
// print the AppSet YAML to Strdout
163+
if err := printr.PrintObj(appset, os.Stdout); err != nil {
164+
log.Fatal(err)
165+
}
166+
153167
},
154168
}
155169

go.mod

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,37 @@ module github.com/christianh814/mta
22

33
go 1.19
44

5+
replace (
6+
k8s.io/api => k8s.io/api v0.24.2
7+
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.2
8+
k8s.io/apimachinery => k8s.io/apimachinery v0.24.2
9+
k8s.io/apiserver => k8s.io/apiserver v0.24.2
10+
k8s.io/cli-runtime => k8s.io/cli-runtime v0.24.2
11+
k8s.io/client-go => k8s.io/client-go v0.24.2
12+
k8s.io/cloud-provider => k8s.io/cloud-provider v0.24.2
13+
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.24.2
14+
k8s.io/code-generator => k8s.io/code-generator v0.24.2
15+
k8s.io/component-base => k8s.io/component-base v0.24.2
16+
k8s.io/component-helpers => k8s.io/component-helpers v0.24.2
17+
k8s.io/controller-manager => k8s.io/controller-manager v0.24.2
18+
k8s.io/cri-api => k8s.io/cri-api v0.24.2
19+
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.24.2
20+
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.24.2
21+
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.24.2
22+
k8s.io/kube-proxy => k8s.io/kube-proxy v0.24.2
23+
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.24.2
24+
k8s.io/kubectl => k8s.io/kubectl v0.24.2
25+
k8s.io/kubelet => k8s.io/kubelet v0.24.2
26+
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.24.2
27+
k8s.io/metrics => k8s.io/metrics v0.24.2
28+
k8s.io/mount-utils => k8s.io/mount-utils v0.24.2
29+
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.24.2
30+
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.24.2
31+
)
32+
533
require (
634
github.com/Masterminds/sprig/v3 v3.2.2
35+
github.com/argoproj/argo-cd/v2 v2.5.2
736
github.com/fluxcd/helm-controller/api v0.27.0
837
github.com/fluxcd/kustomize-controller/api v0.31.0
938
github.com/fluxcd/source-controller/api v0.32.1
@@ -18,69 +47,141 @@ require (
1847
)
1948

2049
require (
50+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
51+
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
2152
github.com/Masterminds/goutils v1.1.1 // indirect
2253
github.com/Masterminds/semver/v3 v3.1.1 // indirect
54+
github.com/Microsoft/go-winio v0.4.17 // indirect
55+
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
2356
github.com/PuerkitoBio/purell v1.1.1 // indirect
2457
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
58+
github.com/acomagu/bufpipe v1.0.3 // indirect
59+
github.com/argoproj/gitops-engine v0.7.1-0.20221004132320-98ccd3d43fd9 // indirect
60+
github.com/argoproj/pkg v0.11.1-0.20211203175135-36c59d8fafe0 // indirect
61+
github.com/bombsimon/logrusr/v2 v2.0.1 // indirect
62+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 // indirect
63+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
64+
github.com/chai2010/gettext-go v0.0.0-20170215093142-bf70f2a70fb1 // indirect
2565
github.com/davecgh/go-spew v1.1.1 // indirect
66+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
67+
github.com/docker/distribution v2.8.1+incompatible // indirect
2668
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
69+
github.com/emirpasic/gods v1.12.0 // indirect
70+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
2771
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
72+
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
73+
github.com/fatih/camelcase v1.0.0 // indirect
2874
github.com/fluxcd/pkg/apis/acl v0.1.0 // indirect
2975
github.com/fluxcd/pkg/apis/kustomize v0.7.0 // indirect
3076
github.com/fluxcd/pkg/apis/meta v0.18.0 // indirect
3177
github.com/fsnotify/fsnotify v1.6.0 // indirect
78+
github.com/fvbommel/sortorder v1.0.1 // indirect
79+
github.com/ghodss/yaml v1.0.0 // indirect
80+
github.com/go-errors/errors v1.0.1 // indirect
81+
github.com/go-git/gcfg v1.5.0 // indirect
82+
github.com/go-git/go-billy/v5 v5.3.1 // indirect
83+
github.com/go-git/go-git/v5 v5.4.2 // indirect
3284
github.com/go-logr/logr v1.2.3 // indirect
3385
github.com/go-openapi/jsonpointer v0.19.5 // indirect
3486
github.com/go-openapi/jsonreference v0.19.5 // indirect
3587
github.com/go-openapi/swag v0.19.14 // indirect
88+
github.com/go-redis/cache/v8 v8.4.2 // indirect
89+
github.com/go-redis/redis/v8 v8.11.3 // indirect
90+
github.com/gobwas/glob v0.2.3 // indirect
3691
github.com/gogo/protobuf v1.3.2 // indirect
92+
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
3793
github.com/golang/protobuf v1.5.2 // indirect
94+
github.com/google/btree v1.0.1 // indirect
3895
github.com/google/gnostic v0.5.7-v3refs // indirect
96+
github.com/google/go-cmp v0.5.9 // indirect
97+
github.com/google/go-github/v41 v41.0.0 // indirect
98+
github.com/google/go-querystring v1.1.0 // indirect
3999
github.com/google/gofuzz v1.2.0 // indirect
100+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
40101
github.com/google/uuid v1.1.2 // indirect
102+
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
41103
github.com/hashicorp/hcl v1.0.0 // indirect
42104
github.com/huandu/xstrings v1.3.1 // indirect
43105
github.com/imdario/mergo v0.3.12 // indirect
44106
github.com/inconshreveable/mousetrap v1.0.1 // indirect
107+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
108+
github.com/jonboulle/clockwork v0.2.2 // indirect
45109
github.com/josharian/intern v1.0.0 // indirect
46110
github.com/json-iterator/go v1.1.12 // indirect
111+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
112+
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
113+
github.com/klauspost/compress v1.13.5 // indirect
114+
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
47115
github.com/magiconair/properties v1.8.6 // indirect
48116
github.com/mailru/easyjson v0.7.6 // indirect
49117
github.com/mattn/go-runewidth v0.0.13 // indirect
50118
github.com/mitchellh/copystructure v1.0.0 // indirect
119+
github.com/mitchellh/go-homedir v1.1.0 // indirect
120+
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
51121
github.com/mitchellh/mapstructure v1.5.0 // indirect
52122
github.com/mitchellh/reflectwalk v1.0.0 // indirect
123+
github.com/moby/spdystream v0.2.0 // indirect
124+
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
53125
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
54126
github.com/modern-go/reflect2 v1.0.2 // indirect
127+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
55128
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
129+
github.com/opencontainers/go-digest v1.0.0 // indirect
130+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
56131
github.com/pelletier/go-toml v1.9.5 // indirect
57132
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
133+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
58134
github.com/pkg/errors v0.9.1 // indirect
135+
github.com/pmezard/go-difflib v1.0.0 // indirect
59136
github.com/rivo/uniseg v0.2.0 // indirect
137+
github.com/robfig/cron v1.2.0 // indirect
138+
github.com/russross/blackfriday v1.5.2 // indirect
139+
github.com/sergi/go-diff v1.1.0 // indirect
60140
github.com/shopspring/decimal v1.2.0 // indirect
61141
github.com/spf13/afero v1.9.2 // indirect
62142
github.com/spf13/cast v1.5.0 // indirect
63143
github.com/spf13/jwalterweatherman v1.1.0 // indirect
64144
github.com/spf13/pflag v1.0.5 // indirect
145+
github.com/stretchr/testify v1.8.1 // indirect
65146
github.com/subosito/gotenv v1.4.1 // indirect
147+
github.com/vmihailenco/go-tinylfu v0.2.1 // indirect
148+
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
149+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
150+
github.com/xanzy/ssh-agent v0.3.0 // indirect
151+
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
152+
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
66153
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
154+
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 // indirect
67155
golang.org/x/net v0.2.0 // indirect
68156
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
157+
golang.org/x/sync v0.1.0 // indirect
69158
golang.org/x/sys v0.2.0 // indirect
70159
golang.org/x/term v0.2.0 // indirect
71160
golang.org/x/text v0.4.0 // indirect
72161
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
73162
google.golang.org/appengine v1.6.7 // indirect
163+
google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e // indirect
164+
google.golang.org/grpc v1.50.1 // indirect
74165
google.golang.org/protobuf v1.28.1 // indirect
75166
gopkg.in/inf.v0 v0.9.1 // indirect
76167
gopkg.in/ini.v1 v1.67.0 // indirect
168+
gopkg.in/warnings.v0 v0.1.2 // indirect
77169
gopkg.in/yaml.v2 v2.4.0 // indirect
78170
gopkg.in/yaml.v3 v3.0.1 // indirect
79171
k8s.io/api v0.25.4 // indirect
80172
k8s.io/apiextensions-apiserver v0.25.4 // indirect
173+
k8s.io/apiserver v0.24.2 // indirect
174+
k8s.io/cli-runtime v0.24.2 // indirect
175+
k8s.io/component-base v0.25.0 // indirect
176+
k8s.io/component-helpers v0.24.2 // indirect
81177
k8s.io/klog/v2 v2.80.1 // indirect
178+
k8s.io/kube-aggregator v0.24.2 // indirect
82179
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
180+
k8s.io/kubectl v0.24.2 // indirect
181+
k8s.io/kubernetes v1.24.2 // indirect
83182
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect
84183
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
184+
sigs.k8s.io/kustomize/api v0.11.4 // indirect
185+
sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect
85186
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
86187
)

0 commit comments

Comments
 (0)