Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix local application infer #88

Merged
merged 4 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import (

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
billyUtils "github.com/go-git/go-billy/v5/util"
"github.com/spf13/cobra"
)

type (
AppCreateOptions struct {
CloneOpts *git.CloneOptions
ProjectName string
AppOpts *application.CreateOptions
CloneOpts *git.CloneOptions
ProjectName string
AppOpts *application.CreateOptions
}

AppDeleteOptions struct {
Expand All @@ -48,9 +49,6 @@ func NewAppCommand() *cobra.Command {
Use: "application",
Aliases: []string{"app"},
Short: "Manage applications",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
cloneOpts.Parse()
},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
exit(1)
Expand All @@ -67,8 +65,8 @@ func NewAppCommand() *cobra.Command {

func NewAppCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command {
var (
appOpts *application.CreateOptions
projectName string
appOpts *application.CreateOptions
projectName string
)

cmd := &cobra.Command{
Expand All @@ -92,16 +90,17 @@ func NewAppCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command {

<BIN> app create <new_app_name> --app github.com/some_org/some_repo/manifests?ref=v1.2.3 --project project_name
`),
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.G().Fatal("must enter application name")
}

appOpts.AppName = args[0]
return RunAppCreate(cmd.Context(), &AppCreateOptions{
CloneOpts: cloneOpts,
ProjectName: projectName,
AppOpts: appOpts,
CloneOpts: cloneOpts,
ProjectName: projectName,
AppOpts: appOpts,
})
},
}
Expand Down Expand Up @@ -162,23 +161,33 @@ func setAppOptsDefaults(ctx context.Context, repofs fs.FS, opts *AppCreateOption
opts.AppOpts.DestNamespace = "default"
}

if opts.AppOpts.AppType == "" {
log.G().Infof("Cloning app from %s to infer App Type", opts.AppOpts.AppSpecifier)
if opts.AppOpts.AppType != "" {
return nil
}

var fsys fs.FS
if _, err := os.Stat(opts.AppOpts.AppSpecifier); err == nil {
// local directory
fsys = fs.Create(osfs.New(opts.AppOpts.AppSpecifier))
} else {
host, orgRepo, p, _, _, _, _ := util.ParseGitUrl(opts.AppOpts.AppSpecifier)
url := host + orgRepo
log.G().Infof("cloning repo: '%s', to infer app type from path '%s'", url, p)
cloneOpts := &git.CloneOptions{
Repo: opts.AppOpts.AppSpecifier,
Auth: opts.CloneOpts.Auth,
FS: memfs.New(),
FS: fs.Create(memfs.New()),
}
cloneOpts.Parse()
_, fs, err := clone(ctx, cloneOpts)
_, fsys, err = clone(ctx, cloneOpts)
if err != nil {
return err
}

opts.AppOpts.AppType = application.InferAppType(fs)
log.G().Infof("Inferred AppType: %s", opts.AppOpts.AppType)
}

opts.AppOpts.AppType = application.InferAppType(fsys)
log.G().Infof("inferred application type: %s", opts.AppOpts.AppType)

return nil
}

Expand Down Expand Up @@ -220,6 +229,7 @@ func NewAppListCommand(cloneOpts *git.CloneOptions) *cobra.Command {

<BIN> app list <project_name>
`),
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.G().Fatal("must enter a project name")
Expand Down Expand Up @@ -303,6 +313,7 @@ func NewAppDeleteCommand(cloneOpts *git.CloneOptions) *cobra.Command {

<BIN> app delete <app_name> --project <project_name>
`),
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.G().Fatal("must enter application name")
Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ func NewProjectCommand() *cobra.Command {
Use: "project",
Aliases: []string{"proj"},
Short: "Manage projects",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
cloneOpts.Parse()
},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
exit(1)
Expand Down Expand Up @@ -107,6 +104,7 @@ func NewProjectCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command {

<BIN> project create <PROJECT_NAME>
`),
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.G().Fatal("must enter project name")
Expand Down Expand Up @@ -341,6 +339,7 @@ func NewProjectListCommand(cloneOpts *git.CloneOptions) *cobra.Command {

<BIN> project list
`),
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
return RunProjectList(cmd.Context(), &ProjectListOptions{
CloneOpts: cloneOpts,
Expand Down Expand Up @@ -408,6 +407,7 @@ func NewProjectDeleteCommand(cloneOpts *git.CloneOptions) *cobra.Command {

<BIN> project delete <project_name>
`),
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.G().Fatal("must enter project name")
Expand Down
9 changes: 4 additions & 5 deletions cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ func NewRepoBootstrapCommand() *cobra.Command {

<BIN> repo bootstrap --repo https://github.com/example/repo/path/to/installation_root
`),
PersistentPreRun: func(_ *cobra.Command, _ []string) {
cloneOpts.Parse()
}, RunE: func(cmd *cobra.Command, args []string) error {
PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
return RunRepoBootstrap(cmd.Context(), &RepoBootstrapOptions{
AppSpecifier: appSpecifier,
InstallationMode: installationMode,
Expand Down Expand Up @@ -260,7 +259,7 @@ func RunRepoCreate(ctx context.Context, opts *RepoCreateOptions) (*git.CloneOpti

co := &git.CloneOptions{
Repo: repoUrl,
FS: memfs.New(),
FS: fs.Create(memfs.New()),
Auth: git.Auth{
Password: opts.Token,
},
Expand Down Expand Up @@ -306,7 +305,7 @@ func RunRepoBootstrap(ctx context.Context, opts *RepoBootstrapOptions) error {
return nil
}

log.G().Infof("cloning repo: %s", opts.CloneOptions.URL)
log.G().Infof("cloning repo: %s", opts.CloneOptions.URL())

// clone GitOps repo
r, repofs, err := clone(ctx, opts.CloneOptions)
Expand Down
25 changes: 17 additions & 8 deletions pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,20 +454,24 @@ func checkBaseCollision(repofs fs.FS, orgBasePath string, newBase *kusttypes.Kus
}

// fixResourcesPaths adjusts all relative paths in the kustomization file to the specified
// `kustomizationPath`.
func fixResourcesPaths(k *kusttypes.Kustomization, kustomizationPath string) error {
// newKustDir.
func fixResourcesPaths(k *kusttypes.Kustomization, newKustDir string) error {
for i, path := range k.Resources {
// if path is a local file
// if path is a remote resource ignore it
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
continue
}

ap, err := filepath.Abs(path)
absRes, err := filepath.Abs(path)
if err != nil {
return err
}

k.Resources[i], err = filepath.Rel(kustomizationPath, ap)
k.Resources[i], err = filepath.Rel(newKustDir, absRes)
log.G().WithFields(log.Fields{
"from": absRes,
"to": k.Resources[i],
}).Debug("adjusting kustomization paths to local filesystem")
if err != nil {
return err
}
Expand All @@ -477,14 +481,18 @@ func fixResourcesPaths(k *kusttypes.Kustomization, kustomizationPath string) err
}

var generateManifests = func(k *kusttypes.Kustomization) ([]byte, error) {
td, err := ioutil.TempDir("", "auto-pilot")
td, err := ioutil.TempDir(".", "auto-pilot")
if err != nil {
return nil, err
}
defer os.RemoveAll(td)

kustomizationPath := filepath.Join(td, "kustomization.yaml")
if err = fixResourcesPaths(k, kustomizationPath); err != nil {
absTd, err := filepath.Abs(td)
if err != nil {
return nil, err
}

if err = fixResourcesPaths(k, absTd); err != nil {
return nil, err
}

Expand All @@ -493,6 +501,7 @@ var generateManifests = func(k *kusttypes.Kustomization) ([]byte, error) {
return nil, err
}

kustomizationPath := filepath.Join(td, "kustomization.yaml")
if err = ioutil.WriteFile(kustomizationPath, kyaml, 0400); err != nil {
return nil, err
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -38,7 +39,8 @@ type (
// URL clone url
Repo string
Auth Auth
FS billy.Filesystem
FS fs.FS
Progress io.Writer
url string
revision string
path string
Expand Down Expand Up @@ -77,9 +79,9 @@ var (
}
)

func AddFlags(cmd *cobra.Command, fs billy.Filesystem, prefix string) *CloneOptions {
func AddFlags(cmd *cobra.Command, bfs billy.Filesystem, prefix string) *CloneOptions {
co := &CloneOptions{
FS: fs,
FS: fs.Create(bfs),
}

if prefix == "" {
Expand Down Expand Up @@ -193,11 +195,15 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
return nil, ErrNilOpts
}

if opts.Progress == nil {
opts.Progress = os.Stderr
}

cloneOpts := &gg.CloneOptions{
URL: opts.url,
Auth: getAuth(opts.Auth),
Depth: 1,
Progress: os.Stderr,
Progress: opts.Progress,
Tags: gg.NoTags,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func TestClone(t *testing.T) {

if tt.opts != nil {
tt.opts.Parse()
tt.opts.FS = memfs.New()
tt.opts.FS = fs.Create(memfs.New())
}

gotRepo, gotFS, err := tt.opts.Clone(context.Background())
Expand Down