Skip to content

Commit

Permalink
chore(scorecard): refactor to remove duplicate codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tthvo committed Mar 11, 2024
1 parent a6c4b5b commit fb28a3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 54 deletions.
77 changes: 24 additions & 53 deletions internal/test/scorecard/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,22 @@ type ContainerLog struct {
Log string
}

func LogCryostatContainer(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat, ch chan *ContainerLog) {
func LogContainer(clientset *kubernetes.Clientset, namespace, podName, containerName string, ch chan *ContainerLog) {
containerLog := &ContainerLog{
Container: "cryostat",
Container: containerName,
}
buf := &strings.Builder{}
podName, err := getCryostatPodNameForCR(clientset, cr)
if err != nil {
buf.WriteString(fmt.Sprintf("failed to get pod name: %s", err.Error()))
} else {
err := LogContainer(clientset, cr.Namespace, podName, cr.Name, buf)
if err != nil {
buf.WriteString(err.Error())
}
}

containerLog.Log = buf.String()
ch <- containerLog
}

func LogGrafanaContainer(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat, ch chan *ContainerLog) {
containerLog := &ContainerLog{
Container: "grafana",
}
buf := &strings.Builder{}
podName, err := getCryostatPodNameForCR(clientset, cr)
err := GetContainerLogs(clientset, namespace, podName, containerName, buf)
if err != nil {
buf.WriteString(fmt.Sprintf("failed to get pod name: %s", err.Error()))
} else {
err := LogContainer(clientset, cr.Namespace, podName, cr.Name+"-grafana", buf)
if err != nil {
buf.WriteString(err.Error())
}
buf.WriteString(fmt.Sprintf("%s\n", err.Error()))
}

containerLog.Log = buf.String()
ch <- containerLog
}

func LogDatasourceContainer(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat, ch chan *ContainerLog) {
containerLog := &ContainerLog{
Container: "jfr-datasource",
}
buf := &strings.Builder{}
podName, err := getCryostatPodNameForCR(clientset, cr)
if err != nil {
buf.WriteString(fmt.Sprintf("failed to get pod name: %s", err.Error()))
} else {
err := LogContainer(clientset, cr.Namespace, podName, cr.Name+"-jfr-datasource", buf)
if err != nil {
buf.WriteString(err.Error())
}
}

containerLog.Log = buf.String()
ch <- containerLog
}

func LogContainer(clientset *kubernetes.Clientset, namespace, podName, containerName string, dest io.Writer) error {
func GetContainerLogs(clientset *kubernetes.Clientset, namespace, podName, containerName string, dest io.Writer) error {
ctx, cancel := context.WithTimeout(context.TODO(), testTimeout)
defer cancel()

Expand Down Expand Up @@ -126,10 +84,23 @@ func CollectContainersLogsToResult(result *scapiv1alpha3.TestResult, ch chan *Co
}
}

func StartLogs(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat) chan *ContainerLog {
ch := make(chan *ContainerLog, 3)
go LogCryostatContainer(clientset, cr, ch)
go LogGrafanaContainer(clientset, cr, ch)
go LogDatasourceContainer(clientset, cr, ch)
return ch
func StartLogs(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat) (chan *ContainerLog, error) {
podName, err := getCryostatPodNameForCR(clientset, cr)
if err != nil {
return nil, fmt.Errorf("failed to get pod name for CR: %s", err.Error())
}

containerNames := []string{
cr.Name,
cr.Name + "-grafana",
cr.Name + "-jfr-datasource",
}

ch := make(chan *ContainerLog, len(containerNames))

for _, containerName := range containerNames {
go LogContainer(clientset, cr.Namespace, podName, containerName, ch)
}

return ch, nil
}
5 changes: 4 additions & 1 deletion internal/test/scorecard/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openSh
if err != nil {
return fail(*r, fmt.Sprintf("failed to determine application URL: %s", err.Error()))
}
ch := StartLogs(tr.Client.Clientset, cr)
ch, err := StartLogs(tr.Client.Clientset, cr)
if err != nil {
return fail(*r, fmt.Sprintf("failed to retrieve logs for the application: %s", err.Error()))
}
defer CollectContainersLogsToResult(&result, ch)

defer cleanupCryostat(r, tr.Client, CryostatRecordingTestName, namespace)
Expand Down

0 comments on commit fb28a3f

Please sign in to comment.