11/*
2- Copyright © 2022 Christian Hernandez christian@email.com
2+ Copyright © 2022 Christian Hernandez christian@chernand.io
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you 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
3637var 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 )
0 commit comments