@@ -17,7 +17,7 @@ package commands
1717import (
1818 "context"
1919 "fmt"
20- "io/ioutil "
20+ "time "
2121
2222 "github.com/codefresh-io/cli-v2/pkg/cdUtils"
2323 "github.com/codefresh-io/cli-v2/pkg/eventUtils"
@@ -32,10 +32,12 @@ import (
3232 "github.com/argoproj-labs/argocd-autopilot/pkg/git"
3333 "github.com/argoproj-labs/argocd-autopilot/pkg/kube"
3434 apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
35+ aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
3536 argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
3637 wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
3738 wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
3839 "github.com/ghodss/yaml"
40+ "github.com/go-git/go-billy/v5/memfs"
3941 "github.com/spf13/cobra"
4042 v1 "k8s.io/api/core/v1"
4143 rbacv1 "k8s.io/api/rbac/v1"
@@ -44,11 +46,11 @@ import (
4446
4547type (
4648 RuntimeCreateOptions struct {
47- RuntimeName string
48- KubeContext string
49- KubeFactory kube.Factory
50- insCreateOpts * apcmd. RepoCreateOptions
51- gsCreateOpts * apcmd. RepoCreateOptions
49+ RuntimeName string
50+ KubeContext string
51+ KubeFactory kube.Factory
52+ insCloneOpts * git. CloneOptions
53+ gsCloneOpts * git. CloneOptions
5254 }
5355)
5456
@@ -70,9 +72,9 @@ func NewRuntimeCommand() *cobra.Command {
7072
7173func NewRuntimeCreateCommand () * cobra.Command {
7274 var (
73- f kube.Factory
74- insCreateOpts * apcmd. RepoCreateOptions
75- gsCreateOpts * apcmd. RepoCreateOptions
75+ f kube.Factory
76+ insCloneOpts * git. CloneOptions
77+ gsCloneOpts * git. CloneOptions
7678 )
7779
7880 cmd := & cobra.Command {
@@ -92,120 +94,103 @@ func NewRuntimeCreateCommand() *cobra.Command {
9294
9395 <BIN> runtime create runtime-name --install-owner owner --install-name gitops_repo
9496` ),
95- RunE : func (cmd * cobra.Command , args []string ) error {
96- opts := & RuntimeCreateOptions {
97- KubeContext : "" ,
98- KubeFactory : f ,
99- insCreateOpts : insCreateOpts ,
100- gsCreateOpts : gsCreateOpts ,
97+ PreRun : func (_ * cobra.Command , _ []string ) {
98+ if gsCloneOpts .Auth .Password == "" {
99+ gsCloneOpts .Auth .Password = insCloneOpts .Auth .Password
100+ }
101+
102+ insCloneOpts .Parse ()
103+ if gsCloneOpts .Repo == "" {
104+ host , orgRepo , _ , _ , _ , suffix , _ := aputil .ParseGitUrl (insCloneOpts .Repo )
105+ gsCloneOpts .Repo = host + orgRepo + "_git_source" + suffix
101106 }
107+
108+ gsCloneOpts .Parse ()
109+ },
110+ RunE : func (cmd * cobra.Command , args []string ) error {
102111 if len (args ) < 1 {
103112 log .G ().Fatal ("must enter runtime name" )
104113 }
105114
106- opts .RuntimeName = args [0 ]
107- insCreateOpts .Public = false
108- return RunRuntimeCreate (cmd .Context (), opts )
115+ return RunRuntimeCreate (cmd .Context (), & RuntimeCreateOptions {
116+ RuntimeName : args [0 ],
117+ KubeContext : "" ,
118+ KubeFactory : f ,
119+ insCloneOpts : insCloneOpts ,
120+ gsCloneOpts : gsCloneOpts ,
121+ })
109122 },
110123 }
111124
112- insCreateOpts = apcmd .AddRepoCreateFlags (cmd , "install" )
113- gsCreateOpts = apcmd .AddRepoCreateFlags (cmd , "git-src" )
125+ insCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
126+ Prefix : "install" ,
127+ CreateIfNotExist : true ,
128+ FS : memfs .New (),
129+ })
130+ gsCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
131+ Prefix : "git-src" ,
132+ Optional : true ,
133+ CreateIfNotExist : true ,
134+ FS : memfs .New (),
135+ })
114136 f = kube .AddFlags (cmd .Flags ())
115137
116138 return cmd
117139}
118140
119141func RunRuntimeCreate (ctx context.Context , opts * RuntimeCreateOptions ) error {
120- insCloneOpts , err := apcmd .RunRepoCreate (ctx , opts .insCreateOpts )
121- if err != nil {
122- return err
123- }
124-
125- // var err error
126- // installOpts := &git.CloneOptions{
127- // Repo: "github.com/noam-codefresh/demo",
128- // Auth: git.Auth{
129- // Password: "<TOKEN>",
130- // },
131- // FS: fs.Create(memfs.New()),
132- // }
133- // installOpts.Parse()
134-
135- insCloneOpts .Progress = ioutil .Discard
136- err = apcmd .RunRepoBootstrap (ctx , & apcmd.RepoBootstrapOptions {
142+ err := apcmd .RunRepoBootstrap (ctx , & apcmd.RepoBootstrapOptions {
137143 AppSpecifier : store .Get ().ArgoCDManifestsURL ,
138144 Namespace : opts .RuntimeName ,
139145 KubeContext : opts .KubeContext ,
140146 KubeFactory : opts .KubeFactory ,
141- CloneOptions : insCloneOpts ,
147+ CloneOptions : opts . insCloneOpts ,
142148 })
143149 if err != nil {
144150 return err
145151 }
146152
147153 err = apcmd .RunProjectCreate (ctx , & apcmd.ProjectCreateOptions {
148- CloneOpts : insCloneOpts ,
154+ CloneOpts : opts . insCloneOpts ,
149155 ProjectName : opts .RuntimeName ,
150156 })
151157 if err != nil {
152158 return err
153159 }
154160
155- if err = createApp (ctx , insCloneOpts , opts .RuntimeName , "events" , store .Get ().ArgoEventsManifestsURL , application .AppTypeKustomize , opts .RuntimeName ); err != nil {
156- return fmt .Errorf ("failed to create events application: %w" , err )
157- }
158-
159- if err = createApp (ctx , insCloneOpts , opts .RuntimeName , "rollouts" , store .Get ().ArgoRolloutsManifestsURL , application .AppTypeKustomize , opts .RuntimeName ); err != nil {
161+ if err = createApp (ctx , opts .KubeFactory , opts .insCloneOpts , opts .RuntimeName , "rollouts" , store .Get ().ArgoRolloutsManifestsURL , application .AppTypeKustomize , opts .RuntimeName , false ); err != nil {
160162 return fmt .Errorf ("failed to create rollouts application: %w" , err )
161163 }
162164
163- if err = createApp (ctx , insCloneOpts , opts .RuntimeName , "workflows" , store .Get ().ArgoWorkflowsManifestsURL , application .AppTypeKustomize , opts .RuntimeName ); err != nil {
165+ if err = createApp (ctx , opts . KubeFactory , opts . insCloneOpts , opts .RuntimeName , "workflows" , store .Get ().ArgoWorkflowsManifestsURL , application .AppTypeKustomize , opts .RuntimeName , false ); err != nil {
164166 return fmt .Errorf ("failed to create workflows application: %w" , err )
165167 }
166168
167- if err = createComponentsReporter (ctx , insCloneOpts , opts ); err != nil {
168- return fmt .Errorf ("failed to create components-reporter: %w" , err )
169- }
170-
171- if opts .gsCreateOpts .Owner == "" {
172- opts .gsCreateOpts .Owner = opts .insCreateOpts .Owner
173- }
174-
175- if opts .gsCreateOpts .Repo == "" {
176- opts .gsCreateOpts .Repo = opts .insCreateOpts .Repo + "-git-source"
177- }
178-
179- if opts .gsCreateOpts .Token == "" {
180- opts .gsCreateOpts .Token = opts .insCreateOpts .Token
169+ if err = createApp (ctx , opts .KubeFactory , opts .insCloneOpts , opts .RuntimeName , "events" , store .Get ().ArgoEventsManifestsURL , application .AppTypeKustomize , opts .RuntimeName , true ); err != nil {
170+ return fmt .Errorf ("failed to create events application: %w" , err )
181171 }
182172
183- gsCloneOpts , err := apcmd .RunRepoCreate (ctx , opts .gsCreateOpts )
184- if err != nil {
185- return err
173+ if err = createComponentsReporter (ctx , opts .insCloneOpts , opts ); err != nil {
174+ return fmt .Errorf ("failed to create components-reporter: %w" , err )
186175 }
187176
188- // gsCloneOpts := &git.CloneOptions{
189- // Repo: "github.com/noam-codefresh/git-source",
190- // Auth: git.Auth{
191- // Password: gsCreateOpts.Token,
192- // },
193- // FS: fs.Create(memfs.New()),
194- // }
195- // gsCloneOpts.Parse()
196-
197- if err = createDemoWorkflowTemplate (ctx , gsCloneOpts , store .Get ().GitSourceName , opts .RuntimeName ); err != nil {
177+ if err = createDemoWorkflowTemplate (ctx , opts .gsCloneOpts , store .Get ().GitSourceName , opts .RuntimeName ); err != nil {
198178 return err
199179 }
200180
201- if err = createGitSource (ctx , insCloneOpts , gsCloneOpts , store .Get ().GitSourceName , opts .RuntimeName ); err != nil {
181+ if err = createGitSource (ctx , opts . insCloneOpts , opts . gsCloneOpts , store .Get ().GitSourceName , opts .RuntimeName ); err != nil {
202182 return fmt .Errorf ("failed to create `%s`: %w" , store .Get ().GitSourceName , err )
203183 }
204184
205185 return nil
206186}
207187
208- func createApp (ctx context.Context , cloneOpts * git.CloneOptions , projectName , appName , appURL , appType , namespace string ) error {
188+ func createApp (ctx context.Context , f kube.Factory , cloneOpts * git.CloneOptions , projectName , appName , appURL , appType , namespace string , wait bool ) error {
189+ timeout := time .Duration (0 )
190+ if wait {
191+ timeout = store .Get ().WaitTimeout
192+ }
193+
209194 return apcmd .RunAppCreate (ctx , & apcmd.AppCreateOptions {
210195 CloneOpts : cloneOpts ,
211196 AppsCloneOpts : & git.CloneOptions {},
@@ -216,6 +201,8 @@ func createApp(ctx context.Context, cloneOpts *git.CloneOptions, projectName, ap
216201 AppType : appType ,
217202 DestNamespace : namespace ,
218203 },
204+ KubeFactory : f ,
205+ Timeout : timeout ,
219206 })
220207}
221208
@@ -230,11 +217,11 @@ func createComponentsReporter(ctx context.Context, cloneOpts *git.CloneOptions,
230217 }
231218
232219 resPath := cloneOpts .FS .Join (apstore .Default .AppsDir , store .Get ().ComponentsReporterName , opts .RuntimeName , "resources" )
233- if err := createApp (ctx , cloneOpts , opts .RuntimeName , store .Get ().ComponentsReporterName , cloneOpts .URL ()+ "/" + resPath , application .AppTypeDirectory , opts .RuntimeName ); err != nil {
220+ if err := createApp (ctx , opts . KubeFactory , cloneOpts , opts .RuntimeName , store .Get ().ComponentsReporterName , cloneOpts .URL ()+ "/" + resPath , application .AppTypeDirectory , opts .RuntimeName , false ); err != nil {
234221 return err
235222 }
236223
237- r , repofs , err := cloneOpts .Clone (ctx )
224+ r , repofs , err := cloneOpts .GetRepo (ctx )
238225 if err != nil {
239226 return err
240227 }
@@ -419,7 +406,7 @@ func createSensor(repofs fs.FS, name, path, namespace, eventSourceName string) e
419406}
420407
421408func createDemoWorkflowTemplate (ctx context.Context , gsCloneOpts * git.CloneOptions , gsName , runtimeName string ) error {
422- gsRepo , gsFs , err := gsCloneOpts .Clone (ctx )
409+ gsRepo , gsFs , err := gsCloneOpts .GetRepo (ctx )
423410 if err != nil {
424411 return err
425412 }
@@ -462,7 +449,7 @@ func createDemoWorkflowTemplate(ctx context.Context, gsCloneOpts *git.CloneOptio
462449func createGitSource (ctx context.Context , insCloneOpts * git.CloneOptions , gsCloneOpts * git.CloneOptions , gsName , runtimeName string ) error {
463450 var err error
464451
465- insRepo , insFs , err := insCloneOpts .Clone (ctx )
452+ insRepo , insFs , err := insCloneOpts .GetRepo (ctx )
466453 if err != nil {
467454 return err
468455 }
@@ -604,7 +591,7 @@ func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsClon
604591 }
605592
606593 fullResPath := insFs .Join (insFs .Root (), resPath )
607- if err = createApp (ctx , insCloneOpts , runtimeName , gsName , insCloneOpts .URL ()+ fullResPath , application .AppTypeDirectory , runtimeName ); err != nil {
594+ if err = createApp (ctx , nil , insCloneOpts , runtimeName , gsName , insCloneOpts .URL ()+ fullResPath , application .AppTypeDirectory , runtimeName , false ); err != nil {
608595 return fmt .Errorf ("failed to create git-source: %w" , err )
609596 }
610597
0 commit comments