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 VPC client code #668

Merged
merged 1 commit into from
May 11, 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
40 changes: 0 additions & 40 deletions cloud/scope/clients.go

This file was deleted.

47 changes: 24 additions & 23 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)

// ClusterScopeParams defines the input parameters used to create a new ClusterScope.
type ClusterScopeParams struct {
IBMVPCClients
IBMVPCClient vpc.Vpc
Client client.Client
Logger logr.Logger
Cluster *capiv1beta1.Cluster
Expand All @@ -48,7 +49,7 @@ type ClusterScope struct {
client client.Client
patchHelper *patch.Helper

IBMVPCClients
IBMVPCClient vpc.Vpc
Cluster *capiv1beta1.Cluster
IBMVPCCluster *infrav1beta1.IBMVPCCluster
}
Expand All @@ -71,9 +72,9 @@ func NewClusterScope(params ClusterScopeParams, authenticator core.Authenticator
return nil, errors.Wrap(err, "failed to init patch helper")
}

vpcErr := params.IBMVPCClients.setIBMVPCService(authenticator, svcEndpoint)
if vpcErr != nil {
return nil, errors.Wrap(vpcErr, "failed to create IBM VPC session")
vpcClient, err := vpc.NewService(svcEndpoint)
if err != nil {
return nil, errors.Wrap(err, "failed to create IBM VPC session")
}

if params.Logger.V(DEBUGLEVEL).Enabled() {
Expand All @@ -83,7 +84,7 @@ func NewClusterScope(params ClusterScopeParams, authenticator core.Authenticator
return &ClusterScope{
Logger: params.Logger,
client: params.Client,
IBMVPCClients: params.IBMVPCClients,
IBMVPCClient: vpcClient,
Cluster: params.Cluster,
IBMVPCCluster: params.IBMVPCCluster,
patchHelper: helper,
Expand All @@ -105,7 +106,7 @@ func (s *ClusterScope) CreateVPC() (*vpcv1.VPC, error) {
ID: &s.IBMVPCCluster.Spec.ResourceGroup,
})
options.SetName(s.IBMVPCCluster.Spec.VPC)
vpc, _, err := s.IBMVPCClients.VPCService.CreateVPC(options)
vpc, _, err := s.IBMVPCClient.CreateVPC(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreateVPC", "Failed vpc creation - %v", err)
return nil, err
Expand All @@ -121,7 +122,7 @@ func (s *ClusterScope) CreateVPC() (*vpcv1.VPC, error) {
func (s *ClusterScope) DeleteVPC() error {
deleteVpcOptions := &vpcv1.DeleteVPCOptions{}
deleteVpcOptions.SetID(s.IBMVPCCluster.Status.VPC.ID)
_, err := s.IBMVPCClients.VPCService.DeleteVPC(deleteVpcOptions)
_, err := s.IBMVPCClient.DeleteVPC(deleteVpcOptions)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDeleteVPC", "Failed vpc deletion - %v", err)
} else {
Expand All @@ -133,7 +134,7 @@ func (s *ClusterScope) DeleteVPC() error {

func (s *ClusterScope) ensureVPCUnique(vpcName string) (*vpcv1.VPC, error) {
listVpcsOptions := &vpcv1.ListVpcsOptions{}
vpcs, _, err := s.IBMVPCClients.VPCService.ListVpcs(listVpcsOptions)
vpcs, _, err := s.IBMVPCClient.ListVpcs(listVpcsOptions)
if err != nil {
return nil, err
}
Expand All @@ -153,7 +154,7 @@ func (s *ClusterScope) updateDefaultSG(sgID string) error {
Protocol: core.StringPtr("all"),
IPVersion: core.StringPtr("ipv4"),
})
_, _, err := s.IBMVPCClients.VPCService.CreateSecurityGroupRule(options)
_, _, err := s.IBMVPCClient.CreateSecurityGroupRule(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreateSecurityGroupRule", "Failed security group rule creation - %v", err)
}
Expand Down Expand Up @@ -182,16 +183,16 @@ func (s *ClusterScope) ReserveFIP() (*vpcv1.FloatingIP, error) {
},
})

floatingIP, _, err := s.IBMVPCClients.VPCService.CreateFloatingIP(options)
floatingIP, _, err := s.IBMVPCClient.CreateFloatingIP(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreateFloatingIP", "Failed floatingIP creation - %v", err)
}
return floatingIP, err
}

func (s *ClusterScope) ensureFIPUnique(fipName string) (*vpcv1.FloatingIP, error) {
listFloatingIpsOptions := s.IBMVPCClients.VPCService.NewListFloatingIpsOptions()
floatingIPs, _, err := s.IBMVPCClients.VPCService.ListFloatingIps(listFloatingIpsOptions)
listFloatingIpsOptions := &vpcv1.ListFloatingIpsOptions{}
floatingIPs, _, err := s.IBMVPCClient.ListFloatingIps(listFloatingIpsOptions)
if err != nil {
return nil, err
}
Expand All @@ -208,7 +209,7 @@ func (s *ClusterScope) DeleteFloatingIP() error {
if fipID := *s.IBMVPCCluster.Status.VPCEndpoint.FIPID; fipID != "" {
deleteFIPOption := &vpcv1.DeleteFloatingIPOptions{}
deleteFIPOption.SetID(fipID)
_, err := s.IBMVPCClients.VPCService.DeleteFloatingIP(deleteFIPOption)
_, err := s.IBMVPCClient.DeleteFloatingIP(deleteFIPOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDeleteFloatingIP", "Failed floatingIP deletion - %v", err)
}
Expand Down Expand Up @@ -247,7 +248,7 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
ID: &s.IBMVPCCluster.Spec.ResourceGroup,
},
})
subnet, _, err := s.IBMVPCClients.VPCService.CreateSubnet(options)
subnet, _, err := s.IBMVPCClient.CreateSubnet(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreateSubnet", "Failed subnet creation - %v", err)
}
Expand All @@ -269,7 +270,7 @@ func (s *ClusterScope) getSubnetAddrPrefix(vpcID, zone string) (string, error) {
options := &vpcv1.ListVPCAddressPrefixesOptions{
VPCID: &vpcID,
}
addrCollection, _, err := s.IBMVPCClients.VPCService.ListVPCAddressPrefixes(options)
addrCollection, _, err := s.IBMVPCClient.ListVPCAddressPrefixes(options)

if err != nil {
return "", err
Expand All @@ -284,7 +285,7 @@ func (s *ClusterScope) getSubnetAddrPrefix(vpcID, zone string) (string, error) {

func (s *ClusterScope) ensureSubnetUnique(subnetName string) (*vpcv1.Subnet, error) {
options := &vpcv1.ListSubnetsOptions{}
subnets, _, err := s.IBMVPCClients.VPCService.ListSubnets(options)
subnets, _, err := s.IBMVPCClient.ListSubnets(options)

if err != nil {
return nil, err
Expand All @@ -304,7 +305,7 @@ func (s *ClusterScope) DeleteSubnet() error {
// get the pgw id for given subnet, so we can delete it later
getPGWOptions := &vpcv1.GetSubnetPublicGatewayOptions{}
getPGWOptions.SetID(subnetID)
pgw, _, err := s.IBMVPCClients.VPCService.GetSubnetPublicGateway(getPGWOptions)
pgw, _, err := s.IBMVPCClient.GetSubnetPublicGateway(getPGWOptions)
if pgw != nil && err == nil { // public gateway found
// Unset the public gateway for subnet first
err = s.detachPublicGateway(subnetID, *pgw.ID)
Expand All @@ -316,7 +317,7 @@ func (s *ClusterScope) DeleteSubnet() error {
// Delete subnet
deleteSubnetOption := &vpcv1.DeleteSubnetOptions{}
deleteSubnetOption.SetID(subnetID)
_, err = s.IBMVPCClients.VPCService.DeleteSubnet(deleteSubnetOption)
_, err = s.IBMVPCClient.DeleteSubnet(deleteSubnetOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDeleteSubnet", "Failed subnet deletion - %v", err)
return errors.Wrap(err, "Error when deleting subnet ")
Expand All @@ -335,7 +336,7 @@ func (s *ClusterScope) createPublicGateWay(vpcID string, zoneName string, resour
options.SetResourceGroup(&vpcv1.ResourceGroupIdentity{
ID: &resourceGroupID,
})
publicGateway, _, err := s.IBMVPCClients.VPCService.CreatePublicGateway(options)
publicGateway, _, err := s.IBMVPCClient.CreatePublicGateway(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreatePublicGateway", "Failed publicgateway creation - %v", err)
}
Expand All @@ -348,7 +349,7 @@ func (s *ClusterScope) attachPublicGateWay(subnetID string, pgwID string) (*vpcv
options.SetPublicGatewayIdentity(&vpcv1.PublicGatewayIdentity{
ID: &pgwID,
})
publicGateway, _, err := s.IBMVPCClients.VPCService.SetSubnetPublicGateway(options)
publicGateway, _, err := s.IBMVPCClient.SetSubnetPublicGateway(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedAttachPublicGateway", "Failed publicgateway attachment - %v", err)
}
Expand All @@ -359,7 +360,7 @@ func (s *ClusterScope) detachPublicGateway(subnetID string, pgwID string) error
// Unset the publicgateway first, and then delete it
unsetPGWOption := &vpcv1.UnsetSubnetPublicGatewayOptions{}
unsetPGWOption.SetID(subnetID)
_, err := s.IBMVPCClients.VPCService.UnsetSubnetPublicGateway(unsetPGWOption)
_, err := s.IBMVPCClient.UnsetSubnetPublicGateway(unsetPGWOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDetachPublicGateway", "Failed publicgateway detachment - %v", err)
return errors.Wrap(err, "Error when unsetting publicgateway for subnet "+subnetID)
Expand All @@ -368,7 +369,7 @@ func (s *ClusterScope) detachPublicGateway(subnetID string, pgwID string) error
// Delete the public gateway
deletePGWOption := &vpcv1.DeletePublicGatewayOptions{}
deletePGWOption.SetID(pgwID)
_, err = s.IBMVPCClients.VPCService.DeletePublicGateway(deletePGWOption)
_, err = s.IBMVPCClient.DeletePublicGateway(deletePGWOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDeletePublicGateway", "Failed publicgateway deletion - %v", err)
return errors.Wrap(err, "Error when deleting publicgateway for subnet "+subnetID)
Expand Down
21 changes: 11 additions & 10 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)

// MachineScopeParams defines the input parameters used to create a new MachineScope.
type MachineScopeParams struct {
IBMVPCClients
IBMVPCClient vpc.Vpc
Client client.Client
Logger logr.Logger
Cluster *capiv1beta1.Cluster
Expand All @@ -52,7 +53,7 @@ type MachineScope struct {
client client.Client
patchHelper *patch.Helper

IBMVPCClients
IBMVPCClient vpc.Vpc
Cluster *capiv1beta1.Cluster
Machine *capiv1beta1.Machine
IBMVPCCluster *infrav1beta1.IBMVPCCluster
Expand All @@ -77,9 +78,9 @@ func NewMachineScope(params MachineScopeParams, authenticator core.Authenticator
return nil, errors.Wrap(err, "failed to init patch helper")
}

vpcErr := params.IBMVPCClients.setIBMVPCService(authenticator, svcEndpoint)
if vpcErr != nil {
return nil, errors.Wrap(vpcErr, "failed to create IBM VPC session")
vpcClient, err := vpc.NewService(svcEndpoint)
if err != nil {
return nil, errors.Wrap(err, "failed to create IBM VPC session")
}

if params.Logger.V(DEBUGLEVEL).Enabled() {
Expand All @@ -89,7 +90,7 @@ func NewMachineScope(params MachineScopeParams, authenticator core.Authenticator
return &MachineScope{
Logger: params.Logger,
client: params.Client,
IBMVPCClients: params.IBMVPCClients,
IBMVPCClient: vpcClient,
Cluster: params.Cluster,
IBMVPCCluster: params.IBMVPCCluster,
patchHelper: helper,
Expand Down Expand Up @@ -147,7 +148,7 @@ func (m *MachineScope) CreateMachine() (*vpcv1.Instance, error) {
}

options.SetInstancePrototype(instancePrototype)
instance, response, err := m.IBMVPCClients.VPCService.CreateInstance(options)
instance, response, err := m.IBMVPCClient.CreateInstance(options)
if err != nil {
record.Warnf(m.IBMVPCMachine, "FailedCreateInstance", "Failed instance creation - %v", err)
} else {
Expand All @@ -161,7 +162,7 @@ func (m *MachineScope) CreateMachine() (*vpcv1.Instance, error) {
func (m *MachineScope) DeleteMachine() error {
options := &vpcv1.DeleteInstanceOptions{}
options.SetID(m.IBMVPCMachine.Status.InstanceID)
_, err := m.IBMVPCClients.VPCService.DeleteInstance(options)
_, err := m.IBMVPCClient.DeleteInstance(options)
if err != nil {
record.Warnf(m.IBMVPCMachine, "FailedDeleteInstance", "Failed instance deletion - %v", err)
} else {
Expand All @@ -172,7 +173,7 @@ func (m *MachineScope) DeleteMachine() error {

func (m *MachineScope) ensureInstanceUnique(instanceName string) (*vpcv1.Instance, error) {
options := &vpcv1.ListInstancesOptions{}
instances, _, err := m.IBMVPCClients.VPCService.ListInstances(options)
instances, _, err := m.IBMVPCClient.ListInstances(options)

if err != nil {
return nil, err
Expand All @@ -190,7 +191,7 @@ func (m *MachineScope) GetMachine(instanceID string) (*vpcv1.Instance, error) {
options := &vpcv1.GetInstanceOptions{}
options.SetID(instanceID)

instance, _, err := m.IBMVPCClients.VPCService.GetInstance(options)
instance, _, err := m.IBMVPCClient.GetInstance(options)
return instance, err
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/ibmvpccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (r *IBMVPCClusterReconciler) reconcileDelete(clusterScope *scope.ClusterSco
listVSIOpts := &vpcv1.ListInstancesOptions{
VPCID: &clusterScope.IBMVPCCluster.Status.VPC.ID,
}
vsis, _, err := clusterScope.VPCService.ListInstances(listVSIOpts)
vsis, _, err := clusterScope.IBMVPCClient.ListInstances(listVSIOpts)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "Error when listing VSIs when tried to delete subnet ")
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/ibmvpcmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (r *IBMVPCMachineReconciler) reconcileNormal(machineScope *scope.MachineSco
options.SetInstanceID(*instance.ID)
options.SetNetworkInterfaceID(*instance.PrimaryNetworkInterface.ID)
floatingIP, _, err :=
machineScope.IBMVPCClients.VPCService.AddInstanceNetworkInterfaceFloatingIP(options)
machineScope.IBMVPCClient.AddInstanceNetworkInterfaceFloatingIP(options)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to bind floating IP to control plane %s/%s", machineScope.IBMVPCMachine.Namespace, machineScope.IBMVPCMachine.Name)
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/cloud/services/vpc/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2022 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 vpc implements VPC code.
package vpc
Loading