-
Notifications
You must be signed in to change notification settings - Fork 53
/
mutators.go
47 lines (42 loc) · 1.05 KB
/
mutators.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
package storage
import (
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
)
func NewCompositeMutator[T any](mutators ...MutatorFunc[T]) MutatorFunc[T] {
return func(t T) {
for _, mutator := range mutators {
mutator(t)
}
}
}
func NewIncrementUsageCountMutator() MutatorFunc[*corev1.BootstrapToken] {
return func(obj *corev1.BootstrapToken) {
obj.Metadata.UsageCount++
}
}
func NewAddCapabilityMutator[O corev1.MetadataAccessor[T], T corev1.Capability[T]](capability T) MutatorFunc[O] {
return func(obj O) {
exists := false
for _, c := range obj.GetCapabilities() {
if c.Equal(capability) {
exists = true
break
}
}
if !exists {
obj.SetCapabilities(
append(obj.GetCapabilities(), capability))
}
}
}
func NewRemoveCapabilityMutator[O corev1.MetadataAccessor[T], T corev1.Capability[T]](capability T) MutatorFunc[O] {
return func(obj O) {
capabilities := []T{}
for _, c := range obj.GetCapabilities() {
if !c.Equal(capability) {
capabilities = append(capabilities, c)
}
}
obj.SetCapabilities(capabilities)
}
}