Skip to content

Commit 123cc2a

Browse files
committed
add kubernetes
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 05c9a0a commit 123cc2a

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

kubernetes/selector/selector.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)