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

1853 - Zarf Metrics & Tests #1915

Merged
merged 10 commits into from
Jul 27, 2023
2 changes: 1 addition & 1 deletion examples/kiwix/manifests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ spec:
name: data
containers:
- name: kiwix-serve
image: "ghcr.io/kiwix/kiwix-serve:3.5.0"
image: "ghcr.io/kiwix/kiwix-serve:3.5.0-2"
command:
[
"sh",
Expand Down
2 changes: 1 addition & 1 deletion examples/kiwix/manifests/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ spec:
- name: http
port: 8080
protocol: TCP
targetPort: 80
targetPort: http
2 changes: 1 addition & 1 deletion examples/kiwix/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ components:
- manifests/deployment.yaml
- manifests/service.yaml
images:
- ghcr.io/kiwix/kiwix-serve:3.5.0
- ghcr.io/kiwix/kiwix-serve:3.5.0-2
- alpine:3.18
# Add new data into the cluster, these will keep trying up until their timeout
dataInjections:
Expand Down
3 changes: 2 additions & 1 deletion examples/manifests/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ components:
files:
# local manifests are specified relative to the `zarf.yaml` that uses them:
- httpd-deployment.yaml
- httpd-service.yaml
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
actions:
onDeploy:
# the following checks were computed by viewing the success state of the package deployment
Expand All @@ -22,7 +23,7 @@ components:
kind: deployment
name: httpd-deployment
namespace: httpd
condition: available
condition: '{.status.readyReplicas}=2'
# image discovery is supported in all manifests and charts using:
# zarf prepare find-images
images:
Expand Down
3 changes: 3 additions & 0 deletions src/internal/agent/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/defenseunicorns/zarf/src/internal/agent/hooks"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// NewAdmissionServer creates an http.Server for the mutating webhook admission handler.
Expand All @@ -26,6 +27,7 @@ func NewAdmissionServer(port string) *http.Server {
mux.Handle("/healthz", healthz())
mux.Handle("/mutate/pod", ah.Serve(podsMutation))
mux.Handle("/mutate/flux-gitrepository", ah.Serve(gitRepositoryMutation))
mux.Handle("/metrics", promhttp.Handler())

return &http.Server{
Addr: fmt.Sprintf(":%s", port),
Expand All @@ -40,6 +42,7 @@ func NewProxyServer(port string) *http.Server {
mux := http.NewServeMux()
mux.Handle("/healthz", healthz())
mux.Handle("/", ProxyHandler())
mux.Handle("/metrics", promhttp.Handler())

return &http.Server{
Addr: fmt.Sprintf(":%s", port),
Expand Down
39 changes: 39 additions & 0 deletions src/test/e2e/21_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package test

import (
"crypto/tls"
"io/ioutil"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -58,3 +60,40 @@ func TestConnect(t *testing.T) {
stdOut, stdErr, err = e2e.Zarf("package", "remove", "init", "--components=logging", "--confirm")
require.NoError(t, err, stdOut, stdErr)
}

func TestMetrics(t *testing.T) {
t.Log("E2E: Emits metrics")
e2e.SetupWithCluster(t)

tunnel, err := cluster.NewTunnel("zarf", "svc", "agent-hook", 8888, 8443)

require.NoError(t, err)
err = tunnel.Connect("", false)
require.NoError(t, err)
defer tunnel.Close()

// Get untrusted https endpoint
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

client := &http.Client{Transport: tr}
https_endpoint := strings.ReplaceAll(tunnel.HTTPEndpoint(), "http", "https")
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
resp, err := client.Get(https_endpoint + "/metrics")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

desiredString := "go_gc_duration_seconds_count"
require.Equal(t, true, strings.Contains(string(body), desiredString))
require.NoError(t, err, resp)
require.Equal(t, 200, resp.StatusCode)

}
17 changes: 17 additions & 0 deletions src/test/e2e/23_data_injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ package test
import (
"context"
"fmt"
"net/http"
"path/filepath"
"testing"
"time"

"github.com/defenseunicorns/zarf/src/internal/cluster"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/stretchr/testify/require"
)
Expand All @@ -35,6 +37,21 @@ func TestDataInjection(t *testing.T) {
require.Contains(t, stdOut, "devops.stackexchange.com_en_all_2023-05.zim")
require.Contains(t, stdOut, ".zarf-injection-")

// Test Named Port
// Set remotePort to 0 so it checks for the targetPort on the pod
tunnel, err := cluster.NewTunnel("kiwix", "svc", "kiwix", 8080, 0)

require.NoError(t, err)
// need target equal svc that we are trying to connect to call checkForZarfConnectLabel
err = tunnel.Connect("kiwix", false)
require.NoError(t, err)
defer tunnel.Close()

// Ensure connection
resp, err := http.Get(tunnel.HTTPEndpoint())
require.NoError(t, err, resp)
require.Equal(t, 200, resp.StatusCode)

// Remove the data injection example
stdOut, stdErr, err = e2e.Zarf("package", "remove", path, "--confirm")
require.NoError(t, err, stdOut, stdErr)
Expand Down