Skip to content

Commit 42c37de

Browse files
committed
HIVE-2302: Refactor ConfigureCreds to modify installer metadata
A couple of providers (nutanix, vsphere) need bespoke code to populate credentials in the metadata.json object for destroying a cluster. In a prior commit this was being done in the deprovisioner (the new one, that uses metadata.json directly, per HIVE-2302) after ConfigureCreds. Since ConfigureCreds is where we (stay with me) configure creds, and is already platform-specific, it makes more sense to do this work there. This commit refactors to do so. Legacy code paths pass in a `nil` metadata object, which is coded to result in no change from the previous functionality. (In particular, ConfigureCreds is also used when provisioning, where no metadata object is present/necessary.)
1 parent e147330 commit 42c37de

File tree

16 files changed

+94
-77
lines changed

16 files changed

+94
-77
lines changed

contrib/pkg/deprovision/awstagdeprovision.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func completeAWSUninstaller(o *aws.ClusterUninstaller, logLevel string, args []s
6565
"This is expected when in standalone mode. "+
6666
"We expect to find your AWS credentials in one of the usual places.", err)
6767
}
68-
awsutils.ConfigureCreds(client)
68+
awsutils.ConfigureCreds(client, nil)
6969

7070
return nil
7171
}

contrib/pkg/deprovision/azure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (opt *AzureOptions) completeAzureUninstaller(args []string) (providers.Dest
7474
if err != nil {
7575
return nil, errors.Wrap(err, "failed to get client")
7676
}
77-
azureutils.ConfigureCreds(client)
77+
azureutils.ConfigureCreds(client, nil)
7878

7979
metadata := &types.ClusterMetadata{
8080
InfraID: args[0],

contrib/pkg/deprovision/deprovision.go

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -81,53 +81,7 @@ To run the generic destroyer, use the --metadata-json-secret-name parameter.`,
8181
logger.Fatal("no platform configured in metadata.json")
8282
}
8383

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)
84+
ConfigureCreds[platform](c, metadata)
13185

13286
destroyerBuilder, ok := providers.Registry[platform]
13387
if !ok {
@@ -162,3 +116,13 @@ To run the generic destroyer, use the --metadata-json-secret-name parameter.`,
162116
cmd.AddCommand(NewDeprovisionNutanixCommand(logLevel))
163117
return cmd
164118
}
119+
120+
var ConfigureCreds = map[string]func(client.Client, *types.ClusterMetadata){
121+
aws.Name: awsutil.ConfigureCreds,
122+
azure.Name: azureutil.ConfigureCreds,
123+
gcp.Name: gcputil.ConfigureCreds,
124+
ibmcloud.Name: ibmcloudutil.ConfigureCreds,
125+
nutanix.Name: nutanixutil.ConfigureCreds,
126+
openstack.Name: openstackutil.ConfigureCreds,
127+
vsphere.Name: vsphereutil.ConfigureCreds,
128+
}

contrib/pkg/deprovision/gcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (o *gcpOptions) Complete(cmd *cobra.Command, args []string) error {
6060
if err != nil {
6161
return errors.Wrap(err, "failed to get client")
6262
}
63-
gcputils.ConfigureCreds(client)
63+
gcputils.ConfigureCreds(client, nil)
6464

6565
return nil
6666
}

contrib/pkg/deprovision/ibmcloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (o *ibmCloudDeprovisionOptions) Complete(cmd *cobra.Command, args []string)
6969
if err != nil {
7070
return errors.Wrap(err, "failed to get client")
7171
}
72-
ibmutils.ConfigureCreds(client)
72+
ibmutils.ConfigureCreds(client, nil)
7373

7474
// Create IBMCloud Client
7575
ibmCloudAPIKey := os.Getenv(constants.IBMCloudAPIKeyEnvVar)

contrib/pkg/deprovision/nutanix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (o *nutanixOptions) Complete(cmd *cobra.Command, args []string) error {
6161
if err != nil {
6262
return errors.Wrap(err, "failed to get client")
6363
}
64-
nutanixutils.ConfigureCreds(client)
64+
nutanixutils.ConfigureCreds(client, nil)
6565

6666
return nil
6767
}

contrib/pkg/deprovision/openstack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (o *openStackOptions) Complete(cmd *cobra.Command, args []string) error {
5656
if err != nil {
5757
return errors.Wrap(err, "failed to get client")
5858
}
59-
openstackutils.ConfigureCreds(client)
59+
openstackutils.ConfigureCreds(client, nil)
6060

6161
return nil
6262
}

contrib/pkg/deprovision/vsphere.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (o *vSphereOptions) Complete(cmd *cobra.Command, args []string) error {
6060
if err != nil {
6161
return errors.Wrap(err, "failed to get client")
6262
}
63-
vsphereutils.ConfigureCreds(client)
63+
vsphereutils.ConfigureCreds(client, nil)
6464

6565
return nil
6666
}

contrib/pkg/utils/aws/aws.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import (
77
"path/filepath"
88
"strings"
99

10+
log "github.com/sirupsen/logrus"
11+
ini "gopkg.in/ini.v1"
12+
1013
corev1 "k8s.io/api/core/v1"
14+
"k8s.io/apimachinery/pkg/util/sets"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
1116

1217
"github.com/aws/aws-sdk-go-v2/aws"
1318
"github.com/aws/aws-sdk-go-v2/service/ec2"
@@ -18,10 +23,7 @@ import (
1823
"github.com/openshift/hive/pkg/awsclient"
1924
"github.com/openshift/hive/pkg/constants"
2025

21-
log "github.com/sirupsen/logrus"
22-
ini "gopkg.in/ini.v1"
23-
"k8s.io/apimachinery/pkg/util/sets"
24-
"sigs.k8s.io/controller-runtime/pkg/client"
26+
installertypes "github.com/openshift/installer/pkg/types"
2527
)
2628

2729
const (
@@ -258,7 +260,7 @@ var awsConfigForbidCredentialProcess utils.ProjectToDirFileFilter = func(key str
258260
// ConfigureCreds loads a secret designated by the environment variables CLUSTERDEPLOYMENT_NAMESPACE
259261
// and CREDS_SECRET_NAME and configures AWS credential environment variables and config files
260262
// accordingly.
261-
func ConfigureCreds(c client.Client) {
263+
func ConfigureCreds(c client.Client, metadata *installertypes.ClusterMetadata) {
262264
credsSecret := utils.LoadSecretOrDie(c, "CREDS_SECRET_NAME")
263265
if credsSecret == nil {
264266
return

contrib/pkg/utils/azure/azure.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"os"
55
"path/filepath"
66

7+
installertypes "github.com/openshift/installer/pkg/types"
78
log "github.com/sirupsen/logrus"
8-
"sigs.k8s.io/controller-runtime/pkg/client"
99

1010
"k8s.io/client-go/util/homedir"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
1112

1213
"github.com/openshift/hive/contrib/pkg/utils"
1314
"github.com/openshift/hive/pkg/constants"
@@ -31,7 +32,7 @@ func GetCreds(credsFile string) ([]byte, error) {
3132
// ConfigureCreds loads a secret designated by the environment variables CLUSTERDEPLOYMENT_NAMESPACE
3233
// and CREDS_SECRET_NAME and configures Azure credential environment variables and config files
3334
// accordingly.
34-
func ConfigureCreds(c client.Client) {
35+
func ConfigureCreds(c client.Client, metadata *installertypes.ClusterMetadata) {
3536
credsSecret := utils.LoadSecretOrDie(c, "CREDS_SECRET_NAME")
3637
if credsSecret == nil {
3738
return

0 commit comments

Comments
 (0)