Skip to content

Commit

Permalink
Modify provider id set logic to explicity return error (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik-K-N authored Nov 7, 2024
1 parent 4de83a5 commit c39857b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
38 changes: 27 additions & 11 deletions cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,29 +929,45 @@ func (m *PowerVSMachineScope) GetZone() string {
}

// GetServiceInstanceID returns the service instance id.
func (m *PowerVSMachineScope) GetServiceInstanceID() string {
func (m *PowerVSMachineScope) GetServiceInstanceID() (string, error) {
if m.IBMPowerVSCluster.Status.ServiceInstance != nil && m.IBMPowerVSCluster.Status.ServiceInstance.ID != nil {
return *m.IBMPowerVSCluster.Status.ServiceInstance.ID
return *m.IBMPowerVSCluster.Status.ServiceInstance.ID, nil
}
if m.IBMPowerVSCluster.Spec.ServiceInstanceID != "" {
return m.IBMPowerVSCluster.Spec.ServiceInstanceID
return m.IBMPowerVSCluster.Spec.ServiceInstanceID, nil
}
if m.IBMPowerVSCluster.Spec.ServiceInstance != nil && m.IBMPowerVSCluster.Spec.ServiceInstance.ID != nil {
return *m.IBMPowerVSCluster.Spec.ServiceInstance.ID
return *m.IBMPowerVSCluster.Spec.ServiceInstance.ID, nil
}
return ""
// If we are not able to find service instance id, derive it from name if defined.
if m.IBMPowerVSCluster.Spec.ServiceInstance != nil && m.IBMPowerVSCluster.Spec.ServiceInstance.Name == nil {
return "", fmt.Errorf("failed to find service instance id as both name and id are not set")
}
serviceInstance, err := m.ResourceClient.GetServiceInstance("", *m.IBMPowerVSCluster.Spec.ServiceInstance.Name, ptr.To(m.GetZone()))
if err != nil {
m.Error(err, "failed to get Power VS service instance id", "serviceInstanceName", *m.IBMPowerVSCluster.Spec.ServiceInstance.Name)
return "", err
}
// It's safe to directly dereference GUID as its already done in NewPowerVSMachineScope
return *serviceInstance.GUID, nil
}

// SetProviderID will set the provider id for the machine.
func (m *PowerVSMachineScope) SetProviderID(id *string) {
func (m *PowerVSMachineScope) SetProviderID(instanceID string) error {
// Based on the ProviderIDFormat version the providerID format will be decided.
if options.ProviderIDFormatType(options.ProviderIDFormat) == options.ProviderIDFormatV2 {
if id != nil {
m.IBMPowerVSMachine.Spec.ProviderID = ptr.To(fmt.Sprintf("ibmpowervs://%s/%s/%s/%s", m.GetRegion(), m.GetZone(), m.GetServiceInstanceID(), *id))
}
} else {
if options.ProviderIDFormatType(options.ProviderIDFormat) == options.ProviderIDFormatV1 {
m.IBMPowerVSMachine.Spec.ProviderID = ptr.To(fmt.Sprintf("ibmpowervs://%s/%s", m.Machine.Spec.ClusterName, m.IBMPowerVSMachine.Name))
return nil
}
m.V(3).Info("setting provider id in v2 format")

serviceInstanceID, err := m.GetServiceInstanceID()
if err != nil {
m.Error(err, "failed to get service instance ID")
return err
}
m.IBMPowerVSMachine.Spec.ProviderID = ptr.To(fmt.Sprintf("ibmpowervs://%s/%s/%s/%s", m.GetRegion(), m.GetZone(), serviceInstanceID, instanceID))
return nil
}

// GetMachineInternalIP returns the machine's internal IP.
Expand Down
4 changes: 3 additions & 1 deletion controllers/ibmpowervsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(machineScope *scope.PowerV
if err != nil {
return ctrl.Result{}, err
}
machineScope.SetProviderID(instance.PvmInstanceID)
if err := machineScope.SetProviderID(*ins.PvmInstanceID); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to set provider id")
}
machineScope.SetInstanceID(instance.PvmInstanceID)
machineScope.SetAddresses(instance)
machineScope.SetHealth(instance.Health)
Expand Down
21 changes: 18 additions & 3 deletions controllers/ibmpowervsmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ func TestIBMPowerVSMachineReconciler_ReconcileOperations(t *testing.T) {
},
},
Spec: infrav1beta2.IBMPowerVSClusterSpec{
ServiceInstance: &infrav1beta2.IBMPowerVSResourceReference{
ID: ptr.To("serviceInstanceID"),
},
VPC: &infrav1beta2.VPCResourceReference{
Region: ptr.To("us-south"),
},
Expand Down Expand Up @@ -606,6 +609,9 @@ func TestIBMPowerVSMachineReconciler_ReconcileOperations(t *testing.T) {
},
},
Spec: infrav1beta2.IBMPowerVSClusterSpec{
ServiceInstance: &infrav1beta2.IBMPowerVSResourceReference{
ID: ptr.To("serviceInstanceID"),
},
VPC: &infrav1beta2.VPCResourceReference{
Region: ptr.To("us-south"),
},
Expand Down Expand Up @@ -708,9 +714,15 @@ func TestIBMPowerVSMachineReconciler_ReconcileOperations(t *testing.T) {
Ready: true,
},
},
IBMPowerVSClient: mockpowervs,
DHCPIPCacheStore: cache.NewTTLStore(powervs.CacheKeyFunc, powervs.CacheTTL),
IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{},
IBMPowerVSClient: mockpowervs,
DHCPIPCacheStore: cache.NewTTLStore(powervs.CacheKeyFunc, powervs.CacheTTL),
IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{
Spec: infrav1beta2.IBMPowerVSClusterSpec{
ServiceInstance: &infrav1beta2.IBMPowerVSResourceReference{
ID: ptr.To("serviceInstanceID"),
},
},
},
}

instanceReferences := &models.PVMInstances{
Expand Down Expand Up @@ -822,6 +834,9 @@ func TestIBMPowerVSMachineReconciler_ReconcileOperations(t *testing.T) {
},
},
Spec: infrav1beta2.IBMPowerVSClusterSpec{
ServiceInstance: &infrav1beta2.IBMPowerVSResourceReference{
ID: ptr.To("serviceInstanceID"),
},
LoadBalancers: []infrav1beta2.VPCLoadBalancerSpec{
{
Name: "capi-test-lb",
Expand Down

0 comments on commit c39857b

Please sign in to comment.