Repository: threefoldtech/zos
Pull request #1929 (2023-03-22 14:06:30)

Traefik autobuild


Patch:
diff --git a/.github/workflows/bins.yaml b/.github/workflows/bins.yaml
index 55cc4468..a5d3b475 100644
--- a/.github/workflows/bins.yaml
+++ b/.github/workflows/bins.yaml
@@ -108,6 +108,12 @@ jobs:
       package: qsfs
     secrets:
       token: ${{ secrets.HUB_JWT }}
+  traefik:
+    uses: ./.github/workflows/bin-no-publish.yaml
+    with:
+      package: traefik
+    secrets:
+      token: ${{ secrets.HUB_JWT }}
   cloudconsole:
     uses: ./.github/workflows/bin-package.yaml
     with:
diff --git a/bins/packages/traefik/traefik.sh b/bins/packages/traefik/traefik.sh
new file mode 100644
index 00000000..e1fe9954
--- /dev/null
+++ b/bins/packages/traefik/traefik.sh
@@ -0,0 +1,38 @@
+TRAEFIK_VERSION="2.9.9"
+TRAEFIK_CHECKSUM="83d21fc65ac5f68df985c4dca523e590"
+TRAEFIK_LINK="https://github.com/traefik/traefik/releases/download/v${TRAEFIK_VERSION}/traefik_v${TRAEFIK_VERSION}_linux_amd64.tar.gz"
+TRAEFIK_PACKAGE="vector.tar.gz"
+
+download_traefik() {
+    echo "download traefik"
+    download_file ${TRAEFIK_LINK} ${TRAEFIK_CHECKSUM} ${TRAEFIK_PACKAGE}
+}
+
+extract_traefik() {
+    tar -xf  ${DISTDIR}/${TRAEFIK_PACKAGE} -C ${WORKDIR}
+}
+
+prepare_traefik() {
+    echo "[+] prepare traefik"
+    github_name "traefik-${TRAEFIK_VERSION}"
+}
+
+install_traefik() {
+    echo "[+] install traefik"
+
+    mkdir -p "${ROOTDIR}"
+
+    cp ${WORKDIR}/traefik ${ROOTDIR}/
+    chmod +x ${ROOTDIR}/*
+    strip ${ROOTDIR}/*
+}
+
+build_traefik() {
+    pushd "${DISTDIR}"
+    download_traefik
+    extract_traefik
+    popd
+
+    prepare_traefik
+    install_traefik
+}
diff --git a/cmds/modules/provisiond/reporter.go b/cmds/modules/provisiond/reporter.go
index d42df339..626a4ad8 100644
--- a/cmds/modules/provisiond/reporter.go
+++ b/cmds/modules/provisiond/reporter.go
@@ -208,16 +208,16 @@ func (r *Reporter) getGwMetrics(ctx context.Context, slot rrd.Slot) error {
 	if err != nil {
 		return err
 	}
-	sent := metrics.Sent
-	recv := metrics.Received
+	requests := metrics.Request
+	responses := metrics.Response
 
 	sums := make(map[string]float64)
-	for wl, v := range sent {
-		sums[wl] = v + recv[wl]
-		delete(recv, wl)
+	for wl, v := range requests {
+		sums[wl] = v + responses[wl]
+		delete(responses, wl)
 	}
 
-	for wl, v := range recv {
+	for wl, v := range responses {
 		sums[wl] = v
 	}
 
diff --git a/pkg/gateway.go b/pkg/gateway.go
index c5725af0..205e6277 100644
--- a/pkg/gateway.go
+++ b/pkg/gateway.go
@@ -7,16 +7,16 @@ import "github.com/threefoldtech/zos/pkg/gridtypes/zos"
 //go:generate zbusc -module gateway -version 0.0.1 -name manager -package stubs github.com/threefoldtech/zos/pkg+Gateway stubs/gateway_stub.go
 
 type GatewayMetrics struct {
-	Sent     map[string]float64
-	Received map[string]float64
+	Request  map[string]float64
+	Response map[string]float64
 }
 
 func (m *GatewayMetrics) Nu(service string) (result uint64) {
-	if v, ok := m.Sent[service]; ok {
+	if v, ok := m.Request[service]; ok {
 		result += uint64(v)
 	}
 
-	if v, ok := m.Received[service]; ok {
+	if v, ok := m.Response[service]; ok {
 		result += uint64(v)
 	}
 
diff --git a/pkg/gateway/flist.go b/pkg/gateway/flist.go
index 8f2aa5cb..88e04317 100644
--- a/pkg/gateway/flist.go
+++ b/pkg/gateway/flist.go
@@ -12,7 +12,7 @@ import (
 )
 
 const (
-	flist = "https://hub.grid.tf/omar0.3bot/traefik.flist"
+	flist = "https://hub.grid.tf/tf-autobuilder/traefik-2.9.9.flist"
 )
 
 // ensureTraefikBin makes sure traefik flist is mounted.
diff --git a/pkg/gateway/metrics.go b/pkg/gateway/metrics.go
index 26e56a8d..a5918afa 100644
--- a/pkg/gateway/metrics.go
+++ b/pkg/gateway/metrics.go
@@ -22,8 +22,8 @@ const (
 	publicNS   = "public"
 	metricsURL = "http://127.0.0.1:8082/metrics"
 
-	metricReceived = "traefik_service_bytes_received_total"
-	metricSent     = "traefik_service_bytes_sent_total"
+	metricRequest  = "traefik_service_requests_bytes_total"
+	metricResponse = "traefik_service_responses_bytes_total"
 )
 
 var (
@@ -194,13 +194,13 @@ func (g *gatewayModule) Metrics() (result pkg.GatewayMetrics, err error) {
 	mapping := func(s string) string {
 		return strings.TrimSuffix(s, "@file")
 	}
-	if m, ok := values[metricSent]; ok {
+	if m, ok := values[metricRequest]; ok {
 		// sent metrics.
-		result.Sent = m.group("service", mapping)
+		result.Request = m.group("service", mapping)
 	}
 
-	if m, ok := values[metricReceived]; ok {
-		result.Received = m.group("service", mapping)
+	if m, ok := values[metricResponse]; ok {
+		result.Response = m.group("service", mapping)
 	}
 
 	return


Test patch:
diff --git a/pkg/gateway/metrics_test.go b/pkg/gateway/metrics_test.go
index 4dbf8722..afbe274a 100644
--- a/pkg/gateway/metrics_test.go
+++ b/pkg/gateway/metrics_test.go
@@ -3,6 +3,7 @@ package gateway
 import (
 	"bytes"
 	"fmt"
+	"strings"
 	"testing"
 
 	"github.com/stretchr/testify/require"
@@ -63,6 +64,15 @@ traefik_service_requests_total{code="502",method="POST",protocol="http",service=
 	bigNumber = `
 traefik_service_bytes_sent_total{service="50-715-gw@file"} 8.95470206e+08
 `
+	testValues = `
+# TYPE traefik_service_requests_bytes_total counter
+traefik_service_requests_bytes_total{code="200",method="GET",protocol="http",service="10-123-gateway@file"} 100
+traefik_service_requests_bytes_total{code="404",method="GET",protocol="http",service="10-123-gateway@file"} 120
+traefik_service_requests_bytes_total{code="502",method="GET",protocol="http",service="10-123-gateway@file"} 10
+traefik_service_responses_bytes_total{code="200",method="GET",protocol="http",service="10-123-gateway@file"} 1307
+traefik_service_responses_bytes_total{code="404",method="GET",protocol="http",service="10-123-gateway@file"} 469
+traefik_service_responses_bytes_total{code="502",method="GET",protocol="http",service="10-123-gateway@file"} 22
+	`
 )
 
 func TestParseMetrics(t *testing.T) {
@@ -112,3 +122,28 @@ func TestParseBigMetrics(t *testing.T) {
 	require.Len(t, m.values, 1)
 	require.EqualValues(t, float64(8.95470206e+08), m.values[0].value)
 }
+
+func TestMetrics(t *testing.T) {
+
+	buf := bytes.NewBuffer([]byte(testValues))
+
+	results, err := parseMetrics(buf)
+	require.NoError(t, err)
+
+	request := results[metricRequest]
+	responses := results[metricResponse]
+
+	servicesRequests := request.group("service", func(s string) string {
+		return strings.TrimSuffix(s, "@file")
+	})
+
+	servicesResponses := responses.group("service", func(s string) string {
+		return strings.TrimSuffix(s, "@file")
+	})
+
+	require.Len(t, servicesRequests, 1)
+	require.Len(t, servicesResponses, 1)
+
+	require.EqualValues(t, 230, servicesRequests["10-123-gateway"])
+	require.EqualValues(t, 1798, servicesResponses["10-123-gateway"])
+}