1
1
package openshift
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
7
+ "io"
6
8
"maps"
7
9
8
10
"github.com/go-logr/logr"
@@ -18,19 +20,22 @@ import (
18
20
apierrors "k8s.io/apimachinery/pkg/api/errors"
19
21
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
22
apiruntime "k8s.io/apimachinery/pkg/runtime"
23
+ "k8s.io/client-go/kubernetes"
21
24
crclient "sigs.k8s.io/controller-runtime/pkg/client"
22
25
)
23
26
24
27
type openshiftClient struct {
25
- Client crclient.Client
28
+ Client crclient.Client
29
+ K8sInterface kubernetes.Interface
26
30
}
27
31
28
32
// NewClient provides a wrapper around the passed in client in
29
33
// order to present convenience functions for each of the object
30
34
// types that are interacted with.
31
- func NewClient (client crclient.Client ) Client {
35
+ func NewClient (client crclient.Client , k8sInterface kubernetes. Interface ) Client {
32
36
var osclient Client = & openshiftClient {
33
- Client : client ,
37
+ Client : client ,
38
+ K8sInterface : k8sInterface ,
34
39
}
35
40
return osclient
36
41
}
@@ -546,3 +551,82 @@ func (oe *openshiftClient) GetDeploymentPods(ctx context.Context, name string, n
546
551
547
552
return podList .Items , nil
548
553
}
554
+
555
+ // GetPod can return an ErrNotFound
556
+ func (oe * openshiftClient ) GetPod (ctx context.Context , name string , namespace string ) (* corev1.Pod , error ) {
557
+ logger := logr .FromContextOrDiscard (ctx )
558
+
559
+ logger .V (log .TRC ).Info ("fetching pod" , "namespace" , namespace , "name" , name )
560
+ pod := corev1.Pod {}
561
+ err := oe .Client .Get (ctx , crclient.ObjectKey {
562
+ Name : name ,
563
+ Namespace : namespace ,
564
+ }, & pod )
565
+ if apierrors .IsNotFound (err ) {
566
+ return nil , fmt .Errorf ("could not retrieve pod: %s/%s: %w: %v" , namespace , name , ErrNotFound , err )
567
+ }
568
+ if err != nil {
569
+ return nil , fmt .Errorf ("could not retrieve pod: %s/%s: %v" , namespace , name , err )
570
+ }
571
+ return & pod , nil
572
+ }
573
+
574
+ func (oe * openshiftClient ) getContainerLogs (ctx context.Context , namespace string , pod string , container string ) (* bytes.Buffer , error ) {
575
+ podLogOpts := corev1.PodLogOptions {
576
+ Container : container ,
577
+ }
578
+ req := oe .K8sInterface .CoreV1 ().Pods (namespace ).GetLogs (pod , & podLogOpts )
579
+ logs , err := req .Stream (ctx )
580
+ if err != nil {
581
+ return nil , fmt .Errorf ("failed to open log stream: %s/%s/%s: %v" , namespace , pod , container , err )
582
+ }
583
+ defer logs .Close ()
584
+ buf := new (bytes.Buffer )
585
+ _ , err = io .Copy (buf , logs )
586
+ if err != nil {
587
+ return nil , fmt .Errorf ("failed to copy log stream: %s/%s/%s: %v" , namespace , pod , container , err )
588
+ }
589
+
590
+ return buf , nil
591
+ }
592
+
593
+ // GetPodLogs can return an ErrNotFound
594
+ func (oe * openshiftClient ) GetPodLogs (ctx context.Context , name string , namespace string ) (map [string ]* bytes.Buffer , error ) {
595
+ logger := logr .FromContextOrDiscard (ctx )
596
+
597
+ pod , err := oe .GetPod (ctx , name , namespace )
598
+ if err != nil {
599
+ return nil , err
600
+ }
601
+
602
+ results := map [string ]* bytes.Buffer {}
603
+
604
+ for _ , container := range pod .Spec .InitContainers {
605
+ buf , err := oe .getContainerLogs (ctx , namespace , name , container .Name )
606
+ if err != nil {
607
+ logger .V (log .TRC ).Info ("failed to get pod logs" , "error" , err )
608
+ continue
609
+ }
610
+ results [container .Name ] = buf
611
+ }
612
+
613
+ for _ , container := range pod .Spec .Containers {
614
+ buf , err := oe .getContainerLogs (ctx , namespace , name , container .Name )
615
+ if err != nil {
616
+ logger .V (log .TRC ).Info ("failed to get pod logs" , "error" , err )
617
+ continue
618
+ }
619
+ results [container .Name ] = buf
620
+ }
621
+
622
+ for _ , container := range pod .Spec .EphemeralContainers {
623
+ buf , err := oe .getContainerLogs (ctx , namespace , name , container .Name )
624
+ if err != nil {
625
+ logger .V (log .TRC ).Info ("failed to get pod logs" , "error" , err )
626
+ continue
627
+ }
628
+ results [container .Name ] = buf
629
+ }
630
+
631
+ return results , nil
632
+ }
0 commit comments