forked from open-cluster-management-io/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinformer.go
More file actions
131 lines (108 loc) · 3.36 KB
/
Copy pathinformer.go
File metadata and controls
131 lines (108 loc) · 3.36 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package store
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/utils"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
)
// AgentInformerWatcherStore extends the BaseClientWatchStore.
// It gets/lists the resources from the given local store and send
// the resource add/update/delete event to the watch channel directly.
//
// It is used for building resource agent client.
type AgentInformerWatcherStore[T generic.ResourceObject] struct {
BaseClientWatchStore[T]
Watcher *Watcher
}
func NewAgentInformerWatcherStore[T generic.ResourceObject]() *AgentInformerWatcherStore[T] {
return &AgentInformerWatcherStore[T]{
BaseClientWatchStore: BaseClientWatchStore[T]{
Store: cache.NewStore(cache.MetaNamespaceKeyFunc),
},
Watcher: NewWatcher(),
}
}
func (s *AgentInformerWatcherStore[T]) Add(resource runtime.Object) error {
s.Watcher.Receive(watch.Event{Type: watch.Added, Object: resource})
return s.Store.Add(resource)
}
func (s *AgentInformerWatcherStore[T]) Update(resource runtime.Object) error {
s.Watcher.Receive(watch.Event{Type: watch.Modified, Object: resource})
return s.Store.Update(resource)
}
func (s *AgentInformerWatcherStore[T]) Delete(resource runtime.Object) error {
s.Watcher.Receive(watch.Event{Type: watch.Deleted, Object: resource})
return s.Store.Delete(resource)
}
func (s *AgentInformerWatcherStore[T]) HandleReceivedResource(ctx context.Context, action types.ResourceAction, resource T) error {
logger := klog.FromContext(ctx)
switch action {
case types.Added:
newObj, err := utils.ToRuntimeObject(resource)
if err != nil {
return err
}
return s.Add(newObj)
case types.Modified:
newObj, err := meta.Accessor(resource)
if err != nil {
return err
}
lastObj, exists, err := s.Get(newObj.GetNamespace(), newObj.GetName())
if err != nil {
return err
}
if !exists {
return fmt.Errorf("the resource %s/%s does not exist", newObj.GetNamespace(), newObj.GetName())
}
// prevent the resource from being updated if it is deleting
if !lastObj.GetDeletionTimestamp().IsZero() {
logger.Info("the resource is deleting, ignore the update",
"resourceNamespace", newObj.GetNamespace(), "resourceName", newObj.GetName())
return nil
}
updated, err := utils.ToRuntimeObject(resource)
if err != nil {
return err
}
return s.Update(updated)
case types.Deleted:
newObj, err := meta.Accessor(resource)
if err != nil {
return err
}
if newObj.GetDeletionTimestamp().IsZero() {
return nil
}
if len(newObj.GetFinalizers()) != 0 {
return nil
}
last, exists, err := s.Get(newObj.GetNamespace(), newObj.GetName())
if err != nil {
return err
}
if !exists {
return nil
}
deletingObj, err := utils.ToRuntimeObject(last)
if err != nil {
return err
}
return s.Delete(deletingObj)
default:
return fmt.Errorf("unsupported resource action %s", action)
}
}
func (s *AgentInformerWatcherStore[T]) GetWatcher(namespace string, opts metav1.ListOptions) (watch.Interface, error) {
return s.Watcher, nil
}
func (s *AgentInformerWatcherStore[T]) HasInitiated() bool {
return true
}