Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly return error incase of not able to set provider id in correct format #2039

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +942 to +952
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we have removed this code intentionally before right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkumatag, this a different function called using PowerVSMachineScope
The function we modified earlier is https://github.com/kubernetes-sigs/cluster-api-provider-ibmcloud/blob/main/cloud/scope/powervs_cluster.go#L388 called using PowerVSClusterScope

}

// 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