|
| 1 | +package cleanup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/loft-sh/devspace/cmd/flags" |
| 7 | + "github.com/loft-sh/devspace/pkg/devspace/build/registry" |
| 8 | + "github.com/loft-sh/devspace/pkg/devspace/kubectl" |
| 9 | + "github.com/loft-sh/devspace/pkg/util/factory" |
| 10 | + "github.com/loft-sh/devspace/pkg/util/message" |
| 11 | + "github.com/loft-sh/devspace/pkg/util/survey" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + |
| 16 | + "github.com/pkg/errors" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +type localRegistryCmd struct { |
| 21 | + *flags.GlobalFlags |
| 22 | +} |
| 23 | + |
| 24 | +func newLocalRegistryCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command { |
| 25 | + cmd := &localRegistryCmd{GlobalFlags: globalFlags} |
| 26 | + |
| 27 | + localRegistryCmd := &cobra.Command{ |
| 28 | + Use: "local-registry", |
| 29 | + Short: "Deletes the local image registry", |
| 30 | + Long: ` |
| 31 | +####################################################### |
| 32 | +######### devspace cleanup local-registry ############# |
| 33 | +####################################################### |
| 34 | +Deletes the local image registry |
| 35 | +####################################################### |
| 36 | + `, |
| 37 | + Args: cobra.NoArgs, |
| 38 | + RunE: func(cobraCmd *cobra.Command, args []string) error { |
| 39 | + return cmd.RunCleanupLocalRegistry(f, cobraCmd, args) |
| 40 | + }} |
| 41 | + |
| 42 | + return localRegistryCmd |
| 43 | +} |
| 44 | + |
| 45 | +// RunCleanupLocalRegistry executes the cleanup local-registry command logic |
| 46 | +func (cmd *localRegistryCmd) RunCleanupLocalRegistry(f factory.Factory, cobraCmd *cobra.Command, args []string) error { |
| 47 | + ctx := context.Background() |
| 48 | + log := f.GetLog() |
| 49 | + |
| 50 | + // set config root |
| 51 | + configLoader, err := f.NewConfigLoader(cmd.ConfigPath) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + configExists, err := configLoader.SetDevSpaceRoot(log) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } else if !configExists { |
| 59 | + return errors.New(message.ConfigNotFound) |
| 60 | + } |
| 61 | + |
| 62 | + // create kubectl client |
| 63 | + client, err := f.NewKubeClientFromContext(cmd.KubeContext, cmd.Namespace) |
| 64 | + if err != nil { |
| 65 | + log.Warnf("Unable to create new kubectl client: %v", err) |
| 66 | + log.WriteString(logrus.WarnLevel, "\n") |
| 67 | + client = nil |
| 68 | + } |
| 69 | + |
| 70 | + // load generated config |
| 71 | + localCache, err := configLoader.LoadLocalCache() |
| 72 | + if err != nil { |
| 73 | + return errors.Errorf("error loading local cache: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if client != nil { |
| 77 | + // If the current kube context or namespace is different than old, |
| 78 | + // show warnings and reset kube client if necessary |
| 79 | + client, err = kubectl.CheckKubeContext(client, localCache, cmd.NoWarn, cmd.SwitchContext, false, log) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // load config |
| 86 | + configInterface, err := configLoader.LoadWithCache(ctx, localCache, client, cmd.ToConfigOptions(), log) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + // clean up registry according to options |
| 92 | + config := configInterface.Config() |
| 93 | + options := registry.NewDefaultOptions(). |
| 94 | + WithNamespace(client.Namespace()). |
| 95 | + WithLocalRegistryConfig(config.LocalRegistry) |
| 96 | + |
| 97 | + hasStatefulSet := true |
| 98 | + _, err = client.KubeClient().AppsV1().StatefulSets(options.Namespace).Get(ctx, options.Name, v1.GetOptions{}) |
| 99 | + if err != nil { |
| 100 | + if kerrors.IsNotFound(err) { |
| 101 | + hasStatefulSet = false |
| 102 | + } else { |
| 103 | + return errors.Wrap(err, "clean up statefulset") |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + hasDeployment := true |
| 108 | + _, err = client.KubeClient().AppsV1().Deployments(options.Namespace).Get(ctx, options.Name, v1.GetOptions{}) |
| 109 | + if err != nil { |
| 110 | + if kerrors.IsNotFound(err) { |
| 111 | + hasDeployment = false |
| 112 | + } else { |
| 113 | + return errors.Wrap(err, "clean up statefulset") |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + hasService := true |
| 118 | + _, err = client.KubeClient().CoreV1().Services(options.Namespace).Get(ctx, options.Name, v1.GetOptions{}) |
| 119 | + if err != nil { |
| 120 | + if kerrors.IsNotFound(err) { |
| 121 | + hasService = false |
| 122 | + } else { |
| 123 | + return errors.Wrap(err, "clean up statefulset") |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if !hasStatefulSet && !hasDeployment && !hasService { |
| 128 | + log.Donef("No local registry found.") |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + // prompt user since this is a destructive action |
| 133 | + cleanupAnswer, err := log.Question(&survey.QuestionOptions{ |
| 134 | + Question: "This will delete your local registry and all the images it contains. Do you wish to continue?", |
| 135 | + Options: []string{ |
| 136 | + "Yes", |
| 137 | + "No", |
| 138 | + }, |
| 139 | + }) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + if cleanupAnswer == "No" { |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + if hasStatefulSet { |
| 149 | + err = client.KubeClient().AppsV1().StatefulSets(options.Namespace).Delete(ctx, options.Name, v1.DeleteOptions{}) |
| 150 | + if err != nil && !kerrors.IsNotFound(err) { |
| 151 | + return errors.Wrap(err, "clean up statefulset") |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if hasDeployment { |
| 156 | + err = client.KubeClient().AppsV1().Deployments(options.Namespace).Delete(ctx, options.Name, v1.DeleteOptions{}) |
| 157 | + if err != nil && !kerrors.IsNotFound(err) { |
| 158 | + return errors.Wrap(err, "clean up deployment") |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if hasService { |
| 163 | + err = client.KubeClient().CoreV1().Services(options.Namespace).Delete(ctx, options.Name, v1.DeleteOptions{}) |
| 164 | + if err != nil && !kerrors.IsNotFound(err) { |
| 165 | + return errors.Wrap(err, "clean up service") |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + log.Donef("Successfully cleaned up local registry") |
| 170 | + return nil |
| 171 | +} |
0 commit comments