Skip to content

Commit 89c72e5

Browse files
committed
Don't assign IPs if the network's routing mode is dynamic
1 parent 05d2b38 commit 89c72e5

10 files changed

+79
-30
lines changed

api/v1beta3/cloudstackfailuredomain_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type Network struct {
7272
// Cloudstack VPC the network belongs to.
7373
// +optional
7474
VPC *VPC `json:"vpc,omitempty"`
75+
76+
// Cloudstack Network's routing mode.
77+
// +optional
78+
NetworkMode string `json:"networkMode,omitempty"`
7579
}
7680

7781
type VPC struct {

api/v1beta3/cloudstackisolatednetwork_types.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,23 @@ type CloudStackIsolatedNetworkStatus struct {
6868
// The ID of the lb rule used to assign VMs to the lb.
6969
LBRuleID string `json:"loadBalancerRuleID,omitempty"`
7070

71+
// Network mode of the network.
72+
NetworkMode string `json:"networkMode,omitempty"`
73+
7174
// Ready indicates the readiness of this provider resource.
7275
Ready bool `json:"ready"`
7376
}
7477

7578
func (n *CloudStackIsolatedNetwork) Network() *Network {
7679
return &Network{
77-
Name: n.Spec.Name,
78-
Type: "IsolatedNetwork",
79-
ID: n.Spec.ID,
80-
Gateway: n.Spec.Gateway,
81-
Netmask: n.Spec.Netmask,
82-
VPC: n.Spec.VPC,
83-
Offering: n.Spec.Offering,
80+
Name: n.Spec.Name,
81+
Type: "IsolatedNetwork",
82+
ID: n.Spec.ID,
83+
Gateway: n.Spec.Gateway,
84+
Netmask: n.Spec.Netmask,
85+
VPC: n.Spec.VPC,
86+
Offering: n.Spec.Offering,
87+
NetworkMode: n.Status.NetworkMode,
8488
}
8589
}
8690

config/.flag-test.mk

Whitespace-only changes.

config/crd/bases/infrastructure.cluster.x-k8s.io_cloudstackclusters.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ spec:
425425
description: Cloudstack Network Netmask the cluster
426426
is built in.
427427
type: string
428+
networkMode:
429+
description: Cloudstack Network's routing mode.
430+
type: string
428431
offering:
429432
description: |-
430433
Cloudstack Network Offering the cluster is built in.

config/crd/bases/infrastructure.cluster.x-k8s.io_cloudstackfailuredomains.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ spec:
193193
description: Cloudstack Network Netmask the cluster is built
194194
in.
195195
type: string
196+
networkMode:
197+
description: Cloudstack Network's routing mode.
198+
type: string
196199
offering:
197200
description: |-
198201
Cloudstack Network Offering the cluster is built in.

config/crd/bases/infrastructure.cluster.x-k8s.io_cloudstackisolatednetworks.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ spec:
254254
loadBalancerRuleID:
255255
description: The ID of the lb rule used to assign VMs to the lb.
256256
type: string
257+
networkMode:
258+
description: Network mode of the network.
259+
type: string
257260
publicIPID:
258261
description: The CS public IP ID to use for the k8s endpoint.
259262
type: string

controllers/cloudstackmachine_controller.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,17 @@ func (r *CloudStackMachineReconciliationRunner) RequeueIfInstanceNotRunning() (r
295295
// AddToLBIfNeeded adds instance to load balancer if it is a control plane in an isolated network.
296296
func (r *CloudStackMachineReconciliationRunner) AddToLBIfNeeded() (retRes ctrl.Result, reterr error) {
297297
if util.IsControlPlaneMachine(r.CAPIMachine) && r.FailureDomain.Spec.Zone.Network.Type == cloud.NetworkTypeIsolated {
298-
r.Log.Info("Assigning VM to load balancer rule.")
299298
if r.IsoNet.Spec.Name == "" {
300299
return r.RequeueWithMessage("Could not get required Isolated Network for VM, requeueing.")
301300
}
302-
err := r.CSUser.AssignVMToLoadBalancerRule(r.IsoNet, *r.ReconciliationSubject.Spec.InstanceID)
303-
if err != nil {
304-
return ctrl.Result{}, err
301+
302+
if r.IsoNet.Status.NetworkMode == "" {
303+
// For non-routed networks, use load balancer
304+
r.Log.Info("Assigning VM to load balancer rule.")
305+
err := r.CSUser.AssignVMToLoadBalancerRule(r.IsoNet, *r.ReconciliationSubject.Spec.InstanceID)
306+
if err != nil {
307+
return ctrl.Result{}, err
308+
}
305309
}
306310
}
307311
return ctrl.Result{}, nil

pkg/cloud/isolated_network.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func (c *client) CreateIsolatedNetwork(fd *infrav1.CloudStackFailureDomain, isoN
148148
isoNet.Spec.ID = resp.Id
149149
isoNet.Spec.Gateway = resp.Gateway
150150
isoNet.Spec.Netmask = resp.Netmask
151+
isoNet.Status.NetworkMode = resp.Ip4routing
151152
return c.AddCreatedByCAPCTag(ResourceTypeNetwork, isoNet.Spec.ID)
152153
}
153154

@@ -171,17 +172,28 @@ func (c *client) OpenFirewallRules(isoNet *infrav1.CloudStackIsolatedNetwork) (r
171172

172173
protocols := []string{NetworkProtocolTCP, NetworkProtocolUDP, NetworkProtocolICMP}
173174
for _, proto := range protocols {
174-
p := c.cs.Firewall.NewCreateEgressFirewallRuleParams(isoNet.Spec.ID, proto)
175+
var err error
176+
if isoNet.Status.NetworkMode != "" {
177+
p := c.cs.Firewall.NewCreateRoutingFirewallRuleParams(isoNet.Spec.ID, proto)
178+
if proto == "icmp" {
179+
p.SetIcmptype(-1)
180+
p.SetIcmpcode(-1)
181+
}
182+
_, err = c.cs.Firewall.CreateRoutingFirewallRule(p)
183+
} else {
184+
p := c.cs.Firewall.NewCreateEgressFirewallRuleParams(isoNet.Spec.ID, proto)
175185

176-
if proto == "icmp" {
177-
p.SetIcmptype(-1)
178-
p.SetIcmpcode(-1)
179-
}
186+
if proto == "icmp" {
187+
p.SetIcmptype(-1)
188+
p.SetIcmpcode(-1)
189+
}
180190

181-
_, err := c.cs.Firewall.CreateEgressFirewallRule(p)
191+
_, err = c.cs.Firewall.CreateEgressFirewallRule(p)
192+
}
182193
if err != nil &&
183-
// Ignore errors regarding already existing fw rules for TCP/UDP
194+
// Ignore errors regarding already existing fw rules for TCP/UDP for non-dynamic routing mode
184195
!strings.Contains(strings.ToLower(err.Error()), "there is already") &&
196+
!strings.Contains(strings.ToLower(err.Error()), "conflicts with rule") &&
185197
// Ignore errors regarding already existing fw rule for ICMP
186198
!strings.Contains(strings.ToLower(err.Error()), "new rule conflicts with existing rule") {
187199
retErr = errors.Wrapf(
@@ -298,6 +310,7 @@ func (c *client) GetOrCreateIsolatedNetwork(
298310
isoNet.Spec.ID = net.ID
299311
isoNet.Spec.Gateway = net.Gateway
300312
isoNet.Spec.Netmask = net.Netmask
313+
isoNet.Status.NetworkMode = net.NetworkMode
301314
if net.VPC != nil && net.VPC.ID != "" {
302315
isoNet.Spec.VPC = net.VPC
303316
}
@@ -316,14 +329,17 @@ func (c *client) GetOrCreateIsolatedNetwork(
316329
}
317330
}
318331

319-
// Associate Public IP with CloudStackIsolatedNetwork
320-
if err := c.AssociatePublicIPAddress(fd, isoNet, csCluster); err != nil {
321-
return errors.Wrapf(err, "associating public IP address to csCluster")
322-
}
332+
// Handle control plane endpoint based on network type
333+
if isoNet.Status.NetworkMode == "" {
334+
// For non-routed networks, use public IP and load balancer
335+
if err := c.AssociatePublicIPAddress(fd, isoNet, csCluster); err != nil {
336+
return errors.Wrapf(err, "associating public IP address to csCluster")
337+
}
323338

324-
// Setup a load balancing rule to map VMs to Public IP.
325-
if err := c.GetOrCreateLoadBalancerRule(isoNet, csCluster); err != nil {
326-
return errors.Wrap(err, "getting or creating load balancing rule")
339+
// Setup a load balancing rule to map VMs to Public IP.
340+
if err := c.GetOrCreateLoadBalancerRule(isoNet, csCluster); err != nil {
341+
return errors.Wrap(err, "getting or creating load balancing rule")
342+
}
327343
}
328344

329345
// Open the Isolated Network on endopint port.

pkg/cloud/network.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (c *client) ResolveNetwork(net *infrav1.Network) (retErr error) {
6666
net.Gateway = netDetails.Gateway
6767
net.Netmask = netDetails.Netmask
6868
net.Offering = netDetails.Networkofferingname
69+
net.NetworkMode = netDetails.Ip4routing
6970
if netDetails.Vpcid != "" {
7071
if net.VPC == nil {
7172
net.VPC = &infrav1.VPC{}
@@ -90,6 +91,7 @@ func (c *client) ResolveNetwork(net *infrav1.Network) (retErr error) {
9091
net.Gateway = netDetails.Gateway
9192
net.Netmask = netDetails.Netmask
9293
net.Offering = netDetails.Networkofferingname
94+
net.NetworkMode = netDetails.Ip4routing
9395
if netDetails.Vpcid != "" {
9496
if net.VPC == nil {
9597
net.VPC = &infrav1.VPC{}

templates/cluster-template-with-kube-vip.yaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,29 @@ spec:
6868
spec:
6969
containers:
7070
- args:
71-
- start
71+
- manager
7272
env:
7373
- name: vip_arp
7474
value: "true"
7575
- name: vip_leaderelection
7676
value: "true"
77-
- name: vip_address
78-
value: ${CLUSTER_ENDPOINT_IP}
7977
- name: vip_interface
8078
value: ens3
79+
- name: address
80+
value: ${CLUSTER_ENDPOINT_IP}
81+
- name: vip_cidr
82+
value: "32"
83+
- name: cp_enable
84+
value: "true"
85+
- name: cp_namespace
86+
value: kube-system
8187
- name: vip_leaseduration
8288
value: "15"
8389
- name: vip_renewdeadline
8490
value: "10"
8591
- name: vip_retryperiod
8692
value: "2"
87-
image: public.ecr.aws/i3w0y7q3/plunder-app/kube-vip:v0.3.7-eks-a-v0.0.0-dev-build.0
93+
image: ghcr.io/kube-vip/kube-vip:v0.4.0
8894
imagePullPolicy: IfNotPresent
8995
name: kube-vip
9096
resources: {}
@@ -96,10 +102,14 @@ spec:
96102
volumeMounts:
97103
- mountPath: /etc/kubernetes/admin.conf
98104
name: kubeconfig
105+
hostAliases:
106+
- hostnames:
107+
- kubernetes
108+
ip: 127.0.0.1
99109
hostNetwork: true
100110
volumes:
101111
- hostPath:
102-
path: /etc/kubernetes/admin.conf
112+
path: /etc/kubernetes/super-admin.conf
103113
type: FileOrCreate
104114
name: kubeconfig
105115
status: {}

0 commit comments

Comments
 (0)