Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Fix/tests #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 65 additions & 0 deletions .github/workflows/traefik-hub.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Traefik Hub Static Analyzer

on:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
permissions:
checks: write
contents: write
steps:
- uses: actions/checkout@v4

- name: Lint Traefik Hub CRDs with hub-static-analyzer
uses: traefik/hub-static-analyzer-action@main
with:
token: ${{ secrets.GH_TOKEN }}
lint: true
lint-format: checkstyle
lint-output-file: ./output.xml

- name: Annotate code
if: ${{ !cancelled() }}
uses: Juuxel/publish-checkstyle-report@v1
with:
reports: |
./output.xml

diff:
runs-on: ubuntu-latest
permissions:
checks: write
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Lint Traefik Hub CRDs with hub-static-analyzer
uses: traefik/hub-static-analyzer-action@main
with:
token: ${{ secrets.GH_TOKEN }}
diff: true
diff-range: "origin/${GITHUB_BASE_REF}...origin/${GITHUB_HEAD_REF}"
diff-output-file: ./output.md

- name: Prepare report
shell: bash
run: |
set -u

echo "# Traefik Hub Report:" > header.md
echo "" >> header.md
echo "The following changes have been detected." >> header.md
echo "" >> header.md

- name: Write report
if: ${{ hashFiles('./output.md') != ''}}
uses: mshick/add-pr-comment@v2
with:
message-path: |
header.md
output.md
1 change: 1 addition & 0 deletions api-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
12 changes: 12 additions & 0 deletions api-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# syntax=docker/dockerfile:1.2
# Alpine
FROM alpine

RUN apk --no-cache --no-progress add ca-certificates tzdata git \
&& rm -rf /var/cache/apk/*

ARG TARGETPLATFORM
COPY ./dist/$TARGETPLATFORM/api-server /

ENTRYPOINT ["/api-server"]
EXPOSE 3000
34 changes: 34 additions & 0 deletions api-server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.PHONY: default check test build image

# Default build target
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
DOCKER_BUILD_PLATFORMS ?= linux/amd64,linux/arm64

default: check test build

dist:
mkdir dist

build: dist
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -v -trimpath -ldflags '-s' -o "./dist/${GOOS}/${GOARCH}/api-server" .

test:
go test -v -cover ./...

check:
golangci-lint run

build-linux-arm64: export GOOS := linux
build-linux-arm64: export GOARCH := arm64
build-linux-arm64:
make build

build-linux-amd64: export GOOS := linux
build-linux-amd64: export GOARCH := amd64
build-linux-amd64:
make build

## Build Multi archs Docker image
multi-arch-image-%: build-linux-amd64 build-linux-arm64
docker buildx build $(DOCKER_BUILDX_ARGS) -t mmatur/traefik-hub:$* --platform=$(DOCKER_BUILD_PLATFORMS) .
3 changes: 3 additions & 0 deletions api-server/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module api-server

go 1.21
78 changes: 78 additions & 0 deletions api-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"flag"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
)

func main() {
openapispec := flag.String("openapi", "", "openapispec")
datafile := flag.String("data", "", "file to put data in")
latency := flag.Duration("latency", 0, "latency to add")
errorrate := flag.Int("errorrate", 0, "latency to add")
flag.Parse()

var openapi []byte

if openapispec != nil && *openapispec != "" {
var err error
openapi, err = os.ReadFile(*openapispec)
if err != nil {
log.Fatal(err)
}
}

var data []byte
if datafile != nil && *datafile != "" {
var err error
data, err = os.ReadFile(*datafile)
if err != nil {
log.Fatal(err)
}
}

log.Fatal(http.ListenAndServe(":3000", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/openapi.yaml" {
_, _ = rw.Write(openapi)
return
}

if latency != nil && *latency > 0 {
time.Sleep(*latency)
}

status := req.URL.Query().Get("status")
if status == "" {
if errorrate != nil && *errorrate > 0 {
if rand.Int()%100 < *errorrate {
rw.WriteHeader(http.StatusInternalServerError)
return
}
}
switch req.Method {
case http.MethodGet, http.MethodPut:
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write(data)
case http.MethodPost:
rw.WriteHeader(http.StatusCreated)
_, _ = rw.Write([]byte(`{"id":4}`))
case http.MethodDelete:
rw.WriteHeader(http.StatusNoContent)
}

return
}

atoi, err := strconv.Atoi(status)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.WriteHeader(atoi)
})))
}
56 changes: 56 additions & 0 deletions apps/base/apps/api-access.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIAccess
metadata:
name: crm-internal
spec:
groups:
- internal
apiSelector:
matchLabels:
module: crm
area: employee

---
apiVersion: hub.traefik.io/v1alpha1
kind: APIAccess
metadata:
name: crm-all
spec:
groups:
- crm-user
apiCollectionSelector:
matchLabels:
module: crm

---
apiVersion: hub.traefik.io/v1alpha1
kind: APIAccess
metadata:
name: custom-pick
spec:
groups:
- support
apis:
- name: world-time-api-external-name
namespace: apps
apiSelector:
matchExpressions:
- key: area
operator: In
values:
- flights
- tickets

---
apiVersion: hub.traefik.io/v1alpha1
kind: APIAccess
metadata:
name: admins
spec:
groups:
- admin
apiSelector:
matchExpressions:
- key: area
operator: Exists
11 changes: 11 additions & 0 deletions apps/base/apps/api-collections.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: hub.traefik.io/v1alpha1
kind: APICollection
metadata:
name: crm-all
labels:
module: crm
spec:
pathPrefix: "/crm"
apiSelector:
matchLabels:
module: crm
15 changes: 15 additions & 0 deletions apps/base/apps/api-gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIGateway
metadata:
name: my-gateway
labels:
area: crm
spec:
apiAccesses:
- crm-all
- crm-internal
- admins
- custom-pick
# customDomains:
# - gateway.YOUR_ENV_NAME.demo.traefiklabs.tech
19 changes: 19 additions & 0 deletions apps/base/apps/api-portal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIPortal
metadata:
name: my-kubecon-portal
spec:
title: "Traefik Kubecon"
description: "API Portal for Kubecon"
apiGateway: my-gateway
# ui:
# logoUrl: https://traefik.io/favicon.png
# ui:
# service:
# name: hub-apiportal-ui
# namespace: portal-ui
# port:
# number: 80
# customDomains:
# - portal.YOUR_ENV_NAME.demo.traefiklabs.tech
43 changes: 43 additions & 0 deletions apps/base/apps/api-rate-limit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIRateLimit
metadata:
name: crm-user-ratelimit
spec:
limit: 2 # 2 requests
period: 5s # 5 seconds
groups:
- crm-user
apiSelector:
matchLabels:
module: crm
apis:
- name: employee-api
namespace: apps
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIRateLimit
metadata:
name: support-ratelimit
spec:
limit: 1
period: 1m
groups:
- support
apiSelector:
matchExpressions:
- key: area
operator: In
values:
- flights
- tickets
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIRateLimit
metadata:
name: fallback-ratelimit
spec:
limit: 2000
period: 65s
anyGroups: true
apiSelector: {}
Loading