...

Source file src/sigs.k8s.io/gateway-api/conformance/utils/kubernetes/logs.go

Documentation: sigs.k8s.io/gateway-api/conformance/utils/kubernetes

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kubernetes
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/labels"
    25  	clientset "k8s.io/client-go/kubernetes"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  )
    28  
    29  // DumpEchoLogs returns logs of the echoserver pod in
    30  // in the given namespace and with the given name.
    31  func DumpEchoLogs(ns, name string, c client.Client, cs clientset.Interface) ([][]byte, error) {
    32  	var logs [][]byte
    33  
    34  	pods := new(corev1.PodList)
    35  	podListOptions := &client.ListOptions{
    36  		LabelSelector: labels.SelectorFromSet(map[string]string{"app": name}),
    37  		Namespace:     ns,
    38  	}
    39  	if err := c.List(context.TODO(), pods, podListOptions); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	podLogOptions := &corev1.PodLogOptions{
    44  		Container: name,
    45  	}
    46  	for _, pod := range pods.Items {
    47  		if pod.Status.Phase == corev1.PodFailed {
    48  			continue
    49  		}
    50  		req := cs.CoreV1().Pods(ns).GetLogs(pod.Name, podLogOptions)
    51  		logStream, err := req.Stream(context.TODO())
    52  		if err != nil {
    53  			continue
    54  		}
    55  		defer logStream.Close()
    56  		logBytes, err := io.ReadAll(logStream)
    57  		if err != nil {
    58  			continue
    59  		}
    60  		logs = append(logs, logBytes)
    61  	}
    62  
    63  	return logs, nil
    64  }
    65  

View as plain text