Skip to content

Commit

Permalink
Merge "Generate loadbalancer config in json format"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed May 20, 2015
2 parents f319187 + f094601 commit 17f622d
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 311 deletions.
6 changes: 4 additions & 2 deletions src/ksync/ksync_object.cc
Expand Up @@ -29,7 +29,7 @@

KSyncObject::FwdRefTree KSyncObject::fwd_ref_tree_;
KSyncObject::BackRefTree KSyncObject::back_ref_tree_;
KSyncObjectManager *KSyncObjectManager::singleton_;
KSyncObjectManager *KSyncObjectManager::singleton_ = NULL;
std::auto_ptr<KSyncEntry> KSyncObjectManager::default_defer_entry_;
bool KSyncDebug::debug_;

Expand Down Expand Up @@ -1356,7 +1356,9 @@ KSyncObjectManager::~KSyncObjectManager() {
SandeshTraceBufferPtr KSyncTraceBuf(SandeshTraceBufferCreate("KSync", 1000));

KSyncObjectManager *KSyncObjectManager::Init() {
singleton_ = new KSyncObjectManager();
if (singleton_ == NULL) {
singleton_ = new KSyncObjectManager();
}
return singleton_;
}

Expand Down
12 changes: 6 additions & 6 deletions src/vnsw/agent/init/agent_param.cc
Expand Up @@ -511,8 +511,8 @@ void AgentParam::ParseServiceInstance() {
"SERVICE-INSTANCE.netns_workers");
GetValueFromTree<int>(si_netns_timeout_,
"SERVICE-INSTANCE.netns_timeout");
GetValueFromTree<string>(si_haproxy_ssl_cert_path_,
"SERVICE-INSTANCE.haproxy_ssl_cert_path");
GetValueFromTree<string>(si_lb_ssl_cert_path_,
"SERVICE-INSTANCE.lb_ssl_cert_path");

}

Expand Down Expand Up @@ -678,8 +678,8 @@ void AgentParam::ParseServiceInstanceArguments
GetOptValue<string>(var_map, si_docker_command_, "SERVICE-INSTANCE.docker_command");
GetOptValue<int>(var_map, si_netns_workers_, "SERVICE-INSTANCE.netns_workers");
GetOptValue<int>(var_map, si_netns_timeout_, "SERVICE-INSTANCE.netns_timeout");
GetOptValue<string>(var_map, si_haproxy_ssl_cert_path_,
"SERVICE-INSTANCE.haproxy_ssl_cert_path");
GetOptValue<string>(var_map, si_lb_ssl_cert_path_,
"SERVICE-INSTANCE.lb_ssl_cert_path");

}

Expand Down Expand Up @@ -1009,7 +1009,7 @@ void AgentParam::LogConfig() const {
LOG(DEBUG, "Service instance docker cmd : " << si_docker_command_);
LOG(DEBUG, "Service instance workers : " << si_netns_workers_);
LOG(DEBUG, "Service instance timeout : " << si_netns_timeout_);
LOG(DEBUG, "Service instance HAProxy ssl: " << si_haproxy_ssl_cert_path_);
LOG(DEBUG, "Service instance lb ssl : " << si_lb_ssl_cert_path_);
if (hypervisor_mode_ == MODE_KVM) {
LOG(DEBUG, "Hypervisor mode : kvm");
return;
Expand Down Expand Up @@ -1094,7 +1094,7 @@ AgentParam::AgentParam(Agent *agent, bool enable_flow_options,
xmpp_auth_enable_(false), xmpp_server_cert_(""),
simulate_evpn_tor_(false), si_netns_command_(),
si_docker_command_(), si_netns_workers_(0),
si_netns_timeout_(0), si_haproxy_ssl_cert_path_(),
si_netns_timeout_(0), si_lb_ssl_cert_path_(),
vmware_mode_(ESXI_NEUTRON), nexthop_server_endpoint_(),
vrouter_on_nic_mode_(false),
exception_packet_interface_(""), physical_interface_mac_addr_("") {
Expand Down
6 changes: 3 additions & 3 deletions src/vnsw/agent/init/agent_param.h
Expand Up @@ -117,8 +117,8 @@ class AgentParam {
std::string si_docker_command() const {return si_docker_command_;}
const int si_netns_workers() const {return si_netns_workers_;}
const int si_netns_timeout() const {return si_netns_timeout_;}
std::string si_haproxy_ssl_cert_path() const {
return si_haproxy_ssl_cert_path_;
std::string si_lb_ssl_cert_path() const {
return si_lb_ssl_cert_path_;
}

std::string nexthop_server_endpoint() const {
Expand Down Expand Up @@ -383,7 +383,7 @@ class AgentParam {
std::string si_docker_command_;
int si_netns_workers_;
int si_netns_timeout_;
std::string si_haproxy_ssl_cert_path_;
std::string si_lb_ssl_cert_path_;
VmwareMode vmware_mode_;
// List of IP addresses on the compute node.
AddressList compute_node_address_list_;
Expand Down
2 changes: 1 addition & 1 deletion src/vnsw/agent/oper/SConscript
Expand Up @@ -41,7 +41,7 @@ vnswoperdb = env.Library('vnswoperdb',
'inet_unicast_route.cc',
'interface.cc',
'loadbalancer.cc',
'loadbalancer_haproxy.cc',
'loadbalancer_config.cc',
'loadbalancer_properties.cc',
'logical_interface.cc',
'mirror_table.cc',
Expand Down
15 changes: 7 additions & 8 deletions src/vnsw/agent/oper/instance_manager.cc
Expand Up @@ -15,7 +15,7 @@
#include "io/event_manager.h"
#include "oper/instance_task.h"
#include "oper/loadbalancer.h"
#include "oper/loadbalancer_haproxy.h"
#include "oper/loadbalancer_config.h"
#include "oper/loadbalancer_properties.h"
#include "oper/operdb_init.h"
#include "oper/service_instance.h"
Expand Down Expand Up @@ -108,7 +108,7 @@ class InstanceManager::NamespaceStaleCleaner {
if (fs::exists(cfg_dir_path.str())) {
fs::remove_all(cfg_dir_path.str(), error);
if (error) {
LOG(ERROR, "Stale Haproxy cfg fle delete error"
LOG(ERROR, "Stale loadbalancer cfg fle delete error"
<< error.message());
}
}
Expand All @@ -135,7 +135,7 @@ InstanceManager::InstanceManager(Agent *agent)
loadbalancer_config_path_(loadbalancer_config_path_default),
namespace_store_path_(namespace_store_path_default),
stale_timer_interval_(5 * 60 * 1000),
haproxy_(new LoadbalancerHaproxy(agent)),
lb_config_(new LoadbalancerConfig(agent)),
stale_timer_(TimerManager::CreateTimer(*(agent->event_manager()->io_service()),
"NameSpaceStaleTimer", TaskScheduler::GetInstance()->
GetTaskId("db::DBTable"), 0)), agent_(agent) {
Expand Down Expand Up @@ -578,7 +578,7 @@ void InstanceManager::StopStaleNetNS(ServiceInstance::Properties &props) {
cmd_str << " " << UuidToString(boost::uuids::nil_uuid());
if (props.service_type == ServiceInstance::LoadBalancer) {
cmd_str << " --cfg-file " << loadbalancer_config_path_default <<
props.pool_id << "/etc/haproxy/haproxy.cfg";
props.pool_id << "/conf.json";
cmd_str << " --pool-id " << props.pool_id;
}

Expand Down Expand Up @@ -672,7 +672,6 @@ void InstanceManager::LoadbalancerObserver(

boost::system::error_code error;
if (!loadbalancer->IsDeleted() && loadbalancer->properties() != NULL) {
pathgen << "/etc/haproxy";
boost::filesystem::path dir(pathgen.str());
if (!boost::filesystem::exists(dir, error)) {
#if 0
Expand All @@ -687,9 +686,9 @@ void InstanceManager::LoadbalancerObserver(
return;
}
}
pathgen << "/haproxy.cfg";
haproxy_->GenerateConfig(pathgen.str(), loadbalancer->uuid(),
*loadbalancer->properties());
pathgen << "/conf.json";
lb_config_->GenerateConfig(pathgen.str(), loadbalancer->uuid(),
*loadbalancer->properties());
} else {
boost::filesystem::remove_all(pathgen.str(), error);
if (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/vnsw/agent/oper/instance_manager.h
Expand Up @@ -13,7 +13,7 @@

class Agent;
class DB;
class LoadbalancerHaproxy;
class LoadbalancerConfig;
class InstanceState;
class InstanceTask;
class InstanceTaskQueue;
Expand Down Expand Up @@ -73,7 +73,7 @@ class InstanceManager {

InstanceState *GetState(ServiceInstance *) const;
bool StaleTimeout();
const LoadbalancerHaproxy &haproxy() const { return *(haproxy_.get()); }
const LoadbalancerConfig &lb_config() const { return *(lb_config_.get()); }
void SetStaleTimerInterval(int minutes);
int StaleTimerInterval() { return stale_timer_interval_;}
void SetNamespaceStorePath(std::string path);
Expand Down Expand Up @@ -149,7 +149,7 @@ class InstanceManager {
std::string loadbalancer_config_path_;
std::string namespace_store_path_;
int stale_timer_interval_;
std::auto_ptr<LoadbalancerHaproxy> haproxy_;
std::auto_ptr<LoadbalancerConfig> lb_config_;
Timer *stale_timer_;
std::auto_ptr<NamespaceStaleCleaner> stale_cleaner_;
Agent *agent_;
Expand Down
142 changes: 142 additions & 0 deletions src/vnsw/agent/oper/loadbalancer_config.cc
@@ -0,0 +1,142 @@
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/

#include "loadbalancer_config.h"

#include <cerrno>
#include <fstream>
#include <boost/assign/list_of.hpp>
#include "base/logging.h"
#include "agent.h"
#include "init/agent_param.h"

#include "loadbalancer_properties.h"

using namespace std;
using boost::assign::map_list_of;

LoadbalancerConfig::LoadbalancerConfig(Agent *agent)
: agent_(agent) {
}

void LoadbalancerConfig::GeneratePool(
ostream *out, const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const {
const autogen::LoadbalancerPoolType &pool = props.pool_properties();

ostringstream ostr;
ostr << " \"pool\":{" << endl
<< " \"id\":\"" << pool_id << "\"," << endl
<< " \"protocol\":\"" << pool.protocol << "\"," << endl
<< " \"method\":\"" << pool.loadbalancer_method << "\"," << endl
<< " \"admin-state\":" << std::boolalpha << pool.admin_state
<< endl
<< " }," << endl;
*out << ostr.str();
}

void LoadbalancerConfig::GenerateVip(
ostream *out, const LoadbalancerProperties &props) const {

const autogen::VirtualIpType &vip = props.vip_properties();
ostringstream ostr;
ostr << " \"vip\":{" << endl
<< " \"id\":\"" << props.vip_uuid() << "\"," << endl
<< " \"address\":\"" << vip.address <<"\"," << endl
<< " \"port\":" << vip.protocol_port << "," << endl
<< " \"protocol\":\"" << vip.protocol <<"\"," << endl
<< " \"connection-limit\":" << vip.connection_limit << ","
<< endl
<< " \"persistence-cookie-name\": \""
<< vip.persistence_cookie_name << "\"," << endl
<< " \"persistence-type\": \"" << vip.persistence_type << "\","
<< endl
<< " \"admin-state\":" << std::boolalpha << vip.admin_state << endl
<< " }," << endl;
*out << ostr.str();
}

void LoadbalancerConfig::GenerateMembers(
ostream *out, const LoadbalancerProperties &props) const {

ostringstream ostr;
ostr << " \"members\":[" << endl;
int count = 0;
for (LoadbalancerProperties::MemberMap::const_iterator iter =
props.members().begin();
iter != props.members().end(); ++iter) {
const autogen::LoadbalancerMemberType &member = iter->second;
if (count) {
ostr << "," << endl;
}
ostr << " {" << endl
<< " \"id\":\"" << iter->first << "\"," << endl
<< " \"address\":\"" << member.address << "\"," << endl
<< " \"port\":" << member.protocol_port << "," << endl
<< " \"weight\":" << member.weight << "," << endl
<< " \"admin-state\":" << std::boolalpha
<< member.admin_state << endl
<< " }";
count++;
}
if (count) {
ostr << endl;
}
ostr << " ]," << endl;
*out << ostr.str();
}

void LoadbalancerConfig::GenerateHealthMonitors(
ostream *out, const LoadbalancerProperties &props) const {

ostringstream ostr;
ostr << " \"healthmonitors\":[" << endl;
int count = 0;
for (LoadbalancerProperties::HealthmonitorMap::const_iterator iter =
props.healthmonitors().begin();
iter != props.healthmonitors().end(); ++iter) {
const autogen::LoadbalancerHealthmonitorType &hm = iter->second;
if (count) {
ostr << "," << endl;
}
ostr << " {" << endl
<< " \"id\":\"" << iter->first << "\"," << endl
<< " \"type\": \"" << hm.monitor_type << "\"," << endl
<< " \"delay\":" << hm.delay << "," << endl
<< " \"timeout\":" << hm.timeout << "," << endl
<< " \"max-retries\":" << hm.max_retries << "," << endl
<< " \"http-method\": \"" << hm.http_method << "\"," << endl
<< " \"url\": \"" << hm.url_path << "\"," << endl
<< " \"expected-codes\": \"" << hm.expected_codes << "\","
<< endl
<< " \"admin-state\":" << std::boolalpha << hm.admin_state
<< endl
<< " }";
count++;
}
if (count) {
ostr << endl;
}
ostr << " ]" << endl;
*out << ostr.str();
}

void LoadbalancerConfig::GenerateConfig(
const string &filename, const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const {
ofstream fs(filename.c_str());
if (fs.fail()) {
LOG(ERROR, "File create " << filename << ": " << strerror(errno));
}

fs << "{" << endl;
fs << " \"ssl-crt\":\"" << agent_->params()->si_lb_ssl_cert_path()
<< "\"," << endl;
GeneratePool(&fs, pool_id, props);
GenerateVip(&fs, props);
GenerateMembers(&fs, props);
GenerateHealthMonitors(&fs, props);
fs << "}" << endl;
fs.close();
}
36 changes: 36 additions & 0 deletions src/vnsw/agent/oper/loadbalancer_config.h
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/

#include <map>
#include <string>
#include <ostream>
#include <boost/uuid/uuid.hpp>
#include "base/util.h"

class LoadbalancerProperties;
class Agent;

class LoadbalancerConfig {
public:
LoadbalancerConfig(Agent *);

void GenerateConfig(const std::string &filename,
const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const;

private:
void GenerateVip(std::ostream *out,
const LoadbalancerProperties &props) const;
void GeneratePool(std::ostream *out,
const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const;
void GenerateMembers(std::ostream *out,
const LoadbalancerProperties &props) const;
void GenerateHealthMonitors(std::ostream *out,
const LoadbalancerProperties &props) const;

Agent *agent_;

DISALLOW_COPY_AND_ASSIGN(LoadbalancerConfig);
};

0 comments on commit 17f622d

Please sign in to comment.