add: kubernetes log tailer
This commit is contained in:
96
internal/pkg/logs/README.md
Normal file
96
internal/pkg/logs/README.md
Normal file
@@ -0,0 +1,96 @@
|
||||
# Logs
|
||||
|
||||
Provides a helper to tail the Kubernetes deployment logs. Usage looks like:
|
||||
|
||||
```golang
|
||||
tailer, done := logs.Logger{
|
||||
Deployment: "mydeployment",
|
||||
Namespace: "mynamespace",
|
||||
}.Start()
|
||||
defer func() {
|
||||
err := done() // closes the tailer
|
||||
if err != nil { ... }
|
||||
}()
|
||||
|
||||
for line := range tailer.NextLine() {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- Tail logs from Kubernetes deployments
|
||||
- Supports in-cluster and out-of-cluster configurations
|
||||
- Automatic pod and container discovery
|
||||
- Configurable timeout
|
||||
- Support for tailing specific number of lines
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```golang
|
||||
tailer, done := logs.Logger{
|
||||
Deployment: "mydeployment",
|
||||
Namespace: "mynamespace",
|
||||
}.Start()
|
||||
defer func() {
|
||||
err := done()
|
||||
if err != nil {
|
||||
// handle error
|
||||
}
|
||||
}()
|
||||
|
||||
for line := range tailer.NextLine() {
|
||||
fmt.Println(line)
|
||||
}
|
||||
```
|
||||
|
||||
### Advanced Usage
|
||||
|
||||
```golang
|
||||
// Tail from a specific pod and container
|
||||
tailer, done := logs.Logger{
|
||||
Deployment: "mydeployment",
|
||||
Namespace: "mynamespace",
|
||||
Pod: "mydeployment-7b5b5c8c9d-xyz12",
|
||||
Container: "mycontainer",
|
||||
TailLines: int64Ptr(100), // Tail last 100 lines
|
||||
Timeout: 5 * time.Minute,
|
||||
}.Start()
|
||||
defer func() {
|
||||
err := done()
|
||||
if err != nil {
|
||||
// handle error
|
||||
}
|
||||
}()
|
||||
|
||||
for line := range tailer.NextLine() {
|
||||
fmt.Println(line)
|
||||
}
|
||||
```
|
||||
|
||||
### Helper Functions
|
||||
|
||||
```golang
|
||||
// Helper to create a pointer to int64
|
||||
func int64Ptr(i int64) *int64 {
|
||||
return &i
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
The Logger struct accepts the following fields:
|
||||
|
||||
- `Deployment` (required): Name of the Kubernetes deployment
|
||||
- `Namespace` (required): Kubernetes namespace
|
||||
- `Pod` (optional): Specific pod name to tail logs from
|
||||
- `Container` (optional): Specific container name to tail logs from
|
||||
- `TailLines` (optional): Number of lines to tail from the end of logs
|
||||
- `Timeout` (optional): Time to wait before giving up on tailing
|
||||
|
||||
## Requirements
|
||||
|
||||
- Kubernetes cluster access (in-cluster or via kubeconfig)
|
||||
- Proper RBAC permissions to read pod logs
|
||||
203
internal/pkg/logs/logs.go
Normal file
203
internal/pkg/logs/logs.go
Normal file
@@ -0,0 +1,203 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
// Logger represents a Kubernetes log tailer
|
||||
type Logger struct {
|
||||
// Deployment name
|
||||
Deployment string
|
||||
// Namespace name
|
||||
Namespace string
|
||||
// Optional: Pod name (if specified, will tail logs from this specific pod)
|
||||
Pod string
|
||||
// Optional: Container name (if specified, will tail logs from this specific container)
|
||||
Container string
|
||||
// Optional: Number of lines to tail from the end of the logs
|
||||
// If not specified, will tail from the beginning
|
||||
TailLines *int64
|
||||
// Optional: Time to wait before giving up on tailing
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// Tailer represents a running log tailer
|
||||
type Tailer struct {
|
||||
lines chan string
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// NextLine returns a channel that will receive log lines
|
||||
func (t *Tailer) NextLine() <-chan string {
|
||||
return t.lines
|
||||
}
|
||||
|
||||
// Stop stops the tailer and closes the line channel
|
||||
func (t *Tailer) Stop() {
|
||||
close(t.done)
|
||||
}
|
||||
|
||||
// Start starts the log tailer
|
||||
func (l *Logger) Start() (*Tailer, func() error) {
|
||||
tailer := &Tailer{
|
||||
lines: make(chan string),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Create Kubernetes client
|
||||
client, err := l.createKubernetesClient()
|
||||
if err != nil {
|
||||
close(tailer.lines)
|
||||
return tailer, func() error { return err }
|
||||
}
|
||||
|
||||
// Get pod name if not specified
|
||||
podName := l.Pod
|
||||
if podName == "" {
|
||||
podName, err = l.getPodName(client)
|
||||
if err != nil {
|
||||
close(tailer.lines)
|
||||
return tailer, func() error { return err }
|
||||
}
|
||||
}
|
||||
|
||||
// Get container name if not specified
|
||||
containerName := l.Container
|
||||
if containerName == "" {
|
||||
containerName, err = l.getContainerName(client, podName)
|
||||
if err != nil {
|
||||
close(tailer.lines)
|
||||
return tailer, func() error { return err }
|
||||
}
|
||||
}
|
||||
|
||||
// Start tailing logs
|
||||
go l.tailLogs(client, podName, containerName, tailer)
|
||||
|
||||
return tailer, func() error {
|
||||
tailer.Stop()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) createKubernetesClient() (*kubernetes.Clientset, error) {
|
||||
// Try to load in-cluster config first
|
||||
config, err := rest.InClusterConfig()
|
||||
if err != nil {
|
||||
// If in-cluster config fails, try kubeconfig
|
||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
configOverrides := &clientcmd.ConfigOverrides{}
|
||||
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
|
||||
config, err = kubeConfig.ClientConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create kubernetes client: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
client, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create kubernetes client: %w", err)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (l *Logger) getPodName(client *kubernetes.Clientset) (string, error) {
|
||||
// List pods with the deployment label
|
||||
podList, err := client.CoreV1().Pods(l.Namespace).List(context.TODO(), metav1.ListOptions{
|
||||
LabelSelector: fmt.Sprintf("app=%s", l.Deployment),
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to list pods: %w", err)
|
||||
}
|
||||
|
||||
if len(podList.Items) == 0 {
|
||||
return "", fmt.Errorf("no pods found for deployment %s in namespace %s", l.Deployment, l.Namespace)
|
||||
}
|
||||
|
||||
// Return the first pod that's running
|
||||
for _, pod := range podList.Items {
|
||||
if pod.Status.Phase == corev1.PodRunning {
|
||||
return pod.Name, nil
|
||||
}
|
||||
}
|
||||
|
||||
// If no running pods, return the first pod
|
||||
return podList.Items[0].Name, nil
|
||||
}
|
||||
|
||||
func (l *Logger) getContainerName(client *kubernetes.Clientset, podName string) (string, error) {
|
||||
pod, err := client.CoreV1().Pods(l.Namespace).Get(context.TODO(), podName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get pod %s: %w", podName, err)
|
||||
}
|
||||
|
||||
if len(pod.Spec.Containers) == 0 {
|
||||
return "", fmt.Errorf("pod %s has no containers", podName)
|
||||
}
|
||||
|
||||
// Return the first container name
|
||||
return pod.Spec.Containers[0].Name, nil
|
||||
}
|
||||
|
||||
func (l *Logger) tailLogs(client *kubernetes.Clientset, podName, containerName string, tailer *Tailer) {
|
||||
defer close(tailer.lines)
|
||||
|
||||
// Prepare log request
|
||||
logOpts := &corev1.PodLogOptions{
|
||||
Container: containerName,
|
||||
Follow: true,
|
||||
}
|
||||
|
||||
if l.TailLines != nil {
|
||||
logOpts.TailLines = l.TailLines
|
||||
}
|
||||
|
||||
// Create context with timeout if specified
|
||||
ctx := context.Background()
|
||||
if l.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, l.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
// Get logs stream
|
||||
req := client.CoreV1().Pods(l.Namespace).GetLogs(podName, logOpts)
|
||||
logsStream, err := req.Stream(ctx)
|
||||
if err != nil {
|
||||
select {
|
||||
case tailer.lines <- fmt.Sprintf("Error: failed to get logs stream: %v", err):
|
||||
case <-tailer.done:
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
defer logsStream.Close()
|
||||
|
||||
// Read logs line by line
|
||||
scanner := bufio.NewScanner(logsStream)
|
||||
for scanner.Scan() {
|
||||
select {
|
||||
case tailer.lines <- scanner.Text():
|
||||
case <-tailer.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
select {
|
||||
case tailer.lines <- fmt.Sprintf("Error: failed to read logs: %v", err):
|
||||
case <-tailer.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
57
internal/pkg/logs/logs_test.go
Normal file
57
internal/pkg/logs/logs_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
// This is a basic test to ensure the structure compiles correctly
|
||||
// Actual integration testing would require a Kubernetes cluster
|
||||
|
||||
logger := Logger{
|
||||
Deployment: "test-deployment",
|
||||
Namespace: "test-namespace",
|
||||
}
|
||||
|
||||
// Test that the struct can be created
|
||||
assert.NotNil(t, logger)
|
||||
assert.Equal(t, "test-deployment", logger.Deployment)
|
||||
assert.Equal(t, "test-namespace", logger.Namespace)
|
||||
}
|
||||
|
||||
func TestTailer(t *testing.T) {
|
||||
// Test tailer creation and methods
|
||||
tailer := &Tailer{
|
||||
lines: make(chan string),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Test NextLine method
|
||||
lineChan := tailer.NextLine()
|
||||
assert.NotNil(t, lineChan)
|
||||
|
||||
// Test Stop method
|
||||
tailer.Stop()
|
||||
|
||||
// Verify the done channel is closed
|
||||
select {
|
||||
case _, ok := <-tailer.done:
|
||||
assert.False(t, ok, "done channel should be closed")
|
||||
default:
|
||||
assert.Fail(t, "done channel should be closed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeout(t *testing.T) {
|
||||
// Test that timeout is handled correctly in Logger
|
||||
logger := Logger{
|
||||
Deployment: "test-deployment",
|
||||
Namespace: "test-namespace",
|
||||
Timeout: 100 * time.Millisecond,
|
||||
}
|
||||
|
||||
assert.Equal(t, 100*time.Millisecond, logger.Timeout)
|
||||
}
|
||||
Reference in New Issue
Block a user