/
identity_annotation.go
72 lines (58 loc) · 1.79 KB
/
identity_annotation.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
package resources
import (
"strings"
)
const (
// Version annotation is used to indicate deployed resource version,
// but is also used to determine transientiveness (kapp deploy vs some controller)
kappIdentityAnnKey = "kapp.k14s.io/identity"
kappIdentityAnnV1 = "v1"
)
type IdentityAnnotation struct {
resource Resource
}
func NewIdentityAnnotation(resource Resource) IdentityAnnotation {
return IdentityAnnotation{resource}
}
// Valid returns true if signature matches resource itself
func (a IdentityAnnotation) Valid() bool {
pieces := strings.Split(a.resource.Annotations()[kappIdentityAnnKey], ";")
switch pieces[0] {
case kappIdentityAnnV1:
if len(pieces) != 3 {
return false
}
return NewUniqueResourceKey(a.resource).String() == pieces[1]
default:
return false
}
}
// MatchesVersion returns true if annotation is valid and it matches version
func (a IdentityAnnotation) MatchesVersion() bool {
if !a.Valid() {
return false
}
pieces := strings.Split(a.resource.Annotations()[kappIdentityAnnKey], ";")
switch pieces[0] {
case kappIdentityAnnV1:
return a.resource.APIVersion() == pieces[2]
default:
return false
}
}
func (a IdentityAnnotation) v1Value() string {
return kappIdentityAnnV1 + ";" + NewUniqueResourceKey(a.resource).String() + ";" + a.resource.APIVersion()
}
func (a IdentityAnnotation) AddMod() StringMapAppendMod {
return StringMapAppendMod{
ResourceMatcher: AllResourceMatcher{},
Path: NewPathFromStrings([]string{"metadata", "annotations"}),
KVs: map[string]string{kappIdentityAnnKey: a.v1Value()},
}
}
func (a IdentityAnnotation) RemoveMod() FieldRemoveMod {
return FieldRemoveMod{
ResourceMatcher: AllResourceMatcher{},
Path: NewPathFromStrings([]string{"metadata", "annotations", kappIdentityAnnKey}),
}
}