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

[WIP]: Add listener based on the machine label and listener label #2155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions api/v1beta2/ibmvpccluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ type AdditionalListenerSpec struct {
// Will default to TCP protocol if not specified.
// +optional
Protocol *VPCLoadBalancerListenerProtocol `json:"protocol,omitempty"`

Selector metav1.LabelSelector `json:"selector"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This needs to be optional and also omitempty
  • Is there a scope for multiple selector? @Karthik-K-N
  • what will happen if someone changes this field? are we allowing this field to be changed? if not - can we block it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will happen if someone changes this field?

I think to start with we can make this field immutable? so we can block the changes to the field.

Is there a scope for multiple selector?

You mean like configure the listener to multiple machine like Port 22 for machine-1 and machine-2

There are two options

  1. Add 2 listener with same port but with different selector
  2. Allow label to have multiple machine separated by comma(,) and add support for this in controller
    like
    infrastructure.cluster.x-k8s.io/machine-name: machine1, machine2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, this make sense.. may be its worth adding more examples so that it becomes clear for the consumers.
I find this reference useful - https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

}

// VPCLoadBalancerBackendPoolSpec defines the desired configuration of a VPC Load Balancer Backend Pool.
Expand Down
1 change: 1 addition & 0 deletions api/v1beta2/zz_generated.deepcopy.go

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

54 changes: 53 additions & 1 deletion cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (
"github.com/IBM/vpc-go-sdk/vpcv1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/cache"
Expand All @@ -56,6 +58,7 @@ import (
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"

"sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
Expand Down Expand Up @@ -1021,7 +1024,35 @@ func (m *PowerVSMachineScope) CreateVPCLoadBalancerPoolMember() (*vpcv1.LoadBala

internalIP := m.GetMachineInternalIP()

// TODO:SHILPA- handle multiple lbs as well
// Update each LoadBalancer pool
loadBalancerListeners := map[string]v1beta2.AdditionalListenerSpec{}
for _, additionalListener := range lb.AdditionalListeners {
// if additionalListener.Selector.MatchLabels == nil {
// continue
// }
// TODO:SHILPA- protocol is added irrespective of whats provided in the additionalListener protocol, need to handle this
if additionalListener.Protocol == nil {
additionalListener.Protocol = &v1beta2.VPCLoadBalancerListenerProtocolTCP
}
loadBalancerListeners[fmt.Sprintf("%d-%s", additionalListener.Port, *additionalListener.Protocol)] = additionalListener
}
for _, listener := range loadBalancer.Listeners {
listenerOptions := &vpcv1.GetLoadBalancerListenerOptions{}
listenerOptions.SetLoadBalancerID(*loadBalancer.ID)
listenerOptions.SetID(*listener.ID)
loadBalancerListener, _, err := m.IBMVPCClient.GetLoadBalancerListener(listenerOptions)
if err != nil {
return nil, fmt.Errorf("failed to list %s load balancer listener: %v", *listener.ID, err)
}
fmt.Println(*loadBalancerListener.Port, *loadBalancerListener.Protocol)
if additionalListener, ok := loadBalancerListeners[fmt.Sprintf("%d-%s", *loadBalancerListener.Port, *loadBalancerListener.Protocol)]; ok {
if loadBalancerListener.DefaultPool != nil {
loadBalancerListeners[*loadBalancerListener.DefaultPool.Name] = additionalListener
}
}
}
fmt.Println(loadBalancerListeners)
for _, pool := range loadBalancer.Pools {
m.V(3).Info("Updating LoadBalancer pool member", "pool", *pool.Name, "loadbalancer", *loadBalancer.Name, "ip", internalIP)
listOptions := &vpcv1.ListLoadBalancerPoolMembersOptions{}
Expand All @@ -1032,7 +1063,27 @@ func (m *PowerVSMachineScope) CreateVPCLoadBalancerPoolMember() (*vpcv1.LoadBala
return nil, fmt.Errorf("failed to list %s VPC load balancer pool error: %v", *pool.Name, err)
}
var targetPort int64
var alreadyRegistered bool
var alreadyRegistered, skipListener bool

if loadBalancerListener, ok := loadBalancerListeners[*pool.Name]; ok {
selector, err := metav1.LabelSelectorAsSelector(&loadBalancerListener.Selector)
if err != nil {
m.V(5).Info("Skipping listener addition, failed to get label selector from spec selector")
continue
}

if selector.Empty() && !util.IsControlPlaneMachine(m.Machine) {
continue
}
// Skip adding the listener if the selector does not match
if !selector.Empty() && !selector.Matches(labels.Set(m.IBMPowerVSMachine.Labels)) {
skipListener = true
}
}
if skipListener {
m.V(3).Info("Skip adding listener, machine label doesn't match with the listener label selector", "pool", *pool.Name, "targetip", internalIP, "machine", m.IBMPowerVSMachine.Name, "clusterName", m.IBMPowerVSCluster.Name)
continue
}

if len(listLoadBalancerPoolMembers.Members) == 0 {
// For adding the first member to the pool we depend on the pool name to get the target port
Expand Down Expand Up @@ -1060,6 +1111,7 @@ func (m *PowerVSMachineScope) CreateVPCLoadBalancerPoolMember() (*vpcv1.LoadBala
}
}
}

if alreadyRegistered {
m.V(3).Info("PoolMember already exist", "pool", *pool.Name, "targetip", internalIP, "port", targetPort)
continue
Expand Down
Loading