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

First commit for bfe ingress controller #3

Merged
merged 6 commits into from
Sep 8, 2021
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# MacOS files
.DS_Store
*/.DS_Store
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: git://github.com/dnephin/pre-commit-golang
rev: v0.4.0
hooks:
- id: go-fmt
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
##################################### stage 1 #####################################
FROM bfe

# adapt BFE conf in docker
COPY output/adapt_bfe_docker.sh /adapt_bfe_docker.sh
RUN sh /adapt_bfe_docker.sh

# pack /home/work directory for COPY in stage 2
COPY build/output/bfe_ingress_controller \
output/ingress.commit \
/home/work/bfe/bin/
COPY build/output/start.sh /home/work/start.sh

##################################### stage 2 #####################################
# build image from CentOS to reduce image size
FROM centos
RUN yum install -y wget unzip vi net-tools redhat-lsb-core nmap-ncat.x86_64 tcpdump tree jre epel-release \
&& yum install -y supervisor \
&& yum clean all

# copy base files from bfe image
RUN groupadd -g 501 work && useradd -g work -G work -u 500 -d /home/work work
COPY --from=bfe --chown=work:work /home/work /home/work

ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
ENV LD_LIBRARY_PATH $LD_LIBRARY_PATH:/usr/lib/jvm/jre-openjdk/lib/amd64/server/

RUN mkdir -p /opt/compiler/gcc-4.8.2/lib64 && \
ln -s /lib64/ld-linux-x86-64.so.2 /opt/compiler/gcc-4.8.2/lib64/ld-linux-x86-64.so.2 && \
mkdir -p /opt/compiler/gcc-8.2/lib64 && \
ln -s /lib64/ld-linux-x86-64.so.2 /opt/compiler/gcc-8.2/lib64/ld-linux-x86-64.so.2 && \
ln -sf /home/work/opbin/pbtool/bin/pblogTool3 /usr/bin/pblogTool3 && \
ln -s /home/work/opbin/bns_tool_stub /home/work/bns_tool_stub && \
ln -sf /home/work/opbin/bns_tool_stub/bns_tool_stub /usr/bin/get_instance_by_service && \
mv /home/work/start.sh /start.sh

WORKDIR /home/work/bfe/
USER work
EXPOSE 8080 8443 8421
ENTRYPOINT ["/start.sh"]


96 changes: 96 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright (c) 2021 The BFE Authors.
#
# Licensed 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.

# init project path
WORKROOT := $(shell pwd)
OUTDIR := $(WORKROOT)/output

# init environment variables
export PATH := $(shell go env GOPATH)/bin:$(PATH)
export GO111MODULE := on

# init command params
GO := go
GOBUILD := $(GO) build
GOTEST := $(GO) test
GOVET := $(GO) vet
GOGET := $(GO) get
GOGEN := $(GO) generate
GOCLEAN := $(GO) clean
GOFLAGS := -race
STATICCHECK := staticcheck

# init arch
ARCH := $(shell getconf LONG_BIT)
ifeq ($(ARCH),64)
GOTEST += $(GOFLAGS)
endif

# init bfe ingress version
INGRESS_VERSION ?= $(shell cat VERSION)
# init git commit id
GIT_COMMIT ?= $(shell git rev-parse HEAD)

# init bfe ingress packages
INGRESS_PACKAGES := $(shell go list ./...)

# make, make all
all: compile package

# make compile, go build
compile: test build
build:
cd $(WORKROOT)/cmd/bfe_ingress_controller && GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags "-X main.version=$(INGRESS_VERSION) -X main.commit=$(GIT_COMMIT)" -o bfe_ingress_controller

# make test, test your code
test: test-case vet-case
test-case:
$(GOTEST) -cover ./...
vet-case:
${GOVET} ./...

# make coverage for codecov
coverage:
echo -n > coverage.txt
for pkg in $(INGRESS_PACKAGES) ; do $(GOTEST) -coverprofile=profile.out -covermode=atomic $${pkg} && cat profile.out >> coverage.txt; done

# make package
package:
mkdir -p $(OUTDIR)
mv $(WORKROOT)/cmd/bfe_ingress_controller $(OUTDIR)/
cp -r $(WORKROOT)/dist/ $(OUTDIR)/
cp $(WORKROOT)/build/adapt_bfe_docker.sh $(OUTDIR)/
chmod a+x $(OUTDIR)/*
echo "$(GIT_COMMIT)" > $(OUTDIR)/ingress.commit

# make check
check:
$(GO) get honnef.co/go/tools/cmd/staticcheck
$(STATICCHECK) ./...

# make docker
docker:
docker build \
-t bfe_ingress_controller:$(INGRESS_VERSION) \
-f Dockerfile \
.

# make clean
clean:
$(GOCLEAN)
rm -rf $(OUTDIR)
rm -rf $(GOPATH)/pkg/linux_amd64

# avoid filename conflict and speed up build
.PHONY: all compile test package clean build
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# ingress-bfe
BFE Ingress Controller for Kubernetes
# BFE Ingress Controller

BFE Ingress Controller 为基于 [BFE][] 实现的 Kubernetes [Ingress Controller][],
用于支持在 Kubernetes 中部署 [Ingress][]。

## 开始使用
参见 [部署指南文档](docs/zh_cn/deployment.md),开始使用BFE Ingress controller。

## 配置文档
参见 [配置文档](docs/zh_cn/ingress/configuration.md),了解BFE Ingress controller更详细的配置和使用说明。

## 参与贡献
* 请首先在 [Issue 列表][] 中创建一个 Issue
* 如有必要,请联系项目维护者/负责人进行进一步讨论
* 请遵循 Golang 编程规范
* 参见 [贡献指南文档](docs/zh_cn/contribute/how-to-contribute.md)

## 许可
基于 Apache 2.0 许可证,详见 [LICENSE](LICENSE) 文件说明

[Ingress Controller]: https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/ "Kubernetes"
[Ingress]: https://kubernetes.io/docs/concepts/services-networking/ingress/ "Kubernetes"
[BFE]: https://github.com/bfenetworks/bfe "Github"
[Issue 列表]: https://github.com/bfenetworks/ingress-bfe/issues
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0-dev
15 changes: 15 additions & 0 deletions build/adapt_bfe_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -x

# origin BFE root in docker
# see https://github.com/bfenetworks/bfe/blob/develop/Dockerfile
DOCKER_BFE_ROOT=/bfe
# new BFE root for ingress
WORK_BFE_ROOT=/home/work/bfe

mkdir -p ${WORK_BFE_ROOT}
cp -r ${DOCKER_BFE_ROOT}/bin ${WORK_BFE_ROOT}/
cp -r ${DOCKER_BFE_ROOT}/conf ${WORK_BFE_ROOT}/

# create directory for cert files
mkdir -p "${WORK_BFE_ROOT}/conf/tls_conf/certs"
158 changes: 158 additions & 0 deletions cmd/bfe_ingress_controller/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) 2021 The BFE Authors.
//
// Licensed 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 main

import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"time"
)

import (
"github.com/baidu/go-lib/log"
"github.com/baidu/go-lib/log/log4go"
)

import (
"github.com/bfenetworks/ingress-bfe/internal/bfe_ingress"
"github.com/bfenetworks/ingress-bfe/internal/utils"
)

zhugelianglongming marked this conversation as resolved.
Show resolved Hide resolved
var (
help = flag.Bool("h", false, "to show help")
stdOut = flag.Bool("s", false, "to show log in stdout")
showVersion = flag.Bool("v", false, "to show version of BFE ingress controller")
showVerbose = flag.Bool("V", false, "to show verbose information")
debugLog = flag.Bool("d", false, "to show debug log (otherwise >= info)")
logPath = flag.String("l", "./log", "dir path of log")
bfeConfigRoot = flag.String("c", utils.DefaultBfeConfigRoot, "root dir path of BFE config")
reloadURLPrefix = flag.String("u", utils.DefaultReloadURLPrefix, "BFE reload URL prefix")
syncPeriod = flag.Int("p", int(utils.DefaultSyncPeriod/time.Second),
"sync period (in second) for Ingress watcher")
namespaceLabels = flag.String("f", "", "namespace label selector, split by ,")
ingressClass = flag.String("k", "", "listen ingress class name")

namespaces utils.Namespaces
)

var version string
var commit string

func checkLabels(namespaces utils.Namespaces, labels string) error {
if labels == "" {
return nil
}
//namespace and label is exclusionary
if len(namespaces) > 0 && labels != "" {
return fmt.Errorf("labels and namespace sholud exclude, namespace[%s], labels[%s]", namespaces, labels)
}
labelsArr := strings.Split(labels, ",")
for _, label := range labelsArr {
keyValue := strings.Split(label, "=")
if len(keyValue) != 2 {
return fmt.Errorf("labels should be key=value, curVal[%s]", label)
}
}
return nil
}

func main() {
flag.Var(&namespaces, "n", "namespace to watch")
flag.Parse()
if *help {
flag.PrintDefaults()
return
}
if *showVerbose {
printIngressVersion(version)
fmt.Printf("go version: %s\n", runtime.Version())
fmt.Printf("git commit: %s\n", commit)
return
}
if *showVersion {
printIngressVersion(version)
return
}

// check ingress parameters
if err := checkParams(); err != nil {
fmt.Printf("bfe_ingress_controller: check params error[%s]", err.Error())
return
}

// init log
if err := initLog(); err != nil {
fmt.Printf("bfe_ingress_controller: err in log.Init():%v\n", err)
log.Logger.Close()
os.Exit(1)
}

labels := strings.Split(*namespaceLabels, ",")

// create BFE Ingress controller
bfeIngress := bfe_ingress.NewBfeIngress(namespaces, labels, *ingressClass)
bfeIngress.ReloadURLPrefix = *reloadURLPrefix
bfeIngress.BfeConfigRoot = *bfeConfigRoot
bfeIngress.SyncPeriod = time.Duration(*syncPeriod) * time.Second

// start BFE Ingress controller
log.Logger.Info("bfe_ingress_controller[version:%s] start", version)
bfeIngress.Start()

time.Sleep(1 * time.Second)
log.Logger.Close()
}

func initLog() error {
var logSwitch string
if *debugLog {
logSwitch = "DEBUG"
} else {
logSwitch = "INFO"
}

log4go.SetLogBufferLength(10000)
log4go.SetLogWithBlocking(false)
log4go.SetLogFormat(log4go.FORMAT_DEFAULT_WITH_PID)
log4go.SetSrcLineForBinLog(false)
return log.Init("bfe_ingress_controller", logSwitch, *logPath, *stdOut, "midnight", 7)
}

func checkParams() error {
if err := checkLabels(namespaces, *namespaceLabels); err != nil {
return err
}

if *bfeConfigRoot == "" {
return fmt.Errorf("BFE config root path should not be empty")
}

if *reloadURLPrefix == "" {
return fmt.Errorf("BFE reload URL prefix should not be empty")
}

if *syncPeriod <= 0 {
return fmt.Errorf("sync period for Ingress watcher sholud be greater then 0, period[%d]", *syncPeriod)
}

return nil
}

func printIngressVersion(version string) {
fmt.Printf("bfe_ingress_controller version: %s\n", version)
}
Loading