@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "fmt"
2020 "os"
21+ "sync"
2122 "time"
2223
2324 "github.com/codefresh-io/cli-v2/pkg/log"
@@ -26,6 +27,7 @@ import (
2627 "github.com/codefresh-io/cli-v2/pkg/util"
2728 cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
2829 eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
30+ "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
2931
3032 "github.com/Masterminds/semver/v3"
3133 appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
@@ -189,28 +191,35 @@ func NewRuntimeInstallCommand() *cobra.Command {
189191}
190192
191193func RunRuntimeInstall (ctx context.Context , opts * RuntimeInstallOptions ) error {
192- rt , err := runtime . Download ( opts . Version , opts . RuntimeName )
194+ runtimes , err := cfConfig . NewClient (). V2 (). Runtime (). List ( ctx )
193195 if err != nil {
194- return fmt . Errorf ( "failed to download runtime definition: %w" , err )
196+ return err
195197 }
196198
197- server , err := util .CurrentServer ()
198- if err != nil {
199- return fmt .Errorf ("failed to get current server address: %w" , err )
199+ for _ , rt := range runtimes {
200+ if rt .Metadata .Name == opts .RuntimeName {
201+ return fmt .Errorf ("failed to create runtime: %s. A runtime by this name already exists" , opts .RuntimeName )
202+ }
200203 }
201204
202- runtimeVersion := "v99.99.99"
203- if rt . Spec . Version != nil { // in dev mode
204- runtimeVersion = rt . Spec . Version . String ( )
205+ rt , err := runtime . Download ( opts . Version , opts . RuntimeName )
206+ if err != nil {
207+ return fmt . Errorf ( "failed to download runtime definition: %w" , err )
205208 }
206209
207- runtimeCreationResponse , err := cfConfig .NewClient ().V2 ().Runtime ().Create (ctx , opts .RuntimeName , server , runtimeVersion )
210+ runtimeCreationResponse , err := cfConfig .NewClient ().V2 ().Runtime ().Create (ctx , opts .RuntimeName )
208211 if err != nil {
209212 return fmt .Errorf ("failed to create a new runtime: %w" , err )
210213 }
211214
212215 opts .RuntimeToken = runtimeCreationResponse .NewAccessToken
213216
217+ server , err := util .CurrentServer ()
218+ if err != nil {
219+ return fmt .Errorf ("failed to get current server address: %w" , err )
220+ }
221+ rt .Spec .Cluster = server
222+
214223 log .G (ctx ).WithField ("version" , rt .Spec .Version ).Infof ("installing runtime '%s'" , opts .RuntimeName )
215224 err = apcmd .RunRepoBootstrap (ctx , & apcmd.RepoBootstrapOptions {
216225 AppSpecifier : rt .Spec .FullSpecifier (),
@@ -250,7 +259,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
250259 }
251260 }
252261
253- if err = createEventsReporter (ctx , opts .insCloneOpts , opts ); err != nil {
262+ if err = createEventsReporter (ctx , opts .insCloneOpts , opts , rt ); err != nil {
254263 return fmt .Errorf ("failed to create events-reporter: %w" , err )
255264 }
256265
@@ -271,10 +280,43 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
271280 return fmt .Errorf ("failed to create `%s`: %w" , store .Get ().GitSourceName , err )
272281 }
273282
283+ var wg sync.WaitGroup
284+
285+ wg .Add (1 )
286+ go intervalCheckIsRuntimePersisted (15000 , ctx , opts .RuntimeName , & wg )
287+ wg .Wait ()
288+
274289 log .G (ctx ).Infof ("done installing runtime '%s'" , opts .RuntimeName )
275290 return nil
276291}
277292
293+ func intervalCheckIsRuntimePersisted (milliseconds int , ctx context.Context , runtimeName string , wg * sync.WaitGroup ) {
294+ interval := time .Duration (milliseconds ) * time .Millisecond
295+ ticker := time .NewTicker (interval )
296+ var err error
297+
298+ for retries := 20 ; retries > 0 ; <- ticker .C {
299+ fmt .Println ("waiting for the runtime installation to complete..." )
300+ var runtimes []model.Runtime
301+ runtimes , err = cfConfig .NewClient ().V2 ().Runtime ().List (ctx )
302+ if err != nil {
303+ continue
304+ }
305+
306+ for _ , rt := range runtimes {
307+ if rt .Metadata .Name == runtimeName {
308+ wg .Done ()
309+ ticker .Stop ()
310+ return
311+ }
312+ }
313+
314+ retries --
315+ }
316+
317+ panic (fmt .Errorf ("failed to complete the runtime installation due to error: %w" , err ))
318+ }
319+
278320func NewRuntimeListCommand () * cobra.Command {
279321 cmd := & cobra.Command {
280322 Use : "list [runtime_name]" ,
@@ -534,7 +576,7 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
534576 return err
535577}
536578
537- func createEventsReporter (ctx context.Context , cloneOpts * git.CloneOptions , opts * RuntimeInstallOptions ) error {
579+ func createEventsReporter (ctx context.Context , cloneOpts * git.CloneOptions , opts * RuntimeInstallOptions , rt * runtime. Runtime ) error {
538580 runtimeTokenSecret , err := getRuntimeTokenSecret (opts .RuntimeName , opts .RuntimeToken )
539581 if err != nil {
540582 return fmt .Errorf ("failed to create codefresh token secret: %w" , err )
@@ -564,7 +606,7 @@ func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts
564606 return err
565607 }
566608
567- if err := updateProject (repofs , opts .RuntimeName ); err != nil {
609+ if err := updateProject (repofs , opts .RuntimeName , rt ); err != nil {
568610 return err
569611 }
570612
@@ -616,7 +658,7 @@ func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, op
616658 return err
617659}
618660
619- func updateProject (repofs fs.FS , runtimeName string ) error {
661+ func updateProject (repofs fs.FS , runtimeName string , rt * runtime. Runtime ) error {
620662 projPath := repofs .Join (apstore .Default .ProjectsDir , runtimeName + ".yaml" )
621663 project , appset , err := getProjectInfoFromFile (repofs , projPath )
622664 if err != nil {
@@ -627,7 +669,14 @@ func updateProject(repofs fs.FS, runtimeName string) error {
627669 project .ObjectMeta .Labels = make (map [string ]string )
628670 }
629671
672+ runtimeVersion := "v99.99.99"
673+ if rt .Spec .Version != nil { // in dev mode
674+ runtimeVersion = rt .Spec .Version .String ()
675+ }
676+
630677 project .ObjectMeta .Labels [store .Get ().LabelKeyCFType ] = store .Get ().CFRuntimeType
678+ project .ObjectMeta .Labels [store .Get ().LabelKeyRuntimeVersion ] = runtimeVersion
679+
631680 return repofs .WriteYamls (projPath , project , appset )
632681}
633682
0 commit comments