/
cache.go
250 lines (226 loc) · 9.35 KB
/
cache.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2019-2024 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shadowpod
import (
"context"
"fmt"
"sync"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharing "github.com/liqotech/liqo/apis/sharing/v1alpha1"
vkv1alpha1 "github.com/liqotech/liqo/apis/virtualkubelet/v1alpha1"
"github.com/liqotech/liqo/pkg/discovery"
fcutils "github.com/liqotech/liqo/pkg/utils/foreignCluster"
liqolabels "github.com/liqotech/liqo/pkg/utils/labels"
)
// peeringCache is a cache that holds the peeringInfo for each peered cluster.
type peeringCache struct {
peeringInfo sync.Map
ready bool
}
/**
* PeeringCache methods
*/
// CacheRefresher is a wrapper function that receives a ShadowPodValidator
// and starts a PollImmediateInfinite timer to periodically refresh the cache.
func (spv *Validator) CacheRefresher(interval time.Duration) func(ctx context.Context) error {
return func(ctx context.Context) error {
return wait.PollImmediateInfiniteWithContext(ctx, interval, spv.refreshCache)
}
}
func (spv *Validator) initializeCache(ctx context.Context) (err error) {
resourceOfferList := sharing.ResourceOfferList{}
if err := spv.client.List(ctx, &resourceOfferList, &client.ListOptions{LabelSelector: liqolabels.LocalLabelSelector()}); err != nil {
return err
}
klog.Infof("Cache initialization started")
for i := range resourceOfferList.Items {
ro := &resourceOfferList.Items[i]
clusterID := ro.Labels[discovery.ClusterIDLabel]
if clusterID == "" {
klog.Warningf("ResourceOffer %s/%s has no cluster id", ro.Namespace, ro.Name)
continue
}
clusterName := retrieveClusterName(ctx, spv.client, clusterID)
klog.V(4).Infof("Generating PeeringInfo in cache for corresponding ResourceOffer %s", klog.KObj(ro))
pi := createPeeringInfo(discoveryv1alpha1.ClusterIdentity{
ClusterID: clusterID,
ClusterName: clusterName,
}, ro.Spec.ResourceQuota.Hard)
// Get the List of shadow pods running on the cluster with a given clusterID
shadowPodList, err := spv.getShadowPodListByClusterID(ctx, clusterID)
if err != nil {
return err
}
klog.V(5).Infof("Found %d ShadowPods running on cluster %s", len(shadowPodList.Items), pi.clusterIdentity.String())
pi.alignExistingShadowPods(shadowPodList)
spv.PeeringCache.peeringInfo.Store(clusterID, pi)
}
klog.Infof("Cache initialization completed. Found %d peered clusters", len(resourceOfferList.Items))
spv.PeeringCache.ready = true
return nil
}
func (spv *Validator) refreshCache(ctx context.Context) (done bool, err error) {
if !spv.PeeringCache.ready {
err = spv.initializeCache(ctx)
if err != nil {
klog.Warning(err)
}
return false, nil
}
klog.V(4).Infof("Cache refresh started")
// Cycling on all recorded peering in Cache
spv.PeeringCache.peeringInfo.Range(
func(key, value interface{}) bool {
pi := value.(*peeringInfo)
clusterID := key.(string)
// Get the List of shadow pods running on the cluster
shadowPodList, err := spv.getShadowPodListByClusterID(ctx, clusterID)
if err != nil {
klog.Warning(err)
return true
}
klog.V(5).Infof("Found %d ShadowPods for cluster %q", len(shadowPodList.Items), pi.clusterIdentity.String())
// Flush terminating ShadowPods and check the correct alignment between cluster and cache
klog.V(5).Infof("Aligning ShadowPods for cluster %q", pi.clusterIdentity.String())
pi.alignTerminatingOrNotExistingShadowPods(shadowPodList)
return true
},
)
klog.V(5).Infof("Aligning ResourceOffers - PeeringInfo")
if err := spv.checkAlignmentResourceOfferPeeringInfo(ctx); err != nil {
klog.Error(err)
return false, nil
}
klog.V(4).Infof("Cache refresh completed")
return false, nil
}
func (pi *peeringInfo) alignTerminatingOrNotExistingShadowPods(shadowPodList *vkv1alpha1.ShadowPodList) {
pi.mu.Lock()
defer pi.mu.Unlock()
spMap := make(map[string]struct{})
// Check on all cluster ShadowPods and saving a list of them in a temporary map
for i := range shadowPodList.Items {
nsname := types.NamespacedName{Name: shadowPodList.Items[i].Name, Namespace: shadowPodList.Items[i].Namespace}
found := pi.checkAndAddShadowPods(&shadowPodList.Items[i], nsname)
if !found {
klog.Warningf("Warning: ShadowPod %s not found in cache, added", nsname.String())
}
spMap[nsname.String()] = struct{}{}
}
klog.V(5).Infof("Searching for terminated ShadowPodDescription to be removed from cache")
// Alignment of all ShadowPodDescriptions in cache
pi.alignTerminatingShadowPodDescriptions(spMap)
}
func (pi *peeringInfo) alignTerminatingShadowPodDescriptions(spMap map[string]struct{}) {
for _, shadowPodDescription := range pi.shadowPods {
// Check if the ShadowPod is in terminating phase in cache and has been already terminated/deleted from the cluster
// if true ShadowPodDescription can be also deleted from the cache
if _, stillPresent := spMap[shadowPodDescription.namespacedName.String()]; !stillPresent {
if !shadowPodDescription.running {
pi.removeShadowPod(shadowPodDescription)
klog.V(5).Infof("ShadowPodDescription %s removed from cache", shadowPodDescription.namespacedName.String())
} else if time.Since(shadowPodDescription.creationTimestamp) > 30*time.Second {
pi.terminateShadowPod(shadowPodDescription)
pi.removeShadowPod(shadowPodDescription)
}
}
}
}
func (pi *peeringInfo) alignExistingShadowPods(shadowPodList *vkv1alpha1.ShadowPodList) {
pi.mu.Lock()
defer pi.mu.Unlock()
for i := range shadowPodList.Items {
nsname := types.NamespacedName{Name: shadowPodList.Items[i].Name, Namespace: shadowPodList.Items[i].Namespace}
found := pi.checkAndAddShadowPods(&shadowPodList.Items[i], nsname)
if !found {
klog.V(4).Infof("ShadowPod %s added in cache", nsname.String())
}
}
}
func (pi *peeringInfo) checkAndAddShadowPods(shadowPod *vkv1alpha1.ShadowPod, nsname types.NamespacedName) (found bool) {
_, found = pi.shadowPods[nsname.String()]
if !found {
// Errors are intentionally ignored here.
spQuota, _ := getQuotaFromShadowPod(shadowPod, false)
pi.addShadowPod(createShadowPodDescription(shadowPod.GetName(), shadowPod.GetNamespace(), shadowPod.GetUID(), *spQuota))
}
return
}
func (spv *Validator) checkAlignmentResourceOfferPeeringInfo(ctx context.Context) error {
resourceOfferList := sharing.ResourceOfferList{}
roMap := make(map[string]struct{})
// Get the List of resource offers
if err := spv.client.List(ctx, &resourceOfferList, &client.ListOptions{LabelSelector: liqolabels.LocalLabelSelector()}); err != nil {
return err
}
// Check if there are new ResourceOffers in the system snapshot
for i := range resourceOfferList.Items {
ro := &resourceOfferList.Items[i]
clusterID := ro.Labels[discovery.ClusterIDLabel]
if clusterID == "" {
return fmt.Errorf("ResourceOffer %q has no cluster id", ro.Name)
}
clusterName := retrieveClusterName(ctx, spv.client, clusterID)
// Populating a map of valid and existing ResourceOffers in the system snapshot
roMap[clusterID] = struct{}{}
// Check if the ResourceOffer is not present in the cache
if newPI, found := spv.PeeringCache.peeringInfo.LoadOrStore(clusterID, createPeeringInfo(discoveryv1alpha1.ClusterIdentity{
ClusterID: clusterID,
ClusterName: clusterName,
}, ro.Spec.ResourceQuota.Hard)); !found {
klog.V(4).Infof("ResourceOffer %q not found in cache, adding it", clusterName)
// Get the List of ShadowPods running on the cluster
shadowPodList, err := spv.getShadowPodListByClusterID(ctx, clusterID)
if err != nil {
return err
}
klog.V(5).Infof("Found %d ShadowPods running on cluster %s", len(shadowPodList.Items), newPI.(*peeringInfo).clusterIdentity.String())
newPI.(*peeringInfo).alignExistingShadowPods(shadowPodList)
}
}
// Check if PeeringInfos still have corresponding ResourceOffers
spv.PeeringCache.peeringInfo.Range(
func(key, value interface{}) bool {
peeringInfo := value.(*peeringInfo)
clusterID := key.(string)
// If the corresponding ResourceOffer is not present anymore, remove the PeeringInfo from the cache
if _, found := roMap[clusterID]; !found {
klog.V(4).Infof("ResourceOffer %q not found in system snapshot, removing it from cache", peeringInfo.clusterIdentity.String())
spv.PeeringCache.peeringInfo.Delete(clusterID)
}
return true
},
)
return nil
}
func (pi *peeringInfo) alignResourceOfferUpdates(resources corev1.ResourceList) {
pi.mu.Lock()
defer pi.mu.Unlock()
pi.updateQuotas(resources)
klog.V(4).Infof("Quota of PeeringInfo for cluster %q has been updated", pi.clusterIdentity.String())
}
func retrieveClusterName(ctx context.Context, c client.Client, clusterID string) string {
cluster, err := fcutils.GetForeignClusterByID(ctx, c, clusterID)
if err != nil {
klog.Warning(err)
return ""
}
return cluster.Spec.ClusterIdentity.ClusterName
}