Skip to content

Commit

Permalink
updated to not use a stringed YAML
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hernandez <[email protected]>
  • Loading branch information
christianh814 committed Nov 24, 2022
1 parent 83b3d37 commit b6b209c
Show file tree
Hide file tree
Showing 8 changed files with 1,271 additions and 147 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Install the `mta` binary from the releases page
__Linux/Mac OS X86_64__

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

__Mac OS Apple Silicon__

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

Make sure it's executable
Expand Down
54 changes: 24 additions & 30 deletions cmd/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ package cmd
import (
"context"
"encoding/json"
"os"

yaml "sigs.k8s.io/yaml"

"github.com/christianh814/mta/pkg/utils"
"github.com/christianh814/mta/vars/templates"
"github.com/christianh814/mta/pkg/argo"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/tools/clientcmd"
client "sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -108,13 +109,6 @@ with kubectl.`,
log.Fatal(err)
}

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

// Generate Template YAML based on things we've figured out
argoCDHelmYAMLVars := struct {
HelmAppName string
HelmAppNamespace string
HelmChart string
HelmRepoUrl string
HelmTargetRevision string
HelmValues string
HelmCreateNamespace string
ArgoCDNamespace string
}{
HelmAppName: helmAppName,
HelmAppNamespace: helmAppNamespace,
HelmChart: helmChart,
HelmRepoUrl: helmRepoUrl,
HelmTargetRevision: helmTargetRevision,
HelmValues: helmValues,
HelmCreateNamespace: helmCreateNamespace,
ArgoCDNamespace: argoCDNamespace,
// Generate the Argo CD Helm Application
helmApp := argo.ArgoCdHelmApplication{
Name: helmRelease.Spec.Chart.Spec.Chart + "-" + helmRelease.Name,
Namespace: argoCDNamespace,
DestinationNamespace: helmRelease.Spec.TargetNamespace,
DestinationServer: "https://kubernetes.default.svc",
Project: "default",
HelmChart: helmRelease.Spec.Chart.Spec.Chart,
HelmRepo: helmRepo.Spec.URL,
HelmTargetRevision: helmRelease.Spec.Chart.Spec.Version,
HelmValues: string(yaml),
HelmCreateNamespace: helmCreateNamespace,
}

//Send the YAML to stdout
err = utils.WriteTemplate(templates.ArgoCDHelmMigrationYAML, argoCDHelmYAMLVars)
helmArgoCdApp, err := argo.GenArgoCdHelmApplication(helmApp)
if err != nil {
log.Fatal(err)
}

// Set the printer type to YAML
printr := printers.NewTypeSetter(k.Scheme()).ToPrinter(&printers.YAMLPrinter{})

// print the AppSet YAML to Strdout
if err := printr.PrintObj(helmArgoCdApp, os.Stdout); err != nil {
log.Fatal(err)
}

},
}

Expand Down
66 changes: 40 additions & 26 deletions cmd/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ package cmd

import (
"context"
"encoding/base64"
"os"
"strings"

"github.com/christianh814/mta/pkg/argo"
"github.com/christianh814/mta/pkg/utils"
"github.com/christianh814/mta/vars/templates"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/tools/clientcmd"
client "sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -115,41 +117,53 @@ with kubectl.`,
spl := strings.SplitAfter(kustomization.Spec.Path, "./")

if len(spl[1]) == 0 {
sourcePath = `'*'`
sourcePath = `*`
sourcePathExclude = "flux-system"
} else {
sourcePath = spl[1] + "/*"
sourcePathExclude = spl[1] + "/flux-system"
}

// Generate Template YAML based on things we've figured out
argoCDYAMLVars := struct {
SSHPrivateKey string
GitOpsRepoB64 string
SourcePath string
SourcePathExclude string
GitOpsRepo string
GitOpsRepoBranch string
RawPathBasename string
RawPath string
ArgoCDNamespace string
}{
SSHPrivateKey: base64.StdEncoding.EncodeToString(secret.Data["identity"]),
GitOpsRepoB64: base64.StdEncoding.EncodeToString([]byte(gitSource.Spec.URL)),
SourcePath: sourcePath,
SourcePathExclude: sourcePathExclude,
GitOpsRepo: gitSource.Spec.URL,
GitOpsRepoBranch: gitSource.Spec.Reference.Branch,
RawPathBasename: `'{{path.basename}}'`,
RawPath: `'{{path}}'`,
ArgoCDNamespace: argoCDNamespace,
// Generate the ApplicationSet manifest based on the struct
applicationSet := argo.GitDirApplicationSet{
Namespace: argoCDNamespace,
GitRepoURL: gitSource.Spec.URL,
GitRepoRevision: gitSource.Spec.Reference.Branch,
GitIncludeDir: sourcePath,
GitExcludeDir: sourcePathExclude,
AppName: "{{path.basename}}",
AppProject: "default",
AppRepoURL: gitSource.Spec.URL,
AppTargetRevision: gitSource.Spec.Reference.Branch,
AppPath: "{{path}}",
AppDestinationServer: "https://kubernetes.default.svc",
AppDestinationNamespace: kustomization.Spec.TargetNamespace,
SSHPrivateKey: string(secret.Data["identity"]),
GitOpsRepo: gitSource.Spec.URL,
}
//Send the YAML to stdout
err = utils.WriteTemplate(templates.ArgoCDAppSetMigrationYAML, argoCDYAMLVars)

appset, err := argo.GenGitDirAppSet(applicationSet)
if err != nil {
log.Fatal(err)
}

// Generate the ApplicationSet Secret and set the GVK
appsetSecret := utils.GenK8SSecret(applicationSet)
appsetSecret.SetGroupVersionKind(schema.GroupVersionKind{}.GroupKind().WithVersion("v1").GroupVersion().WithKind("Secret"))

// Set the printer type to YAML
printr := printers.NewTypeSetter(k.Scheme()).ToPrinter(&printers.YAMLPrinter{})

// Print the AppSet secret to Stdout
if err := printr.PrintObj(appsetSecret, os.Stdout); err != nil {
log.Fatal(err)
}

// print the AppSet YAML to Strdout
if err := printr.PrintObj(appset, os.Stdout); err != nil {
log.Fatal(err)
}

},
}

Expand Down
101 changes: 101 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,37 @@ module github.com/christianh814/mta

go 1.19

replace (
k8s.io/api => k8s.io/api v0.24.2
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.2
k8s.io/apimachinery => k8s.io/apimachinery v0.24.2
k8s.io/apiserver => k8s.io/apiserver v0.24.2
k8s.io/cli-runtime => k8s.io/cli-runtime v0.24.2
k8s.io/client-go => k8s.io/client-go v0.24.2
k8s.io/cloud-provider => k8s.io/cloud-provider v0.24.2
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.24.2
k8s.io/code-generator => k8s.io/code-generator v0.24.2
k8s.io/component-base => k8s.io/component-base v0.24.2
k8s.io/component-helpers => k8s.io/component-helpers v0.24.2
k8s.io/controller-manager => k8s.io/controller-manager v0.24.2
k8s.io/cri-api => k8s.io/cri-api v0.24.2
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.24.2
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.24.2
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.24.2
k8s.io/kube-proxy => k8s.io/kube-proxy v0.24.2
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.24.2
k8s.io/kubectl => k8s.io/kubectl v0.24.2
k8s.io/kubelet => k8s.io/kubelet v0.24.2
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.24.2
k8s.io/metrics => k8s.io/metrics v0.24.2
k8s.io/mount-utils => k8s.io/mount-utils v0.24.2
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.24.2
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.24.2
)

require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/argoproj/argo-cd/v2 v2.5.2
github.com/fluxcd/helm-controller/api v0.27.0
github.com/fluxcd/kustomize-controller/api v0.31.0
github.com/fluxcd/source-controller/api v0.32.1
Expand All @@ -18,69 +47,141 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Microsoft/go-winio v0.4.17 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/argoproj/gitops-engine v0.7.1-0.20221004132320-98ccd3d43fd9 // indirect
github.com/argoproj/pkg v0.11.1-0.20211203175135-36c59d8fafe0 // indirect
github.com/bombsimon/logrusr/v2 v2.0.1 // indirect
github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chai2010/gettext-go v0.0.0-20170215093142-bf70f2a70fb1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fluxcd/pkg/apis/acl v0.1.0 // indirect
github.com/fluxcd/pkg/apis/kustomize v0.7.0 // indirect
github.com/fluxcd/pkg/apis/meta v0.18.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-redis/cache/v8 v8.4.2 // indirect
github.com/go-redis/redis/v8 v8.11.3 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-github/v41 v41.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/klauspost/compress v1.13.5 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/vmihailenco/go-tinylfu v0.2.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.25.4 // indirect
k8s.io/apiextensions-apiserver v0.25.4 // indirect
k8s.io/apiserver v0.24.2 // indirect
k8s.io/cli-runtime v0.24.2 // indirect
k8s.io/component-base v0.25.0 // indirect
k8s.io/component-helpers v0.24.2 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-aggregator v0.24.2 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/kubectl v0.24.2 // indirect
k8s.io/kubernetes v1.24.2 // indirect
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.11.4 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit b6b209c

Please sign in to comment.