github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/common/resource_quota.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	corev1 "k8s.io/api/core/v1"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  // GetResourceQuota returns the ResourceQuota object from a given namespace and ResourceQuota name
    12  func (s *SuiteController) GetResourceQuota(namespace, ResourceQuotaName string) (*corev1.ResourceQuota, error) {
    13  	return s.KubeInterface().CoreV1().ResourceQuotas(namespace).Get(context.Background(), ResourceQuotaName, metav1.GetOptions{})
    14  }
    15  
    16  // GetResourceQuotaInfo returns the available resources and its usage in a given test, namespace, and ResourceQuota name
    17  func (s *SuiteController) GetResourceQuotaInfo(test, namespace, resourceQuotaName string) error {
    18  	rq, err := s.GetResourceQuota(namespace, resourceQuotaName)
    19  	if err != nil {
    20  		GinkgoWriter.Printf("failed to get ResourceQuota %s in namespace %s: %v\n", resourceQuotaName, namespace, err)
    21  		return err
    22  	}
    23  
    24  	notFound := false
    25  	available := rq.Status.Hard
    26  	used := rq.Status.Used
    27  
    28  	for resourceName, availableQuantity := range available {
    29  		if usedQuantity, ok := used[resourceName]; ok {
    30  			GinkgoWriter.Printf("test: %s, namespace: %s, resourceQuota: %s, resource: %s, available: %s, used: %s\n", test, namespace, resourceQuotaName, resourceName, availableQuantity.String(), usedQuantity.String())
    31  		} else {
    32  			notFound = true
    33  		}
    34  	}
    35  
    36  	// something went wrong
    37  	if len(available) == 0 || len(used) == 0 || notFound {
    38  		GinkgoWriter.Printf("test: %s, namespace: %s, resourceQuota: %s, available resources: %s, used resources: %s\n", test, namespace, resourceQuotaName, available, used)
    39  	}
    40  
    41  	return nil
    42  }