@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "fmt"
2020 "os"
21+ "strings"
2122 "sync"
2223 "time"
2324
7576 Timeout time.Duration
7677 CloneOpts * git.CloneOptions
7778 KubeFactory kube.Factory
79+ SkipChecks bool
7880 }
7981
8082 RuntimeUpgradeOptions struct {
@@ -98,7 +100,7 @@ func NewRuntimeCommand() *cobra.Command {
98100
99101 cmd .AddCommand (NewRuntimeInstallCommand ())
100102 cmd .AddCommand (NewRuntimeListCommand ())
101- cmd .AddCommand (NewRuntimeUninsatllCommand ())
103+ cmd .AddCommand (NewRuntimeUninstallCommand ())
102104 cmd .AddCommand (NewRuntimeUpgradeCommand ())
103105
104106 return cmd
@@ -361,18 +363,16 @@ func checkRuntimeCollisions(ctx context.Context, runtime string, kube kube.Facto
361363}
362364
363365func 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 )
365367 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
372370 }
371+
372+ return fmt .Errorf ("failed to get runtime: %w" , err )
373373 }
374374
375- return nil
375+ return fmt . Errorf ( "runtime '%s' already exists" , runtime )
376376}
377377
378378func intervalCheckIsRuntimePersisted (milliseconds int , ctx context.Context , runtimeName string , wg * sync.WaitGroup ) error {
@@ -469,10 +469,11 @@ func RunRuntimeList(ctx context.Context) error {
469469 return tb .Flush ()
470470}
471471
472- func NewRuntimeUninsatllCommand () * cobra.Command {
472+ func NewRuntimeUninstallCommand () * cobra.Command {
473473 var (
474- f kube.Factory
475- cloneOpts * git.CloneOptions
474+ skipChecks bool
475+ f kube.Factory
476+ cloneOpts * git.CloneOptions
476477 )
477478
478479 cmd := & cobra.Command {
@@ -506,10 +507,12 @@ func NewRuntimeUninsatllCommand() *cobra.Command {
506507 Timeout : store .Get ().WaitTimeout ,
507508 CloneOpts : cloneOpts ,
508509 KubeFactory : f ,
510+ SkipChecks : skipChecks ,
509511 })
510512 },
511513 }
512514
515+ cmd .Flags ().BoolVar (& skipChecks , "skip-checks" , false , "If true, will not verify that runtime exists before uninstalling" )
513516 cmd .Flags ().DurationVar (& store .Get ().WaitTimeout , "wait-timeout" , store .Get ().WaitTimeout , "How long to wait for the runtime components to be deleted" )
514517
515518 cloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
@@ -521,17 +524,31 @@ func NewRuntimeUninsatllCommand() *cobra.Command {
521524}
522525
523526func 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+
524535 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 {
526538 Namespace : opts .RuntimeName ,
527539 Timeout : opts .Timeout ,
528540 CloneOptions : opts .CloneOpts ,
529541 KubeFactory : opts .KubeFactory ,
530- })
531- if err != nil {
542+ }); err != nil {
532543 return fmt .Errorf ("failed uninstalling runtime: %w" , err )
533544 }
534545
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+
535552 log .G (ctx ).Infof ("done uninstalling runtime '%s'" , opts .RuntimeName )
536553 return nil
537554}
0 commit comments