-
Notifications
You must be signed in to change notification settings - Fork 115
/
data_resync.go
101 lines (89 loc) · 2.92 KB
/
data_resync.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
/*
* // Copyright (c) 2018 Cisco and/or its affiliates.
* //
* // 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 processor
import (
"fmt"
"github.com/contiv/vpp/plugins/ipam/ipalloc"
controller "github.com/contiv/vpp/plugins/controller/api"
epmodel "github.com/contiv/vpp/plugins/ksr/model/endpoints"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
svcmodel "github.com/contiv/vpp/plugins/ksr/model/service"
)
// ResyncEventData wraps an entire state of K8s services that should be reflected
// into VPP.
type ResyncEventData struct {
Pods []podmodel.ID
Endpoints []*epmodel.Endpoints
Services []*svcmodel.Service
IPAllocations []*ipalloc.CustomIPAllocation
}
// NewResyncEventData creates an empty instance of ResyncEventData.
func NewResyncEventData() *ResyncEventData {
return &ResyncEventData{
Pods: []podmodel.ID{},
Endpoints: []*epmodel.Endpoints{},
Services: []*svcmodel.Service{},
}
}
// String converts ResyncEventData into a human-readable string.
func (red ResyncEventData) String() string {
pods := ""
for idx, podID := range red.Pods {
pods += podID.String()
if idx < len(red.Pods)-1 {
pods += ", "
}
}
endpoints := ""
for idx, endpoint := range red.Endpoints {
endpoints += endpoint.String()
if idx < len(red.Endpoints)-1 {
endpoints += ", "
}
}
services := ""
for idx, service := range red.Services {
services += service.String()
if idx < len(red.Services)-1 {
services += ", "
}
}
return fmt.Sprintf("ResyncEventData <Pods:[%s] Endpoint:[%s] Services:[%s]>",
pods, endpoints, services)
}
func (sp *ServiceProcessor) parseResyncEv(kubeStateData controller.KubeStateData) *ResyncEventData {
event := NewResyncEventData()
// collect pods
for podID := range sp.PodManager.GetLocalPods() {
event.Pods = append(event.Pods, podID)
}
// collect endpoints
for _, epProto := range kubeStateData[epmodel.EndpointsKeyword] {
endpoints := epProto.(*epmodel.Endpoints)
event.Endpoints = append(event.Endpoints, endpoints)
}
// collect services
for _, svcProto := range kubeStateData[svcmodel.ServiceKeyword] {
service := svcProto.(*svcmodel.Service)
event.Services = append(event.Services, service)
}
// collect custom IP allocations
for _, svcProto := range kubeStateData[ipalloc.Keyword] {
alloc := svcProto.(*ipalloc.CustomIPAllocation)
event.IPAllocations = append(event.IPAllocations, alloc)
}
return event
}