Skip to content

Commit

Permalink
Merge branch 'issues/210' into link-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
SOF3 committed Dec 5, 2023
2 parents 5859aac + 561f8ae commit c10ddb0
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 90 deletions.
46 changes: 40 additions & 6 deletions docs/DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,54 @@ Kelemetrix is a separate component that consumes audit logs into metric points.
Tag providers provide string tags to use in kelemetrix.toml,
and quantifiers provide scalar quantities.

## Testing
## End-to-end Testing

Kelemetry runs end-to-end tests on GitHub CI.
Each test suite is a directory under the [`e2e`](../e2e) directory
with the following structure:

- `client.sh`: Executes a series of operations on a running cluster at `$KUBECONFIG`
to trigger generation of a test trace.
It can be assumed that the default `make quickstart` configuration is running for the cluster.
- `config.sh`: Defines the following `local` Bash variables.
- `config.sh`: Sourced by the e2e test script,
executes a series of operations on a running cluster at `$KUBECONFIG`
to trigger a background `make quickstart` to generate the test trace.
Also defines the following `local` Bash variables.
It is directly `source`d by the e2e test script.
- `TRACE_DISPLAY_NAME`, the display name of the trace shown on the [demo index](https://kubewharf.io/kelemetry/)
- `TRACE_DISPLAY_NAME`, the display name of the trace shown on the [demo index](https://kubewharf.io/kelemetry/).
- `TRACE_SEARCH_TAGS`, an associative array for the search tags to uniquely identify the test trace generated by `client.sh`.
- `TEST_DISPLAY_MODE`, the display mode of the trace to test with.
- `EXTRA_DISPLAY_MODES` (optional), an indexed array for additional display modes to show on GitHub Pages without getting tested in the e2e test.
- `validate.jq`: A `jq` script that takes the test trace JSON as input
and [fails](https://jqlang.github.io/jq/manual/#error) if errors are encountered.
The script may use library functions defined under [`e2e/lib`](../e2e/lib).
The current framework uses [gojq](https://github.com/itchyny/gojq/) as the `jq` implementation.

End-to-end tests can be executed locally with the commands:

```console
$ make kind
$ make e2e
```

`make e2e` automatically runs `make quickstart`
using a custom image built from the local `make output/kelemetry` output.
If you prefer to run the kelemetry process directly (e.g. with a debugger),
use the following steps instead:

1. Clean up all previous data by removing the stack volumes completely:

```bash
make stack COMPOSE_COMMAND='down --remove-orphans --volumes'
```

2. Recreate the stack:

```bash
make stack COMPOSE_COMMAND='up --build -d --remove-orphans'
```

3. Run Kelemetry with your preferred method equivalent to `make`.

4. Execute the e2e test script:

```bash
bash e2e/run-all.sh
```
17 changes: 17 additions & 0 deletions e2e/ancestors/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# script
kubectl create deployment ancestors --image=alpine:3.16 --replicas=2 -- sleep infinity
local first_rs_name=$(kubectl get replicasets --selector app=ancestors -o json | jq -r '.items[0].metadata.name')
sleep 5
kubectl set image deployments ancestors alpine=alpine:3.17
sleep 30
kubectl delete deployments ancestors

# config
local TEST_DISPLAY_MODE="22000000" # tracing, ancestors only
local TRACE_DISPLAY_NAME="Deployment"

TRACE_SEARCH_TAGS[cluster]=tracetest
TRACE_SEARCH_TAGS[group]=apps
TRACE_SEARCH_TAGS[resource]=replicasets
TRACE_SEARCH_TAGS[namespace]=default
TRACE_SEARCH_TAGS[name]=$first_rs_name
18 changes: 18 additions & 0 deletions e2e/ancestors/validate.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
include "assert";
include "graph";

.data[0]
| ._ = (
# check root
.spans
| root
| .tags | from_entries
| assertEq("root span is deployment"; .resource; "deployments")
| assertEq("root span has the correct name"; .name; "ancestors")
) | ._ = (
# root only has one child
.spans
| children(root.spanID)
| assertEq("there is only one child replicaset"; length; 1)
)
| null
12 changes: 0 additions & 12 deletions e2e/deployment/client.sh

This file was deleted.

15 changes: 14 additions & 1 deletion e2e/deployment/config.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# script
kubectl create deployment demo --image=alpine:3.16 --replicas=2 -- sleep infinity
sleep 5
kubectl scale deployment demo --replicas=4
sleep 5
kubectl set image deployments demo alpine=alpine:3.17
sleep 5
kubectl scale deployment demo --replicas=2
sleep 5
kubectl delete deployment demo
sleep 30

# config
local TEST_DISPLAY_MODE="21000000" # tracing, full tree
local TRACE_DISPLAY_NAME="Deployment"

declare -A TRACE_SEARCH_TAGS
TRACE_SEARCH_TAGS[cluster]=tracetest
TRACE_SEARCH_TAGS[group]=apps
TRACE_SEARCH_TAGS[resource]=deployments
Expand Down
30 changes: 18 additions & 12 deletions e2e/run-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ cd $(dirname $0)

export REPO_PATH=$(realpath ..)

export TEST_DISPLAY_MODE="21000000"

export DISPLAY_MODES="${TEST_DISPLAY_MODE} ${DISPLAY_MODES:-}"

run_test() {
local test_name=$1

local tmpdir=$(mktemp -d)

if [[ ! -v IS_RERUN ]]; then
bash -x ${test_name}/client.sh
fi

declare -A TRACE_SEARCH_TAGS
declare -a EXTRA_DISPLAY_MODES
set -x
source ${test_name}/config.sh
set +x

local curl_param_string=""
for curl_param_key in "${!TRACE_SEARCH_TAGS[@]}"; do
Expand All @@ -32,6 +28,12 @@ run_test() {
-o ${tmpdir}/curl-output.http \
localhost:8080/redirect

if ! (head -n1 ${tmpdir}/curl-output.http | grep '302 Found'); then
echo "Trace not found for the parameters"
cat curl-output.http
exit 1
fi

local full_trace_id=$(grep -P "^Location: /trace/" ${tmpdir}/curl-output.http | cut -d/ -f3 | tr -d '\r')
if [[ -z $full_trace_id ]]; then
echo "Trace not found for the parameters"
Expand All @@ -46,6 +48,8 @@ run_test() {
echo "${TRACE_DISPLAY_NAME}" >${OUTPUT_TRACE}/trace-${test_name}/trace_display_name
echo ${fixed_id} >${OUTPUT_TRACE}/trace-${test_name}/trace_id

DISPLAY_MODES=($TEST_DISPLAY_MODE ${EXTRA_DISPLAY_MODES[@]})

for mode in ${DISPLAY_MODES}; do
local mode_trace_id=ff${mode}${fixed_id}
curl -o ${OUTPUT_TRACE}/api/traces/${mode_trace_id} localhost:16686/api/traces/${mode_trace_id}
Expand All @@ -59,10 +63,12 @@ run_test() {

declare -A pids

for client in */client.sh; do
test_name=$(basename $(dirname $client))
run_test ${test_name} &
pids[$test_name]=$!
for sh in */validate.jq; do
test_name=$(basename $(dirname $sh))
if [[ ! -v FILTER_TESTS ]] || [[ $FILTER_TESTS == *$test_name* ]]; then
run_test ${test_name} &
pids[$test_name]=$!
fi
done

failed_tests=()
Expand Down
39 changes: 0 additions & 39 deletions e2e/secret-link/client.sh

This file was deleted.

42 changes: 40 additions & 2 deletions e2e/secret-link/config.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
kubectl create -f ${REPO_PATH}/crds/samples/pod-secret.yaml

kubectl create secret generic content

kubectl create -f - << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: reader
labels:
app: reader
spec:
selector:
matchLabels:
app: reader
replicas: 2
template:
metadata:
labels:
app: reader
spec:
containers:
- name: reader
command: ["sleep", "infinity"]
image: alpine:3.16
volumeMounts:
- name: content
mountPath: /mnt/content
volumes:
- name: content
secret:
secretName: content
EOF
sleep 10

kubectl delete deployment/reader secret/content
sleep 20

# config
local TEST_DISPLAY_MODE="24100000" # tracing, owned objects, volumes
local TRACE_DISPLAY_NAME="Volume links"
local TEST_DISPLAY_MODE="24100000"

declare -A TRACE_SEARCH_TAGS
TRACE_SEARCH_TAGS[cluster]=tracetest
TRACE_SEARCH_TAGS[group]=apps
TRACE_SEARCH_TAGS[resource]=deployments
Expand Down
1 change: 1 addition & 0 deletions hack/kind-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nodes:
apiServer:
extraArgs:
audit-webhook-config-file: /mnt/audit/audit-kubeconfig.local.yaml
audit-webhook-batch-max-wait: 5s # speed up event consumption during test
tracing-config-file: /mnt/audit/tracing-config.local.yaml
audit-policy-file: /mnt/audit/audit-policy.yaml
extraVolumes:
Expand Down
7 changes: 5 additions & 2 deletions hack/tfconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ modifiers:
modifierName: link-selector
args:
modifierClass: owner-ref
includeSiblings: true
includeSiblings: false
ifAll:
- linkClass: children
fromParent: false
upwardDistance: 3
"04000000":
# the entire subtree under this object
Expand All @@ -77,7 +80,7 @@ modifiers:
downwardDistance: 3
"00100000":
# include secrets under pods
displayName: volume secrets
displayName: volumes
modifierName: link-selector
args:
ifAll:
Expand Down
11 changes: 10 additions & 1 deletion pkg/frontend/backend/jaeger-storage/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"go.uber.org/zap"

jaegerbackend "github.com/kubewharf/kelemetry/pkg/frontend/backend"
jaegerreader "github.com/kubewharf/kelemetry/pkg/frontend/reader"
tftree "github.com/kubewharf/kelemetry/pkg/frontend/tf/tree"
"github.com/kubewharf/kelemetry/pkg/manager"
utiljaeger "github.com/kubewharf/kelemetry/pkg/util/jaeger"
Expand Down Expand Up @@ -152,7 +153,15 @@ func (backend *Backend) List(
params *spanstore.TraceQueryParameters,
) ([]*jaegerbackend.TraceThumbnail, error) {
traceThumbnails := []*jaegerbackend.TraceThumbnail{}
for _, traceSource := range zconstants.KnownTraceSources(false) {

var traceSources []string
if ctx.Value(jaegerreader.WantPseudoSpansOnly{}) != nil {
traceSources = zconstants.KnownPseudoTraceSources()
} else {
traceSources = zconstants.KnownNonPseudoTraceSources()
}

for _, traceSource := range traceSources {
if params.NumTraces != 0 && len(traceThumbnails) >= params.NumTraces {
break
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/frontend/reader/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ func createVirtualSpan(traceId model.TraceID, span *model.Span, opName string, s
StartTime: span.StartTime,
Duration: span.Duration,
Tags: []model.KeyValue{
{
Key: zconstants.TraceSource,
VType: model.StringType,
VStr: zconstants.TraceSourceObject,
},
{
Key: zconstants.PseudoType,
VType: model.StringType,
Expand Down
Loading

0 comments on commit c10ddb0

Please sign in to comment.