Skip to content

Commit

Permalink
added subcommand: scan
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hernandez <christian@chernand.io>
  • Loading branch information
christianh814 committed Nov 22, 2022
1 parent 0e55908 commit c5f5a03
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 563 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Currently working:

- [x] Migrate Kustomizations
- [x] Migrate HelmReleases
- [ ] Auto Scan/Migrate
- [x] Scan
- [ ] Auto Migrate
- [ ] Uninstall Flux

# Installation
Expand All @@ -18,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.2/mta-amd64-$(uname -s | tr [:upper:] [:lower:])
sudo wget -O /usr/local/bin/mta https://github.com/christianh814/mta/releases/download/v0.0.3/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.2/mta-arm64-darwin
sudo wget -O /usr/local/bin/mta https://github.com/christianh814/mta/releases/download/v0.0.3/mta-arm64-darwin
```

Make sure it's executable
Expand Down
21 changes: 3 additions & 18 deletions cmd/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -18,7 +18,6 @@ package cmd
import (
"context"
"encoding/json"
"os"

yaml "sigs.k8s.io/yaml"

Expand Down Expand Up @@ -84,7 +83,7 @@ with kubectl.`,
log.Fatal(err)
}

// Get the helmchat based on type, report if error
// Get the helmchart based on type, report if error
helmRepo := &sourcev1.HelmRepository{}
err = k.Get(ctx, client.ObjectKey{Namespace: helmReleaseNamespace, Name: helmRelease.Spec.Chart.Spec.SourceRef.Name}, helmRepo)
if err != nil {
Expand Down Expand Up @@ -148,19 +147,5 @@ with kubectl.`,

func init() {
rootCmd.AddCommand(helmreleaseCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// helmreleaseCmd.PersistentFlags().String("foo", "", "A help for foo")
kcf, _ := os.UserHomeDir()
helmreleaseCmd.Flags().String("kubeconfig", kcf+"/.kube/config", "Path to the kubeconfig file to use (if not the standard one).")
helmreleaseCmd.Flags().String("name", "", "Name of HelmRelease to export")
helmreleaseCmd.Flags().String("namespace", "flux-system", "Namespace of where the HelmRelease is")
helmreleaseCmd.MarkFlagRequired("name")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// helmreleaseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.MarkPersistentFlagRequired("name")
}
17 changes: 2 additions & 15 deletions cmd/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -18,7 +18,6 @@ package cmd
import (
"context"
"encoding/base64"
"os"
"strings"

"github.com/christianh814/mta/pkg/utils"
Expand Down Expand Up @@ -148,17 +147,5 @@ with kubectl.`,

func init() {
rootCmd.AddCommand(kustomizationCmd)
// Here you will define your flags and configuration settings.

kcf, _ := os.UserHomeDir()
kustomizationCmd.Flags().String("kubeconfig", kcf+"/.kube/config", "Path to the kubeconfig file to use (if not the standard one).")
kustomizationCmd.Flags().String("name", "flux-system", "Name of Kustomization to export")
kustomizationCmd.Flags().String("namespace", "flux-system", "Namespace of where the Kustomization is")

//Require the following flags
/*
kustomizationCmd.MarkFlagRequired("name")
kustomizationCmd.MarkFlagRequired("namespace")
*/

rootCmd.MarkPersistentFlagRequired("name")
}
13 changes: 9 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "mta",
Version: "v0.0.2",
Version: "v0.0.3",
Short: "This commands turns Flux Kustomizations and HelmReleases into Argo CD Applications",
Long: `This is a migration tool that helps you move your Flux Kustomizations and HelmReleases
into an Argo CD ApplicationSet or Application.
Kustomization example:
mta kustomization --name=mykustomization --namespace=flux-system | kubectl apply -n argocd -f -
mta kustomization --name=mykustomization --namespace=flux-system | kubectl apply -n argocd -f -
HelmRelease example:
mta helmrelease --name=myhelmrelease --namespace=flux-system | kubectl apply -n argocd -f -
mta helmrelease --name=myhelmrelease --namespace=flux-system | kubectl apply -n argocd -f -
This utilty exports the named Kustomization or HelmRelease and the source Git repo or Helm repo and
creates a manifests to stdout, which you can pipe into an apply command
Expand All @@ -66,9 +66,14 @@ func init() {

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mta.yaml)")

kcf, _ := os.UserHomeDir()
rootCmd.PersistentFlags().String("kubeconfig", kcf+"/.kube/config", "Path to the kubeconfig file to use (if not the standard one).")
rootCmd.PersistentFlags().String("name", "", "Name of Kustomization or HelmRelease to export")
rootCmd.PersistentFlags().String("namespace", "flux-system", "Namespace of where the Kustomization or HelmRelease is")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
110 changes: 110 additions & 0 deletions cmd/scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright © 2022 Christian Hernandez <christian@chernand.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"context"
"os"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
client "sigs.k8s.io/controller-runtime/pkg/client"
)

// scanCmd represents the scan command
var scanCmd = &cobra.Command{
Use: "scan",
Short: "Looks for all HelmReleases and Kustomizations",
Long: `Looks for HelmReleases and Kustomizations in the cluster and
displays the results.
`,
Run: func(cmd *cobra.Command, args []string) {
// Set up table
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Kind", "Name", "Namespace", "Status"})

// Set up the default context
ctx := context.TODO()

// Get the Kubeconfig to use
kubeConfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
log.Fatal(err)
}

// Set up the schema because HelmRelease and Kustomization are CRDs
kScheme := runtime.NewScheme()
kustomizev1.AddToScheme(kScheme)
sourcev1.AddToScheme(kScheme)
helmv2.AddToScheme(kScheme)
sourcev1.AddToScheme(kScheme)

// create rest config using the kubeconfig file.
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
log.Fatal(err)
}

// Create a new client based on the restconfig and scheme
k, err := client.New(restConfig, client.Options{
Scheme: kScheme,
})
if err != nil {
log.Fatal(err)
}

// Get all Helm Releases in the cluster
helmReleaseList := &helmv2.HelmReleaseList{}
if err = k.List(ctx, helmReleaseList); err != nil {
log.Fatal(err)
}

// Add all Helm Releases to the table
for _, hr := range helmReleaseList.Items {
t.AppendRow(table.Row{"HelmRelease", hr.Name, hr.Namespace, hr.Status.Conditions[0].Message})
}

// Add a separotor to the table
t.AppendSeparator()

// Get all Kustomizations in the cluster
kustomizationList := &kustomizev1.KustomizationList{}
if err = k.List(ctx, kustomizationList); err != nil {
log.Fatal(err)
}

// Add all Kustomizations to the table
for _, k := range kustomizationList.Items {
t.AppendRow(table.Row{"Kustomization", k.Name, k.Namespace, k.Status.Conditions[0].Message})
}

//Render the table to the console
t.SetStyle(table.StyleLight)
t.Render()

},
}

func init() {
rootCmd.AddCommand(scanCmd)
}
106 changes: 53 additions & 53 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
module github.com/christianh814/mta

go 1.18
go 1.19

require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/fluxcd/helm-controller/api v0.22.1
github.com/fluxcd/source-controller/api v0.25.5
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
k8s.io/apimachinery v0.24.1
k8s.io/client-go v0.24.1
sigs.k8s.io/controller-runtime v0.12.1
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
github.com/jedib0t/go-pretty/v6 v6.4.2
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
sigs.k8s.io/controller-runtime v0.13.1
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fluxcd/pkg/apis/acl v0.0.3 // indirect
github.com/fluxcd/pkg/apis/kustomize v0.4.2 // indirect
github.com/fluxcd/pkg/apis/meta v0.14.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.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/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/google/gnostic v0.5.7-v3refs // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
k8s.io/api v0.24.1 // indirect
k8s.io/apiextensions-apiserver v0.24.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fluxcd/kustomize-controller/api v0.26.1
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.1.2 // 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.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // 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/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.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/subosito/gotenv v1.3.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // 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/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0
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/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit c5f5a03

Please sign in to comment.