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

feat: Kong Plugin #54

Merged
merged 16 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ jobs:
- name: Build Docker image
run: make docker

- name: Build plugins Docker images
run: make docker-plugins
4 changes: 2 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
with:
context: .
tags: ghcr.io/apiclarity/apiclarity:latest
file: Dockerfile.kong
file: Dockerfile
push: true
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
Expand All @@ -54,7 +54,7 @@ jobs:
uses: docker/build-push-action@v2
with:
context: plugins
tags: ghcr.io/apiclarity/kong-plugin:test
tags: ghcr.io/apiclarity/kong-plugin:latest
file: plugins/Dockerfile.kong
push: true
cache-from: type=local,src=/tmp/.buildx-cache
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
git commit deployment/apiclarity.yaml -m "workflow: update tag in deployment yaml"

sed -i 's/image: ghcr.io\/apiclarity\/kong-plugin:latest/image: ghcr.io\/apiclarity\/kong-plugin:'"$tag"'/g' plugins/gateway/kong/deploy/deploy.sh
FrimIdan marked this conversation as resolved.
Show resolved Hide resolved
git commit plugins/gateway/kong/deploy/deploy.sh -m "workflow: update tag in kong deploy script"

git tag --annotate --message "Tag for release $tag" "$tag"
git push origin "refs/tags/$tag"
Expand Down Expand Up @@ -88,7 +89,7 @@ jobs:
with:
context: .
tags: ghcr.io/apiclarity/apiclarity:${{ github.event.inputs.version }}
file: Dockerfile.kong
file: Dockerfile
push: true
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ archives:
- wasm-filters/envoyFilter.yaml
- wasm-filters/bin/release/http-trace-filter.wasm
- deployment/apiclarity.yaml
- plugins/gateway/kong/deploy/deploy.sh
- plugins/gateway/kong/deploy/*

checksum:
name_template: 'checksums.txt'
Expand Down
15 changes: 9 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,27 @@ api: ## Generating API code
@(cd api; ./generate.sh)

.PHONY: docker
docker: ## Build Docker image
@(echo "Building docker image ..." )
docker: docker-backend docker-plugins

.PHONY: docker-backend
docker-backend: ## Build Docker image
@(echo "Building backend docker image ..." )
docker build --build-arg VERSION=${VERSION} \
--build-arg BUILD_TIMESTAMP=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
--build-arg COMMIT_HASH=$(shell git rev-parse HEAD) \
-t ${DOCKER_IMAGE}:${DOCKER_TAG} .

.PHONY: push-docker
push-docker: docker ## Build and Push Docker image
@echo "Publishing Docker image ..."
.PHONY: push-docker-backend
push-docker-backend: docker-backend ## Build and Push Docker image
@echo "Publishing backend Docker image ..."
docker push ${DOCKER_IMAGE}:${DOCKER_TAG}

.PHONY: docker-plugins
docker-plugins: ## Build plugins Docker image
$(MAKE) docker -C plugins

.PHONY: push-docker-plugins
push-docker-plugins: docker-plugins ## Build and Push Docker image
push-docker-plugins: ## Build and Push plugins Docker image
$(MAKE) push-docker -C plugins

.PHONY: test
Expand Down
6 changes: 2 additions & 4 deletions plugins/Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# Project variables
BINARY_NAME ?= kong-plugin
DOCKER_REGISTRY ?= ghcr.io/apiclarity
VERSION ?= $(shell git rev-parse HEAD)
DOCKER_IMAGE ?= $(DOCKER_REGISTRY)/$(BINARY_NAME)
DOCKER_TAG ?= ${VERSION}

.PHONY: docker
docker: docker-kong

.PHONY: docker-kong
docker-kong:
@DOCKER_BUILDKIT=1 docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} --file Dockerfile.kong .
@DOCKER_BUILDKIT=1 docker build -t ${DOCKER_REGISTRY}/kong-plugin:${DOCKER_TAG} --file Dockerfile.kong .

.PHONY: push-docker
push-docker: push-docker-kong

.PHONY: push-docker-kong
push-docker-kong: docker-kong
@echo "Publishing Kong plugin Docker image ..."
docker push ${DOCKER_IMAGE}:${DOCKER_TAG}
docker push ${DOCKER_REGISTRY}/kong-plugin:${DOCKER_TAG}
2 changes: 1 addition & 1 deletion plugins/gateway/kong/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ run:


```shell
KONG_PROXY_CONTAINER_NAME=<name> KONG_GATEWAY_DEPLOYMENT_NAME=<name> KONG_GATEWAY_INGRESS_NAME=<name> ./deploy
KONG_PROXY_CONTAINER_NAME=<name> KONG_GATEWAY_DEPLOYMENT_NAME=<name> KONG_GATEWAY_INGRESS_NAME=<name> ./deploy/deploy.sh
```

Where:
Expand Down
6 changes: 3 additions & 3 deletions plugins/gateway/kong/deploy/patch-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ spec:
spec:
volumes:
- emptyDir: {}
name: kong-securecn-trace-plugin
name: kong-apiclarity-trace-plugin
containers:
- name: {{KONG_PROXY_CONTAINER_NAME}}
volumeMounts:
- mountPath: /plugins/
name: kong-securecn-trace-plugin
name: kong-apiclarity-trace-plugin
env:
- name: KONG_PLUGINS
value: kong-plugin
Expand All @@ -28,4 +28,4 @@ spec:
name: securecn-kong-plugin-injector
volumeMounts:
- mountPath: /plugin
name: kong-securecn-trace-plugin
name: kong-apiclarity-trace-plugin
23 changes: 12 additions & 11 deletions plugins/gateway/kong/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func New() interface{} {
func (conf Config) Response(kong *pdk.PDK) {
telemetry, err := createTelemetry(kong)
if err != nil {
_ = kong.Log.Err(fmt.Sprintf("Failed to create telemetry. %v", err))
_ = kong.Log.Err(fmt.Sprintf("Failed to create telemetry: %v", err))
return
}

Expand All @@ -67,15 +67,11 @@ func createTelemetry(kong *pdk.PDK) (*models.Telemetry, error) {
if err != nil {
return nil, fmt.Errorf("failed to get client ip. %v", err)
}
//clientPort, err := kong.Client.GetPort()
//if err != nil {
// return nil, fmt.Errorf("failed to get client port. %v", err)
//}
destPort := routedService.Port
host := routedService.Host

// Will get the actual path that the request was sent to, not the routed one
path, err := kong.Request.GetPath()
path, err := kong.Request.GetPathWithQuery()
if err != nil {
return nil, fmt.Errorf("failed to get request path. %v", err)
}
Expand Down Expand Up @@ -115,18 +111,19 @@ func createTelemetry(kong *pdk.PDK) (*models.Telemetry, error) {
if err != nil {
return nil, fmt.Errorf("failed to get response headers. %v", err)
}
parsedHost, namespace := parseHost(host)

telemetry := models.Telemetry{
DestinationAddress: ":" + strconv.Itoa(destPort), // No destination ip for now
DestinationNamespace: "",
DestinationNamespace: namespace,
Request: &models.Request{
Common: &models.Common{
TruncatedBody: false,
Body: strfmt.Base64(reqBody),
Headers: createHeaders(reqHeaders),
Version: fmt.Sprintf("%f", version),
},
Host: parseHost(host),
Host: parsedHost,
Method: method,
Path: path,
},
Expand All @@ -147,15 +144,19 @@ func createTelemetry(kong *pdk.PDK) (*models.Telemetry, error) {
return &telemetry, nil
}

func parseHost(kongHost string) string {
// KongHost: <namespace>.<svc-name>.svc.cluster.local.80
FrimIdan marked this conversation as resolved.
Show resolved Hide resolved
// convert to namespace.name
FrimIdan marked this conversation as resolved.
Show resolved Hide resolved
func parseHost(kongHost string) (host, namespace string) {
sp := strings.Split(kongHost, ".")

// nolint:gomnd
if len(sp) < 2 {
return kongHost
return kongHost, ""
}
host = sp[0] + "." + sp[1]
namespace = sp[0]

return sp[0] + "." + sp[1]
return
}

func createHeaders(headers map[string][]string) []*models.Header {
Expand Down