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

Refactor resource manager to use interface and override resource manager endpoint URL #1740

Merged
merged 1 commit into from
Apr 22, 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
68 changes: 41 additions & 27 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/transitgateway"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc"
Expand Down Expand Up @@ -89,11 +90,12 @@ type PowerVSClusterScope struct {
patchHelper *patch.Helper
session *ibmpisession.IBMPISession

IBMPowerVSClient powervs.PowerVS
IBMVPCClient vpc.Vpc
TransitGatewayClient transitgateway.TransitGateway
ResourceClient resourcecontroller.ResourceController
COSClient cos.Cos
IBMPowerVSClient powervs.PowerVS
IBMVPCClient vpc.Vpc
TransitGatewayClient transitgateway.TransitGateway
ResourceClient resourcecontroller.ResourceController
COSClient cos.Cos
ResourceManagerClient resourcemanager.ResourceManager

Cluster *capiv1beta1.Cluster
IBMPowerVSCluster *infrav1beta2.IBMPowerVSCluster
Expand Down Expand Up @@ -226,13 +228,16 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
core.SetLoggingLevel(core.LevelDebug)
}

// Fetch the VPC service endpoint.
svcEndpoint := endpoints.FetchVPCEndpoint(*params.IBMPowerVSCluster.Spec.VPC.Region, params.ServiceEndpoint)

// Create VPC client.
vpcClient, err := vpc.NewService(svcEndpoint)
if err != nil {
return nil, fmt.Errorf("error failed to create IBM VPC client: %w", err)
}

// Create TransitGateway client
// Create TransitGateway client.
tgOptions := &tgapiv1.TransitGatewayApisV1Options{
Authenticator: auth,
}
Expand Down Expand Up @@ -265,18 +270,35 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
return nil, fmt.Errorf("error failed to create resource client: %w", err)
}

// Create Resource Manager client.
rcManagerOptions := &resourcemanagerv2.ResourceManagerV2Options{
Authenticator: auth,
}

rmEndpoint := endpoints.FetchEndpoints(string(endpoints.RM), params.ServiceEndpoint)
if rmEndpoint != "" {
rcManagerOptions.URL = rmEndpoint
params.Logger.V(3).Info("Overriding the default resource manager endpoint", "ResourceManagerEndpoint", rmEndpoint)
}

rmClient, err := resourcemanager.NewService(rcManagerOptions)
if err != nil {
return nil, fmt.Errorf("failed to create resource manager client: %w", err)
}

clusterScope := &PowerVSClusterScope{
session: session,
Logger: params.Logger,
Client: params.Client,
patchHelper: helper,
Cluster: params.Cluster,
IBMPowerVSCluster: params.IBMPowerVSCluster,
ServiceEndpoint: params.ServiceEndpoint,
IBMPowerVSClient: powerVSClient,
IBMVPCClient: vpcClient,
TransitGatewayClient: tgClient,
ResourceClient: resourceClient,
session: session,
Logger: params.Logger,
Client: params.Client,
patchHelper: helper,
Cluster: params.Cluster,
IBMPowerVSCluster: params.IBMPowerVSCluster,
ServiceEndpoint: params.ServiceEndpoint,
IBMPowerVSClient: powerVSClient,
IBMVPCClient: vpcClient,
TransitGatewayClient: tgClient,
ResourceClient: resourceClient,
ResourceManagerClient: rmClient,
}
return clusterScope, nil
}
Expand Down Expand Up @@ -1698,18 +1720,10 @@ func (s *PowerVSClusterScope) fetchResourceGroupID() (string, error) {
if s.ResourceGroup() == nil || s.ResourceGroup().Name == nil {
return "", fmt.Errorf("resource group name is not set")
}
rmv2, err := resourcemanagerv2.NewResourceManagerV2(&resourcemanagerv2.ResourceManagerV2Options{
Authenticator: s.session.Options.Authenticator,
})
if err != nil {
return "", err
}
if rmv2 == nil {
return "", fmt.Errorf("unable to get resource controller")
}

resourceGroup := s.ResourceGroup().Name
rmv2ListResourceGroupOpt := resourcemanagerv2.ListResourceGroupsOptions{Name: resourceGroup, AccountID: &s.session.Options.UserAccount}
resourceGroupListResult, _, err := rmv2.ListResourceGroups(&rmv2ListResourceGroupOpt)
resourceGroupListResult, _, err := s.ResourceManagerClient.ListResourceGroups(&rmv2ListResourceGroupOpt)
if err != nil {
return "", err
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/cloud/services/resourcemanager/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package resourcemanager implements resourcemanager code.
// Manage lifecycle of cloud resource groups using Resource Manager APIs.
package resourcemanager
28 changes: 28 additions & 0 deletions pkg/cloud/services/resourcemanager/resourcemanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcemanager

import (
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
)

// ResourceManager interface defines a method that a IBMCLOUD service object should implement in order to
// use the manage lifecycle of cloud resource groups using Resource Manager APIs.
type ResourceManager interface {
ListResourceGroups(*resourcemanagerv2.ListResourceGroupsOptions) (*resourcemanagerv2.ResourceGroupList, *core.DetailedResponse, error)
}
55 changes: 55 additions & 0 deletions pkg/cloud/services/resourcemanager/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcemanager

import (
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"

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

// Service holds the IBM Cloud Resource Manager Service specific information.
type Service struct {
client *resourcemanagerv2.ResourceManagerV2
}

// NewService returns a new service for the resource manager.
func NewService(options *resourcemanagerv2.ResourceManagerV2Options) (ResourceManager, error) {
if options == nil {
options = &resourcemanagerv2.ResourceManagerV2Options{}
}
if options.Authenticator == nil {
auth, err := authenticator.GetAuthenticator()
if err != nil {
return nil, err
}
options.Authenticator = auth
}
rmClient, err := resourcemanagerv2.NewResourceManagerV2(options)
if err != nil {
return nil, err
}
return &Service{
client: rmClient,
}, nil
}

// ListResourceGroups lists the resource groups.
func (s *Service) ListResourceGroups(listResourceGroupsOptions *resourcemanagerv2.ListResourceGroupsOptions) (result *resourcemanagerv2.ResourceGroupList, response *core.DetailedResponse, err error) {
return s.client.ListResourceGroups(listResourceGroupsOptions)
}
2 changes: 2 additions & 0 deletions pkg/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (
TransitGateway serviceID = "transitgateway"
// COS service.
COS serviceID = "cos"
// RM used to identify Resource-Manager service.
RM serviceID = "rm"
)

type serviceID string
Expand Down