Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 2d7b764

Browse files
authored
Merge pull request #5 from stefanprodan/appmesh-apis
Refactor controller and cmd
2 parents e11b98a + c75ff02 commit 2d7b764

File tree

19 files changed

+632
-832
lines changed

19 files changed

+632
-832
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
name: CI
1+
name: build
22

33
on: [push, pull_request]
44

55
jobs:
6-
kustomize:
6+
test:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v1
10-
- name: Validate manifests
10+
- name: Go fmt
11+
uses: stefanprodan/kube-tools@v1
12+
with:
13+
command: |
14+
make go-fmt
15+
- name: Go test
16+
uses: stefanprodan/kube-tools@v1
17+
with:
18+
command: |
19+
make test
20+
- name: Validate kustomization
1121
uses: stefanprodan/kube-tools@v1
1222
with:
1323
command: |
@@ -17,7 +27,7 @@ jobs:
1727
kustomize build ./kustomize/test | kubeval --strict --ignore-missing-schemas
1828
build:
1929
runs-on: ubuntu-latest
20-
needs: [kustomize]
30+
needs: [test]
2131
steps:
2232
- uses: actions/checkout@v1
2333
- name: Build container

.github/workflows/release.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
name: PUSH
1+
name: release
22

3-
on: [push]
3+
on:
4+
push:
5+
branches-ignore:
6+
- '**'
7+
tags:
8+
- 'v*.*.*'
49

510
jobs:
611
push-container:

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ COPY . .
88

99
RUN go mod download
1010

11-
RUN go test -v -race ./...
12-
1311
RUN CGO_ENABLED=0 GOOS=linux go build -a -o bin/appmesh-gateway cmd/appmesh-gateway/*
1412

1513
FROM alpine:3.10

Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ build:
1010
test:
1111
go test -v -race ./...
1212

13-
run:
14-
go run cmd/appmesh-gateway/*.go kubernetes --kubeconfig=$$HOME/.kube/config \
15-
--port-name=http
13+
go-fmt:
14+
gofmt -l pkg/* | grep ".*\.go"; if [ "$$?" = "0" ]; then exit 1; fi;
1615

17-
appmesh:
18-
go run cmd/appmesh-gateway/*.go appmesh --kubeconfig=$$HOME/.kube/config \
19-
--gateway-mesh=appmesh --gateway-name=gateway --gateway-namespace=appmesh-gateway
16+
run:
17+
go run cmd/appmesh-gateway/*.go --kubeconfig=$$HOME/.kube/config -v=4 \
18+
--gateway-mesh=appmesh --gateway-name=gateway --gateway-namespace=appmesh-gateway
2019

2120
envoy:
2221
envoy -c envoy.yaml -l info

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# appmesh-gateway
2-
[![CI](https://github.com/stefanprodan/appmesh-gateway/workflows/CI/badge.svg)](https://github.com/stefanprodan/appmesh-gateway/actions)
2+
[![CI](https://github.com/stefanprodan/appmesh-gateway/workflows/build/badge.svg)](https://github.com/stefanprodan/appmesh-gateway/actions)
33
[![report](https://goreportcard.com/badge/github.com/stefanprodan/appmesh-gateway)](https://goreportcard.com/report/github.com/stefanprodan/appmesh-gateway)
44

55
App Mesh Gateway is an edge load balancer that exposes applications outside the mesh.
@@ -32,12 +32,35 @@ The gateway registers/de-registers virtual services automatically as they come a
3232
3333
### Install
3434
35+
Requirements:
36+
* App Mesh CRDs, controller and inject [installed](https://github.com/aws/eks-charts#app-mesh)
37+
* A mesh called `appmesh`
38+
3539
Install the API Gateway as NLB in `appmesh-gateway` namespace:
3640

3741
```sh
3842
kubectl apply -k github.com/stefanprodan/appmesh-gateway//kustomize/appmesh-gateway
3943
```
4044

45+
Wait for the deployment rollout to finish:
46+
47+
```sh
48+
kubectl -n appmesh-gateway rollout status deploy/appmesh-gateway
49+
```
50+
51+
When the gateway starts it will create a virtual node. You can verify the install with:
52+
53+
```text
54+
watch kubectl -n appmesh-gateway describe virtualnode appmesh-gateway
55+
56+
Status:
57+
Conditions:
58+
Status: True
59+
Type: VirtualNodeActive
60+
```
61+
62+
### Example
63+
4164
Deploy podinfo in the `test` namespace:
4265

4366
```sh
@@ -61,3 +84,12 @@ Access podinfo on its custom domain:
6184
```sh
6285
curl -vH 'Host: podinfo.internal' localhost:8080
6386
```
87+
88+
Access podinfo using the gateway NLB address:
89+
90+
```sh
91+
URL="http://$(kubectl -n appmesh-gateway get svc/appmesh-gateway -ojson | \
92+
jq -r ".status.loadBalancer.ingress[].hostname")"
93+
94+
curl -vH 'Host: podinfo.internal' $URL
95+
```

cmd/appmesh-gateway/appmesh.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

cmd/appmesh-gateway/kubernetes.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

cmd/appmesh-gateway/main.go

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
package main
22

33
import (
4+
"context"
5+
goflag "flag"
46
"fmt"
57
"os"
68
"strings"
79

810
"github.com/spf13/cobra"
11+
flag "github.com/spf13/pflag"
12+
"k8s.io/client-go/dynamic"
13+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
14+
"k8s.io/client-go/tools/clientcmd"
15+
"k8s.io/klog"
16+
17+
"github.com/stefanprodan/appmesh-gateway/pkg/discovery"
18+
"github.com/stefanprodan/appmesh-gateway/pkg/envoy"
19+
"github.com/stefanprodan/appmesh-gateway/pkg/server"
20+
"github.com/stefanprodan/appmesh-gateway/pkg/signals"
921
)
1022

1123
const VERSION = "0.3.0"
1224

1325
var (
14-
masterURL string
15-
kubeConfig string
16-
port int
17-
namespace string
18-
ads bool
19-
optIn bool
26+
masterURL string
27+
kubeConfig string
28+
port int
29+
namespace string
30+
ads bool
31+
optIn bool
32+
gatewayMesh string
33+
gatewayName string
34+
gatewayNamespace string
2035
)
2136

2237
func init() {
@@ -27,19 +42,75 @@ func init() {
2742
pf.BoolVarP(&ads, "ads", "a", true, "ADS flag forces all Envoy resources to be explicitly named in the request.")
2843
pf.StringVarP(&namespace, "namespace", "n", "", "Namespace to watch for Kubernetes objects, a blank value means all namespaces.")
2944
pf.BoolVarP(&optIn, "opt-in", "", false, "When enabled only services with the 'expose' annotation will be discoverable.")
30-
45+
pf.StringVarP(&gatewayMesh, "gateway-mesh", "", "", "App Mesh mesh that this gateway belongs to.")
46+
cobra.MarkFlagRequired(pf, "gateway-mesh")
47+
pf.StringVarP(&gatewayName, "gateway-name", "", "", "Gateway Kubernetes service name.")
48+
cobra.MarkFlagRequired(pf, "gateway-name")
49+
pf.StringVarP(&gatewayNamespace, "gateway-namespace", "", "", "Gateway Kubernetes namespace.")
50+
cobra.MarkFlagRequired(pf, "gateway-namespace")
3151
}
3252

3353
var rootCmd = &cobra.Command{
34-
Use: "kxds",
54+
Use: "appmesh-gateway",
55+
Long: `appmesh-gateway is responsible for exposing services outside the mesh.`,
3556
Version: VERSION,
57+
RunE: run,
3658
}
3759

3860
func main() {
61+
flag.CommandLine.Parse([]string{})
62+
pf := rootCmd.PersistentFlags()
63+
addKlogFlags(pf)
64+
3965
rootCmd.SetArgs(os.Args[1:])
4066
if err := rootCmd.Execute(); err != nil {
4167
e := err.Error()
4268
fmt.Println(strings.ToUpper(e[:1]) + e[1:])
4369
os.Exit(1)
4470
}
4571
}
72+
73+
func run(cmd *cobra.Command, args []string) error {
74+
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeConfig)
75+
if err != nil {
76+
klog.Fatalf("error building kubeconfig: %v", err)
77+
}
78+
79+
client, err := dynamic.NewForConfig(cfg)
80+
if err != nil {
81+
klog.Fatalf("error building kubernetes client: %v", err)
82+
}
83+
84+
stopCh := signals.SetupSignalHandler()
85+
ctx := context.Background()
86+
cache := envoy.NewCache(ads)
87+
snapshot := envoy.NewSnapshot(cache)
88+
89+
klog.Infof("starting xDS server on port %d", port)
90+
srv := server.NewServer(port, cache)
91+
go srv.Serve(ctx)
92+
93+
klog.Info("waiting for Envoy to connect to the xDS server")
94+
srv.Report()
95+
96+
klog.Info("starting App Mesh discovery workers")
97+
vsManager := discovery.NewVirtualServiceManager(client, optIn)
98+
vnManager := discovery.NewVirtualNodeManager(client, gatewayMesh, gatewayName, gatewayNamespace)
99+
kd := discovery.NewController(client, namespace, snapshot, vsManager, vnManager)
100+
kd.Run(2, stopCh)
101+
102+
return nil
103+
}
104+
105+
func addKlogFlags(fs *flag.FlagSet) {
106+
local := goflag.NewFlagSet(os.Args[0], goflag.ExitOnError)
107+
klog.InitFlags(local)
108+
local.VisitAll(func(fl *goflag.Flag) {
109+
fl.Name = normalizeFlag(fl.Name)
110+
fs.AddGoFlag(fl)
111+
})
112+
}
113+
114+
func normalizeFlag(s string) string {
115+
return strings.Replace(s, "_", "-", -1)
116+
}

0 commit comments

Comments
 (0)