RunCleanupImages executes the cleanup images command logic
(f factory.Factory, cobraCmd *cobra.Command, args []string)
| 41 | |
| 42 | // RunCleanupImages executes the cleanup images command logic |
| 43 | func (cmd *imagesCmd) RunCleanupImages(f factory.Factory, cobraCmd *cobra.Command, args []string) error { |
| 44 | // Set config root |
| 45 | ctx := context.Background() |
| 46 | log := f.GetLog() |
| 47 | |
| 48 | configLoader, err := f.NewConfigLoader(cmd.ConfigPath) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | |
| 53 | configExists, err := configLoader.SetDevSpaceRoot(log) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | if !configExists { |
| 59 | return errors.New(message.ConfigNotFound) |
| 60 | } |
| 61 | |
| 62 | kubeClient, err := f.NewKubeClientFromContext(cmd.KubeContext, cmd.Namespace) |
| 63 | if err != nil { |
| 64 | return errors.Wrap(err, "new kube client") |
| 65 | } |
| 66 | |
| 67 | // Create docker client |
| 68 | client, err := docker.NewClientWithMinikube(ctx, kubeClient, true, log) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | // Load config |
| 74 | configInterface, err := configLoader.Load(ctx, kubeClient, cmd.ToConfigOptions(), log) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | config := configInterface.Config() |
| 80 | if len(config.Images) == 0 { |
| 81 | log.Done("No images found in config to delete") |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | _, err = client.Ping(ctx) |
| 86 | if err != nil { |
| 87 | return errors.Errorf("Docker seems to be not running: %v", err) |
| 88 | } |
| 89 | |
| 90 | // Delete all images |
| 91 | for _, imageConfig := range config.Images { |
| 92 | log.Info("Deleting local image " + imageConfig.Image + "...") |
| 93 | |
| 94 | response, err := client.DeleteImageByName(ctx, imageConfig.Image, log) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | |
| 99 | for _, t := range response { |
| 100 | if t.Deleted != "" { |
no test coverage detected