/
service.go
60 lines (50 loc) · 1.41 KB
/
service.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
package instanceid
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/argo-workflows/v3/util/labels"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
type Service interface {
Label(obj metav1.Object)
With(options *metav1.ListOptions)
Validate(obj metav1.Object) error
InstanceID() string
}
func NewService(instanceID string) Service {
return &service{instanceID}
}
type service struct {
instanceID string
}
func (s *service) InstanceID() string {
return s.instanceID
}
func (s *service) Label(obj metav1.Object) {
if s.instanceID != "" {
labels.Label(obj, common.LabelKeyControllerInstanceID, s.instanceID)
} else {
labels.UnLabel(obj, common.LabelKeyControllerInstanceID)
}
}
func (s *service) With(opts *metav1.ListOptions) {
if len(opts.LabelSelector) > 0 {
opts.LabelSelector += ","
}
if s.instanceID == "" {
opts.LabelSelector += fmt.Sprintf("!%s", common.LabelKeyControllerInstanceID)
} else {
opts.LabelSelector += fmt.Sprintf("%s=%s", common.LabelKeyControllerInstanceID, s.instanceID)
}
}
func (s *service) Validate(obj metav1.Object) error {
l := obj.GetLabels()
if s.instanceID == "" {
if _, ok := l[common.LabelKeyControllerInstanceID]; !ok {
return nil
}
} else if val, ok := l[common.LabelKeyControllerInstanceID]; ok && val == s.instanceID {
return nil
}
return fmt.Errorf("'%s' is not managed by the current Argo Server", obj.GetName())
}