|
1 | 1 | package deprovision |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/openshift/installer/pkg/destroy/providers" |
| 9 | + "github.com/openshift/installer/pkg/types" |
| 10 | + |
| 11 | + "github.com/openshift/hive/contrib/pkg/utils" |
| 12 | + "github.com/openshift/hive/pkg/constants" |
| 13 | + |
4 | 14 | "github.com/spf13/cobra" |
5 | 15 | ) |
6 | 16 |
|
7 | 17 | // NewDeprovisionCommand is the entrypoint to create the 'deprovision' subcommand |
8 | 18 | func NewDeprovisionCommand() *cobra.Command { |
9 | 19 | var credsDir string |
| 20 | + var mjSecretName string |
| 21 | + var logLevel string |
10 | 22 | cmd := &cobra.Command{ |
11 | 23 | Use: "deprovision", |
12 | 24 | Short: "Deprovision clusters in supported cloud providers", |
| 25 | + Long: `Platform subcommands use a legacy code path and are deprecated. \ |
| 26 | +To run the generic destroyer, use the --metadata-json-secret-name parameter.`, |
13 | 27 | Run: func(cmd *cobra.Command, args []string) { |
14 | | - cmd.Usage() |
| 28 | + if mjSecretName == "" { |
| 29 | + cmd.Usage() |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + // Generic deprovision flow using metadata.json |
| 34 | + logger, err := utils.NewLogger(logLevel) |
| 35 | + if err != nil { |
| 36 | + log.Fatalf("failed to create logger: %s", err) |
| 37 | + } |
| 38 | + |
| 39 | + client, err := utils.GetClient("hiveutil-deprovision-generic") |
| 40 | + if err != nil { |
| 41 | + logger.WithError(err).Fatal("failed to create kube client") |
| 42 | + } |
| 43 | + |
| 44 | + // TODO: Refactor LoadSecretOrDie to avoid this setenv/getenv cycle |
| 45 | + k := "METADATA_JSON_SECRET_NAME" |
| 46 | + os.Setenv(k, mjSecretName) |
| 47 | + mjSecret := utils.LoadSecretOrDie(client, k) |
| 48 | + if mjSecret == nil { |
| 49 | + // This should not be reachable -- we should have Fatal()ed in LoadSecretOrDie() |
| 50 | + logger.WithField("secretName", mjSecretName).Fatal("failed to load metadata.json Secret") |
| 51 | + } |
| 52 | + |
| 53 | + mjBytes, ok := mjSecret.Data[constants.MetadataJSONSecretKey] |
| 54 | + if !ok { |
| 55 | + logger.Fatalf("metadata.json Secret did not contain %q key", constants.MetadataJSONSecretKey) |
| 56 | + } |
| 57 | + |
| 58 | + var metadata *types.ClusterMetadata |
| 59 | + if err = json.Unmarshal(mjBytes, &metadata); err != nil { |
| 60 | + logger.WithError(err).Fatal("failed to unmarshal metadata.json") |
| 61 | + } |
| 62 | + |
| 63 | + platform := metadata.Platform() |
| 64 | + if platform == "" { |
| 65 | + logger.Fatal("no platform configured in metadata.json") |
| 66 | + } |
| 67 | + |
| 68 | + destroyerBuilder, ok := providers.Registry[platform] |
| 69 | + if !ok { |
| 70 | + logger.WithField("platform", platform).Fatal("no destroyers registered for platform") |
| 71 | + } |
| 72 | + |
| 73 | + destroyer, err := destroyerBuilder(logger, metadata) |
| 74 | + if err != nil { |
| 75 | + logger.WithError(err).Fatal("failed to create destroyer") |
| 76 | + } |
| 77 | + |
| 78 | + // Ignore quota return |
| 79 | + _, err = destroyer.Run() |
| 80 | + if err != nil { |
| 81 | + logger.WithError(err).Fatal("destroyer returned an error") |
| 82 | + } |
15 | 83 | }, |
16 | 84 | } |
17 | 85 | flags := cmd.PersistentFlags() |
| 86 | + // TODO: Unused -- remove from here and generate.go |
18 | 87 | flags.StringVar(&credsDir, "creds-dir", "", "directory of the creds. Changes in the creds will cause the program to terminate") |
19 | | - cmd.AddCommand(NewDeprovisionAzureCommand()) |
20 | | - cmd.AddCommand(NewDeprovisionGCPCommand()) |
21 | | - cmd.AddCommand(NewDeprovisionIBMCloudCommand()) |
22 | | - cmd.AddCommand(NewDeprovisionOpenStackCommand()) |
23 | | - cmd.AddCommand(NewDeprovisionvSphereCommand()) |
24 | | - cmd.AddCommand(NewDeprovisionOvirtCommand()) |
25 | | - cmd.AddCommand(NewDeprovisionNutanixCommand()) |
| 88 | + // TODO: Make this more useful to CLI users by accepting a path to a metadata.json file in the file system |
| 89 | + flags.StringVar(&mjSecretName, "metadata-json-secret-name", "", "name of a Secret in the current namespace containing `metadata.json` from the installer") |
| 90 | + flags.StringVar(&logLevel, "loglevel", "info", "log level, one of: debug, info, warn, error, fatal, panic") |
| 91 | + |
| 92 | + // Legacy destroyers |
| 93 | + cmd.AddCommand(NewDeprovisionAzureCommand(logLevel)) |
| 94 | + cmd.AddCommand(NewDeprovisionGCPCommand(logLevel)) |
| 95 | + cmd.AddCommand(NewDeprovisionIBMCloudCommand(logLevel)) |
| 96 | + cmd.AddCommand(NewDeprovisionOpenStackCommand(logLevel)) |
| 97 | + cmd.AddCommand(NewDeprovisionvSphereCommand(logLevel)) |
| 98 | + cmd.AddCommand(NewDeprovisionOvirtCommand(logLevel)) |
| 99 | + cmd.AddCommand(NewDeprovisionNutanixCommand(logLevel)) |
26 | 100 | return cmd |
27 | 101 | } |
0 commit comments