-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnode_controller.go
More file actions
147 lines (121 loc) · 3.94 KB
/
Copy pathnode_controller.go
File metadata and controls
147 lines (121 loc) · 3.94 KB
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
// Copyright 2023 Aalyria Technologies, Inc., and 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 agent
import (
"context"
"errors"
"fmt"
"time"
"aalyria.com/spacetime/agent/internal/task"
schedpb "aalyria.com/spacetime/api/scheduling/v1alpha"
telemetrypb "aalyria.com/spacetime/api/telemetry/v1alpha"
"github.com/google/uuid"
"github.com/jonboulle/clockwork"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// nodeController is the logical owner of a node and its various services
// (telemetry, enactments, etc.).
type nodeController struct {
// The node ID this controller is responsible for.
id string
// done is called when the controller should stop.
done func()
clock clockwork.Clock
services []task.Task
enactmentStats func() any
telemetryStats func() any
closers []func() error
newToken func() string
}
func (a *Agent) newNodeController(node *node, done func()) (*nodeController, error) {
nc := &nodeController{
id: node.id,
done: done,
clock: a.clock,
enactmentStats: func() any { return nil },
telemetryStats: func() any { return nil },
newToken: uuid.NewString,
}
rc := task.RetryConfig{
BackoffDuration: 5 * time.Second,
ErrIsFatal: func(err error) bool {
switch c := status.Code(err); {
case c == codes.Unauthenticated, c == codes.Canceled, c == codes.Unimplemented, errors.Is(err, context.Canceled):
return true
default:
return false
}
},
Clock: nc.clock,
}
nc.services = []task.Task{}
if node.telemetryEnabled {
telemetryConn, err := grpc.NewClient(node.telemetryEndpoint, node.telemetryDialOpts...)
if err != nil {
return nil, fmt.Errorf("failed connecting to telemetry endpoint: %w", err)
}
nc.closers = append(nc.closers, telemetryConn.Close)
// If the telemetry driver implements io.Closer, register it for cleanup
if closer, ok := node.td.(interface{ Close() error }); ok {
nc.closers = append(nc.closers, closer.Close)
}
telemetryClient := telemetrypb.NewTelemetryClient(telemetryConn)
ts := nc.newTelemetryService(telemetryClient, node.td)
nc.services = append(nc.services, task.Task(ts.run).
WithNewSpan("telemetry_service").
WithLogField("service", "telemetry").
WithRetries(rc).
WithPanicCatcher())
nc.telemetryStats = ts.Stats
}
if node.enactmentsEnabled {
enactmentConn, err := grpc.NewClient(node.enactmentEndpoint, node.enactmentDialOpts...)
if err != nil {
return nil, fmt.Errorf("failed connecting to enactment endpoint: %w", err)
}
nc.closers = append(nc.closers, enactmentConn.Close)
schedClient := schedpb.NewSchedulingClient(enactmentConn)
es := nc.newEnactmentService(schedClient, node.ed, nc.newToken())
nc.services = append(nc.services, task.Task(es.run).
WithNewSpan("enactment_service").
WithLogField("service", "enactment").
WithRetries(rc).
WithPanicCatcher())
nc.enactmentStats = es.Stats
}
return nc, nil
}
func (nc *nodeController) run(ctx context.Context) (resErr error) {
defer func() {
nc.done()
errs := []error{resErr}
for _, c := range nc.closers {
errs = append(errs, c())
}
resErr = errors.Join(errs...)
}()
return task.Group(nc.services...).WithPanicCatcher()(ctx)
}
type nodeControllerStats struct {
Enactment any
Telemetry any
}
func (nc *nodeController) Stats() any {
return nodeControllerStats{
Enactment: nc.enactmentStats(),
Telemetry: nc.telemetryStats(),
}
}