knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/clients.go (about)

     1  /*
     2  Copyright 2018 The Knative 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  // This file contains an object which encapsulates k8s clients which are useful for e2e tests.
    18  
    19  package test
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"strings"
    25  
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/client-go/kubernetes"
    29  	"k8s.io/client-go/rest"
    30  	"k8s.io/client-go/tools/clientcmd"
    31  	"knative.dev/pkg/test/logging"
    32  	"knative.dev/pkg/test/spoof"
    33  )
    34  
    35  // NewSpoofingClient returns a spoofing client to make requests
    36  func NewSpoofingClient(ctx context.Context, client kubernetes.Interface, logf logging.FormatLogger,
    37  	domain string, resolvable bool, opts ...spoof.TransportOption,
    38  ) (*spoof.SpoofingClient, error) {
    39  	return spoof.New(ctx, client, logf, domain, resolvable, Flags.IngressEndpoint,
    40  		Flags.SpoofRequestInterval, Flags.SpoofRequestTimeout, opts...)
    41  }
    42  
    43  // BuildClientConfig builds the client config specified by the config path and the cluster name
    44  func BuildClientConfig(kubeConfigPath string, clusterName string) (*rest.Config, error) {
    45  	loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
    46  	overrides := clientcmd.ConfigOverrides{}
    47  
    48  	if kubeConfigPath != "" {
    49  		loadingRules.ExplicitPath = kubeConfigPath
    50  	}
    51  	// Override the cluster name if provided.
    52  	if clusterName != "" {
    53  		overrides.Context.Cluster = clusterName
    54  	}
    55  	return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
    56  		loadingRules,
    57  		&overrides).ClientConfig()
    58  }
    59  
    60  // UpdateConfigMap updates the config map for specified @name with values
    61  func UpdateConfigMap(ctx context.Context, client kubernetes.Interface, name string, configName string, values map[string]string) error {
    62  	configMap, err := client.CoreV1().ConfigMaps(name).Get(ctx, configName, metav1.GetOptions{})
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	for key, value := range values {
    68  		configMap.Data[key] = value
    69  	}
    70  
    71  	_, err = client.CoreV1().ConfigMaps(name).Update(ctx, configMap, metav1.UpdateOptions{})
    72  	return err
    73  }
    74  
    75  // CreatePod will create a Pod
    76  func CreatePod(ctx context.Context, client kubernetes.Interface, pod *corev1.Pod) (*corev1.Pod, error) {
    77  	pods := client.CoreV1().Pods(pod.GetNamespace())
    78  	return pods.Create(ctx, pod, metav1.CreateOptions{})
    79  }
    80  
    81  // PodLogs returns Pod logs for given Pod and Container in the namespace
    82  func PodLogs(ctx context.Context, client kubernetes.Interface, podName, containerName, namespace string) ([]byte, error) {
    83  	pods := client.CoreV1().Pods(namespace)
    84  	podList, err := pods.List(ctx, metav1.ListOptions{})
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	for i := range podList.Items {
    89  		// Pods are big, so avoid copying.
    90  		pod := &podList.Items[i]
    91  		if strings.Contains(pod.Name, podName) {
    92  			result := pods.GetLogs(pod.Name, &corev1.PodLogOptions{
    93  				Container: containerName,
    94  			}).Do(ctx)
    95  			return result.Raw()
    96  		}
    97  	}
    98  	return nil, fmt.Errorf("could not find logs for %s/%s:%s", namespace, podName, containerName)
    99  }