@@ -20,10 +20,14 @@ import (
20
20
"os"
21
21
"time"
22
22
23
+ apicommon "github.com/argoproj/argo-events/pkg/apis/common"
24
+ sensorreg "github.com/argoproj/argo-events/pkg/apis/sensor"
23
25
"github.com/codefresh-io/cli-v2/pkg/log"
24
26
"github.com/codefresh-io/cli-v2/pkg/runtime"
25
27
"github.com/codefresh-io/cli-v2/pkg/store"
26
28
"github.com/codefresh-io/cli-v2/pkg/util"
29
+ apu "github.com/codefresh-io/cli-v2/pkg/util/aputil"
30
+ wfutil "github.com/codefresh-io/cli-v2/pkg/util/workflow"
27
31
"github.com/juju/ansiterm"
28
32
29
33
apcmd "github.com/argoproj-labs/argocd-autopilot/cmd/commands"
@@ -32,21 +36,23 @@ import (
32
36
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
33
37
apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
34
38
aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
39
+ eventsourcereg "github.com/argoproj/argo-events/pkg/apis/eventsource"
40
+ eventsourcev1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
41
+ sensorsv1alpha1 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
35
42
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
36
43
wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
37
- apu "github.com/codefresh-io/cli-v2/pkg/util/aputil"
38
44
"github.com/spf13/cobra"
39
45
v1 "k8s.io/api/core/v1"
40
46
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41
47
)
42
48
43
49
type (
44
50
GitSourceCreateOptions struct {
45
- insCloneOpts * git.CloneOptions
46
- gsCloneOpts * git.CloneOptions
47
- gsName string
48
- runtimeName string
49
- fullGsPath string
51
+ InsCloneOpts * git.CloneOptions
52
+ GsCloneOpts * git.CloneOptions
53
+ GsName string
54
+ RuntimeName string
55
+ FullGsPath string
50
56
}
51
57
52
58
GitSourceDeleteOptions struct {
@@ -62,6 +68,12 @@ type (
62
68
InsCloneOpts * git.CloneOptions
63
69
GsCloneOpts * git.CloneOptions
64
70
}
71
+
72
+ gitSourceCronExampleOptions struct {
73
+ runtimeName string
74
+ gsCloneOpts * git.CloneOptions
75
+ gsFs fs.FS
76
+ }
65
77
)
66
78
67
79
func NewGitSourceCommand () * cobra.Command {
@@ -130,11 +142,11 @@ func NewGitSourceCreateCommand() *cobra.Command {
130
142
ctx := cmd .Context ()
131
143
132
144
return RunGitSourceCreate (ctx , & GitSourceCreateOptions {
133
- insCloneOpts : insCloneOpts ,
134
- gsCloneOpts : gsCloneOpts ,
135
- gsName : args [1 ],
136
- runtimeName : args [0 ],
137
- fullGsPath : gsCloneOpts .Path (),
145
+ InsCloneOpts : insCloneOpts ,
146
+ GsCloneOpts : gsCloneOpts ,
147
+ GsName : args [1 ],
148
+ RuntimeName : args [0 ],
149
+ FullGsPath : gsCloneOpts .Path (),
138
150
})
139
151
},
140
152
}
@@ -152,7 +164,7 @@ func NewGitSourceCreateCommand() *cobra.Command {
152
164
}
153
165
154
166
func RunGitSourceCreate (ctx context.Context , opts * GitSourceCreateOptions ) error {
155
- gsRepo , gsFs , err := opts .gsCloneOpts .GetRepo (ctx )
167
+ gsRepo , gsFs , err := opts .GsCloneOpts .GetRepo (ctx )
156
168
if err != nil {
157
169
return err
158
170
}
@@ -163,11 +175,16 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
163
175
}
164
176
165
177
if len (fi ) == 0 {
166
- if err = createDemoWorkflowTemplate (gsFs , opts .runtimeName ); err != nil {
167
- return fmt .Errorf ("failed to create demo workflowTemplate: %w" , err )
178
+ err = createCronExamplePipeline (& gitSourceCronExampleOptions {
179
+ runtimeName : opts .RuntimeName ,
180
+ gsCloneOpts : opts .GsCloneOpts ,
181
+ gsFs : gsFs ,
182
+ })
183
+ if err != nil {
184
+ return fmt .Errorf ("failed to create cron example pipeline. Error: %w" , err )
168
185
}
169
186
170
- commitMsg := fmt .Sprintf ("Created demo workflow template in %s Directory" , opts .gsCloneOpts .Path ())
187
+ commitMsg := fmt .Sprintf ("Created demo workflow template in %s Directory" , opts .GsCloneOpts .Path ())
171
188
172
189
log .G (ctx ).Info ("Pushing a demo workflow-template to the new git-source repo" )
173
190
if err := apu .PushWithMessage (ctx , gsRepo , commitMsg ); err != nil {
@@ -176,19 +193,137 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
176
193
}
177
194
178
195
appDef := & runtime.AppDef {
179
- Name : opts .gsName ,
196
+ Name : opts .GsName ,
180
197
Type : application .AppTypeDirectory ,
181
- URL : opts .gsCloneOpts .Repo ,
198
+ URL : opts .GsCloneOpts .Repo ,
182
199
}
183
- if err := appDef .CreateApp (ctx , nil , opts .insCloneOpts , opts .runtimeName , store .Get ().CFGitSourceType ); err != nil {
200
+ if err := appDef .CreateApp (ctx , nil , opts .InsCloneOpts , opts .RuntimeName , store .Get ().CFGitSourceType ); err != nil {
184
201
return fmt .Errorf ("failed to create git-source application. Err: %w" , err )
185
202
}
203
+ log .G (ctx ).Infof ("Successfully created the git-source: '%s'" , opts .GsName )
204
+
205
+ return nil
206
+ }
207
+
208
+ func createCronExamplePipeline (opts * gitSourceCronExampleOptions ) error {
209
+ err := createDemoWorkflowTemplate (opts .gsFs , opts .runtimeName )
210
+ if err != nil {
211
+ return fmt .Errorf ("failed to create demo workflowTemplate: %w" , err )
212
+ }
213
+
214
+ eventSourceFilePath := opts .gsFs .Join ("resources" , store .Get ().CronExampleEventSourceFileName )
215
+ sensorFilePath := opts .gsFs .Join ("resources" ,store .Get ().CronExampleSensorFileName )
216
+
217
+ eventSource := & eventsourcev1alpha1.EventSource {
218
+ TypeMeta : metav1.TypeMeta {
219
+ Kind : eventsourcereg .Kind ,
220
+ APIVersion : eventsourcereg .Group + "/v1alpha1" ,
221
+ },
222
+ ObjectMeta : metav1.ObjectMeta {
223
+ Name : store .Get ().CronExampleEventSourceName ,
224
+ Namespace : opts .runtimeName ,
225
+ },
226
+ Spec : eventsourcev1alpha1.EventSourceSpec {
227
+ EventBusName : store .Get ().EventBusName ,
228
+ Calendar : map [string ]eventsourcev1alpha1.CalendarEventSource {
229
+ store .Get ().CronExampleEventName : {
230
+ Interval : "5m" ,
231
+ },
232
+ },
233
+ },
234
+ }
235
+
236
+ err = opts .gsCloneOpts .FS .WriteYamls (eventSourceFilePath , eventSource )
237
+ if err != nil {
238
+ return fmt .Errorf ("failed to write yaml of eventsource. Error: %w" , err )
239
+ }
240
+
241
+ trigger , err := createCronExampleTrigger ()
242
+ triggers := []sensorsv1alpha1.Trigger {* trigger }
243
+ if err != nil {
244
+ return fmt .Errorf ("failed to create cron example trigger. Error: %w" , err )
245
+ }
246
+
247
+ sensor , err := createCronExampleSensor (triggers , opts .runtimeName )
248
+ if err != nil {
249
+ return fmt .Errorf ("failed to create cron example sensor. Error: %w" , err )
250
+ }
186
251
187
- log .G (ctx ).Infof ("Successfully created the git-source: '%s'" , opts .gsName )
252
+ err = opts .gsCloneOpts .FS .WriteYamls (sensorFilePath , sensor )
253
+ if err != nil {
254
+ return fmt .Errorf ("failed to write yaml of cron example sensor. Error: %w" , err )
255
+ }
188
256
189
257
return nil
190
258
}
191
259
260
+ func createCronExampleSensor (triggers []sensorsv1alpha1.Trigger , runtimeName string ) (* sensorsv1alpha1.Sensor , error ) {
261
+ dependencies := []sensorsv1alpha1.EventDependency {
262
+ {
263
+ Name : store .Get ().CronExampleDependencyName ,
264
+ EventSourceName : store .Get ().CronExampleEventSourceName ,
265
+ EventName : store .Get ().CronExampleEventName ,
266
+ },
267
+ }
268
+
269
+ return & sensorsv1alpha1.Sensor {
270
+ TypeMeta : metav1.TypeMeta {
271
+ Kind : sensorreg .Kind ,
272
+ APIVersion : sensorreg .Group + "/v1alpha1" ,
273
+ },
274
+ ObjectMeta : metav1.ObjectMeta {
275
+ Name : "cron" ,
276
+ Namespace : runtimeName ,
277
+ },
278
+ Spec : sensorsv1alpha1.SensorSpec {
279
+ EventBusName : "codefresh-eventbus" ,
280
+ Template : & sensorsv1alpha1.Template {
281
+ ServiceAccountName : "argo-server" ,
282
+ },
283
+ Dependencies : dependencies ,
284
+ Triggers : triggers ,
285
+ },
286
+ }, nil
287
+ }
288
+
289
+ func createCronExampleTrigger () (* sensorsv1alpha1.Trigger , error ) {
290
+ workflow := wfutil .CreateWorkflow (& wfutil.CreateWorkflowOptions {
291
+ GenerateName : "cron-" ,
292
+ SpecWfTemplateRefName : store .Get ().CronExampleTriggerTemplateName ,
293
+ Parameters : []string {
294
+ "message" ,
295
+ },
296
+ })
297
+
298
+ workflowResource := apicommon .NewResource (workflow )
299
+
300
+ return & sensorsv1alpha1.Trigger {
301
+ Template : & sensorsv1alpha1.TriggerTemplate {
302
+ Name : store .Get ().CronExampleTriggerTemplateName ,
303
+ ArgoWorkflow : & sensorsv1alpha1.ArgoWorkflowTrigger {
304
+ GroupVersionResource : metav1.GroupVersionResource {
305
+ Group : "argoproj.io" ,
306
+ Version : "v1alpha1" ,
307
+ Resource : "workflows" ,
308
+ },
309
+ Operation : sensorsv1alpha1 .Submit ,
310
+ Source : & sensorsv1alpha1.ArtifactLocation {
311
+ Resource : & workflowResource ,
312
+ },
313
+ Parameters : []sensorsv1alpha1.TriggerParameter {
314
+ {
315
+ Src : & sensorsv1alpha1.TriggerParameterSource {
316
+ DependencyName : store .Get ().CronExampleDependencyName ,
317
+ DataKey : "eventTime" ,
318
+ },
319
+ Dest : "spec.arguments.parameters.0.value" ,
320
+ },
321
+ },
322
+ },
323
+ },
324
+ }, nil
325
+ }
326
+
192
327
func NewGitSourceListCommand () * cobra.Command {
193
328
cmd := & cobra.Command {
194
329
Use : "list runtime_name" ,
@@ -404,25 +539,35 @@ func createDemoWorkflowTemplate(gsFs fs.FS, runtimeName string) error {
404
539
APIVersion : wfv1alpha1 .SchemeGroupVersion .String (),
405
540
},
406
541
ObjectMeta : metav1.ObjectMeta {
407
- Name : "demo-workflow-template" ,
408
- Namespace : runtimeName ,
542
+ Name : store .Get ().CronExampleTriggerTemplateName ,
409
543
},
410
544
Spec : wfv1alpha1.WorkflowTemplateSpec {
411
545
WorkflowSpec : wfv1alpha1.WorkflowSpec {
412
- Entrypoint : "whalesay" ,
546
+ Arguments : wfv1alpha1.Arguments {
547
+ Parameters : []wfv1alpha1.Parameter {{Name : "message" }},
548
+ },
549
+ Entrypoint : "whalesay" ,
550
+ ServiceAccountName : store .Get ().CodefreshSA ,
551
+ PodGC : & wfv1alpha1.PodGC {
552
+ Strategy : wfv1alpha1 .PodGCOnWorkflowCompletion ,
553
+ },
413
554
Templates : []wfv1alpha1.Template {
414
555
{
415
556
Name : "whalesay" ,
557
+ Inputs : wfv1alpha1.Inputs {
558
+ Parameters : []wfv1alpha1.Parameter {{Name : "message" , Value : wfv1alpha1 .AnyStringPtr ("hello world" )}},
559
+ Artifacts : wfv1alpha1.Artifacts {},
560
+ },
416
561
Container : & v1.Container {
417
- Image : "docker/whalesay" ,
562
+ Image : "docker/whalesay:latest " ,
418
563
Command : []string {"cowsay" },
419
- Args : []string {"Hello World " },
564
+ Args : []string {"{{inputs.parameters.message}} " },
420
565
},
421
566
},
422
567
},
423
568
},
424
569
},
425
570
}
426
571
427
- return gsFs .WriteYamls ("demo-wf-template.yaml" , wfTemplate )
572
+ return gsFs .WriteYamls (store . Get (). CronExampleWfTemplateFileName , wfTemplate )
428
573
}
0 commit comments