Skip to content

Commit

Permalink
feat: 升级nginx版本,支持docker构建 (#15)
Browse files Browse the repository at this point in the history
* feat:调整构建脚本,删除nginx代码

* 使用rm -f防止删除失败

* feat; 无需执行清理逻辑

* feat: 修改nginx限流的问题

* feat: 重新编写readme

* feat: 支持容器镜像的构建
  • Loading branch information
andrewshan authored Aug 31, 2022
1 parent 82fc302 commit e2b9bfe
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 47 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: DockerImage

on:
release:
types: [published]

jobs:
release:
name: Release Polaris Docker Image
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux]
goarch: [amd64]
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Get version
id: get_version
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
config-inline: |
insecure-entitlements = [ "network.host" ]
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.POLARIS_DOCKER_NAME }}
password: ${{ secrets.POLARIS_DOCKER_PASSWORD }}

- name: Build Server
id: build-server
env:
DOCKER_TAG: ${{ steps.get_version.outputs.VERSION }}
run: |
cd build
./build_docker.sh ${DOCKER_TAG}
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM debian:stable-slim

# install dependencies
RUN set -ex \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
autoconf automake wget git libtool curl make gcc g++ unzip libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

COPY source /build/source
COPY third_party /build/third_party
COPY polaris.yaml /build/

# build polaris-cpp
RUN set -ex \
&& cd /build/third_party \
&& git config --global http.sslverify false \
&& git clone -b release_v1.1.0 https://github.com/polarismesh/polaris-cpp polaris-cpp \
&& cd polaris-cpp \
&& make \
&& make package \
&& tar xf polaris_cpp_sdk.tar.gz \
&& mv polaris_cpp_sdk/* /build/third_party/polaris_client/

RUN set -ex \
&& mkdir -p /server \
&& cd /build/third_party \
&& ngx_file_name=nginx-1.23.1 \
&& curl http://nginx.org/download/"$ngx_file_name".tar.gz -o "$ngx_file_name".tar.gz \
&& tar xf "$ngx_file_name".tar.gz \
&& cp nginx/make "$ngx_file_name"/auto/ \
&& chmod +x "$ngx_file_name"/configure \
&& cd "$ngx_file_name" \
&& ./configure --prefix=/server --add-module=../../source/nginx_polaris_limit_module --add-module=../polaris_client --with-stream --with-cpp=g++ \
&& make

WORKDIR /server

CMD ["nginx", "-g", "daemon off;"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

用户也可以通过源码编译的方式,生成安装包。

- 安装依赖项:在编译之前,需要先安装依赖项。通过执行```yum install autoconf automake libtool curl make g++ unzip```进行安装。
- 安装依赖项:在编译之前,需要先安装依赖项。通过执行```yum install autoconf automake libtool curl make gcc-c++ libstdc++-devel unzip```进行安装。

- 下载源码包:可以直接从[releases](https://github.com/polarismesh/nginx-gateway/releases)下载最新的nginx网关源码包。

Expand Down
20 changes: 12 additions & 8 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ echo "build nginx-gateway for version ${version}"
pushd ../third_party
rm -rf polaris-cpp
git clone -b release_v1.1.0 https://github.com/polarismesh/polaris-cpp polaris-cpp
rm -rf nginx-1.18.0
curl http://nginx.org/download/nginx-1.18.0.tar.gz -o nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz
cp nginx/make nginx-1.18.0/auto/

ngx_file_name=nginx-1.23.1
rm -rf "$ngx_file_name"
curl http://nginx.org/download/"$ngx_file_name".tar.gz -o "$ngx_file_name".tar.gz
tar xf "$ngx_file_name".tar.gz
cp nginx/make "$ngx_file_name"/auto/

pushd polaris-cpp
make
Expand All @@ -31,13 +33,15 @@ echo "target name $folder_name"
rm -rf ../$folder_name
mkdir -p ../$folder_name

pushd nginx-1.18.0/
pushd "$ngx_file_name"/
chmod +x configure
./configure \
--prefix=../../$folder_name \
--add-module=../../source/nginx_polaris_limit_module \
--add-module=../polaris_client \
--with-stream
--add-module=../../source/nginx_polaris_limit_module \
--add-module=../polaris_client \
--with-stream \
--with-cpp=g++

make
make install
cp ../nginx/start.sh ../../$folder_name/sbin/start.sh
Expand Down
30 changes: 30 additions & 0 deletions build/build_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

if [ $# != 1 ]; then
echo "e.g.: bash $0 v1.0"
exit 1
fi

docker_tag=$1

docker_repository="polarismesh"

echo "docker repository : ${docker_repository}/nginx, tag : ${docker_tag}"

arch_list=( "amd64" )
platforms=""

for arch in ${arch_list[@]}; do
platforms+="linux/${arch},"
done

platforms=${platforms::-1}
extra_tags=""

pre_release=`echo ${docker_tag}|egrep "(alpha|beta|rc|[T|t]est)"|wc -l`
if [ ${pre_release} == 0 ]; then
extra_tags="-t ${docker_repository}/nginx:latest"
fi

cd ..
docker buildx build --network=host -t ${docker_repository}/nginx:${docker_tag} ${extra_tags} --platform ${platforms} --push ./
14 changes: 9 additions & 5 deletions build/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

set -e

ngx_file_name=nginx-1.23.1

pushd ../third_party
rm -rf polaris-cpp
git clone -b release_v1.1.0 https://github.com/polarismesh/polaris-cpp polaris-cpp
rm -rf nginx-1.18.0
curl http://nginx.org/download/nginx-1.18.0.tar.gz -o nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz
rm -rf "$ngx_file_name"
curl http://nginx.org/download/"$ngx_file_name".tar.gz -o "$ngx_file_name".tar.gz
tar xf "$ngx_file_name".tar.gz
cp nginx/make "$ngx_file_name"/auto/

pushd polaris-cpp
make
Expand All @@ -25,13 +28,14 @@ echo "target name $folder_name"
rm -rf ../$folder_name
mkdir -p ../$folder_name

pushd nginx-1.18.0/
pushd "$ngx_file_name"/
chmod +x configure
./configure \
--prefix=../../$folder_name \
--add-module=../../source/nginx_polaris_limit_module \
--add-module=../polaris_client \
--with-stream
--with-stream \
--with-cpp=g++
make
popd

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ static ngx_int_t ngx_http_polaris_limit_handler(ngx_http_request_t *r) {
service_namespace_str = plcf->service_namespace;
service_name_str =plcf->service_name;

polaris::LimitApi* limit_api = Limit_API_SINGLETON.GetLimitApi();
if (NULL == limit_api) {
return NGX_OK;
}
std::string service_namespace(reinterpret_cast<char *>(service_namespace_str.data), service_namespace_str.len);
std::string service_name(reinterpret_cast<char *>(service_name_str.data), service_name_str.len);
polaris::ServiceKey serviceKey = {service_namespace, service_name};
Expand Down Expand Up @@ -334,4 +338,48 @@ static void get_labels_from_request(ngx_http_request_t* r, const std::set<std::s
}
}
}
}

static bool endsWith(const std::string& str, const std::string& suffix)
{
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}

static std::string get_polaris_conf_path() {
char *cwd = get_current_dir_name();
std::string dirname(cwd);
free(cwd);
if (endsWith(dirname, PATH_SBIN)) {
dirname = dirname.substr(0, dirname.rfind(PATH_SBIN));
} else {
dirname += "/";
}
dirname += "conf/polaris.yaml";
return dirname;
}

static bool exist_file(const std::string& name) {
std::ifstream file(name.c_str());
return file.good();
}

const std::string defaultConfigContent = R"##(
global:
serverConnector:
addresses:
- ${polaris_address}
)##";

LimitApiWrapper::LimitApiWrapper() {
std::string conf_path = get_polaris_conf_path();
std::string err_msg;
if (exist_file(conf_path)) {
m_limit = polaris::LimitApi::CreateFromFile(conf_path, err_msg);
} else {
std::cout << "[polaris-limiter] config file " << conf_path << " not exists, create with default config" << std::endl;
m_limit = polaris::LimitApi::CreateFromString(defaultConfigContent, err_msg);
}
if (NULL == m_limit) {
std::cout << "[polaris-limiter] fail to create limit api, err: " << err_msg << std::endl;
}
}
21 changes: 2 additions & 19 deletions source/nginx_polaris_limit_module/ngx_http_polaris_limit_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern "C" {
#include <iostream>
#include <string>
#include <unistd.h>
#include <fstream>

static const char KEY_NAMESPACE[] = "namespace=";
static const uint32_t KEY_NAMESPACE_SIZE = sizeof(KEY_NAMESPACE) - 1;
Expand All @@ -38,27 +39,9 @@ static const std::string LABEL_KEY_QUERY = "$query.";
static const std::string LABEL_KEY_CALLER_IP = "$caller_ip";
static const std::string PATH_SBIN = "sbin";

static bool endsWith(const std::string& str, const std::string& suffix)
{
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}

static std::string get_polaris_conf_path() {
char *cwd = get_current_dir_name();
std::string dirname(cwd);
free(cwd);
if (endsWith(dirname, PATH_SBIN)) {
dirname = dirname.substr(0, dirname.rfind(PATH_SBIN));
} else {
dirname += "/";
}
dirname += "conf/polaris.yaml";
return dirname;
}

class LimitApiWrapper {
public:
LimitApiWrapper() { m_limit = polaris::LimitApi::CreateFromFile(get_polaris_conf_path()); }
LimitApiWrapper();

static LimitApiWrapper& Instance() {
static LimitApiWrapper limit_api;
Expand Down
27 changes: 13 additions & 14 deletions third_party/nginx/make
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ cat << END > $NGX_MAKEFILE

CC = $CC
CFLAGS = $CFLAGS
CPP = g++
CXX = g++
CXXFLAGS = -std=c++11 -Wall
LINK = g++
CPP = $CPP
CPPFLAGS = -std=c++11 -Wall
LINK = $LINK

END

Expand Down Expand Up @@ -315,7 +314,7 @@ $ngx_obj: \$(CORE_DEPS) \$(HTTP_DEPS)$ngx_cont$ngx_src
END

fi
done
done

fi

Expand Down Expand Up @@ -345,7 +344,7 @@ $ngx_obj: \$(CORE_DEPS) \$(MAIL_DEPS)$ngx_cont$ngx_src
$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX

END
done
done

fi

Expand Down Expand Up @@ -375,7 +374,7 @@ $ngx_obj: \$(CORE_DEPS) \$(STREAM_DEPS)$ngx_cont$ngx_src
$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX

END
done
done

fi

Expand All @@ -401,7 +400,7 @@ $ngx_obj: \$(CORE_DEPS) $ngx_cont$ngx_src
$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX

END
done
done

fi

Expand All @@ -410,9 +409,8 @@ fi

if test -n "$NGX_ADDON_SRCS"; then

ngx_cc="\$(CPP) $ngx_compile_opt -std=c++11 -g -O0 \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
ngx_cxx="\$(CXX) $ngx_compile_opt \$(CXXFLAGS) $ngx_use_pch ddons sources\$(ALL_INCS)"

ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
ngx_cpp="\$(CPP) $ngx_compile_opt \$(CPPFLAGS) $ngx_use_pch \$(ALL_INCS)"
for ngx_src in $NGX_ADDON_SRCS
do
ngx_obj="addon/`basename \`dirname $ngx_src\``"
Expand All @@ -431,7 +429,7 @@ if test -n "$NGX_ADDON_SRCS"; then
ext=`echo ${ngx_src} | cut -d . -f 2`
ngx_gcc=$ngx_cc
if [ $ext = "cpp" ]; then
ngx_gcc=$ngc_cxx
ngx_gcc=$ngx_cpp
fi

cat << END >> $NGX_MAKEFILE
Expand All @@ -440,7 +438,7 @@ $ngx_obj: \$(ADDON_DEPS)$ngx_cont$ngx_src
$ngx_gcc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX

END
done
done

fi

Expand Down Expand Up @@ -511,6 +509,7 @@ fi
for ngx_module in $DYNAMIC_MODULES
do
eval ngx_module_srcs="\$${ngx_module}_SRCS"
eval ngx_module_shrd="\$${ngx_module}_SHRD"
eval eval ngx_module_libs="\\\"\$${ngx_module}_LIBS\\\""

eval ngx_module_modules="\$${ngx_module}_MODULES"
Expand Down Expand Up @@ -576,7 +575,7 @@ END
| sed -e "s/\(.*\.\)c/\1$ngx_objext/"`

ngx_module_objs=
for ngx_src in $ngx_module_srcs
for ngx_src in $ngx_module_srcs $ngx_module_shrd
do
case "$ngx_src" in
src/*)
Expand Down

0 comments on commit e2b9bfe

Please sign in to comment.