File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package selector
2+
3+ import (
4+ "fmt"
5+
6+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+ )
8+
9+ const (
10+ SelectorError = "<error>"
11+ SelectorNone = "<none>"
12+ )
13+
14+ type (
15+ KubernetesSelector struct {
16+ metav1.LabelSelector `json:",inline"`
17+ selector * string
18+ }
19+ )
20+
21+ // IsEmpty checks if the selector is empty/defined or not
22+ func (selector * KubernetesSelector ) IsEmpty () bool {
23+ if selector == nil || (len (selector .MatchLabels ) == 0 && len (selector .MatchExpressions ) == 0 ) {
24+ return true
25+ }
26+
27+ return false
28+ }
29+
30+ // Compile compiles the label selector struct to a string
31+ func (selector * KubernetesSelector ) Compile () (string , error ) {
32+ // no selector
33+ if selector .IsEmpty () {
34+ return "" , nil
35+ }
36+
37+ if selector .selector == nil {
38+ labelSelector := metav1 .FormatLabelSelector (& selector .LabelSelector )
39+
40+ switch labelSelector {
41+ case SelectorError :
42+ return "" , fmt .Errorf (`unable to compile Kubernetes selector for resource: %v` , selector )
43+ case SelectorNone :
44+ labelSelector = ""
45+ }
46+
47+ selector .selector = & labelSelector
48+ }
49+
50+ return * selector .selector , nil
51+ }
You can’t perform that action at this time.
0 commit comments