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

[release-1.6] 🌱 test/framework add WatchDaemonSetLogsByLabelSelector method #9994

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
38 changes: 38 additions & 0 deletions test/framework/daemonset_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
toolscache "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/cluster-api/internal/util/kubeadm"
Expand Down Expand Up @@ -60,3 +62,39 @@ func WaitForKubeProxyUpgrade(ctx context.Context, input WaitForKubeProxyUpgradeI
return false, nil
}, intervals...).Should(BeTrue())
}

// WatchDaemonSetLogsByLabelSelectorInput is the input for WatchDaemonSetLogsByLabelSelector.
type WatchDaemonSetLogsByLabelSelectorInput struct {
GetLister GetLister
Cache toolscache.Cache
ClientSet *kubernetes.Clientset
Labels map[string]string
LogPath string
}

// WatchDaemonSetLogsByLabelSelector streams logs for all containers for all pods belonging to a daemonset on the basis of label. Each container's logs are streamed
// in a separate goroutine so they can all be streamed concurrently. This only causes a test failure if there are errors
// retrieving the daemonset, its pods, or setting up a log file. If there is an error with the log streaming itself,
// that does not cause the test to fail.
func WatchDaemonSetLogsByLabelSelector(ctx context.Context, input WatchDaemonSetLogsByLabelSelectorInput) {
Expect(ctx).NotTo(BeNil(), "ctx is required for WatchDaemonSetLogsByLabelSelector")
Expect(input.Cache).NotTo(BeNil(), "input.Cache is required for WatchDaemonSetLogsByLabelSelector")
Expect(input.ClientSet).NotTo(BeNil(), "input.ClientSet is required for WatchDaemonSetLogsByLabelSelector")
Expect(input.Labels).NotTo(BeNil(), "input.Selector is required for WatchDaemonSetLogsByLabelSelector")

daemonSetList := &appsv1.DaemonSetList{}
Eventually(func() error {
return input.GetLister.List(ctx, daemonSetList, client.MatchingLabels(input.Labels))
}, retryableOperationTimeout, retryableOperationInterval).Should(Succeed(), "Failed to get DaemonSets for labels")

for _, daemonSet := range daemonSetList.Items {
watchPodLogs(ctx, watchPodLogsInput{
Cache: input.Cache,
ClientSet: input.ClientSet,
Namespace: daemonSet.Namespace,
ManagingResourceName: daemonSet.Name,
LabelSelector: daemonSet.Spec.Selector,
LogPath: input.LogPath,
})
}
}
46 changes: 23 additions & 23 deletions test/framework/deployment_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ func WatchDeploymentLogsByLabelSelector(ctx context.Context, input WatchDeployme

for _, deployment := range deploymentList.Items {
watchPodLogs(ctx, watchPodLogsInput{
Cache: input.Cache,
ClientSet: input.ClientSet,
Namespace: deployment.Namespace,
DeploymentName: deployment.Name,
LabelSelector: deployment.Spec.Selector,
LogPath: input.LogPath,
Cache: input.Cache,
ClientSet: input.ClientSet,
Namespace: deployment.Namespace,
ManagingResourceName: deployment.Name,
LabelSelector: deployment.Spec.Selector,
LogPath: input.LogPath,
})
}
}
Expand Down Expand Up @@ -163,23 +163,23 @@ func WatchDeploymentLogsByName(ctx context.Context, input WatchDeploymentLogsByN
}, retryableOperationTimeout, retryableOperationInterval).Should(Succeed(), "Failed to get deployment %s", klog.KObj(input.Deployment))

watchPodLogs(ctx, watchPodLogsInput{
Cache: input.Cache,
ClientSet: input.ClientSet,
Namespace: deployment.Namespace,
DeploymentName: deployment.Name,
LabelSelector: deployment.Spec.Selector,
LogPath: input.LogPath,
Cache: input.Cache,
ClientSet: input.ClientSet,
Namespace: deployment.Namespace,
ManagingResourceName: deployment.Name,
LabelSelector: deployment.Spec.Selector,
LogPath: input.LogPath,
})
}

// watchPodLogsInput is the input for watchPodLogs.
type watchPodLogsInput struct {
Cache toolscache.Cache
ClientSet *kubernetes.Clientset
Namespace string
DeploymentName string
LabelSelector *metav1.LabelSelector
LogPath string
Cache toolscache.Cache
ClientSet *kubernetes.Clientset
Namespace string
ManagingResourceName string
LabelSelector *metav1.LabelSelector
LogPath string
}

// watchPodLogs streams logs for all containers for all pods belonging to a deployment with the given label. Each container's logs are streamed
Expand Down Expand Up @@ -251,16 +251,16 @@ func (eh *watchPodLogsEventHandler) streamPodLogs(pod *corev1.Pod) {
}

for _, container := range pod.Spec.Containers {
log.Logf("Creating log watcher for controller %s, pod %s, container %s", klog.KRef(eh.input.Namespace, eh.input.DeploymentName), pod.Name, container.Name)
log.Logf("Creating log watcher for controller %s, pod %s, container %s", klog.KRef(eh.input.Namespace, eh.input.ManagingResourceName), pod.Name, container.Name)

// Create log metadata file.
logMetadataFile := filepath.Clean(path.Join(eh.input.LogPath, eh.input.DeploymentName, pod.Name, container.Name+"-log-metadata.json"))
logMetadataFile := filepath.Clean(path.Join(eh.input.LogPath, eh.input.ManagingResourceName, pod.Name, container.Name+"-log-metadata.json"))
Expect(os.MkdirAll(filepath.Dir(logMetadataFile), 0750)).To(Succeed())

metadata := logMetadata{
Job: eh.input.Namespace + "/" + eh.input.DeploymentName,
Job: eh.input.Namespace + "/" + eh.input.ManagingResourceName,
Namespace: eh.input.Namespace,
App: eh.input.DeploymentName,
App: eh.input.ManagingResourceName,
Pod: pod.Name,
Container: container.Name,
NodeName: pod.Spec.NodeName,
Expand All @@ -274,7 +274,7 @@ func (eh *watchPodLogsEventHandler) streamPodLogs(pod *corev1.Pod) {
go func(pod *corev1.Pod, container corev1.Container) {
defer GinkgoRecover()

logFile := filepath.Clean(path.Join(eh.input.LogPath, eh.input.DeploymentName, pod.Name, container.Name+".log"))
logFile := filepath.Clean(path.Join(eh.input.LogPath, eh.input.ManagingResourceName, pod.Name, container.Name+".log"))
Expect(os.MkdirAll(filepath.Dir(logFile), 0750)).To(Succeed())

f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
Expand Down