-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeep.go
169 lines (143 loc) · 4.69 KB
/
nodeep.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
/*
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"
"github.com/henderiw-nephio/wire-connector/pkg/proto/endpointpb"
"github.com/henderiw-nephio/wire-connector/pkg/wirer"
"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"
)
// Endoint provides the
type NodeEndpoint struct {
//state.StateTransition
dispatcher Dispatcher
wirer.Object
NodeEpReq *NodeEpReq
NodeEpResp *EpResp
State state.State
l *slog.Logger
}
type NodeEpReq struct {
*endpointpb.EndpointRequest
}
func NewNodeEndpoint(ctx context.Context, d Dispatcher, req *NodeEpReq) *NodeEndpoint {
return &NodeEndpoint{
dispatcher: d,
NodeEpReq: req,
NodeEpResp: newNodeEndpointResp(req),
State: &state.Deleted{},
l: log.FromContext(ctx).WithGroup("nodeep").With("nsn", req.GetNSN()),
}
}
func newNodeEndpointResp(req *NodeEpReq) *EpResp {
return &EpResp{
EndpointResponse: &endpointpb.EndpointResponse{
NodeKey: req.GetNodeKey(),
StatusCode: endpointpb.StatusCode_NOK,
Reason: "deleted",
},
}
}
// GetAdditionalState returns no additional state, since there is only 1 endpoint
func (r *NodeEndpoint) GetAdditionalState(eventCtx *state.EventCtx) []state.StateCtx {
return []state.StateCtx{}
}
func (r *NodeEndpoint) 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.State, newState), "eventCtx", eventCtx)
log.Info("transition", "wireResp", r.NodeEpResp, "generated events", generatedEvents)
r.State = newState
r.NodeEpResp.UpdateStatus(newState, eventCtx)
for _, ge := range generatedEvents {
log.Info("transition generated event", "ge", ge, "resolved", r.NodeEpReq.IsResolved())
if r.NodeEpReq.IsResolved() {
// should always resolve
workerNsn := types.NamespacedName{
Name: r.NodeEpReq.HostNodeName,
}
if err := r.dispatcher.Write(workerNsn, state.WorkerEvent{Action: ge, Req: r.NodeEpReq, EventCtx: eventCtx}); err != nil {
// should never happen, as it means the worker does not exist
r.HandleEvent(ctx, state.FailedEvent, eventCtx)
continue
}
}
}
}
func (r *NodeEndpoint) HandleEvent(ctx context.Context, event state.Event, eventCtx *state.EventCtx) {
r.State.HandleEvent(ctx, event, eventCtx, r)
}
func (r *NodeEndpoint) GetResponse() *endpointpb.EndpointResponse {
return r.NodeEpResp.EndpointResponse
}
func (r *NodeEpReq) GetNSN() types.NamespacedName {
return types.NamespacedName{
Namespace: r.NodeKey.Topology,
Name: r.NodeKey.NodeName,
}
}
func (r *NodeEpReq) IsResolved() bool {
return r.ServiceEndpoint != ""
}
func (r *NodeEpReq) Unresolve() {
r.HostIP = ""
r.HostNodeName = ""
r.ServiceEndpoint = ""
}
func (r *NodeEpReq) Resolve(res *resolve.Data) {
if res != nil {
//r.Endpoints[epIdx].NodeName = res.PodNodeName
r.HostIP = res.HostIP
r.HostNodeName = res.HostNodeName
r.ServiceEndpoint = res.ServiceEndpoint
} else {
r.Unresolve()
}
}
func (r *NodeEpReq) GetHostNodeName() string {
return r.HostNodeName
}
func (r *NodeEpReq) CompareName(evaluate EvaluateName, name string) bool {
switch evaluate {
case EvaluateHostNodeName:
return r.HostNodeName == name
case EvaluateNodeName:
return r.NodeKey.NodeName == name
default:
return false
}
}
type EpResp struct {
*endpointpb.EndpointResponse
}
func (r *EpResp) UpdateStatus(newState state.State, eventCtx *state.EventCtx) {
// 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.StatusCode = endpointpb.StatusCode_OK
r.Reason = ""
} else {
// the state machine is still transition, we put the reason to the state
r.StatusCode = endpointpb.StatusCode_NOK
r.Reason = newState.String()
}
} else {
r.StatusCode = endpointpb.StatusCode_NOK
r.Reason = eventCtx.Message
}
}