github.com/kiali/kiali@v1.84.0/business/registry_status.go (about)

     1  package business
     2  
     3  import (
     4  	"k8s.io/apimachinery/pkg/labels"
     5  
     6  	"github.com/kiali/kiali/kubernetes"
     7  	"github.com/kiali/kiali/kubernetes/cache"
     8  	"github.com/kiali/kiali/log"
     9  )
    10  
    11  type RegistryStatusService struct {
    12  	kialiCache cache.KialiCache
    13  }
    14  
    15  type RegistryCriteria struct {
    16  	// When AllNamespaces is true Namespace criteria is ignored
    17  	// Note this flag is only supported in Registry queries
    18  	AllNamespaces   bool
    19  	Cluster         string
    20  	Namespace       string
    21  	ServiceName     string
    22  	ServiceSelector string
    23  }
    24  
    25  func (in *RegistryStatusService) GetRegistryServices(criteria RegistryCriteria) []*kubernetes.RegistryService {
    26  	registryStatus := kialiCache.GetRegistryStatus(criteria.Cluster)
    27  	registryServices := filterRegistryServices(registryStatus, criteria)
    28  	return registryServices
    29  }
    30  
    31  func filterRegistryServices(registryStatus *kubernetes.RegistryStatus, criteria RegistryCriteria) []*kubernetes.RegistryService {
    32  	var filteredRegistryServices []*kubernetes.RegistryService
    33  	if registryStatus == nil {
    34  		return filteredRegistryServices
    35  	}
    36  	if criteria.AllNamespaces {
    37  		return registryStatus.Services
    38  	}
    39  	if criteria.Namespace != "" {
    40  		for _, rService := range registryStatus.Services {
    41  			if rService.Attributes.Namespace == criteria.Namespace {
    42  				filteredRegistryServices = append(filteredRegistryServices, rService)
    43  			}
    44  		}
    45  		if criteria.ServiceSelector != "" {
    46  			if selector, err3 := labels.ConvertSelectorToLabelsMap(criteria.ServiceSelector); err3 == nil {
    47  				var filteredSelectorServices []*kubernetes.RegistryService
    48  				for _, rService := range filteredRegistryServices {
    49  					svcSelector := labels.Set(rService.Attributes.LabelSelectors).AsSelector()
    50  					if !svcSelector.Empty() && svcSelector.Matches(selector) {
    51  						filteredSelectorServices = append(filteredSelectorServices, rService)
    52  					}
    53  				}
    54  				return filteredSelectorServices
    55  			} else {
    56  				log.Warningf("Services not filtered. Selector %s not valid", criteria.ServiceSelector)
    57  			}
    58  		}
    59  	}
    60  	return filteredRegistryServices
    61  }