-
Notifications
You must be signed in to change notification settings - Fork 36
/
cache.go
211 lines (174 loc) · 4.97 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
/*
Copyright 2019 Cisco Systems Inc. All rights reserved.
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 kafkac
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"reflect"
"time"
"github.com/Shopify/sarama"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
restclient "k8s.io/client-go/rest"
crdv1 "github.com/noironetworks/aci-containers/pkg/gbpcrd/apis/acipolicy/v1"
crdclientset "github.com/noironetworks/aci-containers/pkg/gbpcrd/clientset/versioned"
aciv1 "github.com/noironetworks/aci-containers/pkg/gbpcrd/clientset/versioned/typed/acipolicy/v1"
)
const (
markerName = "marker.marker"
initial = iota
markerSet
markerReceived
)
// implements the k8s side sync cache
type podIFCache struct {
log *logrus.Entry
cache map[string]*CapicEPMsg
crdClient aciv1.AciV1Interface
state int
markerID string
readyChan chan bool
}
func (pc *podIFCache) Init() error {
pc.cache = make(map[string]*CapicEPMsg)
pc.markerID = fmt.Sprintf("%04d%08d", rand.Intn(10000), rand.Intn(100000))
pc.state = markerSet
k8sCfg, err := restclient.InClusterConfig()
if err != nil {
return err
}
aciawClient, err := crdclientset.NewForConfig(k8sCfg)
if err != nil {
return err
}
pc.crdClient = aciawClient.AciV1()
marker := &crdv1.PodIF{Status: crdv1.PodIFStatus{ContainerID: pc.markerID}}
marker.ObjectMeta.Name = markerName
go func() {
for {
podif, err := pc.crdClient.PodIFs("kube-system").Get(context.TODO(), markerName, metav1.GetOptions{})
if err != nil {
// create podif
_, err = pc.crdClient.PodIFs("kube-system").Create(context.TODO(), marker, metav1.CreateOptions{})
if err == nil {
break
}
pc.log.Errorf("Create podif marker - %v, will retry", err)
time.Sleep(retryTime)
continue
}
// update with new markerID
podif.Status.ContainerID = pc.markerID
_, err = pc.crdClient.PodIFs("kube-system").Update(context.TODO(), podif, metav1.UpdateOptions{})
if err == nil {
break
}
pc.log.Errorf("Update podif marker - %v, will retry", err)
}
pc.log.Infof("Marker with ID: %s set", pc.markerID)
}()
pc.readyChan = make(chan bool)
return nil
}
func (pc *podIFCache) Ready() chan bool {
return pc.readyChan
}
// Updates the cache, returns true if cache is ready
func (pc *podIFCache) ReadyToFwd(key string, msg *CapicEPMsg) bool {
if msg.ContainerID == pc.markerID {
if pc.state == markerSet { // ignore duplicates
pc.state = markerReceived
pc.log.Infof("PodIF marker received")
close(pc.readyChan)
}
return false
}
if msg.delete {
delete(pc.cache, key)
} else if msg.IPAddr != "" { // ignore old markers
pc.cache[key] = msg
}
return pc.state == markerReceived
}
func (pc *podIFCache) Read() map[string]*CapicEPMsg {
return pc.cache
}
type epCache struct {
log *logrus.Entry
cache map[string]*CapicEPMsg
consumer sarama.PartitionConsumer
markerOffset int64
readyChan chan bool
}
func (ec *epCache) Init(markerOffset int64, consumer sarama.PartitionConsumer) chan bool {
ec.cache = make(map[string]*CapicEPMsg)
ec.consumer = consumer
ec.markerOffset = markerOffset
ec.readyChan = make(chan bool)
go func() {
consChan := consumer.Messages()
for {
m, ok := <-consChan
if !ok {
ec.log.Infof("Consumer closed")
return
}
if m.Offset == markerOffset {
ec.log.Infof("Marker received")
close(ec.readyChan)
return
}
if m.Value == nil { // delete
delete(ec.cache, string(m.Key))
ec.log.Debugf(">>>> epCache: Received delete: %s", m.Key)
continue
}
epMsg := new(CapicEPMsg)
err := json.Unmarshal(m.Value, epMsg)
if err != nil {
ec.log.Errorf("epcache.Init : %v, unmarshaling %s", err, m.Value)
continue
}
ec.cache[string(m.Key)] = epMsg
}
}()
return ec.readyChan
}
func (ec *epCache) MsgDiff(desired map[string]*CapicEPMsg) map[string]*CapicEPMsg {
curr := ec.cache
diff := make(map[string]*CapicEPMsg)
// insert delete msgs for items present in curr, but not in desired
// note that in kafka terms, delete means key present with nil value
for k := range curr {
_, present := desired[k]
if !present {
diff[k] = nil
ec.log.Debugf("Delete %s", k)
}
}
// insert create msgs for items present in desired and either
// missing, or different in curr
for k, v := range desired {
currV := curr[k]
if reflect.DeepEqual(v, currV) {
ec.log.Debugf("%s already exists", k)
continue
}
diff[k] = v
ec.log.Debugf("%s added", k)
}
ec.log.Debugf(">>>> Diff: %+v", diff)
return diff
}