/
v1alpha1_time.go
71 lines (57 loc) · 1.97 KB
/
v1alpha1_time.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package runtime
import (
"context"
"fmt"
"time"
"github.com/beevik/ntp"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
timeapi "github.com/talos-systems/talos/pkg/machinery/api/time"
"github.com/talos-systems/talos/pkg/machinery/config"
)
// ConfigProvider defines an interface sufficient for the TimeServer.
type ConfigProvider interface {
Config() config.Provider
}
// TimeServer implements TimeService API.
type TimeServer struct {
timeapi.UnimplementedTimeServiceServer
ConfigProvider ConfigProvider
}
// Register implements the factory.Registrator interface.
func (r *TimeServer) Register(s *grpc.Server) {
timeapi.RegisterTimeServiceServer(s, r)
}
// Time issues a query to the configured ntp server and displays the results.
func (r *TimeServer) Time(ctx context.Context, in *empty.Empty) (reply *timeapi.TimeResponse, err error) {
timeServers := r.ConfigProvider.Config().Machine().Time().Servers()
if len(timeServers) == 0 {
return nil, fmt.Errorf("no time servers configured")
}
return r.TimeCheck(ctx, &timeapi.TimeRequest{
Server: timeServers[0],
})
}
// TimeCheck issues a query to the specified ntp server and displays the results.
func (r *TimeServer) TimeCheck(ctx context.Context, in *timeapi.TimeRequest) (reply *timeapi.TimeResponse, err error) {
rt, err := ntp.Query(in.Server)
if err != nil {
return nil, fmt.Errorf("error querying NTP server %q: %w", in.Server, err)
}
if err = rt.Validate(); err != nil {
return nil, fmt.Errorf("error validating NTP response: %w", err)
}
return &timeapi.TimeResponse{
Messages: []*timeapi.Time{
{
Server: in.Server,
Localtime: timestamppb.New(time.Now()),
Remotetime: timestamppb.New(rt.Time),
},
},
}, nil
}