|
1 | 1 | package deprovision
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 9 | + |
| 10 | + "github.com/openshift/installer/pkg/destroy/providers" |
| 11 | + "github.com/openshift/installer/pkg/types" |
| 12 | + "github.com/openshift/installer/pkg/types/aws" |
| 13 | + "github.com/openshift/installer/pkg/types/azure" |
| 14 | + "github.com/openshift/installer/pkg/types/gcp" |
| 15 | + "github.com/openshift/installer/pkg/types/ibmcloud" |
| 16 | + "github.com/openshift/installer/pkg/types/nutanix" |
| 17 | + "github.com/openshift/installer/pkg/types/openstack" |
| 18 | + "github.com/openshift/installer/pkg/types/vsphere" |
| 19 | + |
| 20 | + "github.com/openshift/hive/contrib/pkg/utils" |
| 21 | + awsutil "github.com/openshift/hive/contrib/pkg/utils/aws" |
| 22 | + azureutil "github.com/openshift/hive/contrib/pkg/utils/azure" |
| 23 | + gcputil "github.com/openshift/hive/contrib/pkg/utils/gcp" |
| 24 | + ibmcloudutil "github.com/openshift/hive/contrib/pkg/utils/ibmcloud" |
| 25 | + nutanixutil "github.com/openshift/hive/contrib/pkg/utils/nutanix" |
| 26 | + openstackutil "github.com/openshift/hive/contrib/pkg/utils/openstack" |
| 27 | + vsphereutil "github.com/openshift/hive/contrib/pkg/utils/vsphere" |
| 28 | + "github.com/openshift/hive/pkg/constants" |
| 29 | + |
4 | 30 | "github.com/spf13/cobra"
|
5 | 31 | )
|
6 | 32 |
|
7 | 33 | // NewDeprovisionCommand is the entrypoint to create the 'deprovision' subcommand
|
8 | 34 | func NewDeprovisionCommand() *cobra.Command {
|
9 | 35 | var credsDir string
|
| 36 | + var mjSecretName string |
| 37 | + var logLevel string |
10 | 38 | cmd := &cobra.Command{
|
11 | 39 | Use: "deprovision",
|
12 | 40 | Short: "Deprovision clusters in supported cloud providers",
|
| 41 | + Long: `Platform subcommands use a legacy code path and are deprecated. \ |
| 42 | +To run the generic destroyer, use the --metadata-json-secret-name parameter.`, |
13 | 43 | Run: func(cmd *cobra.Command, args []string) {
|
14 |
| - cmd.Usage() |
| 44 | + if mjSecretName == "" { |
| 45 | + cmd.Usage() |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // Generic deprovision flow using metadata.json |
| 50 | + logger, err := utils.NewLogger(logLevel) |
| 51 | + if err != nil { |
| 52 | + log.Fatalf("failed to create logger: %s", err) |
| 53 | + } |
| 54 | + |
| 55 | + c, err := utils.GetClient("hiveutil-deprovision-generic") |
| 56 | + if err != nil { |
| 57 | + logger.WithError(err).Fatal("failed to create kube client") |
| 58 | + } |
| 59 | + |
| 60 | + // TODO: Refactor LoadSecretOrDie to avoid this setenv/getenv cycle |
| 61 | + k := "METADATA_JSON_SECRET_NAME" |
| 62 | + os.Setenv(k, mjSecretName) |
| 63 | + mjSecret := utils.LoadSecretOrDie(c, k) |
| 64 | + if mjSecret == nil { |
| 65 | + // This should not be reachable -- we should have Fatal()ed in LoadSecretOrDie() |
| 66 | + logger.WithField("secretName", mjSecretName).Fatal("failed to load metadata.json Secret") |
| 67 | + } |
| 68 | + |
| 69 | + mjBytes, ok := mjSecret.Data[constants.MetadataJSONSecretKey] |
| 70 | + if !ok { |
| 71 | + logger.Fatalf("metadata.json Secret did not contain %q key", constants.MetadataJSONSecretKey) |
| 72 | + } |
| 73 | + |
| 74 | + var metadata *types.ClusterMetadata |
| 75 | + if err = json.Unmarshal(mjBytes, &metadata); err != nil { |
| 76 | + logger.WithError(err).Fatal("failed to unmarshal metadata.json") |
| 77 | + } |
| 78 | + |
| 79 | + platform := metadata.Platform() |
| 80 | + if platform == "" { |
| 81 | + logger.Fatal("no platform configured in metadata.json") |
| 82 | + } |
| 83 | + |
| 84 | + // TODO: Make a registry or interface for this |
| 85 | + var ConfigureCreds func(client.Client) |
| 86 | + switch platform { |
| 87 | + case aws.Name: |
| 88 | + ConfigureCreds = awsutil.ConfigureCreds |
| 89 | + case azure.Name: |
| 90 | + ConfigureCreds = azureutil.ConfigureCreds |
| 91 | + case gcp.Name: |
| 92 | + ConfigureCreds = gcputil.ConfigureCreds |
| 93 | + case ibmcloud.Name: |
| 94 | + ConfigureCreds = ibmcloudutil.ConfigureCreds |
| 95 | + case nutanix.Name: |
| 96 | + // Snowflake! We need to inject the creds into the metadata. |
| 97 | + // If env vars are unset, the destroyer will fail organically. |
| 98 | + ConfigureCreds = func(c client.Client) { |
| 99 | + nutanixutil.ConfigureCreds(c) |
| 100 | + metadata.Nutanix.Username = os.Getenv(constants.NutanixUsernameEnvVar) |
| 101 | + metadata.Nutanix.Password = os.Getenv(constants.NutanixPasswordEnvVar) |
| 102 | + } |
| 103 | + case openstack.Name: |
| 104 | + ConfigureCreds = openstackutil.ConfigureCreds |
| 105 | + case vsphere.Name: |
| 106 | + // Snowflake! We need to (re)inject the creds into the metadata. |
| 107 | + // (They were there originally, but we scrubbed them for security.) |
| 108 | + // If env vars are unset, the destroyer will fail organically. |
| 109 | + ConfigureCreds = func(c client.Client) { |
| 110 | + vsphereutil.ConfigureCreds(c) |
| 111 | + username, password := os.Getenv(constants.VSphereUsernameEnvVar), os.Getenv(constants.VSpherePasswordEnvVar) |
| 112 | + // Accommodate both pre- and post-zonal formats |
| 113 | + if metadata.VSphere.Username != "" { |
| 114 | + metadata.VSphere.Username = username |
| 115 | + } |
| 116 | + if metadata.VSphere.Password != "" { |
| 117 | + metadata.VSphere.Password = password |
| 118 | + } |
| 119 | + for i := range metadata.VSphere.VCenters { |
| 120 | + if metadata.VSphere.VCenters[i].Username != "" { |
| 121 | + metadata.VSphere.VCenters[i].Username = username |
| 122 | + } |
| 123 | + if metadata.VSphere.VCenters[i].Password != "" { |
| 124 | + metadata.VSphere.VCenters[i].Password = password |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + ConfigureCreds(c) |
| 131 | + |
| 132 | + destroyerBuilder, ok := providers.Registry[platform] |
| 133 | + if !ok { |
| 134 | + logger.WithField("platform", platform).Fatal("no destroyers registered for platform") |
| 135 | + } |
| 136 | + |
| 137 | + destroyer, err := destroyerBuilder(logger, metadata) |
| 138 | + if err != nil { |
| 139 | + logger.WithError(err).Fatal("failed to create destroyer") |
| 140 | + } |
| 141 | + |
| 142 | + // Ignore quota return |
| 143 | + _, err = destroyer.Run() |
| 144 | + if err != nil { |
| 145 | + logger.WithError(err).Fatal("destroyer returned an error") |
| 146 | + } |
15 | 147 | },
|
16 | 148 | }
|
17 | 149 | flags := cmd.PersistentFlags()
|
| 150 | + // TODO: Unused -- remove from here and generate.go |
18 | 151 | 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(NewDeprovisionNutanixCommand()) |
| 152 | + // TODO: Make this more useful to CLI users by accepting a path to a metadata.json file in the file system |
| 153 | + flags.StringVar(&mjSecretName, "metadata-json-secret-name", "", "name of a Secret in the current namespace containing `metadata.json` from the installer") |
| 154 | + flags.StringVar(&logLevel, "loglevel", "info", "log level, one of: debug, info, warn, error, fatal, panic") |
| 155 | + |
| 156 | + // Legacy destroyers |
| 157 | + cmd.AddCommand(NewDeprovisionAzureCommand(logLevel)) |
| 158 | + cmd.AddCommand(NewDeprovisionGCPCommand(logLevel)) |
| 159 | + cmd.AddCommand(NewDeprovisionIBMCloudCommand(logLevel)) |
| 160 | + cmd.AddCommand(NewDeprovisionOpenStackCommand(logLevel)) |
| 161 | + cmd.AddCommand(NewDeprovisionvSphereCommand(logLevel)) |
| 162 | + cmd.AddCommand(NewDeprovisionNutanixCommand(logLevel)) |
25 | 163 | return cmd
|
26 | 164 | }
|
0 commit comments