-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.go
262 lines (227 loc) · 7.81 KB
/
wire.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
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright 2022 Nokia.
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 wirecontroller
import (
"context"
"fmt"
"log/slog"
"os"
"github.com/henderiw-nephio/wire-connector/pkg/proto/wirepb"
"github.com/henderiw-nephio/wire-connector/pkg/wirer/cache/resolve"
"github.com/henderiw-nephio/wire-connector/pkg/wirer/state"
"github.com/henderiw/logger/log"
"k8s.io/apimachinery/pkg/types"
)
type DesiredAction string
const (
DesiredActionCreate DesiredAction = "create"
DesiredActionDelete DesiredAction = "delete"
)
type Wire struct {
dispatcher Dispatcher
DesiredAction DesiredAction
WireReq *WireReq
WireResp *WireResp
EndpointsState []state.State
l *slog.Logger
}
// NewWire is like create link/wire, once the object exists, this is no longer required
func NewWire(ctx context.Context, d Dispatcher, wreq *WireReq, vpnID uint32) *Wire {
wreq.AddVPN(vpnID)
return &Wire{
dispatcher: d,
DesiredAction: DesiredActionCreate,
WireReq: wreq,
WireResp: newWireResp(wreq),
EndpointsState: []state.State{&state.Deleted{}, &state.Deleted{}},
l: log.FromContext(ctx).WithGroup("wire").With("nsn", wreq.GetNSN()),
}
}
func (r *Wire) GetWireResponse() *wirepb.WireResponse {
return r.WireResp.WireResponse
}
func (r *Wire) SetDesiredAction(a DesiredAction) {
r.DesiredAction = a
}
// GetAdditionalState returns the other endpoint state on the wire
// + its associated event context to handle further events on the adjacent endpoint
func (r *Wire) GetAdditionalState(eventCtx *state.EventCtx) []state.StateCtx {
otherEpIdx := (eventCtx.EpIdx + 1) % 2
return []state.StateCtx{
{
State: r.EndpointsState[otherEpIdx],
EventCtx: state.EventCtx{EpIdx: otherEpIdx},
},
}
}
func (r *Wire) Transition(ctx context.Context, newState state.State, eventCtx *state.EventCtx, generatedEvents ...state.WorkerAction) {
log := log.FromContext(ctx).With("from/to", fmt.Sprintf("%s/%s", r.EndpointsState[eventCtx.EpIdx], newState), "eventCtx", eventCtx, "wireResp", r.WireResp, "generated events", generatedEvents)
log.Info("transition")
r.EndpointsState[eventCtx.EpIdx] = newState
r.WireResp.UpdateStatus(newState, eventCtx)
log.Info("transition", "link status", fmt.Sprintf("%s/%s", r.WireResp.StatusCode.String(), r.WireResp.Reason),
"ep0 status", fmt.Sprintf("%s/%s", r.WireResp.EndpointsStatus[0].StatusCode.String(), r.WireResp.EndpointsStatus[0].Reason),
"ep1 status", fmt.Sprintf("%s/%s", r.WireResp.EndpointsStatus[1].StatusCode.String(), r.WireResp.EndpointsStatus[1].Reason),
)
for _, ge := range generatedEvents {
log = log.With("from/to", fmt.Sprintf("%s/%s", r.EndpointsState[eventCtx.EpIdx], newState), "ge", ge)
log.Info("transition generated event")
if r.WireReq.IsResolved(eventCtx.EpIdx) {
// should always resolve
workerNsn := types.NamespacedName{
Name: r.WireReq.GetHostNodeName(eventCtx.EpIdx),
}
if os.Getenv("WIRER_INTERCLUSTER") == "true" {
workerNsn = types.NamespacedName{
Name: r.WireReq.GetClusterName(eventCtx.EpIdx),
}
}
log.Info("transition generated event", "workerNsn", workerNsn)
if err := r.dispatcher.Write(workerNsn, state.WorkerEvent{Action: ge, Req: r.WireReq, EventCtx: eventCtx}); err != nil {
// should never happen, as it means the worker does not exist
newEventCtx := *eventCtx
newEventCtx.Message = err.Error()
r.HandleEvent(ctx, state.FailedEvent, &newEventCtx)
continue
}
}
}
}
func (r *Wire) HandleEvent(ctx context.Context, event state.Event, eventCtx *state.EventCtx) {
r.EndpointsState[eventCtx.EpIdx].HandleEvent(ctx, event, eventCtx, r)
}
type WireReq struct {
*wirepb.WireRequest
}
func (r *WireReq) GetNSN() types.NamespacedName {
return types.NamespacedName{
Namespace: r.WireKey.Namespace,
Name: r.WireKey.Name,
}
}
func (r *WireReq) Act(epIdx int) bool {
return !r.Endpoints[epIdx].NoAction
}
func (r *WireReq) IsResolved(epIdx int) bool {
return r.Endpoints[epIdx].ServiceEndpoint != ""
}
func (r *WireReq) Unresolve(epIdx int) {
r.Endpoints[epIdx].HostIP = ""
r.Endpoints[epIdx].HostNodeName = ""
r.Endpoints[epIdx].ServiceEndpoint = ""
r.Endpoints[epIdx].NoAction = false
}
func (r *WireReq) Resolve(resolvedData []*resolve.Data) {
for epIdx, res := range resolvedData {
if res != nil {
r.Endpoints[epIdx].HostIP = res.HostIP
r.Endpoints[epIdx].HostNodeName = res.HostNodeName
r.Endpoints[epIdx].ServiceEndpoint = res.ServiceEndpoint
r.Endpoints[epIdx].NoAction = res.NoAction
} else {
r.Unresolve(epIdx)
}
}
}
func (r *WireReq) AddVPN(vpnID uint32) {
r.VpnID = vpnID
}
func (r *WireReq) GetEndpointNodeNSN(epIdx int) types.NamespacedName {
return types.NamespacedName{
Namespace: r.Endpoints[epIdx].Topology,
Name: r.Endpoints[epIdx].NodeName,
}
}
func (r *WireReq) GetEndpoint(epIdx int) *wirepb.Endpoint {
return r.Endpoints[epIdx]
}
func (r *WireReq) GetHostNodeName(epIdx int) string {
return r.Endpoints[epIdx].HostNodeName
}
func (r *WireReq) GetClusterName(epIdx int) string {
return r.Endpoints[epIdx].ClusterName
}
// TODO add the fn for the service lookup
func (r *WireReq) CompareName(epIdx int, evaluate EvaluateName, name string) bool {
switch evaluate {
case EvaluateClusterName:
return r.Endpoints[epIdx].ClusterName == name
case EvaluateHostNodeName:
return r.Endpoints[epIdx].HostNodeName == name
case EvaluateNodeName:
return r.Endpoints[epIdx].NodeName == name
default:
return false
}
}
func newWireResp(req *WireReq) *WireResp {
// initialize the endpoint status -> for endpoints w/o an action we resolve to success
epStatus := make([]*wirepb.EndpointStatus, 0, len(req.Endpoints))
for _, epReq := range req.Endpoints {
if epReq.NoAction {
epStatus = append(epStatus, &wirepb.EndpointStatus{StatusCode: wirepb.StatusCode_OK, Reason: ""})
} else {
epStatus = append(epStatus, &wirepb.EndpointStatus{StatusCode: wirepb.StatusCode_NOK, Reason: "to be created"})
}
}
return &WireResp{
WireResponse: &wirepb.WireResponse{
WireKey: req.GetWireKey(),
StatusCode: wirepb.StatusCode_NOK,
Reason: "to be created",
EndpointsStatus: epStatus,
},
}
}
type WireResp struct {
*wirepb.WireResponse
}
func (r *WireResp) UpdateStatus(newState state.State, eventCtx *state.EventCtx) {
if r.EndpointsStatus == nil || len(r.EndpointsStatus) == 0 {
r.EndpointsStatus = []*wirepb.EndpointStatus{{Reason: ""}, {Reason: ""}}
}
// if the eventCtx massage is empty it means the transition was successfull
// only when we transition to Created or Deleted we put the status to OK
// when message is empty but the newState is not
if eventCtx.Message == "" {
if newState.String() == "Created" || newState.String() == "Deleted" {
r.EndpointsStatus[eventCtx.EpIdx] = &wirepb.EndpointStatus{
Reason: "",
StatusCode: wirepb.StatusCode_OK,
}
} else {
r.EndpointsStatus[eventCtx.EpIdx] = &wirepb.EndpointStatus{
Reason: newState.String(),
StatusCode: wirepb.StatusCode_NOK,
}
}
} else {
r.EndpointsStatus[eventCtx.EpIdx] = &wirepb.EndpointStatus{
Reason: eventCtx.Message,
StatusCode: wirepb.StatusCode_NOK,
}
}
// update the overall status
ok := true
for _, eps := range r.EndpointsStatus {
if eps.StatusCode == wirepb.StatusCode_NOK {
ok = false
}
}
if ok {
r.StatusCode = wirepb.StatusCode_OK
r.Reason = ""
} else {
r.StatusCode = wirepb.StatusCode_NOK
}
}