Skip to content

Commit

Permalink
update dependency and add demo for consul2istio
Browse files Browse the repository at this point in the history
  • Loading branch information
tanjunchen committed Jul 28, 2023
1 parent 4e4ed84 commit d498ee4
Show file tree
Hide file tree
Showing 19 changed files with 613 additions and 1,190 deletions.
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
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ GOBIN?=$(GOPATH)/bin
# Build parameters
OUT?=./out
DOCKER_TMP?=$(OUT)/docker_temp/
DOCKER_TAG?=aeraki/consul2istio:latest
DOCKER_TAG?=docker.io/tanjunchen/consul2istio:latest
BINARY_NAME?=$(OUT)/consul2istio
BINARY_NAME_DARWIN?=$(BINARY_NAME)-darwin
MAIN_PATH_CONSUL_MCP=./cmd/consul2istio/main.go

build:
CGO_ENABLED=0 GOOS=linux $(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH_CONSUL_MCP)
CGO_ENABLED=0 GOARCH=amd64 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)
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin $(GOBUILD) -o $(BINARY_NAME_DARWIN) $(MAIN_PATH_CONSUL_MCP)
docker-build: build
rm -rf $(DOCKER_TMP)
mkdir $(DOCKER_TMP)
cp ./docker/Dockerfile $(DOCKER_TMP)
cp $(BINARY_NAME) $(DOCKER_TMP)
docker build -t $(DOCKER_TAG) $(DOCKER_TMP)
docker build --no-cache --platform=linux/amd64 -t $(DOCKER_TAG) $(DOCKER_TMP)
rm -rf $(DOCKER_TMP)
docker-push:
docker-push: docker-build
docker push $(DOCKER_TAG)
style-check:
gofmt -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-demo-mesh.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.NewConsulArgs()

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.ConsulArgs) {
consulAddress := os.Getenv("consulAddress")
if consulAddress != "" {
args.ConsulAddress = consulAddress
}

namespace := os.Getenv("namespace")
if namespace != "" {
args.Namespace = namespace
}
}
37 changes: 5 additions & 32 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
FROM ubuntu:bionic
# Base image for debug builds.
# Built manually uploaded as "istionightly/base_debug"
FROM alpine:3.17

# 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

# hadolint ignore=DL3005,DL3008

ENV consulAddress="127.0.0.1:8500"

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
RUN apk update && \
apk add curl

COPY consul2istio /usr/local/bin/
ENTRYPOINT /usr/local/bin/consul2istio -consulAddress=$consulAddress

ENTRYPOINT /usr/local/bin/consul2istio
100 changes: 90 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,95 @@
module github.com/aeraki-framework/consul2istio

go 1.14
go 1.19

// https://github.com/containerd/containerd/issues/5781
exclude k8s.io/kubernetes v1.13.0

// Client-go does not handle different versions of mergo due to some breaking changes - use the matching version
replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.5

require (
github.com/hashicorp/consul/api v1.8.1
google.golang.org/protobuf v1.28.1
istio.io/api v0.0.0-20230518153929-d0aebaa77ab8
istio.io/client-go v1.16.4-0.20230518154329-f75cb9ff8e52
istio.io/istio v0.0.0-20230519000352-ae8d5164776c
istio.io/pkg v0.0.0-20221107183613-574f8d141535
k8s.io/apimachinery v0.25.2
sigs.k8s.io/controller-runtime v0.13.0
)

require (
github.com/gogo/protobuf v1.3.1
github.com/hashicorp/consul/api v1.6.0
golang.org/x/tools v0.0.0-20201009162240-fcf82128ed91 // indirect
istio.io/api v0.0.0-20200905155905-4e0c8da420a4
istio.io/client-go v0.0.0-20200812230733-f5504d568313
istio.io/istio v0.0.0-20200908094756-cc8ad211d14c
istio.io/pkg v0.0.0-20200831193257-fe7110296cbc
k8s.io/apimachinery v0.19.0
sigs.k8s.io/controller-runtime v0.6.1
cloud.google.com/go v0.105.0 // indirect
cloud.google.com/go/compute v1.14.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/logging v1.6.1 // indirect
cloud.google.com/go/longrunning v0.3.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v0.12.0 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.9.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.22.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/api v0.103.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221202195650-67e5cbc046fd // indirect
google.golang.org/grpc v1.50.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.25.2 // indirect
k8s.io/client-go v0.25.2 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803164354-a70c9af30aea // indirect
k8s.io/utils v0.0.0-20220823124924-e9cbc92d1a73 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit d498ee4

Please sign in to comment.