Skip to content

Commit f8d7bff

Browse files
committed
update dependency and add demo for consul2istio
Signed-off-by: chentanjun <[email protected]>
1 parent f6ba6a6 commit f8d7bff

File tree

21 files changed

+710
-1228
lines changed

21 files changed

+710
-1228
lines changed

.github/workflows/ci.yaml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,30 @@ jobs:
1111
- name: Setup Go
1212
uses: actions/setup-go@v1
1313
with:
14-
go-version: 1.14.4
14+
go-version: 1.19
1515
- name: Build
1616
run: go build -race ./...
1717
- name: Test
18-
run: go test -race ./...
19-
golangci-lint:
20-
name: golangci-lint
18+
run: go test -race `go list ./... | grep -v e2e`
19+
go-lint:
20+
name: go-lint
2121
runs-on: ubuntu-latest
2222
steps:
2323
- name: Check out code into the Go module directory
2424
uses: actions/checkout@v2
25+
- name: golint
26+
uses: Jerome1337/[email protected]
27+
with:
28+
golint-path: './...'
29+
- name: Setup Go
30+
uses: actions/setup-go@v1
31+
with:
32+
go-version: 1.19
2533
- name: golangci-lint
26-
uses: golangci/golangci-lint-action@v2
27-
- uses: actions/checkout@v2
34+
uses: golangci/[email protected]
2835
with:
29-
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
30-
version: v1.31
36+
args: --timeout=10m --tests="false"
37+
version: v1.47.0
3138
style-check:
3239
name: gofmt and goimports
3340
runs-on: ubuntu-latest
@@ -37,23 +44,21 @@ jobs:
3744
- name: Setup Go
3845
uses: actions/setup-go@v2
3946
with:
40-
go-version: 1.14.4
47+
go-version: 1.19
4148
- name: Install dependencies
4249
run: |
4350
go version
44-
go get golang.org/x/tools/cmd/goimports
51+
go install golang.org/x/tools/cmd/goimports@latest
4552
- name: gofmt and goimports
46-
run: |
47-
gofmt -l -d ./
48-
goimports -l -d ./
53+
run: make style-check
4954
checkgomod:
5055
name: check go.mod and go.sum
5156
runs-on: ubuntu-latest
5257
steps:
5358
- uses: actions/checkout@v2
5459
- uses: actions/setup-go@v1
5560
with:
56-
go-version: 1.14.4
61+
go-version: 1.19
5762
- run: go mod tidy
5863
- name: Check for changes in go.mod or go.sum
5964
run: |

.github/workflows/publish.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Auto Build and Push image
2+
3+
on:
4+
create
5+
6+
jobs:
7+
publish_image:
8+
name: Build and Push consul2istio image
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Check out code
12+
uses: actions/[email protected]
13+
with:
14+
submodules: recursive
15+
16+
- name: Extract Tags name
17+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
18+
id: tag_env
19+
shell: bash
20+
run: |
21+
echo "##[set-output name=version;]$(echo ${GITHUB_REF##*/})"
22+
23+
- name: Extract Tags Type
24+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
25+
id: tag_type
26+
shell: bash
27+
run: |
28+
echo "##[set-output name=version;]$(echo ${GITHUB_REF#refs/tags/})"
29+
30+
- name: Login to Docker Hub
31+
uses: docker/login-action@v1
32+
with:
33+
username: ${{ secrets.DOCKERHUB_USERNAME }}
34+
password: ${{ secrets.DOCKERHUB_TOKEN }}
35+
36+
- name: Build consul2istio Docker Image
37+
if: ${{ startsWith(steps.tag_type.outputs.version, 'consul2istio/') }}
38+
run: |
39+
make docker-build tag=${{ steps.tag_env.outputs.version }}
40+
41+
- name: Push Aeraki Docker image
42+
if: ${{ startsWith(steps.tag_type.outputs.version, 'aeraki/') }}
43+
run: |
44+
docker push ghcr.io/aeraki-mesh/consul2istio:${{ steps.tag_env.outputs.version }}

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
.idea/
2-
out/
1+
**/.DS_Store
2+
debug
3+
out
4+
.idea
5+
tmp
6+
.vscode

Makefile

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,23 @@ GOTEST?=$(GOCMD) test
66
GOGET?=$(GOCMD) get
77
GOBIN?=$(GOPATH)/bin
88

9-
# Build parameters
109
OUT?=./out
11-
DOCKER_TMP?=$(OUT)/docker_temp/
12-
DOCKER_TAG?=aeraki/consul2istio:latest
13-
BINARY_NAME?=$(OUT)/consul2istio
14-
BINARY_NAME_DARWIN?=$(BINARY_NAME)-darwin
15-
MAIN_PATH_CONSUL_MCP=./cmd/consul2istio/main.go
10+
IMAGE_REPO?=ghcr.io/aeraki-mesh
11+
IMAGE_NAME?=consul2istio
12+
IMAGE_TAG?=latest
13+
IMAGE?=$(IMAGE_REPO)/$(IMAGE_NAME):$(IMAGE_TAG)
14+
MAIN_PATH=./cmd/consul2istio/main.go
15+
IMAGE_OS?=linux
16+
IMAGE_ARCH?=amd64
17+
IMAGE_DOCKERFILE_PATH?=docker/Dockerfile
1618

17-
build:
18-
CGO_ENABLED=0 GOOS=linux $(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH_CONSUL_MCP)
19-
build-mac:
20-
CGO_ENABLED=0 GOOS=darwin $(GOBUILD) -o $(BINARY_NAME_DARWIN) $(MAIN_PATH_CONSUL_MCP)
19+
build: test
20+
CGO_ENABLED=0 GOOS=$(IMAGE_OS) GOARCH=$(IMAGE_ARCH) $(GOBUILD) -o $(OUT)/$(IMAGE_ARCH)/$(IMAGE_OS)/$(IMAGE_NAME) $(MAIN_PATH)
2121
docker-build: build
22-
mkdir $(DOCKER_TMP)
23-
cp ./docker/Dockerfile $(DOCKER_TMP)
24-
cp $(BINARY_NAME) $(DOCKER_TMP)
25-
docker build -t $(DOCKER_TAG) $(DOCKER_TMP)
26-
rm -rf $(DOCKER_TMP)
27-
docker-push:
28-
docker push $(DOCKER_TAG)
22+
docker build --build-arg CONSUL2ISTIO_BIN_DIR=${OUT} --build-arg ARCH=${IMAGE_ARCH} --build-arg OS=${IMAGE_OS} \
23+
--no-cache --platform=${IMAGE_OS}/${IMAGE_ARCH} -t ${IMAGE} -f ${IMAGE_DOCKERFILE_PATH} .
24+
docker-push: docker-build
25+
docker push $(IMAGE)
2926
style-check:
3027
gofmt -l -d ./
3128
goimports -l -d ./

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,37 @@ Consul2istio watches Consul catalog and synchronize all the Consul services to I
66

77
Consul2istio will create a ServiceEntry resource for each service in the Consul catalog.
88

9-
![ consul2istio ](doc/consul2istio.png)
9+
![ consul2istio ](doc/consul2istio.png)
10+
11+
## example
12+
13+
Firstly, deploy consul and consul2istio to your Kubernetes cluster.
14+
15+
```bash
16+
kubectl apply -f k8s/consul.yaml
17+
kubectl apply -f k8s/consul2istio.yaml
18+
```
19+
20+
Secondly, deploy consumer-demo and provider-demo to your Kubernetes cluster.
21+
The consumer-demo service *9999/echo-rest/* to us.
22+
23+
```bash
24+
kubectl apply -f k8s/sample.yaml
25+
```
26+
27+
Finally, request from consumer-demo to provider-demo.
28+
```
29+
kubectl get pod -owide
30+
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
31+
consul-7bd648d9f-qpkrj 1/1 Running 0 58m 10.0.1.80 192.168.1.17 <none> <none>
32+
consul2istio-75c9dd98fd-cqglr 1/1 Running 0 9m32s 10.0.1.89 192.168.1.17 <none> <none>
33+
consumer-demo-66766c8d78-5stvw 2/2 Running 0 9m54s 10.0.1.88 192.168.1.17 <none> <none>
34+
provider-demo-v1-59ddd86974-z4sln 2/2 Running 0 9m54s 10.0.1.86 192.168.1.17 <none> <none>
35+
provider-demo-v2-5ccf64cdfd-dxdj5 2/2 Running 0 9m54s 10.0.1.87 192.168.1.17 <none> <none>
36+
```
37+
38+
the result of request.
39+
```
40+
kubectl exec -it consumer-demo-66766c8d78-5stvw -c istio-proxy -- curl 10.0.1.88:9999/echo-rest/aaaa
41+
echo() -> ip [ 10.0.1.86 ] param [ aaaa ]
42+
```

cmd/consul2istio/main.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package main
1516

1617
import (
@@ -19,18 +20,33 @@ import (
1920
"os/signal"
2021
"syscall"
2122

22-
"github.com/aeraki-framework/consul2istio/pkg"
2323
"istio.io/pkg/log"
24-
)
2524

26-
const (
27-
defaultConsulAddress = "127.0.0.1:8500"
25+
"github.com/aeraki-framework/consul2istio/pkg"
26+
"github.com/aeraki-framework/consul2istio/pkg/constants"
27+
"github.com/aeraki-framework/consul2istio/pkg/serviceregistry/consul"
2828
)
2929

3030
func main() {
31-
consulAddress := flag.String("consulAddress", defaultConsulAddress, "Consul Address")
31+
args := consul.NewConsulBootStrapArgs()
32+
33+
flag.StringVar(&args.ConsulAddress, "consulAddress", constants.DefaultConsulAddress, "Consul Address")
34+
flag.StringVar(&args.Namespace, "namespace", constants.ConfigRootNS, "namespace")
35+
flag.StringVar(&args.FQDN, "fqdn", "", "The FQDN for consul service")
36+
flag.BoolVar(&args.EnableDefaultPort, "enableDefaultPort", true,
37+
"The flag to start default port for consul service")
38+
3239
flag.Parse()
33-
controller := pkg.NewController(*consulAddress)
40+
41+
flag.VisitAll(func(flag *flag.Flag) {
42+
log.Infof("consul2istio parameter: %s: %v", flag.Name, flag.Value)
43+
})
44+
45+
initArgsWithEnv(args)
46+
log.Infof("consul2istio bootstrap parameter: %v", args)
47+
48+
controller := pkg.NewController(args)
49+
3450
// Create the stop channel for all of the servers.
3551
stopChan := make(chan struct{}, 1)
3652
err := controller.Run(stopChan)
@@ -44,3 +60,15 @@ func main() {
4460
<-signalChan
4561
stopChan <- struct{}{}
4662
}
63+
64+
func initArgsWithEnv(args *consul.BootStrapArgs) {
65+
consulAddress := os.Getenv("consulAddress")
66+
if consulAddress != "" {
67+
args.ConsulAddress = consulAddress
68+
}
69+
70+
namespace := os.Getenv("namespace")
71+
if namespace != "" {
72+
args.Namespace = namespace
73+
}
74+
}

docker/Dockerfile

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
FROM ubuntu:bionic
2-
# Base image for debug builds.
3-
# Built manually uploaded as "istionightly/base_debug"
1+
# Copyright Aeraki Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
414

5-
# Do not add more stuff to this list that isn't small or critically useful.
6-
# If you occasionally need something on the container do
7-
# sudo apt-get update && apt-get whichever
15+
FROM alpine:3.17
816

9-
# hadolint ignore=DL3005,DL3008
17+
ARG CONSUL2ISTIO_BIN_DIR
18+
ARG ARCH
19+
ARG OS
1020

11-
ENV consulAddress="127.0.0.1:8500"
21+
RUN apk update && \
22+
apk add curl
1223

13-
RUN apt-get update && \
14-
apt-get install --no-install-recommends -y \
15-
ca-certificates \
16-
curl \
17-
iptables \
18-
iproute2 \
19-
iputils-ping \
20-
knot-dnsutils \
21-
netcat \
22-
tcpdump \
23-
conntrack \
24-
bsdmainutils \
25-
net-tools \
26-
lsof \
27-
linux-tools-generic \
28-
sudo \
29-
&& update-ca-certificates \
30-
&& apt-get upgrade -y \
31-
&& apt-get clean \
32-
&& rm -rf /var/log/*log /var/lib/apt/lists/* /var/log/apt/* /var/lib/dpkg/*-old /var/cache/debconf/*-old
33-
34-
COPY consul2istio /usr/local/bin/
35-
ENTRYPOINT /usr/local/bin/consul2istio -consulAddress=$consulAddress
24+
COPY ${CONSUL2ISTIO_BIN_DIR}/${ARCH}/${OS}/consul2istio /usr/local/bin/
25+
ENTRYPOINT /usr/local/bin/consul2istio

0 commit comments

Comments
 (0)