utils

package
v0.7.4-premiumv2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2022 License: Apache-2.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TenantIDEnv               = "AZURE_TENANT_ID"
	SubscriptionEnv           = "AZURE_SUBSCRIPTION_ID"
	ServicePrincipleIDEnv     = "AZURE_CLIENT_ID"
	ServicePrincipleSecretEnv = "AZURE_CLIENT_SECRET" // #nosec G101
	ClusterLocationEnv        = "AZURE_LOCATION"
	ClusterEnvironment        = "AZURE_ENVIRONMENT"
	LoadBalancerSkuEnv        = "AZURE_LOADBALANCER_SKU"
	// If "TEST_CCM" is true, the test is running on a CAPZ cluster.
	CAPZTestCCM = "TEST_CCM"
)

Environmental variables for validating Azure resource status.

View Source
const (
	TestSuiteLabelFeatureAutoscaling = "Feature:Autoscaling"
	TestSuiteLabelSerial             = "Serial"
	TestSuiteLabelSlow               = "Slow"
	TestSuiteLabelMultiNodePools     = "Multi-Nodepool"
	TestSuiteLabelSingleNodePool     = "Single-Nodepool"
	TestSuiteLabelVMSS               = "VMSS"
	TestSuiteLabelSpotVM             = "Spot-VM"
	TestSuiteLabelKubenet            = "Kubenet"
	TestSuiteLabelMultiGroup         = "Multi-Group"
	TestSuiteLabelAvailabilitySet    = "AvailabilitySet"
	TestSuiteLabelPrivateLinkService = "PLS"
	TestSuiteLabelSLBOutbound        = "SLBOutbound"
	TestSuiteLabelServiceAnnotation  = "ServiceAnnotation"
	TestSuiteLabelCredential         = "Credential"
	TestSuiteLabelNode               = "Node"
	TestSuiteLabelLB                 = "LB"
	TestSuiteLabelMultiPorts         = "Multi-Ports"
	TestSuiteLabelNSG                = "NSG"
)

test suite labels

View Source
const (
	ExecAgnhostPod = "exec-agnhost-pod"
)
View Source
const (

	// GPUResourceKey is the key of the GPU in the resource map of a node
	GPUResourceKey = "nvidia.com/gpu"
)

Variables

View Source
var PodIPRE = regexp.MustCompile(`\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}`)

PodIPRE tests if there's a valid IP in a easy way

Functions

func CheckPodExist

func CheckPodExist(cs clientset.Interface, ns, name string) bool

CheckPodExist checks if a Pod exists in a namespace with its name.

func CountPendingPods

func CountPendingPods(cs clientset.Interface, ns string) (int, error)

CountPendingPods counts how many pods is in the `pending` state

func CreateHostExecPod

func CreateHostExecPod(cs clientset.Interface, ns, name string) (bool, error)

CreateHostExecPod creates an Agnhost Pod to exec. It returns if the Pod is running and error.

func CreateKubeClientSet

func CreateKubeClientSet() (clientset.Interface, error)

CreateKubeClientSet obtains the client set interface from Kubeconfig

func CreateLoadBalancerServiceManifest

func CreateLoadBalancerServiceManifest(name string, annotation map[string]string, labels map[string]string, namespace string, ports []v1.ServicePort) *v1.Service

CreateLoadBalancerServiceManifest return the specific service to be created

func CreatePod

func CreatePod(cs clientset.Interface, ns string, manifest *v1.Pod) error

CreatePod creates a new pod

func CreateTestResourceGroup

func CreateTestResourceGroup(tc *AzureTestClient) (*resources.Group, func(string))

CreateTestResourceGroup create a test rg

func CreateTestingNamespace

func CreateTestingNamespace(baseName string, cs clientset.Interface) (*v1.Namespace, error)

CreateTestingNamespace builds namespace for each test baseName and labels determine name of the space

func DeleteNamespace

func DeleteNamespace(cs clientset.Interface, namespace string) error

DeleteNamespace deletes the provided namespace, waits for it to be completely deleted, and then checks whether there are any pods remaining in a non-terminating state.

func DeleteNodes

func DeleteNodes(cs clientset.Interface, names []string) error

DeleteNodes ensures a list of nodes to be deleted

func DeletePIPPrefixWithRetry

func DeletePIPPrefixWithRetry(cli *AzureTestClient, name string) error

func DeletePIPWithRetry

func DeletePIPWithRetry(azureTestClient *AzureTestClient, ipName, rgName string) error

DeletePIPWithRetry tries to delete a public ip resource

func DeletePod

func DeletePod(cs clientset.Interface, ns string, podName string) error

DeletePod deletes a single pod

func DeletePodsInNamespace

func DeletePodsInNamespace(cs clientset.Interface, ns string) error

DeletePodsInNamespace deletes all pods in the namespace

func DeleteService

func DeleteService(cs clientset.Interface, ns string, serviceName string) error

DeleteService deletes a service

func DeleteServiceIfExists

func DeleteServiceIfExists(cs clientset.Interface, ns string, serviceName string) error

DeleteServiceIfExists deletes a service if it exists, return nil if not exists

func DockerLogin

func DockerLogin(registryName string) (err error)

DockerLogin execute the `docker login` if docker is available

func DockerLogout

func DockerLogout() (err error)

DockerLogout execute the `docker logout` if docker is available

func ExtractDNSPrefix

func ExtractDNSPrefix() string

ExtractDNSPrefix obtains the cluster DNS prefix

func FindTestVMSS

func FindTestVMSS(tc *AzureTestClient, rgName string) (*azcompute.VirtualMachineScaleSet, error)

FindTestVMSS returns the first VMSS in the resource group, assume the VMSS is in the cluster

func GetAgentNodes

func GetAgentNodes(cs clientset.Interface) ([]v1.Node, error)

GetAgentNodes obtains the list of agent nodes

func GetAllNodes

func GetAllNodes(cs clientset.Interface) ([]v1.Node, error)

GetAllNodes obtains the list of all nodes include master

func GetGPUResource

func GetGPUResource(node *v1.Node) (bool, int64)

GetGPUResource checks whether the node can provide GPU resource. If so, returns the capacity.

func GetMaster

func GetMaster(cs clientset.Interface) (*v1.Node, error)

GetMaster returns the master node

func GetNICByID

func GetNICByID(nicID string, nicList *[]network.Interface) (*network.Interface, error)

GetNICByID returns the network interface with the input ID among the list

func GetNextSubnetCIDR

func GetNextSubnetCIDR(vnet aznetwork.VirtualNetwork) (string, error)

GetNextSubnetCIDR obtains a new ip address which has no overlap with existing subnets.

func GetNicIDsFromVM

func GetNicIDsFromVM(vm compute.VirtualMachine) (map[string]interface{}, error)

GetNicIDsFromVM returns the NIC ID in the VM

func GetNicIDsFromVMSSVM

func GetNicIDsFromVMSSVM(vm compute.VirtualMachineScaleSetVM) (map[string]interface{}, error)

GetNicIDsFromVMSSVM returns the NIC ID in the VMSS VM

func GetNode

func GetNode(cs clientset.Interface, nodeName string) (*v1.Node, error)

GetNode returns the node with the input name

func GetNodeResourceGroup

func GetNodeResourceGroup(node *v1.Node) (string, error)

GetNodeResourceGroup returns the resource group of the given node

func GetNodeRunningQuantity

func GetNodeRunningQuantity(cs clientset.Interface, nodeName string) (resource.Quantity, error)

GetNodeRunningQuantity will calculate the overall quantity of cpu requested by all running pods in all namespaces on the node

func GetNodepoolNodeMap

func GetNodepoolNodeMap(nodes *[]v1.Node) map[string][]string

func GetNodesInRouteTable

func GetNodesInRouteTable(routeTable aznetwork.RouteTable) (map[string]interface{}, error)

GetNodesInRouteTable returns all the nodes in the route table

func GetPod

func GetPod(cs clientset.Interface, ns, name string) (pod *v1.Pod, err error)

GetPod returns a Pod with namespace and name.

func GetPodList

func GetPodList(cs clientset.Interface, ns string) (*v1.PodList, error)

GetPodList is a wrapper around listing pods

func GetPodLogs

func GetPodLogs(cs clientset.Interface, ns, podName string, opts *v1.PodLogOptions) ([]byte, error)

GetPodLogs gets the log of the given pods

func GetPodOutboundIP

func GetPodOutboundIP(cs clientset.Interface, podTemplate *v1.Pod, nsName string) (string, error)

GetPodOutboundIP returns the outbound IP of the given pod

func GetServiceDomainName

func GetServiceDomainName(prefix string) (ret string)

GetServiceDomainName cat prefix and azure suffix

func GetTargetNICFromList

func GetTargetNICFromList(list *[]network.Interface, targetVMNamePrefix string) (*network.Interface, error)

GetTargetNICFromList pick the target virtual machine's NIC from the given NIC list

func GetVMComputerName

func GetVMComputerName(vm compute.VirtualMachine) (string, error)

GetVMComputerName returns the corresponding node name of the VM

func GetVMSS

GetVMSS gets VMSS object with vmssName.

func GetVMSSVMComputerName

func GetVMSSVMComputerName(vm azcompute.VirtualMachineScaleSetVM) (string, error)

GetVMSSVMComputerName returns the corresponding node name of the VMSS VM

func IsControlPlaneNode

func IsControlPlaneNode(node *v1.Node) bool

IsControlPlaneNode returns true if the node has a control-plane role label. The control-plane role is determined by looking for: * a node-role.kubernetes.io/control-plane or node-role.kubernetes.io/master="" label

func IsInternalEndpoint

func IsInternalEndpoint(ip string) bool

func IsNodeInVMSS

func IsNodeInVMSS(tc *AzureTestClient, nodeName, vmssName string) (bool, error)

IsNodeInVMSS defines whether the node is the instance of the VMSS

func IsRetryableAPIError

func IsRetryableAPIError(err error) bool

IsRetryableAPIError will judge whether an error retryable or not

func IsSpotVMSS

func IsSpotVMSS(vmss azcompute.VirtualMachineScaleSet) bool

IsSpotVMSS checks whether the vmss support azure spot vm instance

func KubectlCmd

func KubectlCmd(namespace string, args ...string) *exec.Cmd

KubectlCmd runs the kubectl executable through the wrapper script.

func LabelNode

func LabelNode(cs clientset.Interface, node *v1.Node, label string, isDelete bool) (*v1.Node, error)

func ListNICs

func ListNICs(tc *AzureTestClient, rgName string) (*[]network.Interface, error)

ListNICs returns the NIC list in the given resource group

func ListRouteTables

func ListRouteTables(tc *AzureTestClient) (*[]aznetwork.RouteTable, error)

ListRouteTables returns the list of all route tables in the resource group

func ListVMSSNICs

func ListVMSSNICs(tc *AzureTestClient, vmssName string) (*[]network.Interface, error)

ListVMSSNICs returns the NIC list in the VMSS

func ListVMSSVMs

func ListVMSSVMs(tc *AzureTestClient, vmssName string) ([]azcompute.VirtualMachineScaleSetVM, error)

ListVMSSVMs returns the VM list of the given VMSS

func ListVMSSes

ListVMSSes returns the list of scale sets

func ListVMs

func ListVMs(tc *AzureTestClient) (*[]compute.VirtualMachine, error)

ListVMs returns all VMs in the resource group

func LogPodStatus

func LogPodStatus(cs clientset.Interface, ns string) error

LogPodStatus logs the rate of pending

func Logf

func Logf(format string, args ...interface{})

Logf prints info logs

func LookForStringInPodExec

func LookForStringInPodExec(ns, podName string, command []string, expectedString string, timeout time.Duration) (result string, err error)

LookForStringInPodExec looks for the given string in the output of a command executed in the first container of specified pod.

func LookForStringInPodExecToContainer

func LookForStringInPodExecToContainer(ns, podName, containerName string, command []string, expectedString string, timeout time.Duration) (result string, err error)

LookForStringInPodExecToContainer looks for the given string in the output of a command executed in specified pod container, or first container if not specified.

func PushImageToACR

func PushImageToACR(registryName, image string) (tag string, err error)

PushImageToACR pull an image from Docker Hub and push it to the given azure container registry

func RunKubectl

func RunKubectl(namespace string, args ...string) (string, error)

RunKubectl is a convenience wrapper over kubectlBuilder

func RunKubectlOrDie

func RunKubectlOrDie(namespace string, args ...string) string

RunKubectlOrDie is a convenience wrapper over kubectlBuilder

func ScaleMachinePool

func ScaleMachinePool(vmssName string, instanceCount int64) error

ScaleMachinePool switches to kind-capz context and scales MachinePool replicas to scale up or down VMSS. This functions is for CAPZ clusters. Since CAPZ controller will reconcile MachinePool replicas automatically, add/delete VMSS instance through VMSS API doesn't work.

func ScaleVMSS

func ScaleVMSS(tc *AzureTestClient, vmssName, rgName string, instanceCount int64) (err error)

ScaleVMSS scales the given VMSS

func SelectAvailablePrivateIP

func SelectAvailablePrivateIP(tc *AzureTestClient) (string, error)

SelectAvailablePrivateIP selects a private IP address in Azure subnet.

func StringInSlice

func StringInSlice(s string, list []string) bool

StringInSlice check if string in a list

func ValidateClusterNodesMatchVMSSInstances

func ValidateClusterNodesMatchVMSSInstances(tc *AzureTestClient, expectedCap map[string]int64) error

func ValidateIPInCIDR

func ValidateIPInCIDR(ip, cidr string) (bool, error)

ValidateIPInCIDR validates whether certain ip fits CIDR

func ValidateServiceConnectivity

func ValidateServiceConnectivity(ns, execPod, serviceIP string, port int, protocol v1.Protocol) error

ValidateServiceConnectivity validates the connectivity of the internal Service IP

func ValidateVMSSNodeLabels

func ValidateVMSSNodeLabels(tc *AzureTestClient, vmss *azcompute.VirtualMachineScaleSet, key string) error

ValidateVMSSNodeLabels gets the label of VMs in VMSS with retry

func WaitAutoScaleNodes

func WaitAutoScaleNodes(cs clientset.Interface, targetNodeCount int, isScaleDown bool) error

WaitAutoScaleNodes returns nodes count after autoscaling in 30 minutes

func WaitCreatePIP

func WaitCreatePIP(azureTestClient *AzureTestClient, ipName, rgName string, ipParameter aznetwork.PublicIPAddress) (aznetwork.PublicIPAddress, error)

WaitCreatePIP waits to create a public ip resource in a specific resource group

func WaitCreatePIPPrefix

func WaitCreatePIPPrefix(
	cli *AzureTestClient,
	name, rgName string,
	parameter aznetwork.PublicIPPrefix,
) (aznetwork.PublicIPPrefix, error)

func WaitForDeleteResourceGroupCompletion

func WaitForDeleteResourceGroupCompletion(gc *resources.GroupsClient, future resources.GroupsDeleteFuture, rgName string) error

WaitForDeleteResourceGroupCompletion waits for delete group operations to finish

func WaitGetAgentNodes

func WaitGetAgentNodes(cs clientset.Interface) (nodes []v1.Node, err error)

WaitGetAgentNodes gets the list of agent nodes and ensures the providerIDs are good

func WaitGetPIP

func WaitGetPIP(azureTestClient *AzureTestClient, ipName string) (pip aznetwork.PublicIPAddress, err error)

WaitGetPIP waits to get a specific public ip resource

func WaitGetPIPByPrefix

func WaitGetPIPByPrefix(
	cli *AzureTestClient,
	prefixName string,
	untilPIPCreated bool,
) (network.PublicIPAddress, error)

WaitGetPIPByPrefix retrieves the ONLY one PIP that created by specified prefix. If untilPIPCreated is true, it will retry until 1 PIP is associated to the prefix.

func WaitGetPIPPrefix

func WaitGetPIPPrefix(
	cli *AzureTestClient,
	name string,
) (aznetwork.PublicIPPrefix, error)

func WaitPodTo

func WaitPodTo(phase v1.PodPhase, cs clientset.Interface, podTemplate *v1.Pod, nsName string) (result bool, err error)

WaitPodTo returns True if pod is in the specific phase during a short period of time

func WaitPodsToBeReady

func WaitPodsToBeReady(cs clientset.Interface, ns string) error

func WaitServiceExposure

func WaitServiceExposure(cs clientset.Interface, namespace string, name string, targetIP string) (*v1.Service, error)

WaitServiceExposure waits for the exposure of the external IP of the service

func WaitServiceExposureAndGetIP

func WaitServiceExposureAndGetIP(cs clientset.Interface, namespace string, name string) (string, error)

WaitServiceExposureAndGetIP returns IP of the Service.

func WaitServiceExposureAndValidateConnectivity

func WaitServiceExposureAndValidateConnectivity(cs clientset.Interface, namespace string, name string, targetIP string) (string, error)

WaitServiceExposureAndValidateConnectivity returns ip of the service and check the connectivity if it is a public IP

Types

type AzureAuthConfig

type AzureAuthConfig struct {
	// The AAD Tenant ID for the Subscription that the cluster is deployed in
	TenantID string
	// The ClientID for an AAD application with RBAC access to talk to Azure RM APIs
	AADClientID string
	// The ClientSecret for an AAD application with RBAC access to talk to Azure RM APIs
	AADClientSecret string
	// The ID of the Azure Subscription that the cluster is deployed in
	SubscriptionID string
	// The Environment represents a set of endpoints for each of Azure's Clouds.
	Environment azure.Environment
}

AzureAuthConfig holds auth related part of cloud config Only consider servicePrinciple now

type AzureTestClient

type AzureTestClient struct {
	// contains filtered or unexported fields
}

AzureTestClient configs Azure specific clients

func CreateAzureTestClient

func CreateAzureTestClient() (*AzureTestClient, error)

CreateAzureTestClient makes a new AzureTestClient Only consider PublicCloud Environment

func (*AzureTestClient) AssignRoleToACR

func (tc *AzureTestClient) AssignRoleToACR(registryName, roleDefinitionID string) (err error)

AssignRoleToACR assigns the role to acr by roleDefinitionID

func (*AzureTestClient) CreateContainerRegistry

func (tc *AzureTestClient) CreateContainerRegistry() (registry acr.Registry, err error)

CreateContainerRegistry creates a test acr

func (*AzureTestClient) CreateSecurityGroupsClient

func (tc *AzureTestClient) CreateSecurityGroupsClient() *aznetwork.SecurityGroupsClient

CreateSecurityGroupsClient generates security group client with the same baseclient as azure test client

func (*AzureTestClient) CreateSubnet

func (azureTestClient *AzureTestClient) CreateSubnet(vnet aznetwork.VirtualNetwork, subnetName *string, prefix *string, waitUntilComplete bool) (network.Subnet, error)

CreateSubnet creates a new subnet in the specified virtual network.

func (*AzureTestClient) DeleteContainerRegistry

func (tc *AzureTestClient) DeleteContainerRegistry(registryName string) (err error)

DeleteContainerRegistry deletes an existing acr

func (*AzureTestClient) DeleteSubnet

func (azureTestClient *AzureTestClient) DeleteSubnet(vnetName string, subnetName string) error

DeleteSubnet deletes a subnet with retry.

func (*AzureTestClient) GetAuthConfig

func (tc *AzureTestClient) GetAuthConfig() AzureAuthConfig

GetAuthConfig gets the authorization configuration information

func (*AzureTestClient) GetClusterSecurityGroups

func (azureTestClient *AzureTestClient) GetClusterSecurityGroups() (ret []aznetwork.SecurityGroup, err error)

GetClusterSecurityGroups gets the security groups of the cluster.

func (*AzureTestClient) GetClusterVirtualNetwork

func (azureTestClient *AzureTestClient) GetClusterVirtualNetwork() (virtualNetwork aznetwork.VirtualNetwork, err error)

GetClusterVirtualNetwork returns the cluster's virtual network.

func (*AzureTestClient) GetLoadBalancer

func (azureTestClient *AzureTestClient) GetLoadBalancer(resourceGroupName, lbName string) (aznetwork.LoadBalancer, error)

GetLoadBalancer gets aznetwork.LoadBalancer by loadBalancer name.

func (*AzureTestClient) GetLocation

func (tc *AzureTestClient) GetLocation() string

GetLocation get location which is same of cluster name as definite in k8s-azure

func (*AzureTestClient) GetPrivateLinkService

func (azureTestClient *AzureTestClient) GetPrivateLinkService(resourceGroupName, plsName string) (aznetwork.PrivateLinkService, error)

GetPrivateLinkService gets aznetwork.PrivateLinkService by privateLinkService name.

func (*AzureTestClient) GetResourceGroup

func (tc *AzureTestClient) GetResourceGroup() string

GetResourceGroup get RG name which is same of cluster name as definite in k8s-azure

func (*AzureTestClient) ListLoadBalancers

func (azureTestClient *AzureTestClient) ListLoadBalancers(resourceGroupName string) ([]aznetwork.LoadBalancer, error)

ListLoadBalancers lists all the load balancers active

func (*AzureTestClient) ListPrivateLinkServices

func (azureTestClient *AzureTestClient) ListPrivateLinkServices(resourceGroupName string) ([]aznetwork.PrivateLinkService, error)

ListPrivateLinkServices lists all the private link services active

func (*AzureTestClient) ListPublicIPs

func (azureTestClient *AzureTestClient) ListPublicIPs(resourceGroupName string) ([]aznetwork.PublicIPAddress, error)

ListPublicIPs lists all the publicIP addresses active

type KubectlBuilder

type KubectlBuilder struct {
	// contains filtered or unexported fields
}

KubectlBuilder is used to build, customize and execute a kubectl Command. Add more functions to customize the builder as needed.

func NewKubectlCommand

func NewKubectlCommand(namespace string, args ...string) *KubectlBuilder

NewKubectlCommand returns a KubectlBuilder for running kubectl.

func (KubectlBuilder) Exec

func (b KubectlBuilder) Exec() (string, error)

Exec runs the kubectl executable.

func (KubectlBuilder) ExecOrDie

func (b KubectlBuilder) ExecOrDie(namespace string) string

ExecOrDie runs the kubectl executable or dies if error occurs.

func (KubectlBuilder) ExecWithFullOutput

func (b KubectlBuilder) ExecWithFullOutput() (string, string, error)

ExecWithFullOutput runs the kubectl executable, and returns the stdout and stderr.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL