Skip to content

Commit

Permalink
update dependency and add demo for consul2istio
Browse files Browse the repository at this point in the history
Signed-off-by: chentanjun <[email protected]>
  • Loading branch information
tanjunchen committed Aug 4, 2023
1 parent f6ba6a6 commit f8d7bff
Show file tree
Hide file tree
Showing 21 changed files with 710 additions and 1,228 deletions.
33 changes: 19 additions & 14 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.14.4
go-version: 1.19
- name: Build
run: go build -race ./...
- name: Test
run: go test -race ./...
golangci-lint:
name: golangci-lint
run: go test -race `go list ./... | grep -v e2e`
go-lint:
name: go-lint
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: golint
uses: Jerome1337/[email protected]
with:
golint-path: './...'
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.19
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
- uses: actions/checkout@v2
uses: golangci/[email protected]
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.31
args: --timeout=10m --tests="false"
version: v1.47.0
style-check:
name: gofmt and goimports
runs-on: ubuntu-latest
Expand All @@ -37,23 +44,21 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.14.4
go-version: 1.19
- name: Install dependencies
run: |
go version
go get golang.org/x/tools/cmd/goimports
go install golang.org/x/tools/cmd/goimports@latest
- name: gofmt and goimports
run: |
gofmt -l -d ./
goimports -l -d ./
run: make style-check
checkgomod:
name: check go.mod and go.sum
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.14.4
go-version: 1.19
- run: go mod tidy
- name: Check for changes in go.mod or go.sum
run: |
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Auto Build and Push image

on:
create

jobs:
publish_image:
name: Build and Push consul2istio image
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/[email protected]
with:
submodules: recursive

- name: Extract Tags name
if: ${{ startsWith(github.ref, 'refs/tags/') }}
id: tag_env
shell: bash
run: |
echo "##[set-output name=version;]$(echo ${GITHUB_REF##*/})"
- name: Extract Tags Type
if: ${{ startsWith(github.ref, 'refs/tags/') }}
id: tag_type
shell: bash
run: |
echo "##[set-output name=version;]$(echo ${GITHUB_REF#refs/tags/})"
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build consul2istio Docker Image
if: ${{ startsWith(steps.tag_type.outputs.version, 'consul2istio/') }}
run: |
make docker-build tag=${{ steps.tag_env.outputs.version }}
- name: Push Aeraki Docker image
if: ${{ startsWith(steps.tag_type.outputs.version, 'aeraki/') }}
run: |
docker push ghcr.io/aeraki-mesh/consul2istio:${{ steps.tag_env.outputs.version }}
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.idea/
out/
**/.DS_Store
debug
out
.idea
tmp
.vscode
31 changes: 14 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@ GOTEST?=$(GOCMD) test
GOGET?=$(GOCMD) get
GOBIN?=$(GOPATH)/bin

# Build parameters
OUT?=./out
DOCKER_TMP?=$(OUT)/docker_temp/
DOCKER_TAG?=aeraki/consul2istio:latest
BINARY_NAME?=$(OUT)/consul2istio
BINARY_NAME_DARWIN?=$(BINARY_NAME)-darwin
MAIN_PATH_CONSUL_MCP=./cmd/consul2istio/main.go
IMAGE_REPO?=ghcr.io/aeraki-mesh
IMAGE_NAME?=consul2istio
IMAGE_TAG?=latest
IMAGE?=$(IMAGE_REPO)/$(IMAGE_NAME):$(IMAGE_TAG)
MAIN_PATH=./cmd/consul2istio/main.go
IMAGE_OS?=linux
IMAGE_ARCH?=amd64
IMAGE_DOCKERFILE_PATH?=docker/Dockerfile

build:
CGO_ENABLED=0 GOOS=linux $(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH_CONSUL_MCP)
build-mac:
CGO_ENABLED=0 GOOS=darwin $(GOBUILD) -o $(BINARY_NAME_DARWIN) $(MAIN_PATH_CONSUL_MCP)
build: test
CGO_ENABLED=0 GOOS=$(IMAGE_OS) GOARCH=$(IMAGE_ARCH) $(GOBUILD) -o $(OUT)/$(IMAGE_ARCH)/$(IMAGE_OS)/$(IMAGE_NAME) $(MAIN_PATH)
docker-build: build
mkdir $(DOCKER_TMP)
cp ./docker/Dockerfile $(DOCKER_TMP)
cp $(BINARY_NAME) $(DOCKER_TMP)
docker build -t $(DOCKER_TAG) $(DOCKER_TMP)
rm -rf $(DOCKER_TMP)
docker-push:
docker push $(DOCKER_TAG)
docker build --build-arg CONSUL2ISTIO_BIN_DIR=${OUT} --build-arg ARCH=${IMAGE_ARCH} --build-arg OS=${IMAGE_OS} \
--no-cache --platform=${IMAGE_OS}/${IMAGE_ARCH} -t ${IMAGE} -f ${IMAGE_DOCKERFILE_PATH} .
docker-push: docker-build
docker push $(IMAGE)
style-check:
gofmt -l -d ./
goimports -l -d ./
Expand Down
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,37 @@ Consul2istio watches Consul catalog and synchronize all the Consul services to I

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

![ consul2istio ](doc/consul2istio.png)
![ consul2istio ](doc/consul2istio.png)

## example

Firstly, deploy consul and consul2istio to your Kubernetes cluster.

```bash
kubectl apply -f k8s/consul.yaml
kubectl apply -f k8s/consul2istio.yaml
```

Secondly, deploy consumer-demo and provider-demo to your Kubernetes cluster.
The consumer-demo service *9999/echo-rest/* to us.

```bash
kubectl apply -f k8s/sample.yaml
```

Finally, request from consumer-demo to provider-demo.
```
kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
consul-7bd648d9f-qpkrj 1/1 Running 0 58m 10.0.1.80 192.168.1.17 <none> <none>
consul2istio-75c9dd98fd-cqglr 1/1 Running 0 9m32s 10.0.1.89 192.168.1.17 <none> <none>
consumer-demo-66766c8d78-5stvw 2/2 Running 0 9m54s 10.0.1.88 192.168.1.17 <none> <none>
provider-demo-v1-59ddd86974-z4sln 2/2 Running 0 9m54s 10.0.1.86 192.168.1.17 <none> <none>
provider-demo-v2-5ccf64cdfd-dxdj5 2/2 Running 0 9m54s 10.0.1.87 192.168.1.17 <none> <none>
```

the result of request.
```
kubectl exec -it consumer-demo-66766c8d78-5stvw -c istio-proxy -- curl 10.0.1.88:9999/echo-rest/aaaa
echo() -> ip [ 10.0.1.86 ] param [ aaaa ]
```
42 changes: 35 additions & 7 deletions cmd/consul2istio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
// 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
// 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 (
Expand All @@ -19,18 +20,33 @@ import (
"os/signal"
"syscall"

"github.com/aeraki-framework/consul2istio/pkg"
"istio.io/pkg/log"
)

const (
defaultConsulAddress = "127.0.0.1:8500"
"github.com/aeraki-framework/consul2istio/pkg"
"github.com/aeraki-framework/consul2istio/pkg/constants"
"github.com/aeraki-framework/consul2istio/pkg/serviceregistry/consul"
)

func main() {
consulAddress := flag.String("consulAddress", defaultConsulAddress, "Consul Address")
args := consul.NewConsulBootStrapArgs()

flag.StringVar(&args.ConsulAddress, "consulAddress", constants.DefaultConsulAddress, "Consul Address")
flag.StringVar(&args.Namespace, "namespace", constants.ConfigRootNS, "namespace")
flag.StringVar(&args.FQDN, "fqdn", "", "The FQDN for consul service")
flag.BoolVar(&args.EnableDefaultPort, "enableDefaultPort", true,
"The flag to start default port for consul service")

flag.Parse()
controller := pkg.NewController(*consulAddress)

flag.VisitAll(func(flag *flag.Flag) {
log.Infof("consul2istio parameter: %s: %v", flag.Name, flag.Value)
})

initArgsWithEnv(args)
log.Infof("consul2istio bootstrap parameter: %v", args)

controller := pkg.NewController(args)

// Create the stop channel for all of the servers.
stopChan := make(chan struct{}, 1)
err := controller.Run(stopChan)
Expand All @@ -44,3 +60,15 @@ func main() {
<-signalChan
stopChan <- struct{}{}
}

func initArgsWithEnv(args *consul.BootStrapArgs) {
consulAddress := os.Getenv("consulAddress")
if consulAddress != "" {
args.ConsulAddress = consulAddress
}

namespace := os.Getenv("namespace")
if namespace != "" {
args.Namespace = namespace
}
}
52 changes: 21 additions & 31 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
FROM ubuntu:bionic
# Base image for debug builds.
# Built manually uploaded as "istionightly/base_debug"
# Copyright Aeraki 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.

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

# hadolint ignore=DL3005,DL3008
ARG CONSUL2ISTIO_BIN_DIR
ARG ARCH
ARG OS

ENV consulAddress="127.0.0.1:8500"
RUN apk update && \
apk add curl

RUN apt-get update && \
apt-get install --no-install-recommends -y \
ca-certificates \
curl \
iptables \
iproute2 \
iputils-ping \
knot-dnsutils \
netcat \
tcpdump \
conntrack \
bsdmainutils \
net-tools \
lsof \
linux-tools-generic \
sudo \
&& update-ca-certificates \
&& apt-get upgrade -y \
&& apt-get clean \
&& rm -rf /var/log/*log /var/lib/apt/lists/* /var/log/apt/* /var/lib/dpkg/*-old /var/cache/debconf/*-old

COPY consul2istio /usr/local/bin/
ENTRYPOINT /usr/local/bin/consul2istio -consulAddress=$consulAddress
COPY ${CONSUL2ISTIO_BIN_DIR}/${ARCH}/${OS}/consul2istio /usr/local/bin/
ENTRYPOINT /usr/local/bin/consul2istio
Loading

0 comments on commit f8d7bff

Please sign in to comment.