Skip to content

Commit d9e77ab

Browse files
IshwarKanseStarefossen
authored andcommitted
[Chore] Test case for scraping OpenShift in-cluster monitroing stack (open-telemetry#2844)
* Test case for scraping OpenShift in-cluster monitroing stack * Bump Chainsaw version
1 parent 8f8ed53 commit d9e77ab

File tree

7 files changed

+197
-1
lines changed

7 files changed

+197
-1
lines changed

tests/e2e-openshift/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ RUN curl -LO https://github.com/kudobuilder/kuttl/releases/download/v0.15.0/kube
2626
&& mv kubectl-kuttl_0.15.0_linux_x86_64 /usr/local/bin/kuttl
2727

2828
# Install Chainsaw e2e
29-
RUN go install github.com/kyverno/chainsaw@v0.1.7
29+
RUN go install github.com/kyverno/chainsaw@v0.2.0
3030

3131
# Install kubectl and oc
3232
RUN curl -LO https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/latest/openshift-client-linux.tar.gz \
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: scrape-in-cluster-monitoring
5+
spec:
6+
namespace: chainsaw-scrape-in-cluster-monitoring
7+
steps:
8+
- name: Create OTEL collector with Prometheus receiver to scrape in-cluster metrics
9+
try:
10+
- apply:
11+
file: create-clusterrolebinding.yaml
12+
- assert:
13+
file: create-clusterrolebinding-assert.yaml
14+
- apply:
15+
file: create-otel-instance.yaml
16+
- assert:
17+
file: create-otel-instance-assert.yaml
18+
- name: Wait for the metrics to be collected
19+
try:
20+
- sleep:
21+
duration: 10s
22+
- name: Check the presence of metrics in the OTEL collector
23+
try:
24+
- script:
25+
timeout: 5m
26+
content: ./check_logs.sh
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
# This script checks the OpenTelemetry collector pod for the presence of Metrics.
3+
4+
# Define the label selector
5+
LABEL_SELECTOR="app.kubernetes.io/component=opentelemetry-collector"
6+
NAMESPACE=chainsaw-scrape-in-cluster-monitoring
7+
8+
# Define the search strings
9+
SEARCH_STRING1='-> container'
10+
SEARCH_STRING2='-> label_pod_security_kubernetes_io_audit: Str(restricted)'
11+
SEARCH_STRING3='-> label_pod_security_kubernetes_io_enforce: Str(privileged)'
12+
SEARCH_STRING4='-> label_kubernetes_io_metadata_name:'
13+
SEARCH_STRING5='-> namespace:'
14+
15+
# Initialize flags to track if strings are found
16+
FOUND1=false
17+
FOUND2=false
18+
FOUND3=false
19+
FOUND4=false
20+
FOUND5=false
21+
22+
# Loop until all strings are found
23+
while ! $FOUND1 || ! $FOUND2 || ! $FOUND3 || ! $FOUND4 || ! $FOUND5; do
24+
# Get the list of pods with the specified label
25+
PODS=($(kubectl -n $NAMESPACE get pods -l $LABEL_SELECTOR -o jsonpath='{.items[*].metadata.name}'))
26+
27+
# Loop through each pod and search for the strings in the logs
28+
for POD in "${PODS[@]}"; do
29+
# Search for the first string
30+
if ! $FOUND1 && kubectl -n $NAMESPACE --tail=500 logs $POD | grep -q -- "$SEARCH_STRING1"; then
31+
echo "\"$SEARCH_STRING1\" found in $POD"
32+
FOUND1=true
33+
fi
34+
# Search for the second string
35+
if ! $FOUND2 && kubectl -n $NAMESPACE --tail=500 logs $POD | grep -q -- "$SEARCH_STRING2"; then
36+
echo "\"$SEARCH_STRING2\" found in $POD"
37+
FOUND2=true
38+
fi
39+
# Search for the third string
40+
if ! $FOUND3 && kubectl -n $NAMESPACE --tail=500 logs $POD | grep -q -- "$SEARCH_STRING3"; then
41+
echo "\"$SEARCH_STRING3\" found in $POD"
42+
FOUND3=true
43+
fi
44+
# Search for the fourth string
45+
if ! $FOUND4 && kubectl -n $NAMESPACE --tail=500 logs $POD | grep -q -- "$SEARCH_STRING4"; then
46+
echo "\"$SEARCH_STRING4\" found in $POD"
47+
FOUND4=true
48+
fi
49+
# Search for the fifth string
50+
if ! $FOUND5 && kubectl -n $NAMESPACE --tail=500 logs $POD | grep -q -- "$SEARCH_STRING5"; then
51+
echo "\"$SEARCH_STRING5\" found in $POD"
52+
FOUND5=true
53+
fi
54+
done
55+
done
56+
57+
echo "Found the matched metrics in collector"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: chainsaw-scrape-in-cluster-monitoring-binding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: cluster-monitoring-view
9+
subjects:
10+
- kind: ServiceAccount
11+
name: otel-collector
12+
namespace: chainsaw-scrape-in-cluster-monitoring
13+
14+
---
15+
apiVersion: v1
16+
kind: ConfigMap
17+
metadata:
18+
name: cabundle
19+
namespace: chainsaw-scrape-in-cluster-monitoring
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: chainsaw-scrape-in-cluster-monitoring-binding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: cluster-monitoring-view
9+
subjects:
10+
- kind: ServiceAccount
11+
name: otel-collector
12+
namespace: chainsaw-scrape-in-cluster-monitoring
13+
14+
---
15+
kind: ConfigMap
16+
apiVersion: v1
17+
metadata:
18+
name: cabundle
19+
namespce: chainsaw-scrape-in-cluster-monitoring
20+
annotations:
21+
service.beta.openshift.io/inject-cabundle: "true"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: otel-collector
5+
namespace: chainsaw-scrape-in-cluster-monitoring
6+
status:
7+
availableReplicas: 1
8+
readyReplicas: 1
9+
replicas: 1
10+
11+
---
12+
apiVersion: v1
13+
kind: Service
14+
metadata:
15+
name: otel-collector-monitoring
16+
namespace: chainsaw-scrape-in-cluster-monitoring
17+
spec:
18+
ports:
19+
- name: monitoring
20+
port: 8888
21+
protocol: TCP
22+
targetPort: 8888
23+
selector:
24+
app.kubernetes.io/component: opentelemetry-collector
25+
app.kubernetes.io/instance: chainsaw-scrape-in-cluster-monitoring.otel
26+
app.kubernetes.io/managed-by: opentelemetry-operator
27+
app.kubernetes.io/part-of: opentelemetry
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apiVersion: opentelemetry.io/v1alpha1
2+
kind: OpenTelemetryCollector
3+
metadata:
4+
name: otel
5+
namespace: chainsaw-scrape-in-cluster-monitoring
6+
spec:
7+
volumeMounts:
8+
- name: cabundle-volume
9+
mountPath: /etc/pki/ca-trust/source/service-ca
10+
readOnly: true
11+
volumes:
12+
- name: cabundle-volume
13+
configMap:
14+
name: cabundle
15+
mode: deployment
16+
config: |
17+
receivers:
18+
prometheus:
19+
config:
20+
scrape_configs:
21+
- job_name: 'federate'
22+
scrape_interval: 15s
23+
scheme: https
24+
tls_config:
25+
ca_file: /etc/pki/ca-trust/source/service-ca/service-ca.crt
26+
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
27+
# honor_labels needs to be set to false due to bug https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32555
28+
honor_labels: false
29+
params:
30+
'match[]':
31+
- '{__name__="kube_namespace_labels"}'
32+
metrics_path: '/federate'
33+
static_configs:
34+
- targets:
35+
- "prometheus-k8s.openshift-monitoring.svc.cluster.local:9091"
36+
37+
exporters:
38+
debug:
39+
verbosity: detailed
40+
41+
service:
42+
pipelines:
43+
metrics:
44+
receivers: [prometheus]
45+
processors: []
46+
exporters: [debug]

0 commit comments

Comments
 (0)