/
annotationrefs.go
54 lines (46 loc) · 1.05 KB
/
annotationrefs.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
package backrefs
import (
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ObjectFactory func() Object
type annotationRefs struct {
ownerApiGroup string
}
func AnnotationReferences(ownerApiGroup string) BackReferenceStrategy {
return &annotationRefs{ownerApiGroup}
}
func (s *annotationRefs) DelReference(from metav1.Object, to Object) bool {
m := from.GetAnnotations()
if m == nil {
return false
}
a := s.annotation(to)
if m[a] == "true" {
delete(m, a)
from.SetAnnotations(m)
return true
}
return false
}
func (s *annotationRefs) AddReference(from metav1.Object, to Object) bool {
m := from.GetAnnotations()
if m == nil {
m = map[string]string{}
}
a := s.annotation(to)
if m[a] == "true" {
return false
}
m[a] = "true"
from.SetAnnotations(m)
return true
}
func (s *annotationRefs) annotation(o Object) string {
kind := o.GetObjectKind().GroupVersionKind().Kind
checkKind(kind)
ns := o.GetNamespace()
name := o.GetName()
return fmt.Sprintf("%s.%s/%s/%s", strings.ToLower(kind), s.ownerApiGroup, ns, name)
}