-
Notifications
You must be signed in to change notification settings - Fork 143
/
labels.go
80 lines (73 loc) · 2.41 KB
/
labels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package controller
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"sigs.k8s.io/controller-runtime/pkg/predicate"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/credentials"
)
// GetShardPredicate constructs a predicate used as an event filter for various
// reconcilers. If a non-empty shard name is passed to this function, it returns
// a predicate that matches ONLY resources labeled for that shard. If an empty
// shard name is passed to this function, it returns a predicate that matches
// ONLY resources that are NOT labeled for ANY shard.
func GetShardPredicate(shard string) (predicate.Predicate, error) {
if shard == "" {
pred, err := predicate.LabelSelectorPredicate(
metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: kargoapi.ShardLabelKey,
Operator: metav1.LabelSelectorOpDoesNotExist,
},
},
},
)
if err != nil {
return nil, fmt.Errorf("error creating default selector predicate: %w", err)
}
return pred, nil
}
pred, err := predicate.LabelSelectorPredicate(
*metav1.SetAsLabelSelector(
labels.Set(
map[string]string{
kargoapi.ShardLabelKey: shard,
},
),
),
)
if err != nil {
return nil, fmt.Errorf("error creating shard selector predicate: %w", err)
}
return pred, nil
}
func GetShardRequirement(shard string) (*labels.Requirement, error) {
req, err := labels.NewRequirement(kargoapi.ShardLabelKey, selection.Equals, []string{shard})
if err != nil {
return nil, fmt.Errorf("error creating shard label selector: %w", err)
}
if shard == "" {
req, err = labels.NewRequirement(kargoapi.ShardLabelKey, selection.DoesNotExist, nil)
if err != nil {
return nil, fmt.Errorf("error creating default label selector: %w", err)
}
}
return req, nil
}
// GetCredentialsRequirement returns a label requirement that matches only
// resources that have a credential type label set to one of the supported
// credential types.
func GetCredentialsRequirement() (*labels.Requirement, error) {
req, err := labels.NewRequirement(kargoapi.CredentialTypeLabelKey, selection.In, []string{
credentials.TypeGit.String(),
credentials.TypeHelm.String(),
credentials.TypeImage.String(),
})
if err != nil {
return nil, fmt.Errorf("error creating credentials label selector: %w", err)
}
return req, nil
}