Skip to content
Open
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
3 changes: 2 additions & 1 deletion extensions/kubeconfig/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubeconfig

import (
"bytes"
"context"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -73,7 +74,7 @@ func KubectlExec(restConfig *restclient.Config, podName, namespace string, comma
}

logStreamer := &LogStreamer{}
err = exec.Stream(remotecommand.StreamOptions{
err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{
Stdin: nil,
Stdout: logStreamer,
Stderr: os.Stderr,
Expand Down
61 changes: 61 additions & 0 deletions extensions/kubeconfig/podlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/rancher/shepherd/clients/rancher"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
k8Scheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -73,6 +74,66 @@ func GetPodLogs(client *rancher.Client, clusterID string, podName string, namesp
return logs, nil
}

// GetPodLogsWithOpts fetches logs from a Kubernetes pod, with an optional PodLogOptions object that can be used to finetune which Logs get pulled.
// Buffer size (e.g., '64KB', '8MB', '1GB') influences log reading; an empty string causes no buffering.
func GetPodLogsWithOpts(client *rancher.Client, clusterID string, podName string, namespace string, bufferSizeStr string, opts *corev1.PodLogOptions) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a go doc comment here?

var restConfig *restclient.Config

kubeConfig, err := GetKubeconfig(client, clusterID)
if err != nil {
return "", err
}

restConfig, err = (*kubeConfig).ClientConfig()
if err != nil {
return "", err
}
restConfig.ContentConfig.NegotiatedSerializer = serializer.NewCodecFactory(k8Scheme.Scheme)
restConfig.ContentConfig.GroupVersion = &podGroupVersion
restConfig.APIPath = apiPath

restClient, err := restclient.RESTClientFor(restConfig)
if err != nil {
return "", err
}

req := restClient.Get().Resource("pods").Name(podName).Namespace(namespace).SubResource("log")
req.VersionedParams(
opts,
k8Scheme.ParameterCodec,
)

stream, err := req.Stream(context.TODO())
if err != nil {
return "", fmt.Errorf("error streaming pod logs for pod %s/%s: %v", namespace, podName, err)
}

defer stream.Close()

reader := bufio.NewScanner(stream)

if bufferSizeStr != "" {
bufferSize, err := parseBufferSize(bufferSizeStr)
if err != nil {
return "", fmt.Errorf("error in parseBufferSize: %v", err)
}

buf := make([]byte, bufferSize)
reader.Buffer(buf, bufferSize)
}

var logs string
for reader.Scan() {
logs = logs + fmt.Sprintf("%s\n", reader.Text())
logrus.Info(reader.Text())
}

if err := reader.Err(); err != nil {
return "", fmt.Errorf("error reading pod logs for pod %s/%s: %v", namespace, podName, err)
}
return logs, nil
}

// parseBufferSize is a helper function that parses a size string and returns
// the equivalent size in bytes. The provided size string should end with a
// suffix of 'KB', 'MB', or 'GB'. If no suffix is provided, the function will
Expand Down
59 changes: 59 additions & 0 deletions extensions/kubeconfig/pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package kubeconfig

import (
"context"
"errors"

"github.com/rancher/shepherd/clients/rancher"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
k8Scheme "k8s.io/client-go/kubernetes/scheme"
)

// GetPods utilizes the upstream K8s corev1 client (from the rancher.Client) to get the given cluster's Pods based on any List options passed
func GetPods(client *rancher.Client, clusterID string, namespace string, listOptions *metav1.ListOptions) ([]corev1.Pod, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with the functions here.


kubeConfig, err := GetKubeconfig(client, clusterID)
if err != nil {
return nil, err
}

restConfig, err := (*kubeConfig).ClientConfig()
if err != nil {
return nil, err
}
restConfig.ContentConfig.NegotiatedSerializer = serializer.NewCodecFactory(k8Scheme.Scheme)
restConfig.ContentConfig.GroupVersion = &podGroupVersion
restConfig.APIPath = apiPath

clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, err
}

pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), *listOptions)
if err != nil {
return nil, err
}
return pods.Items, nil
}

// GetPodNames calls GetPods and filters the list of Pods and extracts their names into a []string
func GetPodNames(client *rancher.Client, clusterID string, namespace string, listOptions *metav1.ListOptions) ([]string, error) {
pods, err := GetPods(client, clusterID, namespace, listOptions)
if err != nil {
return nil, err
}

var names []string
for _, pod := range pods {
names = append(names, pod.Name)
}
if len(names) == 0 {
return nil, errors.New("GetPodNames: no pod names found")
}

return names, nil
}
Loading