github.com/kiali/kiali@v1.84.0/business/checkers/common/workload_selector_checker.go (about)

     1  package common
     2  
     3  import (
     4  	"k8s.io/apimachinery/pkg/labels"
     5  
     6  	"github.com/kiali/kiali/models"
     7  )
     8  
     9  type GenericNoWorkloadFoundChecker struct {
    10  	SubjectType           string
    11  	SelectorLabels        map[string]string
    12  	WorkloadsPerNamespace map[string]models.WorkloadList
    13  	Path                  string
    14  }
    15  
    16  func SelectorNoWorkloadFoundChecker(subjectType string, selectorLabels map[string]string, workloadsPerNamespace map[string]models.WorkloadList) GenericNoWorkloadFoundChecker {
    17  	return GenericNoWorkloadFoundChecker{
    18  		SubjectType:           subjectType,
    19  		SelectorLabels:        selectorLabels,
    20  		WorkloadsPerNamespace: workloadsPerNamespace,
    21  		Path:                  "spec/selector/matchLabels",
    22  	}
    23  }
    24  
    25  func WorkloadSelectorNoWorkloadFoundChecker(subjectType string, selectorLabels map[string]string, workloadsPerNamespace map[string]models.WorkloadList) GenericNoWorkloadFoundChecker {
    26  	return GenericNoWorkloadFoundChecker{
    27  		SubjectType:           subjectType,
    28  		SelectorLabels:        selectorLabels,
    29  		WorkloadsPerNamespace: workloadsPerNamespace,
    30  		Path:                  "spec/workloadSelector/labels",
    31  	}
    32  }
    33  
    34  func (wsc GenericNoWorkloadFoundChecker) Check() ([]*models.IstioCheck, bool) {
    35  	checks := make([]*models.IstioCheck, 0)
    36  
    37  	if len(wsc.SelectorLabels) > 0 {
    38  		if !wsc.hasMatchingWorkload(wsc.SelectorLabels) {
    39  			check := models.Build("generic.selector.workloadnotfound", wsc.Path)
    40  			checks = append(checks, &check)
    41  		}
    42  	}
    43  	return checks, true
    44  }
    45  
    46  func (wsc GenericNoWorkloadFoundChecker) hasMatchingWorkload(labelSelector map[string]string) bool {
    47  	selector := labels.SelectorFromSet(labelSelector)
    48  
    49  	for _, wls := range wsc.WorkloadsPerNamespace {
    50  		for _, wl := range wls.Workloads {
    51  			wlLabelSet := labels.Set(wl.Labels)
    52  			if selector.Matches(wlLabelSet) {
    53  				return true
    54  			}
    55  		}
    56  	}
    57  	return false
    58  }