Skip to content

Commit

Permalink
add elastic loadbalancer
Browse files Browse the repository at this point in the history
Signed-off-by: bo.jiang <bo.jiang@daocloud.io>
  • Loading branch information
ErikJiang authored and pacoxu committed Jan 17, 2025
1 parent 8c6e584 commit ba41b27
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 2 deletions.
7 changes: 7 additions & 0 deletions api/v1alpha1/conditions_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ const (
// ClusterSecurityGroupReconciliationFailedReason used when any errors occur during reconciliation of security groups.
ClusterSecurityGroupReconciliationFailedReason = "SecurityGroupReconciliationFailed"
)

const (
// LoadBalancerReadyCondition reports on whether a control plane load balancer was successfully reconciled.
LoadBalancerReadyCondition clusterv1.ConditionType = "LoadBalancerReady"
// LoadBalancerFailedReason used when an error occurs during load balancer reconciliation.
LoadBalancerFailedReason = "LoadBalancerFailed"
)
30 changes: 30 additions & 0 deletions api/v1alpha1/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type NetworkSpec struct {
// VPC configuration.
// +optional
VPC VPCSpec `json:"vpc,omitempty"`

// Subnets configuration.
// +optional
Subnets Subnets `json:"subnets,omitempty"`
}

type VPCSpec struct {
Expand All @@ -33,3 +37,29 @@ type VPCSpec struct {
// Cidr is the CIDR of the VPC.
Cidr string `json:"cidr"`
}

type Subnets []SubnetSpec

// Subnet
type SubnetSpec struct {
// ID defines a unique identifier to reference this resource.
Id string `json:"id"`

// Name is the name of the subnet. It must be 1-64 characters long and support numbers, letters, Chinese characters, _(underscore), -(hyphen), and .(dot).
Name string `json:"name"`

// CIDR is the CIDR of the subnet. It must be in CIDR format. The mask length cannot be greater than 28.
Cidr string `json:"cidr"`

// GatewayIp is the gateway of the subnet. It must be an IP address in the subnet segment.
GatewayIp string `json:"gateway_ip"`

// VPCId is the identifier of the VPC where the subnet is located.
VpcId string `json:"vpc_id"`

// NeutronNetworkId is the identifier of the network (OpenStack Neutron interface).
NeutronNetworkId string `json:"neutron_network_id"`

// NeutronSubnetId is the identifier of the subnet (OpenStack Neutron interface).
NeutronSubnetId string `json:"neutron_subnet_id"`
}
43 changes: 41 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,51 @@ spec:
description: NetworkSpec encapsulates the configuration options for
HuaweiCloud network.
properties:
subnets:
description: Subnets configuration.
items:
description: Subnet
properties:
cidr:
description: CIDR is the CIDR of the subnet. It must be
in CIDR format. The mask length cannot be greater than
28.
type: string
gateway_ip:
description: GatewayIp is the gateway of the subnet. It
must be an IP address in the subnet segment.
type: string
id:
description: ID defines a unique identifier to reference
this resource.
type: string
name:
description: Name is the name of the subnet. It must be
1-64 characters long and support numbers, letters, Chinese
characters, _(underscore), -(hyphen), and .(dot).
type: string
neutron_network_id:
description: NeutronNetworkId is the identifier of the network
(OpenStack Neutron interface).
type: string
neutron_subnet_id:
description: NeutronSubnetId is the identifier of the subnet
(OpenStack Neutron interface).
type: string
vpc_id:
description: VPCId is the identifier of the VPC where the
subnet is located.
type: string
required:
- cidr
- gateway_ip
- id
- name
- neutron_network_id
- neutron_subnet_id
- vpc_id
type: object
type: array
vpc:
description: VPC configuration.
properties:
Expand Down
19 changes: 19 additions & 0 deletions internal/controller/huaweicloudcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

infrav1alpha1 "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/api/v1alpha1"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/scope"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/services/elb"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/services/network"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/services/securitygroup"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
Expand Down Expand Up @@ -153,6 +154,15 @@ func (r *HuaweiCloudClusterReconciler) reconcileNormal(clusterScope *scope.Clust
return reconcile.Result{RequeueAfter: 30 * time.Second}, errors.Wrap(err, "failed to reconcile security groups")
}

// reconcile elastic load balancer
elbSvc, err := elb.NewService(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create elb service")
}
if err := elbSvc.ReconcileLoadbalancers(); err != nil {
return reconcile.Result{RequeueAfter: 30 * time.Second}, errors.Wrap(err, "failed to reconcile load balancers")
}

hccluster.Status.Ready = true
return reconcile.Result{}, nil
}
Expand All @@ -166,6 +176,15 @@ func (r *HuaweiCloudClusterReconciler) reconcileDelete(clusterScope *scope.Clust

clusterScope.Logger.Info("Deleting HuaweiCloudCluster")

// delete load balancer
elbSvc, err := elb.NewService(clusterScope)
if err != nil {
return errors.Wrap(err, "failed to create elb service")
}
if err := elbSvc.DeleteLoadbalancers(); err != nil {
return errors.Wrap(err, "failed to delete load balancers")
}

// delete security group
sgSvc, err := securitygroup.NewService(clusterScope)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func (s *ClusterScope) Close() error {
return s.PatchObject()
}

// Name returns the CAPI Cluster name.
func (s *ClusterScope) ClusterName() string {
return s.Cluster.Name
}

// CoreCluster returns the core cluster object.
func (s *ClusterScope) CoreCluster() conditions.Setter {
return s.Cluster
Expand All @@ -100,6 +105,16 @@ func (s *ClusterScope) VPC() *infrav1alpha1.VPCSpec {
return &s.HCCluster.Spec.NetworkSpec.VPC
}

// Subnets returns the cluster subnets.
func (s *ClusterScope) Subnets() infrav1alpha1.Subnets {
return s.HCCluster.Spec.NetworkSpec.Subnets
}

// SetSubnets updates the clusters subnets.
func (s *ClusterScope) SetSubnets(subnets infrav1alpha1.Subnets) {
s.HCCluster.Spec.NetworkSpec.Subnets = subnets
}

// Region returns the cluster region.
func (s *ClusterScope) Region() string {
return s.HCCluster.Spec.Region
Expand Down
Loading

0 comments on commit ba41b27

Please sign in to comment.