Skip to content

Commit

Permalink
Use zone to filter PowerVS service instance
Browse files Browse the repository at this point in the history
  • Loading branch information
dharaneeshvrd committed Jun 21, 2024
1 parent c2108ff commit 46c74bc
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ func (s *PowerVSClusterScope) isServiceInstanceExists() (string, bool, error) {
// getServiceInstance return resource instance by name.
func (s *PowerVSClusterScope) getServiceInstance() (*resourcecontrollerv2.ResourceInstance, error) {
//TODO: Support regular expression
return s.ResourceClient.GetServiceInstance("", *s.GetServiceName(infrav1beta2.ResourceTypeServiceInstance))
return s.ResourceClient.GetServiceInstance("", *s.GetServiceName(infrav1beta2.ResourceTypeServiceInstance), s.IBMPowerVSCluster.Spec.Zone)
}

// createServiceInstance creates the service instance.
Expand Down
11 changes: 6 additions & 5 deletions cloud/scope/powervs_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ const BucketAccess = "public"

// PowerVSImageScopeParams defines the input parameters used to create a new PowerVSImageScope.
type PowerVSImageScopeParams struct {
Client client.Client
Logger logr.Logger
IBMPowerVSImage *infrav1beta2.IBMPowerVSImage
ServiceEndpoint []endpoints.ServiceEndpoint
Client client.Client
Logger logr.Logger
IBMPowerVSImage *infrav1beta2.IBMPowerVSImage
ServiceInstanceZone *string
ServiceEndpoint []endpoints.ServiceEndpoint
}

// PowerVSImageScope defines a scope defined around a Power VS Cluster.
Expand Down Expand Up @@ -116,7 +117,7 @@ func NewPowerVSImageScope(params PowerVSImageScopeParams) (scope *PowerVSImageSc
if params.IBMPowerVSImage.Spec.ServiceInstance != nil && params.IBMPowerVSImage.Spec.ServiceInstance.Name != nil {
name = *params.IBMPowerVSImage.Spec.ServiceInstance.Name
}
serviceInstance, err := rc.GetServiceInstance("", name)
serviceInstance, err := rc.GetServiceInstance("", name, params.ServiceInstanceZone)
if err != nil {
params.Logger.Error(err, "error failed to get service instance id from name", "name", name)
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac
}

var serviceInstanceID, serviceInstanceName string
var zone *string
if params.IBMPowerVSMachine.Spec.ServiceInstanceID != "" {
serviceInstanceID = params.IBMPowerVSMachine.Spec.ServiceInstanceID
} else if params.IBMPowerVSMachine.Spec.ServiceInstance != nil && params.IBMPowerVSMachine.Spec.ServiceInstance.ID != nil {
Expand All @@ -178,9 +179,10 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac
serviceInstanceName = fmt.Sprintf("%s-%s", params.IBMPowerVSCluster.GetName(), "serviceInstance")
if params.IBMPowerVSCluster.Spec.ServiceInstance != nil && params.IBMPowerVSCluster.Spec.ServiceInstance.Name != nil {
serviceInstanceName = *params.IBMPowerVSCluster.Spec.ServiceInstance.Name
zone = params.IBMPowerVSCluster.Spec.Zone
}
}
serviceInstance, err := rc.GetServiceInstance(serviceInstanceID, serviceInstanceName)
serviceInstance, err := rc.GetServiceInstance(serviceInstanceID, serviceInstanceName, zone)
if err != nil {
params.Logger.Error(err, "failed to get PowerVS service instance details", "name", serviceInstanceName, "id", serviceInstanceID)
return nil, err
Expand Down
18 changes: 10 additions & 8 deletions controllers/ibmpowervsimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, err
}

cluster, err := scope.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName)
if err != nil {
return ctrl.Result{}, err
}

// Create the scope.
imageScope, err := scope.NewPowerVSImageScope(scope.PowerVSImageScopeParams{
Client: r.Client,
Logger: log,
IBMPowerVSImage: ibmImage,
ServiceEndpoint: r.ServiceEndpoint,
Client: r.Client,
Logger: log,
IBMPowerVSImage: ibmImage,
ServiceInstanceZone: cluster.Spec.Zone,
ServiceEndpoint: r.ServiceEndpoint,
})
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create scope: %w", err)
Expand All @@ -91,10 +97,6 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return r.reconcileDelete(imageScope)
}

cluster, err := scope.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName)
if err != nil {
return ctrl.Result{}, err
}
return r.reconcile(cluster, imageScope)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ResourceController interface {
ListResourceInstances(listResourceInstancesOptions *resourcecontrollerv2.ListResourceInstancesOptions) (result *resourcecontrollerv2.ResourceInstancesList, response *core.DetailedResponse, err error)
GetResourceInstance(*resourcecontrollerv2.GetResourceInstanceOptions) (*resourcecontrollerv2.ResourceInstance, *core.DetailedResponse, error)
CreateResourceInstance(*resourcecontrollerv2.CreateResourceInstanceOptions) (*resourcecontrollerv2.ResourceInstance, *core.DetailedResponse, error)
GetServiceInstance(string, string) (*resourcecontrollerv2.ResourceInstance, error)
GetServiceInstance(string, string, *string) (*resourcecontrollerv2.ResourceInstance, error)
DeleteResourceInstance(*resourcecontrollerv2.DeleteResourceInstanceOptions) (*core.DetailedResponse, error)

GetInstanceByName(string, string, string) (*resourcecontrollerv2.ResourceInstance, error)
Expand Down
13 changes: 11 additions & 2 deletions pkg/cloud/services/resourcecontroller/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *Service) DeleteResourceInstance(options *resourcecontrollerv2.DeleteRes

// GetServiceInstance returns service instance with given name or id. If not found, returns nil.
// TODO: Combine GetSreviceInstance() and GetInstanceByName().
func (s *Service) GetServiceInstance(id, name string) (*resourcecontrollerv2.ResourceInstance, error) {
func (s *Service) GetServiceInstance(id, name string, zone *string) (*resourcecontrollerv2.ResourceInstance, error) {
var serviceInstancesList []resourcecontrollerv2.ResourceInstance
f := func(start string) (bool, string, error) {
listServiceInstanceOptions := &resourcecontrollerv2.ListResourceInstancesOptions{
Expand All @@ -110,7 +110,16 @@ func (s *Service) GetServiceInstance(id, name string) (*resourcecontrollerv2.Res
return false, "", err
}
if serviceInstances != nil {
serviceInstancesList = append(serviceInstancesList, serviceInstances.Resources...)
if zone != nil && *zone != "" {
for _, resource := range serviceInstances.Resources {
if *resource.RegionID == *zone {
serviceInstancesList = append(serviceInstancesList, resource)
}
}
} else {
serviceInstancesList = append(serviceInstancesList, serviceInstances.Resources...)
}

nextURL, err := serviceInstances.GetNextStart()
if err != nil {
return false, "", err
Expand Down

0 comments on commit 46c74bc

Please sign in to comment.