/
store.go
179 lines (163 loc) · 4.81 KB
/
store.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package store
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/bonnefoa/kubectl-fzf/v3/internal/k8s/resources"
"github.com/bonnefoa/kubectl-fzf/v3/internal/util"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
// Store stores the current state of k8s resources
type Store struct {
data map[string]resources.K8sResource
resourceCtor func(obj interface{}, config resources.CtorConfig) resources.K8sResource
ctorConfig resources.CtorConfig
resourceType resources.ResourceType
currentFile *os.File
storeConfig *StoreConfig
firstWrite bool
dataMutex sync.Mutex
dumpRequired bool
lastFullDump time.Time
}
// NewStore creates a new store
func NewStore(ctx context.Context, storeConfig *StoreConfig,
ctorConfig resources.CtorConfig, resourceType resources.ResourceType) *Store {
k := Store{}
k.data = make(map[string]resources.K8sResource, 0)
k.resourceCtor = resources.ResourceTypeToCtor(resourceType)
k.resourceType = resourceType
k.currentFile = nil
k.storeConfig = storeConfig
k.firstWrite = true
k.ctorConfig = ctorConfig
k.lastFullDump = time.Time{}
go k.fullDumpTicker()
return &k
}
func (k *Store) fullDumpTicker() {
timeBetweenFullDump := k.storeConfig.GetTimeBetweenFullDump()
logrus.Debugf("Starting ticker loop for %s: will do full dump every %s", k.resourceType, timeBetweenFullDump)
t := time.NewTicker(timeBetweenFullDump)
for {
<-t.C
err := k.DumpFullState()
util.FatalIf(err)
}
}
func resourceKey(obj interface{}) string {
name := "None"
namespace := "None"
switch v := obj.(type) {
case metav1.ObjectMetaAccessor:
o := v.GetObjectMeta()
namespace = o.GetNamespace()
name = o.GetName()
case *unstructured.Unstructured:
metadata := v.Object["metadata"].(map[string]interface{})
name = metadata["name"].(string)
namespace = metadata["namespace"].(string)
default:
logrus.Warningf("Unknown type %v", obj)
}
return fmt.Sprintf("%s_%s", namespace, name)
}
// AddResourceList clears current state add the objects to the store.
// It will trigger a full dump
// This is used for polled resources, no need for mutex
func (k *Store) AddResourceList(lstRuntime []runtime.Object) {
k.data = make(map[string]resources.K8sResource, 0)
for _, runtimeObject := range lstRuntime {
key := resourceKey(runtimeObject)
resource := k.resourceCtor(runtimeObject, k.ctorConfig)
k.data[key] = resource
}
k.dumpRequired = true
}
// AddResource adds a new k8s object to the store
func (k *Store) AddResource(obj interface{}) {
key := resourceKey(obj)
newObj := k.resourceCtor(obj, k.ctorConfig)
logrus.Tracef("%s added: %s", k.resourceType, key)
k.dataMutex.Lock()
k.data[key] = newObj
k.dataMutex.Unlock()
k.dumpRequired = true
}
// DeleteResource removes an existing k8s object to the store
func (k *Store) DeleteResource(obj interface{}) {
key := "Unknown"
switch v := obj.(type) {
case cache.DeletedFinalStateUnknown:
key = resourceKey(v.Obj)
case unstructured.Unstructured:
case metav1.ObjectMetaAccessor:
key = resourceKey(obj)
default:
logrus.Debugf("Unknown object type %v", obj)
return
}
logrus.Tracef("%s deleted: %s", k.resourceType, key)
k.dataMutex.Lock()
delete(k.data, key)
k.dataMutex.Unlock()
k.dumpRequired = true
}
// UpdateResource update an existing k8s object
func (k *Store) UpdateResource(oldObj, newObj interface{}) {
key := resourceKey(newObj)
k8sObj := k.resourceCtor(newObj, k.ctorConfig)
k.dataMutex.Lock()
if k8sObj.HasChanged(k.data[key]) {
logrus.Tracef("%s changed: %s", k.resourceType, key)
k.data[key] = k8sObj
k.dataMutex.Unlock()
k.dumpRequired = true
} else {
k.dataMutex.Unlock()
}
}
func (k *Store) GetStats() *Stats {
itemPerNamespaces := make(map[string]int, 0)
for _, r := range k.data {
namespace := r.GetNamespace()
_, ok := itemPerNamespaces[namespace]
if !ok {
itemPerNamespaces[namespace] = 1
} else {
itemPerNamespaces[namespace]++
}
}
return &Stats{
ResourceType: k.resourceType,
ItemPerNamespace: itemPerNamespaces,
LastDumped: k.lastFullDump,
}
}
// DumpFullState writes the full state to the cache file
func (k *Store) DumpFullState() error {
if !k.dumpRequired {
logrus.Tracef("No change of %s detected, skipping dump", k.resourceType)
return nil
}
now := time.Now()
delta := now.Sub(k.lastFullDump)
if delta < k.storeConfig.GetTimeBetweenFullDump() {
logrus.Infof("Last full dump for %s happened %s ago, ignoring it", k.resourceType, delta)
return nil
}
k.dumpRequired = false
k.lastFullDump = now
logrus.Infof("Doing full dump of %d %s", len(k.data), k.resourceType)
destFile := k.storeConfig.GetResourceStorePath(k.resourceType)
k.dataMutex.Lock()
err := util.EncodeToFile(k.data, destFile)
k.dataMutex.Unlock()
return err
}