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 the client code #503

Merged
merged 1 commit into from
Jan 10, 2022
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ GOJQ := $(TOOLS_BIN_DIR)/gojq
CONVERSION_GEN := $(TOOLS_BIN_DIR)/conversion-gen
GINKGO := $(TOOLS_BIN_DIR)/ginkgo
ENVSUBST := $(TOOLS_BIN_DIR)/envsubst
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen

STAGING_REGISTRY ?= gcr.io/k8s-staging-capi-ibmcloud
STAGING_BUCKET ?= artifacts.k8s-staging-capi-ibmcloud.appspot.com
Expand Down Expand Up @@ -124,8 +125,13 @@ vet:

# Generate code
generate: controller-gen
$(MAKE) generate-go
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: generate-go
generate-go: $(MOCKGEN)
go generate ./...

images: docker-build
# find or download controller-gen
# download controller-gen if necessary
Expand Down
51 changes: 0 additions & 51 deletions cloud/scope/powervs_clients.go

This file was deleted.

51 changes: 42 additions & 9 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"github.com/go-logr/logr"
"github.com/pkg/errors"

"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
utils "github.com/ppc64le-cloud/powervs-utils"

"k8s.io/klog/v2/klogr"
Expand All @@ -31,7 +34,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// PowerVSClusterScopeParams defines the input parameters used to create a new PowerVSClusterScope.
Expand All @@ -48,7 +54,7 @@ type PowerVSClusterScope struct {
client client.Client
patchHelper *patch.Helper

IBMPowerVSClient *IBMPowerVSClient
IBMPowerVSClient *powervs.Service
Cluster *clusterv1.Cluster
IBMPowerVSCluster *v1beta1.IBMPowerVSCluster
}
Expand All @@ -66,20 +72,47 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
params.Logger = klogr.New()
}

client := pkg.NewClient()

spec := params.IBMPowerVSCluster.Spec
resource, err := client.ResourceClient.GetInstance(spec.ServiceInstanceID)

auth, err := authenticator.GetAuthenticator()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get authenticator")
}

account, err := servicesutils.GetAccount(auth)
if err != nil {
return nil, errors.Wrap(err, "failed to get account")
}
region, err := utils.GetRegion(resource.RegionID)

rc, err := resourcecontroller.NewService(resourcecontroller.ServiceOptions{})
if err != nil {
return nil, err
}
zone := resource.RegionID

c, err := NewIBMPowerVSClient(client.Config.IAMAccessToken, client.User.Account, spec.ServiceInstanceID, region, zone, true)
res, _, err := rc.GetResourceInstance(
&resourcecontrollerv2.GetResourceInstanceOptions{
ID: core.StringPtr(spec.ServiceInstanceID),
})
if err != nil {
return nil, errors.Wrap(err, "failed to get resource instance")
}

region, err := utils.GetRegion(*res.RegionID)
if err != nil {
return nil, errors.Wrap(err, "failed to get region")
}

options := powervs.ServiceOptions{
PIOptions: &ibmpisession.PIOptions{
Debug: true,
UserAccount: account,
Region: region,
Zone: *res.RegionID,
},
CloudInstanceID: spec.ServiceInstanceID,
}
c, err := powervs.NewService(options)

if err != nil {
return nil, fmt.Errorf("failed to create NewIBMPowerVSClient")
}
Expand Down
66 changes: 45 additions & 21 deletions cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
utils "github.com/ppc64le-cloud/powervs-utils"

"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_networks"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_p_vm_instances"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -39,7 +40,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// PowerVSMachineScopeParams defines the input parameters used to create a new PowerVSMachineScope.
Expand All @@ -58,7 +62,7 @@ type PowerVSMachineScope struct {
client client.Client
patchHelper *patch.Helper

IBMPowerVSClient *IBMPowerVSClient
IBMPowerVSClient *powervs.Service
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
IBMPowerVSCluster *v1beta1.IBMPowerVSCluster
Expand All @@ -85,21 +89,47 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (*PowerVSMachineSc
}

m := params.IBMPowerVSMachine
client := pkg.NewClient()

resource, err := client.ResourceClient.GetInstance(m.Spec.ServiceInstanceID)
auth, err := authenticator.GetAuthenticator()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get authenticator")
}

account, err := servicesutils.GetAccount(auth)
if err != nil {
return nil, errors.Wrap(err, "failed to get account")
}
region, err := utils.GetRegion(resource.RegionID)

rc, err := resourcecontroller.NewService(resourcecontroller.ServiceOptions{})
if err != nil {
return nil, err
}
zone := resource.RegionID

c, err := NewIBMPowerVSClient(client.Config.IAMAccessToken, client.User.Account, m.Spec.ServiceInstanceID, region, zone, true)
res, _, err := rc.GetResourceInstance(
&resourcecontrollerv2.GetResourceInstanceOptions{
ID: core.StringPtr(m.Spec.ServiceInstanceID),
})
if err != nil {
return nil, errors.Wrap(err, "failed to get resource instance")
}

region, err := utils.GetRegion(*res.RegionID)
if err != nil {
return nil, fmt.Errorf("failed to create NewIBMPowerVSClient")
return nil, errors.Wrap(err, "failed to get region")
}

options := powervs.ServiceOptions{
PIOptions: &ibmpisession.PIOptions{
Debug: true,
UserAccount: account,
Region: region,
Zone: *res.RegionID,
},
CloudInstanceID: m.Spec.ServiceInstanceID,
}
c, err := powervs.NewService(options)
if err != nil {
return nil, fmt.Errorf("failed to create PowerVS service")
}

helper, err := patch.NewHelper(params.IBMPowerVSMachine, params.Client)
Expand All @@ -120,7 +150,7 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (*PowerVSMachineSc
}

func (m *PowerVSMachineScope) ensureInstanceUnique(instanceName string) (*models.PVMInstanceReference, error) {
instances, err := m.IBMPowerVSClient.InstanceClient.GetAll()
instances, err := m.IBMPowerVSClient.GetAllInstance()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,7 +215,7 @@ func (m *PowerVSMachineScope) CreateMachine() (*models.PVMInstanceReference, err
UserData: cloudInitData,
},
}
_, err = m.IBMPowerVSClient.InstanceClient.Create(params.Body)
_, err = m.IBMPowerVSClient.CreateInstance(params.Body)
if err != nil {
return nil, err
}
Expand All @@ -204,7 +234,7 @@ func (m *PowerVSMachineScope) PatchObject() error {

// DeleteMachine deletes the power vs machine associated with machine instance id and service instance id.
func (m *PowerVSMachineScope) DeleteMachine() error {
return m.IBMPowerVSClient.InstanceClient.Delete(m.IBMPowerVSMachine.Status.InstanceID)
return m.IBMPowerVSClient.DeleteInstance(m.IBMPowerVSMachine.Status.InstanceID)
}

// GetBootstrapData returns the base64 encoded bootstrap data from the secret in the Machine's bootstrap.dataSecretName
Expand Down Expand Up @@ -249,7 +279,7 @@ func getImageID(image v1beta1.IBMPowerVSResourceReference, m *PowerVSMachineScop
}

func (m *PowerVSMachineScope) GetImages() (*models.Images, error) {
return m.IBMPowerVSClient.ImageClient.GetAll()
return m.IBMPowerVSClient.GetAllImage()
}

func getNetworkID(network v1beta1.IBMPowerVSResourceReference, m *PowerVSMachineScope) (*string, error) {
Expand All @@ -275,11 +305,5 @@ func getNetworkID(network v1beta1.IBMPowerVSResourceReference, m *PowerVSMachine
}

func (m *PowerVSMachineScope) GetNetworks() (*models.Networks, error) {
params := p_cloud_networks.NewPcloudNetworksGetallParamsWithTimeout(TIMEOUT).WithCloudInstanceID(m.IBMPowerVSMachine.Spec.ServiceInstanceID)
resp, err := m.IBMPowerVSClient.session.Power.PCloudNetworks.PcloudNetworksGetall(params, ibmpisession.NewAuth(m.IBMPowerVSClient.session, m.IBMPowerVSMachine.Spec.ServiceInstanceID))

if err != nil || resp.Payload == nil {
return nil, err
}
return resp.Payload, nil
return m.IBMPowerVSClient.GetAllNetwork()
}
2 changes: 1 addition & 1 deletion controllers/ibmpowervsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(ctx context.Context, machi
}

if ins != nil {
instance, err := machineScope.IBMPowerVSClient.InstanceClient.Get(*ins.PvmInstanceID)
instance, err := machineScope.IBMPowerVSClient.GetInstance(*ins.PvmInstanceID)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/ibmvpccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"os"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"

"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand All @@ -37,7 +38,6 @@ import (

infrastructurev1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg"
)

// IBMVPCClusterReconciler reconciles a IBMVPCCluster object
Expand Down Expand Up @@ -80,7 +80,7 @@ func (r *IBMVPCClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// TODO: Will be removed once we find a better way of overriding the service endpoint, generate via spec
svcEndpoint := os.Getenv("SERVICE_ENDPOINT")

authenticator, err := pkg.GetAuthenticator()
authenticator, err := authenticator.GetAuthenticator()
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to get authenticator")
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/ibmvpcmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"

"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand All @@ -38,7 +39,6 @@ import (

infrastructurev1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg"
)

// IBMVPCMachineReconciler reconciles a IBMVPCMachine object
Expand Down Expand Up @@ -100,7 +100,7 @@ func (r *IBMVPCMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// Create the cluster scope
svcEndpoint := os.Getenv("SERVICE_ENDPOINT")

authenticator, err := pkg.GetAuthenticator()
authenticator, err := authenticator.GetAuthenticator()
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to get authenticator")
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module sigs.k8s.io/cluster-api-provider-ibmcloud
go 1.17

require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20200921095234-26d1d0148c62
github.com/IBM-Cloud/power-go-client v1.0.88
github.com/IBM/go-sdk-core/v5 v5.9.1
github.com/IBM/platform-services-go-sdk v0.22.7
github.com/IBM/vpc-go-sdk v0.14.0
github.com/go-logr/logr v1.2.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang/mock v1.6.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -69,7 +70,7 @@ require (
github.com/google/go-github/v33 v33.0.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
Expand Down
Loading