@@ -17,7 +17,7 @@ package commands
17
17
import (
18
18
"context"
19
19
"fmt"
20
- "io/ioutil "
20
+ "time "
21
21
22
22
"github.com/codefresh-io/cli-v2/pkg/cdUtils"
23
23
"github.com/codefresh-io/cli-v2/pkg/eventUtils"
@@ -32,10 +32,12 @@ import (
32
32
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
33
33
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
34
34
apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
35
+ aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
35
36
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
36
37
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
37
38
wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
38
39
"github.com/ghodss/yaml"
40
+ "github.com/go-git/go-billy/v5/memfs"
39
41
"github.com/spf13/cobra"
40
42
v1 "k8s.io/api/core/v1"
41
43
rbacv1 "k8s.io/api/rbac/v1"
@@ -44,11 +46,11 @@ import (
44
46
45
47
type (
46
48
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
52
54
}
53
55
)
54
56
@@ -70,9 +72,9 @@ func NewRuntimeCommand() *cobra.Command {
70
72
71
73
func NewRuntimeCreateCommand () * cobra.Command {
72
74
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
76
78
)
77
79
78
80
cmd := & cobra.Command {
@@ -92,120 +94,103 @@ func NewRuntimeCreateCommand() *cobra.Command {
92
94
93
95
<BIN> runtime create runtime-name --install-owner owner --install-name gitops_repo
94
96
` ),
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
101
106
}
107
+
108
+ gsCloneOpts .Parse ()
109
+ },
110
+ RunE : func (cmd * cobra.Command , args []string ) error {
102
111
if len (args ) < 1 {
103
112
log .G ().Fatal ("must enter runtime name" )
104
113
}
105
114
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
+ })
109
122
},
110
123
}
111
124
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
+ })
114
136
f = kube .AddFlags (cmd .Flags ())
115
137
116
138
return cmd
117
139
}
118
140
119
141
func 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 {
137
143
AppSpecifier : store .Get ().ArgoCDManifestsURL ,
138
144
Namespace : opts .RuntimeName ,
139
145
KubeContext : opts .KubeContext ,
140
146
KubeFactory : opts .KubeFactory ,
141
- CloneOptions : insCloneOpts ,
147
+ CloneOptions : opts . insCloneOpts ,
142
148
})
143
149
if err != nil {
144
150
return err
145
151
}
146
152
147
153
err = apcmd .RunProjectCreate (ctx , & apcmd.ProjectCreateOptions {
148
- CloneOpts : insCloneOpts ,
154
+ CloneOpts : opts . insCloneOpts ,
149
155
ProjectName : opts .RuntimeName ,
150
156
})
151
157
if err != nil {
152
158
return err
153
159
}
154
160
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 {
160
162
return fmt .Errorf ("failed to create rollouts application: %w" , err )
161
163
}
162
164
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 {
164
166
return fmt .Errorf ("failed to create workflows application: %w" , err )
165
167
}
166
168
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 )
181
171
}
182
172
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 )
186
175
}
187
176
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 {
198
178
return err
199
179
}
200
180
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 {
202
182
return fmt .Errorf ("failed to create `%s`: %w" , store .Get ().GitSourceName , err )
203
183
}
204
184
205
185
return nil
206
186
}
207
187
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
+
209
194
return apcmd .RunAppCreate (ctx , & apcmd.AppCreateOptions {
210
195
CloneOpts : cloneOpts ,
211
196
AppsCloneOpts : & git.CloneOptions {},
@@ -216,6 +201,8 @@ func createApp(ctx context.Context, cloneOpts *git.CloneOptions, projectName, ap
216
201
AppType : appType ,
217
202
DestNamespace : namespace ,
218
203
},
204
+ KubeFactory : f ,
205
+ Timeout : timeout ,
219
206
})
220
207
}
221
208
@@ -230,11 +217,11 @@ func createComponentsReporter(ctx context.Context, cloneOpts *git.CloneOptions,
230
217
}
231
218
232
219
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 {
234
221
return err
235
222
}
236
223
237
- r , repofs , err := cloneOpts .Clone (ctx )
224
+ r , repofs , err := cloneOpts .GetRepo (ctx )
238
225
if err != nil {
239
226
return err
240
227
}
@@ -419,7 +406,7 @@ func createSensor(repofs fs.FS, name, path, namespace, eventSourceName string) e
419
406
}
420
407
421
408
func 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 )
423
410
if err != nil {
424
411
return err
425
412
}
@@ -462,7 +449,7 @@ func createDemoWorkflowTemplate(ctx context.Context, gsCloneOpts *git.CloneOptio
462
449
func createGitSource (ctx context.Context , insCloneOpts * git.CloneOptions , gsCloneOpts * git.CloneOptions , gsName , runtimeName string ) error {
463
450
var err error
464
451
465
- insRepo , insFs , err := insCloneOpts .Clone (ctx )
452
+ insRepo , insFs , err := insCloneOpts .GetRepo (ctx )
466
453
if err != nil {
467
454
return err
468
455
}
@@ -604,7 +591,7 @@ func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsClon
604
591
}
605
592
606
593
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 {
608
595
return fmt .Errorf ("failed to create git-source: %w" , err )
609
596
}
610
597
0 commit comments