forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
73 lines (56 loc) · 2.15 KB
/
template.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
package podsecuritypolicy
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)
const psptpbByPSPTNameIndex = "something.something.psptpb/pspt-name"
func Register(ctx context.Context, management *config.ManagementContext) {
psptpbInformer := management.Management.PodSecurityPolicyTemplateProjectBindings("").Controller().Informer()
psptpbIndexers := map[string]cache.IndexFunc{
psptpbByPSPTNameIndex: PSPTPBByPSPTName,
}
psptpbInformer.AddIndexers(psptpbIndexers)
lifecycle := &lifecycle{
psptpbs: management.Management.PodSecurityPolicyTemplateProjectBindings(""),
psptpbLister: management.Management.PodSecurityPolicyTemplateProjectBindings("").Controller().Lister(),
psptpbIndexer: psptpbInformer.GetIndexer(),
}
management.Management.PodSecurityPolicyTemplates("").AddLifecycle(ctx, "mgmt-pspt-lfc-handler", lifecycle)
}
type lifecycle struct {
psptpbs v3.PodSecurityPolicyTemplateProjectBindingInterface
psptpbLister v3.PodSecurityPolicyTemplateProjectBindingLister
psptpbIndexer cache.Indexer
}
func (l *lifecycle) Create(obj *v3.PodSecurityPolicyTemplate) (runtime.Object, error) {
return obj, nil
}
func (l *lifecycle) Updated(obj *v3.PodSecurityPolicyTemplate) (runtime.Object, error) {
return obj, nil
}
func (l *lifecycle) Remove(obj *v3.PodSecurityPolicyTemplate) (runtime.Object, error) {
psptpbs, err := l.psptpbIndexer.ByIndex(psptpbByPSPTNameIndex, obj.Name)
if err != nil {
return nil, fmt.Errorf("error getting psptpbs: %v", err)
}
for _, rawPSPTPB := range psptpbs {
psptpb := rawPSPTPB.(*v3.PodSecurityPolicyTemplateProjectBinding)
err := l.psptpbs.DeleteNamespaced(psptpb.Namespace, psptpb.Name, &v1.DeleteOptions{})
if err != nil {
return nil, fmt.Errorf("error deleting psptpb: %v", err)
}
}
return obj, nil
}
func PSPTPBByPSPTName(obj interface{}) ([]string, error) {
psptpb, ok := obj.(*v3.PodSecurityPolicyTemplateProjectBinding)
if !ok {
return []string{}, nil
}
return []string{psptpb.PodSecurityPolicyTemplateName}, nil
}