Skip to content

Commit

Permalink
Move GetAccountID function to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik-K-N committed Jan 10, 2023
1 parent 7f1e8e7 commit a3ba727
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
15 changes: 10 additions & 5 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/cluster-api/util/patch"

infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/options"
Expand Down Expand Up @@ -430,13 +431,17 @@ func fetchImageID(image *infrav1beta2.IBMVPCResourceReference, m *MachineScope)
}

// SetProviderID will set the provider id for the machine.
func (m *MachineScope) SetProviderID(id *string) {
func (m *MachineScope) SetProviderID(id *string) error {
// Based on the ProviderIDFormat version the providerID format will be decided.
if options.ProviderIDFormatType(options.ProviderIDFormat) == options.ProviderIDFormatV2 {
if id != nil {
m.IBMVPCMachine.Spec.ProviderID = pointer.String(fmt.Sprintf("ibm://%s///%s/%s", m.IBMVPCClient.GetAccountID(), m.Machine.Spec.ClusterName, *id))
accountID, err := utils.GetAccountID()
if err != nil {
m.Logger.Error(err, "failed to get cloud account id", err.Error())
return err
}
return
m.IBMVPCMachine.Spec.ProviderID = pointer.String(fmt.Sprintf("ibm://%s///%s/%s", accountID, m.Machine.Spec.ClusterName, *id))
} else {
m.IBMVPCMachine.Spec.ProviderID = pointer.String(fmt.Sprintf("ibmvpc://%s/%s", m.Machine.Spec.ClusterName, m.IBMVPCMachine.Name))
}
m.IBMVPCMachine.Spec.ProviderID = pointer.String(fmt.Sprintf("ibmvpc://%s/%s", m.Machine.Spec.ClusterName, m.IBMVPCMachine.Name))
return nil
}
4 changes: 3 additions & 1 deletion controllers/ibmvpcmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ func (r *IBMVPCMachineReconciler) reconcileNormal(machineScope *scope.MachineSco
},
}
_, ok := machineScope.IBMVPCMachine.Labels[capiv1beta1.MachineControlPlaneLabelName]
machineScope.SetProviderID(instance.ID)
if err = machineScope.SetProviderID(instance.ID); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to set provider id IBMVPCMachine %s/%s", machineScope.IBMVPCMachine.Namespace, machineScope.IBMVPCMachine.Name)
}
if ok {
if machineScope.IBMVPCCluster.Spec.ControlPlaneLoadBalancer == nil {
options := &vpcv1.AddInstanceNetworkInterfaceFloatingIPOptions{}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func initFlags(fs *pflag.FlagSet) {
"The minimum interval at which watched resources are reconciled.",
)

// TODO: Can we depreciate it to use provider-id-fmt for both vpc and power vs
// TODO: Depreciate it to use provider-id-fmt for both vpc and power vs
fs.StringVar(
&options.PowerVSProviderIDFormat,
"powervs-provider-id-fmt",
Expand Down
11 changes: 11 additions & 0 deletions pkg/cloud/services/utils/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/golang-jwt/jwt"

"github.com/IBM/go-sdk-core/v5/core"

"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
)

// GetAccount is function parses the account number from the token and returns it.
Expand Down Expand Up @@ -51,3 +53,12 @@ func GetAccount(auth core.Authenticator) (string, error) {

return token.Claims.(jwt.MapClaims)["account"].(map[string]interface{})["bss"].(string), nil
}

func GetAccountID() (string, error) {
auth, err := authenticator.GetAuthenticator()
if err != nil {
return "", err
}
accountID, err := GetAccount(auth)
return accountID, err
}
14 changes: 0 additions & 14 deletions pkg/cloud/services/vpc/mock/vpc_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a3ba727

Please sign in to comment.