github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/pkg/loggingtrait.go (about)

     1  // Copyright (c) 2020, 2021, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package pkg
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/types"
    12  	"strings"
    13  )
    14  
    15  func DoesLoggingSidecarExist(kubeconfigPath string, name types.NamespacedName, containerName string) (bool, error) {
    16  	clientset, err := GetKubernetesClientsetForCluster(kubeconfigPath)
    17  	if err != nil {
    18  		Log(Error, fmt.Sprintf("Could not get the clientset from the kubeconfig: %v", err))
    19  		return false, err
    20  	}
    21  	podList, err := clientset.CoreV1().Pods(name.Namespace).List(context.TODO(), metav1.ListOptions{})
    22  	if err != nil {
    23  		Log(Error, fmt.Sprintf("Could not List the application pod from the given namespace: %v", err))
    24  		return false, err
    25  	}
    26  	var appPod v1.Pod
    27  	for _, pod := range podList.Items {
    28  		if strings.HasPrefix(pod.Name, name.Name) {
    29  			appPod = pod
    30  		}
    31  	}
    32  	if appPod.Name == "" {
    33  		Log(Error, fmt.Sprintf("Could not find the pod with the given name and namespace: %v", err))
    34  		return false, nil
    35  	}
    36  	for _, container := range appPod.Spec.Containers {
    37  		if container.Name == containerName {
    38  			Log(Info, fmt.Sprintf("Container %v found", containerName))
    39  			return true, nil
    40  		}
    41  	}
    42  	Log(Info, fmt.Sprintf("Container was NOT found for the pod %v", name.Name))
    43  	return false, nil
    44  }