Skip to content

Allow updating of Load Balancer source CIDR list #10968

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

Open
wants to merge 3 commits into
base: 4.19
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.LoadBalancer;
import com.cloud.user.Account;
import java.util.List;

@APICommand(name = "updateLoadBalancerRule", description = "Updates load balancer", responseObject = LoadBalancerResponse.class,
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
Expand Down Expand Up @@ -66,6 +67,9 @@
@Parameter(name = ApiConstants.PROTOCOL, type = CommandType.STRING, description = "The protocol for the LB")
private String lbProtocol;

@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list to forward traffic from")
private List<String> cidrList;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand Down Expand Up @@ -94,6 +98,9 @@
return lbProtocol;
}

public List<String> getCidrList() {

Check warning on line 101 in api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java#L101

Added line #L101 was not covered by tests
return cidrList;
}

Check warning on line 103 in api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java#L103

Added line #L103 was not covered by tests
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,8 @@ public Scheme getScheme() {
public String getCidrList() {
return cidrList;
}

public void setCidrList(String cidrList) {
this.cidrList = cidrList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 com.cloud.network.dao;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class LoadBalancerVOTest {
@Test
public void testSetCidrList() {
LoadBalancerVO loadBalancer = new LoadBalancerVO();
String cidrList = "192.168.1.0/24,10.0.0.0/16";
loadBalancer.setCidrList(cidrList);
assertEquals(cidrList, loadBalancer.getCidrList());
}

@Test
public void testSetCidrListEmpty() {
LoadBalancerVO loadBalancer = new LoadBalancerVO();
loadBalancer.setCidrList("");
assertEquals("", loadBalancer.getCidrList());
}

@Test
public void testSetCidrListNull() {
LoadBalancerVO loadBalancer = new LoadBalancerVO();
loadBalancer.setCidrList(null);
assertNull(loadBalancer.getCidrList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,16 @@
lb.setLbProtocol(lbProtocol);
}

List<String> cidrList = cmd.getCidrList();

if (cidrList != null) {
String cidrListStr = StringUtils.join(cidrList, ",");

Check warning on line 2257 in server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java#L2257

Added line #L2257 was not covered by tests
if (!cidrListStr.isEmpty() && !NetUtils.isValidCidrList(cidrListStr)) {
throw new InvalidParameterValueException("Invalid CIDR list: " + cidrListStr);

Check warning on line 2259 in server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java#L2259

Added line #L2259 was not covered by tests
}
lb.setCidrList(cidrListStr);

Check warning on line 2261 in server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java#L2261

Added line #L2261 was not covered by tests
}

// Validate rule in LB provider
LoadBalancingRule rule = getLoadBalancerRuleToApply(lb);
if (!validateLbRule(rule)) {
Expand All @@ -2260,8 +2270,14 @@
LoadBalancerVO tmplbVo = _lbDao.findById(lbRuleId);
boolean success = _lbDao.update(lbRuleId, lb);

// If algorithm is changed, have to reapply the lb config
if ((algorithm != null) && (tmplbVo.getAlgorithm().compareTo(algorithm) != 0)){
// Check if algorithm or cidrlist has changed, and reapply the lb config if needed
boolean algorithmChanged = (algorithm != null) && (tmplbVo.getAlgorithm().compareTo(algorithm) != 0);
boolean cidrListChanged = (tmplbVo.getCidrList() == null && lb.getCidrList() != null) ||
(tmplbVo.getCidrList() != null && lb.getCidrList() == null) ||
(tmplbVo.getCidrList() != null && lb.getCidrList() != null &&
!tmplbVo.getCidrList().equals(lb.getCidrList()));

if (algorithmChanged || cidrListChanged) {
try {
lb.setState(FirewallRule.State.Add);
_lbDao.persist(lb);
Expand All @@ -2282,6 +2298,9 @@
if (lbBackup.getAlgorithm() != null) {
lb.setAlgorithm(lbBackup.getAlgorithm());
}
if (lbBackup.getCidrList() != null || lb.getCidrList() != null) { // Added for cidrlist rollback
lb.setCidrList(lbBackup.getCidrList());

Check warning on line 2302 in server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java#L2302

Added line #L2302 was not covered by tests
}
lb.setState(lbBackup.getState());
_lbDao.update(lb.getId(), lb);
_lbDao.persist(lb);
Expand Down
Loading