@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"fmt"
20
20
"os"
21
+ "strings"
21
22
"sync"
22
23
"time"
23
24
75
76
Timeout time.Duration
76
77
CloneOpts * git.CloneOptions
77
78
KubeFactory kube.Factory
79
+ SkipChecks bool
78
80
}
79
81
80
82
RuntimeUpgradeOptions struct {
@@ -98,7 +100,7 @@ func NewRuntimeCommand() *cobra.Command {
98
100
99
101
cmd .AddCommand (NewRuntimeInstallCommand ())
100
102
cmd .AddCommand (NewRuntimeListCommand ())
101
- cmd .AddCommand (NewRuntimeUninsatllCommand ())
103
+ cmd .AddCommand (NewRuntimeUninstallCommand ())
102
104
cmd .AddCommand (NewRuntimeUpgradeCommand ())
103
105
104
106
return cmd
@@ -361,18 +363,16 @@ func checkRuntimeCollisions(ctx context.Context, runtime string, kube kube.Facto
361
363
}
362
364
363
365
func checkExistingRuntimes (ctx context.Context , runtime string ) error {
364
- runtimes , err := cfConfig .NewClient ().V2 ().Runtime ().List (ctx )
366
+ _ , err := cfConfig .NewClient ().V2 ().Runtime ().Get (ctx , runtime )
365
367
if err != nil {
366
- return fmt .Errorf ("failed to list runtimes: %w" , err )
367
- }
368
-
369
- for _ , rt := range runtimes {
370
- if rt .Metadata .Name == runtime {
371
- return fmt .Errorf ("runtime '%s' already exists" , runtime )
368
+ if strings .Contains (err .Error (), "does not exist" ) {
369
+ return nil // runtime does exist
372
370
}
371
+
372
+ return fmt .Errorf ("failed to get runtime: %w" , err )
373
373
}
374
374
375
- return nil
375
+ return fmt . Errorf ( "runtime '%s' already exists" , runtime )
376
376
}
377
377
378
378
func intervalCheckIsRuntimePersisted (milliseconds int , ctx context.Context , runtimeName string , wg * sync.WaitGroup ) error {
@@ -469,10 +469,11 @@ func RunRuntimeList(ctx context.Context) error {
469
469
return tb .Flush ()
470
470
}
471
471
472
- func NewRuntimeUninsatllCommand () * cobra.Command {
472
+ func NewRuntimeUninstallCommand () * cobra.Command {
473
473
var (
474
- f kube.Factory
475
- cloneOpts * git.CloneOptions
474
+ skipChecks bool
475
+ f kube.Factory
476
+ cloneOpts * git.CloneOptions
476
477
)
477
478
478
479
cmd := & cobra.Command {
@@ -506,10 +507,12 @@ func NewRuntimeUninsatllCommand() *cobra.Command {
506
507
Timeout : store .Get ().WaitTimeout ,
507
508
CloneOpts : cloneOpts ,
508
509
KubeFactory : f ,
510
+ SkipChecks : skipChecks ,
509
511
})
510
512
},
511
513
}
512
514
515
+ cmd .Flags ().BoolVar (& skipChecks , "skip-checks" , false , "If true, will not verify that runtime exists before uninstalling" )
513
516
cmd .Flags ().DurationVar (& store .Get ().WaitTimeout , "wait-timeout" , store .Get ().WaitTimeout , "How long to wait for the runtime components to be deleted" )
514
517
515
518
cloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
@@ -521,17 +524,31 @@ func NewRuntimeUninsatllCommand() *cobra.Command {
521
524
}
522
525
523
526
func RunRuntimeUninstall (ctx context.Context , opts * RuntimeUninstallOptions ) error {
527
+ // check whether the runtime exists
528
+ if ! opts .SkipChecks {
529
+ _ , err := cfConfig .NewClient ().V2 ().Runtime ().Get (ctx , opts .RuntimeName )
530
+ if err != nil {
531
+ return err
532
+ }
533
+ }
534
+
524
535
log .G (ctx ).Infof ("uninstalling runtime '%s'" , opts .RuntimeName )
525
- err := apcmd .RunRepoUninstall (ctx , & apcmd.RepoUninstallOptions {
536
+
537
+ if err := apcmd .RunRepoUninstall (ctx , & apcmd.RepoUninstallOptions {
526
538
Namespace : opts .RuntimeName ,
527
539
Timeout : opts .Timeout ,
528
540
CloneOptions : opts .CloneOpts ,
529
541
KubeFactory : opts .KubeFactory ,
530
- })
531
- if err != nil {
542
+ }); err != nil {
532
543
return fmt .Errorf ("failed uninstalling runtime: %w" , err )
533
544
}
534
545
546
+ log .G (ctx ).Infof ("deleting runtime '%s' from the platform" , opts .RuntimeName )
547
+
548
+ if _ , err := cfConfig .NewClient ().V2 ().Runtime ().Delete (ctx , opts .RuntimeName ); err != nil {
549
+ return fmt .Errorf ("failed to delete runtime from the platform: %w" , err )
550
+ }
551
+
535
552
log .G (ctx ).Infof ("done uninstalling runtime '%s'" , opts .RuntimeName )
536
553
return nil
537
554
}
0 commit comments