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

test(scorecard): add container logs for report sidecard test (backport #772) #796

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ metadata:
capabilities: Seamless Upgrades
categories: Monitoring, Developer Tools
containerImage: quay.io/cryostat/cryostat-operator:3.0.0-dev
createdAt: "2024-04-04T17:19:02Z"
createdAt: "2024-04-16T19:14:19Z"
description: JVM monitoring and profiling tool
operatorframework.io/initialization-resource: |-
{
Expand Down
12 changes: 6 additions & 6 deletions bundle/tests/scorecard/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- operator-install
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404165315
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245
labels:
suite: cryostat
test: operator-install
Expand All @@ -80,7 +80,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-cr
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404165315
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245
labels:
suite: cryostat
test: cryostat-cr
Expand All @@ -90,7 +90,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-multi-namespace
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404165315
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245
labels:
suite: cryostat
test: cryostat-multi-namespace
Expand All @@ -100,7 +100,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-recording
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404165315
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245
labels:
suite: cryostat
test: cryostat-recording
Expand All @@ -110,7 +110,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-config-change
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404165315
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245
labels:
suite: cryostat
test: cryostat-config-change
Expand All @@ -120,7 +120,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-report
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404165315
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245
labels:
suite: cryostat
test: cryostat-report
Expand Down
12 changes: 6 additions & 6 deletions config/scorecard/patches/custom.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
entrypoint:
- cryostat-scorecard-tests
- operator-install
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404171904"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245"
labels:
suite: cryostat
test: operator-install
Expand All @@ -18,7 +18,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-cr
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404171904"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245"
labels:
suite: cryostat
test: cryostat-cr
Expand All @@ -28,7 +28,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-multi-namespace
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404171904"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245"
labels:
suite: cryostat
test: cryostat-multi-namespace
Expand All @@ -38,7 +38,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-recording
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404171904"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245"
labels:
suite: cryostat
test: cryostat-recording
Expand All @@ -48,7 +48,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-config-change
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404171904"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245"
labels:
suite: cryostat
test: cryostat-config-change
Expand All @@ -58,7 +58,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-report
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240404171904"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416191245"
labels:
suite: cryostat
test: cryostat-report
42 changes: 34 additions & 8 deletions internal/test/scorecard/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,24 +571,50 @@ func (r *TestResources) getCryostatPodNameForCR(cr *operatorv1beta2.Cryostat) (s
selector := metav1.LabelSelector{
MatchLabels: map[string]string{
"app": cr.Name,
"kind": "cryostat",
"component": "cryostat",
},
}
opts := metav1.ListOptions{
LabelSelector: labels.Set(selector.MatchLabels).String(),

names, err := r.getPodnamesForSelector(cr.Namespace, selector)
if err != nil {
return "", err
}

ctx, cancel := context.WithTimeout(context.TODO(), testTimeout)
defer cancel()
if len(names) == 0 {
return "", fmt.Errorf("no matching cryostat pods for cr: %s", cr.Name)
}
return names[0].ObjectMeta.Name, nil
}

pods, err := r.Client.CoreV1().Pods(cr.Namespace).List(ctx, opts)
func (r *TestResources) getReportPodNameForCR(cr *operatorv1beta2.Cryostat) (string, error) {
selector := metav1.LabelSelector{
MatchLabels: map[string]string{
"app": cr.Name,
"kind": "cryostat",
"component": "reports",
},
}

names, err := r.getPodnamesForSelector(cr.Namespace, selector)
if err != nil {
return "", err
}

if len(pods.Items) == 0 {
return "", fmt.Errorf("no matching cryostat pods for cr: %s", cr.Name)
if len(names) == 0 {
return "", fmt.Errorf("no matching report sidecar pods for cr: %s", cr.Name)
}
return names[0].ObjectMeta.Name, nil
}

return pods.Items[0].ObjectMeta.Name, nil
func (r *TestResources) getPodnamesForSelector(namespace string, selector metav1.LabelSelector) ([]corev1.Pod, error) {
labelSelector := labels.Set(selector.MatchLabels).String()

ctx, cancel := context.WithTimeout(context.TODO(), testTimeout)
defer cancel()

pods, err := r.Client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
LabelSelector: labelSelector,
})
return pods.Items, err
}
41 changes: 32 additions & 9 deletions internal/test/scorecard/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import (
)

type ContainerLog struct {
Namespace string
Pod string
Container string
Log string
}

func (r *TestResources) logContainer(namespace, podName, containerName string) {
containerLog := &ContainerLog{
Namespace: namespace,
Pod: podName,
Container: containerName,
}
buf := &strings.Builder{}
Expand Down Expand Up @@ -77,27 +81,46 @@ func (r *TestResources) CollectContainersLogsToResult() {
logs := r.CollectLogs()
for _, log := range logs {
if log != nil {
r.Log += fmt.Sprintf("\n%s CONTAINER LOG:\n\n\t%s\n", strings.ToUpper(log.Container), log.Log)
r.Log += fmt.Sprintf("\nNAMESPACE: %s\nPOD: %s\nCONTAINER: %s\nLOG:\n\t%s\n",
strings.ToUpper(log.Namespace),
strings.ToUpper(log.Pod),
strings.ToUpper(log.Container),
log.Log,
)
}
}
}

func (r *TestResources) StartLogs(cr *operatorv1beta2.Cryostat) error {
podName, err := r.getCryostatPodNameForCR(cr)
cryostatPodName, err := r.getCryostatPodNameForCR(cr)
if err != nil {
return fmt.Errorf("failed to get pod name for CR: %s", err.Error())
}

containerNames := []string{
cr.Name,
cr.Name + "-grafana",
cr.Name + "-jfr-datasource",
logSelections := map[string][]string{
cryostatPodName: {
cr.Name,
cr.Name + "-grafana",
cr.Name + "-jfr-datasource",
},
}
bufferSize := 3

r.LogChannel = make(chan *ContainerLog, len(containerNames))
if cr.Spec.ReportOptions != nil && cr.Spec.ReportOptions.Replicas > 0 {
reportPodName, err := r.getReportPodNameForCR(cr)
if err != nil {
return fmt.Errorf("failed to get pod name for report sidecar: %s", err.Error())
}
logSelections[reportPodName] = []string{cr.Name + "-reports"}
bufferSize++
}

for _, containerName := range containerNames {
go r.logContainer(cr.Namespace, podName, containerName)
r.LogChannel = make(chan *ContainerLog, bufferSize)

for pod, containers := range logSelections {
for _, container := range containers {
go r.logContainer(cr.Namespace, pod, container)
}
}

return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/test/scorecard/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,10 @@ func CryostatReportTest(bundle *apimanifests.Bundle, namespace string, openShift
return r.fail(fmt.Sprintf("failed to reach the application: %s", err.Error()))
}

err = r.StartLogs(cr)
if err != nil {
r.Log += fmt.Sprintf("failed to retrieve logs for the application: %s", err.Error())
}

return r.TestResult
}
Loading