Skip to content

Commit

Permalink
print CSI logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Mar 18, 2024
1 parent a550795 commit 690124e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
53 changes: 53 additions & 0 deletions internal/integ/cluster/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"encoding/base64"
"fmt"
"io"
"log/slog"
"os"
"strings"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/exoscale/exoscale/csi-driver/internal/integ/flags"
Expand Down Expand Up @@ -203,11 +206,13 @@ func (c *Cluster) applyCSI(ctx context.Context) error {
c.restartCSIController(ctx)

controllerName := "exoscale-csi-controller"
c.printDeploymentLogs(ctx, controllerName)
if err := c.awaitDeploymentReadiness(ctx, controllerName); err != nil {
slog.Warn("error while awaiting", "deployment", controllerName, "error", err)
}

nodeDriverName := "exoscale-csi-node"
c.printDaemonSetLogs(ctx, nodeDriverName)
if err := c.awaitDaemonSetReadiness(ctx, nodeDriverName); err != nil {
slog.Warn("error while awaiting", "DaemonSet", nodeDriverName, "error", err)
}
Expand Down Expand Up @@ -235,6 +240,54 @@ func retry(trial func() error, nRetries int, retryInterval time.Duration) error
return trial()
}

func (c *Cluster) printDeploymentLogs(ctx context.Context, deploymentName string) {
deployment, err := c.K8s.ClientSet.AppsV1().Deployments(csiNamespace).Get(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
slog.Warn("failed to get", "deployment", deploymentName)

return
}

c.printPodsLogs(ctx, deployment.Spec.Selector.MatchLabels["app"])
}

func (c *Cluster) printDaemonSetLogs(ctx context.Context, name string) {
daemonSet, err := c.K8s.ClientSet.AppsV1().DaemonSets(csiNamespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
slog.Warn("failed to get", "DaemonSet", name)

return
}

c.printPodsLogs(ctx, daemonSet.Spec.Selector.MatchLabels["app"])
}

func (c *Cluster) printPodsLogs(ctx context.Context, labelSelector string) {
podList, err := c.K8s.ClientSet.CoreV1().Pods(csiNamespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
slog.Warn("failed to get pod list", "labelSelector", labelSelector)

return
}

for _, pod := range podList.Items {
req := c.K8s.ClientSet.CoreV1().Pods(csiNamespace).GetLogs(pod.Name, &v1.PodLogOptions{})
logStream, err := req.Stream(ctx)
if err != nil {
slog.Warn("failed to get log stream", "pod", pod.Name, "err", err)

continue
}

go func(podName string) {
defer logStream.Close()
if _, err := io.Copy(os.Stdout, logStream); err != nil {
slog.Warn("failed to read log stream", "pod", podName, "err", err)
}
}(pod.Name)
}
}

func (c *Cluster) awaitDeploymentReadiness(ctx context.Context, deploymentName string) error {
return retry(func() error {
deployment, err := c.K8s.ClientSet.AppsV1().Deployments(csiNamespace).Get(ctx, deploymentName, metav1.GetOptions{})
Expand Down
2 changes: 0 additions & 2 deletions internal/integ/integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ func TestVolumeAttach(t *testing.T) {
ns.ApplyPVC(pvcName, false)
ns.Apply(fmt.Sprintf(basicDeployment, pvcName))

go ns.K.PrintEvents(ns.CTX, ns.Name)

awaitExpectation(t, "Bound", func() interface{} {
pvc, err := ns.K.ClientSet.CoreV1().PersistentVolumeClaims(ns.Name).Get(ns.CTX, pvcName, metav1.GetOptions{})
assert.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions internal/integ/k8s/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func CreateTestNamespace(t *testing.T, k *K8S, testName string) *Namespace {
_, err := ns.K.ClientSet.CoreV1().Namespaces().Create(ns.CTX, namespace, metav1.CreateOptions{})
assert.NoError(ns.t, err)

go ns.K.PrintEvents(ns.CTX, ns.Name)

if !*flags.DontCleanUpTestNS {
t.Cleanup(func() {
slog.Info("cleaning up test namespace", "name", ns.Name)
Expand Down

0 comments on commit 690124e

Please sign in to comment.